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"),
53 std::string("registerShortcut")
56 const std::vector<std::string> LibHomeScreen::event_list {
57 // std::string("tap_shortcut"),
58 std::string("showWindow"),
59 std::string("on_screen_message"),
60 std::string("on_screen_reply"),
61 std::string("hideWindow"),
62 std::string("replyShowWindow"),
63 std::string("showNotification"),
64 std::string("showInformation"),
65 std::string("application-list-changed"),
66 std::string("registerShortcut"),
75 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
77 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
80 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
82 /* LibHomeScreen is not called from other process */
85 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
87 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
90 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
92 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
96 static void event_loop_run(struct sd_event* loop){
105 LibHomeScreen::LibHomeScreen()
112 LibHomeScreen::~LibHomeScreen()
114 if(sp_websock != NULL)
116 afb_wsj1_unref(sp_websock);
120 sd_event_exit(mploop, 0);
125 * This function is initializer
128 * - port [in] : This argument should be specified to the port number to be used for websocket
129 * - token [in] : This argument should be specified to the token to be used for websocket
135 * Use this constructor
138 int LibHomeScreen::init(const int port, const string& token)
141 if(port > 0 && token.size() > 0)
148 HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
151 ret = initialize_websocket();
154 HMI_ERROR("libhomescreen","Failed to initialize websocket");
157 HMI_DEBUG("libhomescreen","Initialized");
164 * This function register callback function for reply/event message from home screen
167 * - event_cb [in] : This argument should be specified to the callback for subscribed event
168 * - reply_cb [in] : This argument should be specified to the reply callback for call function
174 * Event callback is invoked by home screen for event you subscribed.
175 * If you would like to get event, please call subscribe function before/after this function
177 void LibHomeScreen::registerCallback(
178 void (*event_cb)(const std::string& event, struct json_object* event_contents),
179 void (*reply_cb)(struct json_object* reply_contents),
180 void (*hangup_cb)(void))
184 onHangup = hangup_cb;
187 int LibHomeScreen::initialize_websocket()
192 int ret = sd_event_new(&mploop);
195 HMI_ERROR("libhomescreen","Failed to create event loop");
200 // enforce context to avoid initialization/goto error
201 std::thread th(event_loop_run, mploop);
205 /* Initialize interface from websocket */
206 minterface.on_hangup = _on_hangup_static;
207 minterface.on_call = _on_call_static;
208 minterface.on_event = _on_event_static;
209 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
210 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
211 if(sp_websock == NULL)
213 HMI_ERROR("libhomescreen","Failed to create websocket connection");
217 /* creates the evsrc */
218 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
226 * Sending ShortCut Icon tapped event
228 * When HomeScreen shortcut area is tapped, sending a event
231 * - application_id [in] : Tapped application id (label)
234 * - Returns 0 on success or -1 in case of error.
236 int LibHomeScreen::tapShortcut(const char* application_id)
238 struct json_object* obj = json_object_new_object();
239 struct json_object* val = json_object_new_string("normal");
240 json_object_object_add(obj, "area", val);
242 return showWindow(application_id, obj);
246 * Sending onScreen message event
248 * Sending OnScreen message event to HomeScreen from applications
251 * - display_message [in] : message for display
254 * - Returns 0 on success or -1 in case of error.
256 int LibHomeScreen::onScreenMessage(const char* display_message)
263 struct json_object* j_obj = json_object_new_object();
264 struct json_object* val = json_object_new_string(display_message);
265 json_object_object_add(j_obj, "display_message", val);
266 return this->call("on_screen_message", j_obj);
270 * Sending onScreen reply event
272 * Sending OnScreen reply event to applications from HomeScreen
275 * - reply_message [in] : message for reply
278 * - Returns 0 on success or -1 in case of error.
280 int LibHomeScreen::onScreenReply(const char* reply_message)
287 struct json_object* j_obj = json_object_new_object();
288 struct json_object* val = json_object_new_string(reply_message);
289 json_object_object_add(j_obj, "reply_message", val);
290 return this->call("on_screen_reply", j_obj);
294 * Setting Event Handler
296 * Setting event handler for Homescreen
299 * - et [in] : event name
300 * - f [in] : event handler
306 * Don't release json_object by json_object_put in handler_func.
307 * The resource is released by libafbwsc library.
309 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
311 if (et > Event_Min && et < Event_Max) {
313 case Event_ShowWindow:
314 this->subscribe(LibHomeScreen::event_list[0]);
316 case Event_OnScreenMessage:
317 this->subscribe(LibHomeScreen::event_list[1]);
319 case Event_OnScreenReply:
320 this->subscribe(LibHomeScreen::event_list[2]);
322 case Event_HideWindow:
323 this->subscribe(LibHomeScreen::event_list[3]);
325 case Event_ReplyShowWindow:
326 this->subscribe(LibHomeScreen::event_list[4]);
328 case Event_ShowNotification:
329 this->subscribe(LibHomeScreen::event_list[5]);
331 case Event_ShowInformation:
332 this->subscribe(LibHomeScreen::event_list[6]);
334 case Event_AppListChanged:
335 this->subscribe(LibHomeScreen::event_list[7]);
337 case Event_RegisterShortcut:
338 this->subscribe(LibHomeScreen::event_list[8]);
342 this->handlers[et] = std::move(f);
347 * This function calls the API of HomeScreen via WebSocket
350 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
351 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
354 * - Returns 0 on success or -1 in case of error.
357 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
360 int LibHomeScreen::call(const string& verb, struct json_object* arg)
369 HMI_ERROR("libhomescreen","verb doesn't exit");
372 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
374 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
380 * This function calls the API of HomeScreen via WebSocket
381 * This function is overload function of "call"
384 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
385 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
388 * - Returns 0 on success or -1 in case of error.
391 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
394 int LibHomeScreen::call(const char* verb, struct json_object* arg)
401 if (!has_verb(string(verb)))
403 HMI_ERROR("libhomescreen","verb doesn't exit");
406 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
408 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
414 * Register callback function for each event
417 * - event_name [in] : This argument should be specified to the event name
420 * - Returns 0 on success or -1 in case of error.
423 * This function enables to get an event to your callback function.
426 int LibHomeScreen::subscribe(const string& event_name)
432 struct json_object* j_obj = json_object_new_object();
433 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
435 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
437 HMI_ERROR("libhomescreen","Failed to call verb");
443 * Unregister callback function for each event
446 * - event_name [in] : This argument should be specified to the event name
449 * - Returns 0 on success or -1 in case of error.
452 * This function disables to get an event to your callback function.
455 int LibHomeScreen::unsubscribe(const string& event_name)
461 struct json_object* j_obj = json_object_new_object();
462 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
464 int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
466 HMI_ERROR("libhomescreen","Failed to call verb");
472 * Sending show window event
474 * Call HomeScreen Service's showWindow verb to request display id's screen.
477 * - application_id [in] : This argument should be specified to the application's id.
478 * - json [in] : This argument should be specified to the json parameters.
481 * - Returns 0 on success or -1 in case of error.
484 int LibHomeScreen::showWindow(const char* application_id, json_object* json)
491 struct json_object* j_obj = json_object_new_object();
492 struct json_object* val = json_object_new_string(application_id);
493 json_object_object_add(j_obj, ApplicationId, val);
495 if (json == nullptr) {
496 struct json_object* j_json = json_object_new_object();
497 struct json_object* value = json_object_new_string("normal");
498 json_object_object_add(j_json, "area", value);
499 json_object_object_add(j_obj, "parameter", j_json);
502 json_object_object_add(j_obj, "parameter", json);
505 return this->call("showWindow", j_obj);
509 * Sending hide window event
511 * Call HomeScreen Service's hideWindow verb to release id's screen.
514 * - application_id [in] : This argument should be specified to the application's id.
517 * - Returns 0 on success or -1 in case of error.
520 int LibHomeScreen::hideWindow(const char* application_id)
527 struct json_object* j_obj = json_object_new_object();
528 struct json_object* val = json_object_new_string(application_id);
529 json_object_object_add(j_obj, ApplicationId, val);
531 return this->call("hideWindow", j_obj);
535 * Sending reply onscreen message event
537 * Call HomeScreen Service's replyShowWindow verb to reply onscreen message.
540 * - application_id [in] : This argument should be specified to the onscreen reply to applilcation id.
541 * - json [in] : This argument should be specified to the json parameters.
544 * - Returns 0 on success or -1 in case of error.
547 int LibHomeScreen::replyShowWindow(const char* application_id, json_object* json)
554 if (json == nullptr) {
555 HMI_WARNING("libhomescreen", "replyShowWindow`s parameter is null");
559 struct json_object* j_obj = json_object_new_object();
560 struct json_object* val = json_object_new_string(application_id);
561 json_object_object_add(j_obj, ApplicationId, val);
562 json_object_object_add(j_obj, "parameter", json);
564 return this->call("replyShowWindow", j_obj);
568 * Sending show notification event
570 * Call HomeScreen Service's notification verb to show notification on Status Bar.
573 * - json [in] : This argument should be specified to the json parameters.
576 * - Returns 0 on success or -1 in case of error.
579 int LibHomeScreen::showNotification(json_object* json)
586 return this->call("showNotification", json);
590 * Sending show information event
592 * Call HomeScreen Service's information verb to show notification on Information Bar.
595 * - json [in] : This argument should be specified to the json parameters.
598 * - Returns 0 on success or -1 in case of error.
601 int LibHomeScreen::showInformation(json_object* json)
608 return this->call("showInformation", json);
614 * Call HomeScreen Service's getRunnables verb to get runnalbes list.
620 * - Returns 0 on success or -1 in case of error.
623 int LibHomeScreen::getRunnables(void)
625 return this->call("getRunnables", nullptr);
629 * register shortcut to homescreen
631 * Call HomeScreen Service's registerShortcut verb to regitster shortcut.
634 * - json [in] : This argument should be specified to the json parameters.
637 * - Returns 0 on success or -1 in case of error.
640 int LibHomeScreen::registerShortcut(json_object* json)
647 return this->call("registerShortcut", json);
650 /************* Callback Function *************/
652 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
654 HMI_DEBUG("libhomescreen","called");
655 if(onHangup != nullptr)
661 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
666 * event is like "homescreen/hvac"
667 * msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
669 event name : struct json_object obj = json_object_object_get(msg,"event")
671 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
673 HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
675 if (strstr(event, API) == NULL) {
679 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
680 struct json_object *json_data;
681 if(!json_object_object_get_ex(ev_contents, "data", &json_data)) {
682 HMI_ERROR("libhomescreen", "got ev_contents error.");
686 if(onEvent != nullptr)
688 const string ev(event);
689 onEvent(ev, ev_contents);
692 const char* event_type = nullptr;
693 struct json_object *json_event_type;
694 if(json_object_object_get_ex(json_data, "type", &json_event_type)) {
695 event_type = json_object_get_string(json_event_type);
698 HMI_WARNING("libhomescreen","event_type is null.");
702 if (strcasecmp(event_type, LibHomeScreen::event_list[0].c_str()) == 0) {
703 auto i = this->handlers.find(Event_ShowWindow);
704 if ( i != this->handlers.end() ) {
705 i->second(json_data);
708 else if (strcasecmp(event_type, LibHomeScreen::event_list[1].c_str()) == 0) {
709 auto i = this->handlers.find(Event_OnScreenMessage);
710 if ( i != this->handlers.end() ) {
711 i->second(json_data);
714 else if (strcasecmp(event_type, LibHomeScreen::event_list[2].c_str()) == 0) {
715 auto i = this->handlers.find(Event_OnScreenReply);
716 if ( i != this->handlers.end() ) {
717 i->second(json_data);
720 else if (strcasecmp(event_type, LibHomeScreen::event_list[3].c_str()) == 0) {
721 auto i = this->handlers.find(Event_HideWindow);
722 if ( i != this->handlers.end() ) {
723 i->second(json_data);
726 else if (strcasecmp(event_type, LibHomeScreen::event_list[4].c_str()) == 0) {
727 auto i = this->handlers.find(Event_ReplyShowWindow);
728 if ( i != this->handlers.end() ) {
729 i->second(json_data);
732 else if (strcasecmp(event_type, LibHomeScreen::event_list[5].c_str()) == 0) {
733 auto i = this->handlers.find(Event_ShowNotification);
734 if ( i != this->handlers.end() ) {
735 i->second(json_data);
738 else if (strcasecmp(event_type, LibHomeScreen::event_list[6].c_str()) == 0) {
739 auto i = this->handlers.find(Event_ShowInformation);
740 if ( i != this->handlers.end() ) {
741 i->second(json_data);
744 else if (strcasecmp(event_type, LibHomeScreen::event_list[7].c_str()) == 0) {
745 auto i = this->handlers.find(Event_AppListChanged);
746 if ( i != this->handlers.end() ) {
747 i->second(json_data);
750 else if (strcasecmp(event_type, LibHomeScreen::event_list[8].c_str()) == 0) {
751 auto i = this->handlers.find(Event_RegisterShortcut);
752 if ( i != this->handlers.end() ) {
753 i->second(json_data);
759 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
760 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
762 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
764 HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
765 if(onReply != nullptr)
767 struct json_object* reply = afb_wsj1_msg_object_j(msg);
772 static bool has_verb(const string& verb)
774 HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
775 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())