From: wang_zhiqiang Date: Tue, 16 Apr 2019 10:30:59 +0000 (+0800) Subject: add handshake X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=apps%2Fagl-service-homescreen.git;a=commitdiff_plain;h=0fc1fc7db6ff7cb95ad6cd6f0ef8f2b88f3d08da add handshake Change-Id: Ie5bdd327f5e994730b0d1cbffad68fca93c80766 --- diff --git a/conf/hs-conf.json b/conf/hs-conf.json index e50ad8a..9a7d934 100644 --- a/conf/hs-conf.json +++ b/conf/hs-conf.json @@ -3,7 +3,7 @@ "times": 20000, "sleep": 50 }, - "recover" { + "recover": { "hs-apps": [ { "appid": "homescreen", diff --git a/src/homescreen.cpp b/src/homescreen.cpp index 7b8e653..3e4ff8f 100644 --- a/src/homescreen.cpp +++ b/src/homescreen.cpp @@ -25,6 +25,7 @@ #include "hs-clientmanager.h" #include "hs-appinfo.h" #include "hs-config.h" +#include const char _error[] = "error"; @@ -34,16 +35,77 @@ const char _reply_message[] = "reply_message"; const char _keyData[] = "data"; const char _keyId[] = "id"; +struct hs_handshake { + hs_handshake(int times, int sleep) : m_times(times), m_sleep(sleep) {} + void start(afb_api_t api) const; + + enum HandshakeStatus { + Handshake_Idle = 0, + Handshake_Subscribing, + Handshake_Subscribe_Fail, + Handshake_WaitEvent, + Handshake_Over + }; + static int hs_sts; + +private: + const std::string sub_event = "windowmanager/handshake"; + const int m_times; + const int m_sleep; +}; + +int hs_handshake::hs_sts = hs_handshake::Handshake_Idle; + +void handshake_subscribe_callback(struct json_object *obj, const char *error, const char *info) +{ + if(error == nullptr) { + hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent; + } + else { + hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail; + } +} + +int on_handshake_event(afb_api_t api, const char *event, struct json_object *object) +{ + hs_handshake::hs_sts = hs_handshake::Handshake_Over; + return 1; +} + +void hs_handshake::start(afb_api_t api) const +{ + setEventHook(sub_event.c_str(), on_handshake_event); + int count = 0; + do { + // try to subscribe handshake event + if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle + || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) { + hs_handshake::hs_sts = Handshake_Subscribing; + HS_WmProxy *wm_proxy = new HS_WmProxy(); + wm_proxy->subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback); + } + + // wait handshake event + if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) { + break; + } + + ++count; + usleep(m_sleep*1000); + } while(count < m_times); + AFB_WARNING("wait count is %d.", count); +} + struct hs_instance { - HS_ClientManager *client_manager; // the connection session manager - HS_AppInfo *app_info; // application info + HS_ClientManager *client_manager; // the connection session manager + HS_AppInfo *app_info; // application info - hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {} - int init(afb_api_t api); - void setEventHook(const char *event, const event_hook_func f); - void onEvent(afb_api_t api, const char *event, struct json_object *object); + hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {} + int init(afb_api_t api); + void setEventHook(const char *event, const event_hook_func f); + void onEvent(afb_api_t api, const char *event, struct json_object *object); private: - std::unordered_map> event_hook_list; + std::unordered_map> event_hook_list; }; /** @@ -77,7 +139,12 @@ int hs_instance::init(afb_api_t api) return -1; } - // handshake + // const struct handshake_info *h = hs_config.getHandshakeInfo(); + // struct hs_handshake handshake(h->times, h->sleep); + // handshake.start(api); + + // recover application + return 0; } diff --git a/src/hs-config.h b/src/hs-config.h index 7cc5dd3..a81a660 100644 --- a/src/hs-config.h +++ b/src/hs-config.h @@ -44,8 +44,8 @@ public: HS_Config &operator=(HS_Config &&) = delete; int readConfig(void); - struct handshake_info getHandshakeInfo(void) {return m_handshake_info;} - recover_map getRecoverMap(void) {return m_recover_map;} + const struct handshake_info* getHandshakeInfo(void) const {return &m_handshake_info;} + const recover_map* getRecoverMap(void) const {return &m_recover_map;} private: int parseConfig(void); diff --git a/src/hs-proxy.cpp b/src/hs-proxy.cpp index 967c1b1..1a237d7 100644 --- a/src/hs-proxy.cpp +++ b/src/hs-proxy.cpp @@ -37,6 +37,9 @@ static const char _event[] = "event"; static void api_callback(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api) { AFB_INFO("asynchronous call, error=%s, info=%s, object=%s.", error, info, json_object_get_string(object)); + if(closure != nullptr) { + reinterpret_cast(closure)(object, error, info); + } } /** @@ -52,10 +55,10 @@ static void api_callback(void *closure, struct json_object *object, const char * * None * */ -static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args) +static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args, api_cb_func f = nullptr) { AFB_INFO("service=%s verb=%s, args=%s.", service, verb, json_object_get_string(args)); - afb_api_call(api, service, verb, args, api_callback, nullptr); + afb_api_call(api, service, verb, args, api_callback, (void*)f); } /** @@ -144,14 +147,15 @@ void HS_AfmMainProxy::start(afb_req_t request, const std::string &id) * #### Parameters * - api : the api serving the request * - event : windowmanager event + * - f : callback function * * #### Return * None * */ -void HS_WmProxy::subscribe(afb_api_t api, EventType event) +void HS_WmProxy::subscribe(afb_api_t api, EventType event, api_cb_func f) { struct json_object* push_obj = json_object_new_object(); json_object_object_add(push_obj, _event, json_object_new_int(event)); - api_call(api, _windowmanager, "wm_subscribe", push_obj); + api_call(api, _windowmanager, "wm_subscribe", push_obj, f); } diff --git a/src/hs-proxy.h b/src/hs-proxy.h index b6283d6..1c9a67d 100644 --- a/src/hs-proxy.h +++ b/src/hs-proxy.h @@ -22,7 +22,10 @@ #include #include "hs-helper.h" -struct HS_AfmMainProxy { +typedef void (*api_cb_func)(struct json_object *obj, const char *error, const char *info); + +class HS_AfmMainProxy { +public: // synchronous call, call result in object int runnables(afb_api_t api, struct json_object **object); int detail(afb_api_t api, const std::string &id, struct json_object **object); @@ -33,8 +36,6 @@ struct HS_AfmMainProxy { class HS_WmProxy { public: - HS_WmProxy() = default; - ~HS_WmProxy() = default; enum EventType { @@ -50,6 +51,7 @@ public: Event_FlushDraw, Event_ScreenUpdated, + Event_Handshake, Event_Error, @@ -57,6 +59,7 @@ public: }; // asynchronous call, reply in callback function - void subscribe(afb_api_t api, EventType event); + void subscribe(afb_api_t api, EventType event, api_cb_func f = nullptr); }; + #endif // HOMESCREEN_PROXY_H \ No newline at end of file