X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Flibhomescreen.cpp;h=71afa6626f2949661c0ed9f151320214531086b5;hb=85e6e550f1afb910b840967fae26db7c578ac06a;hp=cfd382d61c3eab5b52ff0f536b12c2c7f87cf873;hpb=cac960f01705b024bf5536bf536a2a7caf9dbe99;p=src%2Flibhomescreen.git diff --git a/src/libhomescreen.cpp b/src/libhomescreen.cpp index cfd382d..71afa66 100644 --- a/src/libhomescreen.cpp +++ b/src/libhomescreen.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,7 @@ #include #include #include +#include #include #include "hmi-debug.h" @@ -33,6 +35,7 @@ using namespace std; static bool has_verb(const string& verb); static const char API[] = "homescreen"; +static const char ApplicationId[] = "application_id"; const std::vector LibHomeScreen::api_list { std::string("ping"), // debug do not use @@ -40,13 +43,25 @@ const std::vector LibHomeScreen::api_list { std::string("on_screen_message"), std::string("on_screen_reply"), std::string("subscribe"), - std::string("unsubscribe") + std::string("unsubscribe"), + std::string("showWindow"), + std::string("hideWindow"), + std::string("replyShowWindow"), + std::string("showNotification"), + std::string("showInformation"), + std::string("getRunnables") }; const std::vector LibHomeScreen::event_list { - std::string("tap_shortcut"), +// std::string("tap_shortcut"), + std::string("showWindow"), std::string("on_screen_message"), std::string("on_screen_reply"), + std::string("hideWindow"), + std::string("replyShowWindow"), + std::string("showNotification"), + std::string("showInformation"), + std::string("application-list-changed"), std::string("none") }; @@ -76,6 +91,12 @@ static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg) } +static void event_loop_run(struct sd_event* loop){ + sd_event_loop(loop); + sd_event_unref(loop); +} + + /** * constructor */ @@ -88,14 +109,14 @@ LibHomeScreen::LibHomeScreen() */ LibHomeScreen::~LibHomeScreen() { - if(mploop) - { - sd_event_unref(mploop); - } if(sp_websock != NULL) { afb_wsj1_unref(sp_websock); } + if(mploop) + { + sd_event_exit(mploop, 0); + } } /** @@ -166,13 +187,19 @@ int LibHomeScreen::initialize_websocket() mploop = NULL; onEvent = nullptr; onReply = nullptr; - int ret = sd_event_default(&mploop); + int ret = sd_event_new(&mploop); if(ret < 0) { HMI_ERROR("libhomescreen","Failed to create event loop"); goto END; } + { + // enforce context to avoid initialization/goto error + std::thread th(event_loop_run, mploop); + th.detach(); + } + /* Initialize interface from websocket */ minterface.on_hangup = _on_hangup_static; minterface.on_call = _on_call_static; @@ -190,10 +217,6 @@ int LibHomeScreen::initialize_websocket() return 0; END: - if(mploop) - { - sd_event_unref(mploop); - } return -1; } @@ -203,22 +226,18 @@ END: * When HomeScreen shortcut area is tapped, sending a event * * #### Parameters - * - application_name [in] : Tapped application name (label) + * - application_id [in] : Tapped application id (label) * * #### Return * - Returns 0 on success or -1 in case of error. */ -int LibHomeScreen::tapShortcut(const char* application_name) +int LibHomeScreen::tapShortcut(const char* application_id) { - if(!sp_websock) - { - return -1; - } + struct json_object* obj = json_object_new_object(); + struct json_object* val = json_object_new_string("normal"); + json_object_object_add(obj, "area", val); - struct json_object* j_obj = json_object_new_object(); - struct json_object* val = json_object_new_string(application_name); - json_object_object_add(j_obj, "application_name", val); - return this->call("tap_shortcut", j_obj); + return showWindow(application_id, obj); } /** @@ -287,23 +306,31 @@ int LibHomeScreen::onScreenReply(const char* reply_message) */ void LibHomeScreen::set_event_handler(enum EventType et, handler_func f) { - if (et >= 1 && et <= 3) { - switch (et) { - case Event_TapShortcut: - this->subscribe(LibHomeScreen::event_list[0]); - break; - case Event_OnScreenMessage: - this->subscribe(LibHomeScreen::event_list[1]); - break; - case Event_OnScreenReply: - this->subscribe(LibHomeScreen::event_list[2]); - break; - } - + if (et > Event_Min && et < Event_Max) { this->handlers[et] = std::move(f); } } +/** + * This function subscribe HomeScreen event + * + * #### Parameters + * None + * + * #### Return + * - Nothing + * + * #### Note + * To call HomeScreen's subscribe APIs. + * + */ +void LibHomeScreen::publishSubscription(void) +{ + for(auto &it : handlers) { + this->subscribe(LibHomeScreen::event_list[it.first - 1]); + } +} + /** * This function calls the API of HomeScreen via WebSocket * @@ -429,6 +456,164 @@ int LibHomeScreen::unsubscribe(const string& event_name) return ret; } +/** + * Sending show window event + * + * Call HomeScreen Service's showWindow verb to request display id's screen. + * + * #### Parameters + * - application_id [in] : This argument should be specified to the application's id. + * - json [in] : This argument should be specified to the json parameters. + * + * #### Return + * - Returns 0 on success or -1 in case of error. + * + */ +int LibHomeScreen::showWindow(const char* application_id, json_object* json) +{ + if(!sp_websock) + { + return -1; + } + + struct json_object* j_obj = json_object_new_object(); + struct json_object* val = json_object_new_string(application_id); + json_object_object_add(j_obj, ApplicationId, val); + + if (json == nullptr) { + struct json_object* j_json = json_object_new_object(); + struct json_object* value = json_object_new_string("normal"); + json_object_object_add(j_json, "area", value); + json_object_object_add(j_obj, "parameter", j_json); + } + else { + json_object_object_add(j_obj, "parameter", json); + } + + return this->call("showWindow", j_obj); +} + +/** + * Sending hide window event + * + * Call HomeScreen Service's hideWindow verb to release id's screen. + * + * #### Parameters + * - application_id [in] : This argument should be specified to the application's id. + * + * #### Return + * - Returns 0 on success or -1 in case of error. + * + */ +int LibHomeScreen::hideWindow(const char* application_id) +{ + if(!sp_websock) + { + return -1; + } + + struct json_object* j_obj = json_object_new_object(); + struct json_object* val = json_object_new_string(application_id); + json_object_object_add(j_obj, ApplicationId, val); + + return this->call("hideWindow", j_obj); +} + +/** + * Sending reply onscreen message event + * + * Call HomeScreen Service's replyShowWindow verb to reply onscreen message. + * + * #### Parameters + * - application_id [in] : This argument should be specified to the onscreen reply to applilcation id. + * - json [in] : This argument should be specified to the json parameters. + * + * #### Return + * - Returns 0 on success or -1 in case of error. + * + */ +int LibHomeScreen::replyShowWindow(const char* application_id, json_object* json) +{ + if(!sp_websock) + { + return -1; + } + + if (json == nullptr) { + HMI_WARNING("libhomescreen", "replyShowWindow`s parameter is null"); + return -1; + } + + struct json_object* j_obj = json_object_new_object(); + struct json_object* val = json_object_new_string(application_id); + json_object_object_add(j_obj, ApplicationId, val); + json_object_object_add(j_obj, "parameter", json); + + return this->call("replyShowWindow", j_obj); +} + +/** + * Sending show notification event + * + * Call HomeScreen Service's notification verb to show notification on Status Bar. + * + * #### Parameters + * - json [in] : This argument should be specified to the json parameters. + * + * #### Return + * - Returns 0 on success or -1 in case of error. + * + */ +int LibHomeScreen::showNotification(json_object* json) +{ + if(!sp_websock) + { + return -1; + } + + return this->call("showNotification", json); +} + +/** + * Sending show information event + * + * Call HomeScreen Service's information verb to show notification on Information Bar. + * + * #### Parameters + * - json [in] : This argument should be specified to the json parameters. + * + * #### Return + * - Returns 0 on success or -1 in case of error. + * + */ +int LibHomeScreen::showInformation(json_object* json) +{ + if(!sp_websock) + { + return -1; + } + + return this->call("showInformation", json); +} + +/** + * get runnables list + * + * Call HomeScreen Service's getRunnables verb to get runnalbes list. + * + * #### Parameters + * - Nothing + * + * #### Return + * - Returns 0 on success or -1 in case of error. + * + */ +int LibHomeScreen::getRunnables(void) +{ + return this->call("getRunnables", nullptr); +} + + /************* Callback Function *************/ void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj) @@ -445,8 +630,8 @@ void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, st } /* -* event is like "homescreen/tap_shortcut" -* msg is like {"event":"homescreen\/tap_shortcut","data":{"application_name":"hoge"},"jtype":"afb-event"} +* event is like "homescreen/hvac" +* msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"} * so you can get event name : struct json_object obj = json_object_object_get(msg,"event") */ @@ -471,32 +656,64 @@ void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_m onEvent(ev, ev_contents); } - const char* event_only = strchr(event, '/'); - if (event_only != nullptr) { - event_only = event_only + 1; - }else{ - HMI_WARNING("libhomescreen","event_only is null."); + const char* event_type = nullptr; + struct json_object *json_event_type; + if(json_object_object_get_ex(json_data, "type", &json_event_type)) { + event_type = json_object_get_string(json_event_type); + } + else { + HMI_WARNING("libhomescreen","event_type is null."); return; } - if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) { - auto i = this->handlers.find(Event_TapShortcut); + if (strcasecmp(event_type, LibHomeScreen::event_list[0].c_str()) == 0) { + auto i = this->handlers.find(Event_ShowWindow); if ( i != this->handlers.end() ) { i->second(json_data); } } - else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) { + else if (strcasecmp(event_type, LibHomeScreen::event_list[1].c_str()) == 0) { auto i = this->handlers.find(Event_OnScreenMessage); if ( i != this->handlers.end() ) { i->second(json_data); } } - else if (strcasecmp(event_only, LibHomeScreen::event_list[2].c_str()) == 0) { + else if (strcasecmp(event_type, LibHomeScreen::event_list[2].c_str()) == 0) { auto i = this->handlers.find(Event_OnScreenReply); if ( i != this->handlers.end() ) { i->second(json_data); } } + else if (strcasecmp(event_type, LibHomeScreen::event_list[3].c_str()) == 0) { + auto i = this->handlers.find(Event_HideWindow); + if ( i != this->handlers.end() ) { + i->second(json_data); + } + } + else if (strcasecmp(event_type, LibHomeScreen::event_list[4].c_str()) == 0) { + auto i = this->handlers.find(Event_ReplyShowWindow); + if ( i != this->handlers.end() ) { + i->second(json_data); + } + } + else if (strcasecmp(event_type, LibHomeScreen::event_list[5].c_str()) == 0) { + auto i = this->handlers.find(Event_ShowNotification); + if ( i != this->handlers.end() ) { + i->second(json_data); + } + } + else if (strcasecmp(event_type, LibHomeScreen::event_list[6].c_str()) == 0) { + auto i = this->handlers.find(Event_ShowInformation); + if ( i != this->handlers.end() ) { + i->second(json_data); + } + } + else if (strcasecmp(event_type, LibHomeScreen::event_list[7].c_str()) == 0) { + auto i = this->handlers.find(Event_AppListChanged); + if ( i != this->handlers.end() ) { + i->second(json_data); + } + } } /**