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"),
54 std::string("updateShortcut")
57 const std::vector<std::string> LibHomeScreen::event_list {
58 // std::string("tap_shortcut"),
59 std::string("showWindow"),
60 std::string("on_screen_message"),
61 std::string("on_screen_reply"),
62 std::string("hideWindow"),
63 std::string("replyShowWindow"),
64 std::string("showNotification"),
65 std::string("showInformation"),
66 std::string("application-list-changed"),
67 std::string("registerShortcut"),
68 std::string("updateShortcut"),
77 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
79 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
82 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
84 /* LibHomeScreen is not called from other process */
87 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
89 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
92 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
94 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
98 static void event_loop_run(struct sd_event* loop){
100 sd_event_unref(loop);
107 LibHomeScreen::LibHomeScreen()
114 LibHomeScreen::~LibHomeScreen()
116 if(sp_websock != NULL)
118 afb_wsj1_unref(sp_websock);
122 sd_event_exit(mploop, 0);
127 * This function is initializer
130 * - port [in] : This argument should be specified to the port number to be used for websocket
131 * - token [in] : This argument should be specified to the token to be used for websocket
137 * Use this constructor
140 int LibHomeScreen::init(const int port, const string& token)
143 if(port > 0 && token.size() > 0)
150 HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
153 ret = initialize_websocket();
156 HMI_ERROR("libhomescreen","Failed to initialize websocket");
159 HMI_DEBUG("libhomescreen","Initialized");
166 * This function register callback function for reply/event message from home screen
169 * - event_cb [in] : This argument should be specified to the callback for subscribed event
170 * - reply_cb [in] : This argument should be specified to the reply callback for call function
176 * Event callback is invoked by home screen for event you subscribed.
177 * If you would like to get event, please call subscribe function before/after this function
179 void LibHomeScreen::registerCallback(
180 void (*event_cb)(const std::string& event, struct json_object* event_contents),
181 void (*reply_cb)(struct json_object* reply_contents),
182 void (*hangup_cb)(void))
186 onHangup = hangup_cb;
189 int LibHomeScreen::initialize_websocket()
194 int ret = sd_event_new(&mploop);
197 HMI_ERROR("libhomescreen","Failed to create event loop");
202 // enforce context to avoid initialization/goto error
203 std::thread th(event_loop_run, mploop);
207 /* Initialize interface from websocket */
208 minterface.on_hangup = _on_hangup_static;
209 minterface.on_call = _on_call_static;
210 minterface.on_event = _on_event_static;
211 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
212 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
213 if(sp_websock == NULL)
215 HMI_ERROR("libhomescreen","Failed to create websocket connection");
219 /* creates the evsrc */
220 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
228 * Sending ShortCut Icon tapped event
230 * When HomeScreen shortcut area is tapped, sending a event
233 * - application_id [in] : Tapped application id (label)
236 * - Returns 0 on success or -1 in case of error.
238 int LibHomeScreen::tapShortcut(const char* application_id)
240 struct json_object* obj = json_object_new_object();
241 struct json_object* val = json_object_new_string("normal");
242 json_object_object_add(obj, "area", val);
244 return showWindow(application_id, obj);
248 * Sending onScreen message event
250 * Sending OnScreen message event to HomeScreen from applications
253 * - display_message [in] : message for display
256 * - Returns 0 on success or -1 in case of error.
258 int LibHomeScreen::onScreenMessage(const char* display_message)
265 struct json_object* j_obj = json_object_new_object();
266 struct json_object* val = json_object_new_string(display_message);
267 json_object_object_add(j_obj, "display_message", val);
268 return this->call("on_screen_message", j_obj);
272 * Sending onScreen reply event
274 * Sending OnScreen reply event to applications from HomeScreen
277 * - reply_message [in] : message for reply
280 * - Returns 0 on success or -1 in case of error.
282 int LibHomeScreen::onScreenReply(const char* reply_message)
289 struct json_object* j_obj = json_object_new_object();
290 struct json_object* val = json_object_new_string(reply_message);
291 json_object_object_add(j_obj, "reply_message", val);
292 return this->call("on_screen_reply", j_obj);
296 * Setting Event Handler
298 * Setting event handler for Homescreen
301 * - et [in] : event name
302 * - f [in] : event handler
308 * Don't release json_object by json_object_put in handler_func.
309 * The resource is released by libafbwsc library.
311 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
313 if (et > Event_Min && et < Event_Max) {
314 this->handlers[et] = std::move(f);
319 * This function subscribe HomeScreen event
328 * To call HomeScreen's subscribe APIs.
331 void LibHomeScreen::publishSubscription(void)
333 for(auto &it : handlers) {
334 this->subscribe(LibHomeScreen::event_list[it.first - 1]);
339 * This function calls the API of HomeScreen via WebSocket
342 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
343 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
346 * - Returns 0 on success or -1 in case of error.
349 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
352 int LibHomeScreen::call(const string& verb, struct json_object* arg)
361 HMI_ERROR("libhomescreen","verb doesn't exit");
364 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
366 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
372 * This function calls the API of HomeScreen via WebSocket
373 * This function is overload function of "call"
376 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
377 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
380 * - Returns 0 on success or -1 in case of error.
383 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
386 int LibHomeScreen::call(const char* verb, struct json_object* arg)
393 if (!has_verb(string(verb)))
395 HMI_ERROR("libhomescreen","verb doesn't exit");
398 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
400 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
406 * Register callback function for each event
409 * - event_name [in] : This argument should be specified to the event name
412 * - Returns 0 on success or -1 in case of error.
415 * This function enables to get an event to your callback function.
418 int LibHomeScreen::subscribe(const string& event_name)
424 struct json_object* j_obj = json_object_new_object();
425 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
427 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
429 HMI_ERROR("libhomescreen","Failed to call verb");
435 * Unregister callback function for each event
438 * - event_name [in] : This argument should be specified to the event name
441 * - Returns 0 on success or -1 in case of error.
444 * This function disables to get an event to your callback function.
447 int LibHomeScreen::unsubscribe(const string& event_name)
453 struct json_object* j_obj = json_object_new_object();
454 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
456 int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
458 HMI_ERROR("libhomescreen","Failed to call verb");
464 * Sending show window event
466 * Call HomeScreen Service's showWindow verb to request display id's screen.
469 * - application_id [in] : This argument should be specified to the application's id.
470 * - json [in] : This argument should be specified to the json parameters.
473 * - Returns 0 on success or -1 in case of error.
476 int LibHomeScreen::showWindow(const char* application_id, json_object* json)
483 struct json_object* j_obj = json_object_new_object();
484 struct json_object* val = json_object_new_string(application_id);
485 json_object_object_add(j_obj, ApplicationId, val);
487 if (json == nullptr) {
488 struct json_object* j_json = json_object_new_object();
489 struct json_object* value = json_object_new_string("normal");
490 json_object_object_add(j_json, "area", value);
491 json_object_object_add(j_obj, "parameter", j_json);
494 json_object_object_add(j_obj, "parameter", json);
497 return this->call("showWindow", j_obj);
501 * Sending hide window event
503 * Call HomeScreen Service's hideWindow verb to release id's screen.
506 * - application_id [in] : This argument should be specified to the application's id.
509 * - Returns 0 on success or -1 in case of error.
512 int LibHomeScreen::hideWindow(const char* application_id)
519 struct json_object* j_obj = json_object_new_object();
520 struct json_object* val = json_object_new_string(application_id);
521 json_object_object_add(j_obj, ApplicationId, val);
523 return this->call("hideWindow", j_obj);
527 * Sending reply onscreen message event
529 * Call HomeScreen Service's replyShowWindow verb to reply onscreen message.
532 * - application_id [in] : This argument should be specified to the onscreen reply to applilcation id.
533 * - json [in] : This argument should be specified to the json parameters.
536 * - Returns 0 on success or -1 in case of error.
539 int LibHomeScreen::replyShowWindow(const char* application_id, json_object* json)
546 if (json == nullptr) {
547 HMI_WARNING("libhomescreen", "replyShowWindow`s parameter is null");
551 struct json_object* j_obj = json_object_new_object();
552 struct json_object* val = json_object_new_string(application_id);
553 json_object_object_add(j_obj, ApplicationId, val);
554 json_object_object_add(j_obj, "parameter", json);
556 return this->call("replyShowWindow", j_obj);
560 * Sending show notification event
562 * Call HomeScreen Service's notification verb to show notification on Status Bar.
565 * - json [in] : This argument should be specified to the json parameters.
568 * - Returns 0 on success or -1 in case of error.
571 int LibHomeScreen::showNotification(json_object* json)
578 return this->call("showNotification", json);
582 * Sending show information event
584 * Call HomeScreen Service's information verb to show notification on Information Bar.
587 * - json [in] : This argument should be specified to the json parameters.
590 * - Returns 0 on success or -1 in case of error.
593 int LibHomeScreen::showInformation(json_object* json)
600 return this->call("showInformation", json);
606 * Call HomeScreen Service's getRunnables verb to get runnalbes list.
612 * - Returns 0 on success or -1 in case of error.
615 int LibHomeScreen::getRunnables(void)
617 return this->call("getRunnables", nullptr);
621 * register shortcut to homescreen
623 * Call HomeScreen Service's registerShortcut verb to regitster shortcut.
626 * - json [in] : This argument should be specified to the json parameters.
629 * - Returns 0 on success or -1 in case of error.
632 int LibHomeScreen::registerShortcut(const char* application_id, json_object* json)
639 struct json_object* j_obj = json_object_new_object();
640 struct json_object* val = json_object_new_string(application_id);
641 json_object_object_add(j_obj, ApplicationId, val);
642 json_object_object_add(j_obj, "parameter", json);
644 return this->call("registerShortcut", j_obj);
649 * update shortcut to launcher
651 * Call HomeScreen Service's updateShortcut verb to update shortcut.
654 * - json [in] : This argument should be specified to the json parameters.
657 * - Returns 0 on success or -1 in case of error.
660 int LibHomeScreen::updateShortcut(const char* application_id, json_object* json)
667 struct json_object* j_obj = json_object_new_object();
668 struct json_object* val = json_object_new_string(application_id);
669 json_object_object_add(j_obj, ApplicationId, val);
670 json_object_object_add(j_obj, "parameter", json);
672 return this->call("updateShortcut", j_obj);
676 /************* Callback Function *************/
678 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
680 HMI_DEBUG("libhomescreen","called");
681 if(onHangup != nullptr)
687 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
692 * event is like "homescreen/hvac"
693 * msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
695 event name : struct json_object obj = json_object_object_get(msg,"event")
697 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
699 HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
701 if (strstr(event, API) == NULL) {
705 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
706 struct json_object *json_data;
707 if(!json_object_object_get_ex(ev_contents, "data", &json_data)) {
708 HMI_ERROR("libhomescreen", "got ev_contents error.");
712 if(onEvent != nullptr)
714 const string ev(event);
715 onEvent(ev, ev_contents);
718 const char* event_type = nullptr;
719 struct json_object *json_event_type;
720 if(json_object_object_get_ex(json_data, "type", &json_event_type)) {
721 event_type = json_object_get_string(json_event_type);
724 HMI_WARNING("libhomescreen","event_type is null.");
728 int e_type = getEventType(event_type);
729 if(e_type < Event_Max) {
730 auto it = this->handlers.find(EventType(e_type));
731 if ( it != this->handlers.end() ) {
732 it->second(json_data);
738 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
739 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
741 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
743 HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
744 if(onReply != nullptr)
746 struct json_object* reply = afb_wsj1_msg_object_j(msg);
752 * convert event name to event type
754 int LibHomeScreen::getEventType(const char *event)
757 for(; i < LibHomeScreen::event_list.size(); ++i) {
758 if (strcasecmp(event, LibHomeScreen::event_list[i].c_str()) == 0) {
762 return (i + 1) < Event_Max ? i : Event_Max;
765 static bool has_verb(const string& verb)
767 HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
768 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())