2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3 * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <sys/socket.h>
31 #include <libhomescreen.hpp>
32 #include "hmi-debug.h"
36 static bool has_verb(const string& verb);
37 static const char API[] = "homescreen";
38 static const char ApplicationId[] = "application_id";
40 const std::vector<std::string> LibHomeScreen::api_list {
41 std::string("ping"), // debug do not use
42 std::string("tap_shortcut"), // HomeScreen Application only
43 std::string("on_screen_message"),
44 std::string("on_screen_reply"),
45 std::string("subscribe"),
46 std::string("unsubscribe"),
47 std::string("showWindow"),
48 std::string("hideWindow"),
49 std::string("replyShowWindow"),
50 std::string("showNotification"),
51 std::string("showInformation"),
52 std::string("getRunnables")
55 const std::vector<std::string> LibHomeScreen::event_list {
56 // std::string("tap_shortcut"),
57 std::string("showWindow"),
58 std::string("on_screen_message"),
59 std::string("on_screen_reply"),
60 std::string("hideWindow"),
61 std::string("replyShowWindow"),
62 std::string("showNotification"),
63 std::string("showInformation"),
64 std::string("application-list-changed"),
73 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
75 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
78 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
80 /* LibHomeScreen is not called from other process */
83 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
85 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
88 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
90 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
94 static void event_loop_run(struct sd_event* loop){
103 LibHomeScreen::LibHomeScreen()
110 LibHomeScreen::~LibHomeScreen()
112 if(sp_websock != NULL)
114 afb_wsj1_unref(sp_websock);
118 sd_event_exit(mploop, 0);
123 * This function is initializer
126 * - port [in] : This argument should be specified to the port number to be used for websocket
127 * - token [in] : This argument should be specified to the token to be used for websocket
133 * Use this constructor
136 int LibHomeScreen::init(const int port, const string& token)
139 if(port > 0 && token.size() > 0)
146 HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
149 ret = initialize_websocket();
152 HMI_ERROR("libhomescreen","Failed to initialize websocket");
155 HMI_DEBUG("libhomescreen","Initialized");
162 * This function register callback function for reply/event message from home screen
165 * - event_cb [in] : This argument should be specified to the callback for subscribed event
166 * - reply_cb [in] : This argument should be specified to the reply callback for call function
172 * Event callback is invoked by home screen for event you subscribed.
173 * If you would like to get event, please call subscribe function before/after this function
175 void LibHomeScreen::registerCallback(
176 void (*event_cb)(const std::string& event, struct json_object* event_contents),
177 void (*reply_cb)(struct json_object* reply_contents),
178 void (*hangup_cb)(void))
182 onHangup = hangup_cb;
185 int LibHomeScreen::initialize_websocket()
190 int ret = sd_event_new(&mploop);
193 HMI_ERROR("libhomescreen","Failed to create event loop");
198 // enforce context to avoid initialization/goto error
199 std::thread th(event_loop_run, mploop);
203 /* Initialize interface from websocket */
204 minterface.on_hangup = _on_hangup_static;
205 minterface.on_call = _on_call_static;
206 minterface.on_event = _on_event_static;
207 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
208 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
209 if(sp_websock == NULL)
211 HMI_ERROR("libhomescreen","Failed to create websocket connection");
215 /* creates the evsrc */
216 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
224 * Sending ShortCut Icon tapped event
226 * When HomeScreen shortcut area is tapped, sending a event
229 * - application_id [in] : Tapped application id (label)
232 * - Returns 0 on success or -1 in case of error.
234 int LibHomeScreen::tapShortcut(const char* application_id)
236 struct json_object* obj = json_object_new_object();
237 struct json_object* val = json_object_new_string("normal");
238 json_object_object_add(obj, "area", val);
240 return showWindow(application_id, obj);
244 * Sending onScreen message event
246 * Sending OnScreen message event to HomeScreen from applications
249 * - display_message [in] : message for display
252 * - Returns 0 on success or -1 in case of error.
254 int LibHomeScreen::onScreenMessage(const char* display_message)
261 struct json_object* j_obj = json_object_new_object();
262 struct json_object* val = json_object_new_string(display_message);
263 json_object_object_add(j_obj, "display_message", val);
264 return this->call("on_screen_message", j_obj);
268 * Sending onScreen reply event
270 * Sending OnScreen reply event to applications from HomeScreen
273 * - reply_message [in] : message for reply
276 * - Returns 0 on success or -1 in case of error.
278 int LibHomeScreen::onScreenReply(const char* reply_message)
285 struct json_object* j_obj = json_object_new_object();
286 struct json_object* val = json_object_new_string(reply_message);
287 json_object_object_add(j_obj, "reply_message", val);
288 return this->call("on_screen_reply", j_obj);
292 * Setting Event Handler
294 * Setting event handler for Homescreen
297 * - et [in] : event name
298 * - f [in] : event handler
304 * Don't release json_object by json_object_put in handler_func.
305 * The resource is released by libafbwsc library.
307 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
309 if (et > Event_Min && et < Event_Max) {
311 case Event_ShowWindow:
312 this->subscribe(LibHomeScreen::event_list[0]);
314 case Event_OnScreenMessage:
315 this->subscribe(LibHomeScreen::event_list[1]);
317 case Event_OnScreenReply:
318 this->subscribe(LibHomeScreen::event_list[2]);
320 case Event_HideWindow:
321 this->subscribe(LibHomeScreen::event_list[3]);
323 case Event_ReplyShowWindow:
324 this->subscribe(LibHomeScreen::event_list[4]);
326 case Event_ShowNotification:
327 this->subscribe(LibHomeScreen::event_list[5]);
329 case Event_ShowInformation:
330 this->subscribe(LibHomeScreen::event_list[6]);
332 case Event_AppListChanged:
333 this->subscribe(LibHomeScreen::event_list[7]);
337 this->handlers[et] = std::move(f);
342 * This function calls the API of HomeScreen via WebSocket
345 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
346 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
349 * - Returns 0 on success or -1 in case of error.
352 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
355 int LibHomeScreen::call(const string& verb, struct json_object* arg)
364 HMI_ERROR("libhomescreen","verb doesn't exit");
367 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
369 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
375 * This function calls the API of HomeScreen via WebSocket
376 * This function is overload function of "call"
379 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
380 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
383 * - Returns 0 on success or -1 in case of error.
386 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
389 int LibHomeScreen::call(const char* verb, struct json_object* arg)
396 if (!has_verb(string(verb)))
398 HMI_ERROR("libhomescreen","verb doesn't exit");
401 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
403 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
409 * Register callback function for each event
412 * - event_name [in] : This argument should be specified to the event name
415 * - Returns 0 on success or -1 in case of error.
418 * This function enables to get an event to your callback function.
421 int LibHomeScreen::subscribe(const string& event_name)
427 struct json_object* j_obj = json_object_new_object();
428 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
430 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
432 HMI_ERROR("libhomescreen","Failed to call verb");
438 * Unregister callback function for each event
441 * - event_name [in] : This argument should be specified to the event name
444 * - Returns 0 on success or -1 in case of error.
447 * This function disables to get an event to your callback function.
450 int LibHomeScreen::unsubscribe(const string& event_name)
456 struct json_object* j_obj = json_object_new_object();
457 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
459 int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
461 HMI_ERROR("libhomescreen","Failed to call verb");
467 * Sending show window event
469 * Call HomeScreen Service's showWindow verb to request display id's screen.
472 * - application_id [in] : This argument should be specified to the application's id.
473 * - json [in] : This argument should be specified to the json parameters.
476 * - Returns 0 on success or -1 in case of error.
479 int LibHomeScreen::showWindow(const char* application_id, json_object* json)
486 struct json_object* j_obj = json_object_new_object();
487 struct json_object* val = json_object_new_string(application_id);
488 json_object_object_add(j_obj, ApplicationId, val);
490 if (json == nullptr) {
491 struct json_object* j_json = json_object_new_object();
492 struct json_object* value = json_object_new_string("normal");
493 json_object_object_add(j_json, "area", value);
494 json_object_object_add(j_obj, "parameter", j_json);
497 json_object_object_add(j_obj, "parameter", json);
500 return this->call("showWindow", j_obj);
504 * Sending hide window event
506 * Call HomeScreen Service's hideWindow verb to release id's screen.
509 * - application_id [in] : This argument should be specified to the application's id.
512 * - Returns 0 on success or -1 in case of error.
515 int LibHomeScreen::hideWindow(const char* application_id)
522 struct json_object* j_obj = json_object_new_object();
523 struct json_object* val = json_object_new_string(application_id);
524 json_object_object_add(j_obj, ApplicationId, val);
526 return this->call("hideWindow", j_obj);
530 * Sending reply onscreen message event
532 * Call HomeScreen Service's replyShowWindow verb to reply onscreen message.
535 * - application_id [in] : This argument should be specified to the onscreen reply to applilcation id.
536 * - json [in] : This argument should be specified to the json parameters.
539 * - Returns 0 on success or -1 in case of error.
542 int LibHomeScreen::replyShowWindow(const char* application_id, json_object* json)
549 if (json == nullptr) {
550 HMI_WARNING("libhomescreen", "replyShowWindow`s parameter is null");
554 struct json_object* j_obj = json_object_new_object();
555 struct json_object* val = json_object_new_string(application_id);
556 json_object_object_add(j_obj, ApplicationId, val);
557 json_object_object_add(j_obj, "parameter", json);
559 return this->call("replyShowWindow", j_obj);
563 * Sending show notification event
565 * Call HomeScreen Service's notification verb to show notification on Status Bar.
568 * - json [in] : This argument should be specified to the json parameters.
571 * - Returns 0 on success or -1 in case of error.
574 int LibHomeScreen::showNotification(json_object* json)
581 return this->call("showNotification", json);
585 * Sending show information event
587 * Call HomeScreen Service's information verb to show notification on Information Bar.
590 * - json [in] : This argument should be specified to the json parameters.
593 * - Returns 0 on success or -1 in case of error.
596 int LibHomeScreen::showInformation(json_object* json)
603 return this->call("showInformation", json);
609 * Call HomeScreen Service's getRunnables verb to get runnalbes list.
615 * - Returns 0 on success or -1 in case of error.
618 int LibHomeScreen::getRunnables(void)
620 return this->call("getRunnables", nullptr);
624 /************* Callback Function *************/
626 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
628 HMI_DEBUG("libhomescreen","called");
629 if(onHangup != nullptr)
635 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
640 * event is like "homescreen/hvac"
641 * msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
643 event name : struct json_object obj = json_object_object_get(msg,"event")
645 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
647 HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
649 if (strstr(event, API) == NULL) {
653 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
654 struct json_object *json_data;
655 if(!json_object_object_get_ex(ev_contents, "data", &json_data)) {
656 HMI_ERROR("libhomescreen", "got ev_contents error.");
660 if(onEvent != nullptr)
662 const string ev(event);
663 onEvent(ev, ev_contents);
666 const char* event_type = nullptr;
667 struct json_object *json_event_type;
668 if(json_object_object_get_ex(json_data, "type", &json_event_type)) {
669 event_type = json_object_get_string(json_event_type);
672 HMI_WARNING("libhomescreen","event_type is null.");
676 if (strcasecmp(event_type, LibHomeScreen::event_list[0].c_str()) == 0) {
677 auto i = this->handlers.find(Event_ShowWindow);
678 if ( i != this->handlers.end() ) {
679 i->second(json_data);
682 else if (strcasecmp(event_type, LibHomeScreen::event_list[1].c_str()) == 0) {
683 auto i = this->handlers.find(Event_OnScreenMessage);
684 if ( i != this->handlers.end() ) {
685 i->second(json_data);
688 else if (strcasecmp(event_type, LibHomeScreen::event_list[2].c_str()) == 0) {
689 auto i = this->handlers.find(Event_OnScreenReply);
690 if ( i != this->handlers.end() ) {
691 i->second(json_data);
694 else if (strcasecmp(event_type, LibHomeScreen::event_list[3].c_str()) == 0) {
695 auto i = this->handlers.find(Event_HideWindow);
696 if ( i != this->handlers.end() ) {
697 i->second(json_data);
700 else if (strcasecmp(event_type, LibHomeScreen::event_list[4].c_str()) == 0) {
701 auto i = this->handlers.find(Event_ReplyShowWindow);
702 if ( i != this->handlers.end() ) {
703 i->second(json_data);
706 else if (strcasecmp(event_type, LibHomeScreen::event_list[5].c_str()) == 0) {
707 auto i = this->handlers.find(Event_ShowNotification);
708 if ( i != this->handlers.end() ) {
709 i->second(json_data);
712 else if (strcasecmp(event_type, LibHomeScreen::event_list[6].c_str()) == 0) {
713 auto i = this->handlers.find(Event_ShowInformation);
714 if ( i != this->handlers.end() ) {
715 i->second(json_data);
718 else if (strcasecmp(event_type, LibHomeScreen::event_list[7].c_str()) == 0) {
719 auto i = this->handlers.find(Event_AppListChanged);
720 if ( i != this->handlers.end() ) {
721 i->second(json_data);
727 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
728 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
730 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
732 HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
733 if(onReply != nullptr)
735 struct json_object* reply = afb_wsj1_msg_object_j(msg);
740 static bool has_verb(const string& verb)
742 HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
743 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())