2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/socket.h>
29 #include <libhomescreen.hpp>
30 #include "hmi-debug.h"
34 static bool has_verb(const string& verb);
35 static const char API[] = "homescreen";
36 static const char ApplicationId[] = "application_id";
38 const std::vector<std::string> LibHomeScreen::api_list {
39 std::string("ping"), // debug do not use
40 std::string("tap_shortcut"), // HomeScreen Application only
41 std::string("on_screen_message"),
42 std::string("on_screen_reply"),
43 std::string("subscribe"),
44 std::string("unsubscribe"),
45 std::string("showWindow"),
46 std::string("hideWindow"),
47 std::string("replyShowWindow"),
48 std::string("showNotification"),
49 std::string("showInformation"),
50 std::string("getRunnables")
53 const std::vector<std::string> LibHomeScreen::event_list {
54 // std::string("tap_shortcut"),
55 std::string("showWindow"),
56 std::string("on_screen_message"),
57 std::string("on_screen_reply"),
58 std::string("hideWindow"),
59 std::string("replyShowWindow"),
60 std::string("showNotification"),
61 std::string("showInformation"),
62 std::string("application-list-changed"),
71 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
73 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
76 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
78 /* LibHomeScreen is not called from other process */
81 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
83 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
86 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
88 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
95 LibHomeScreen::LibHomeScreen()
102 LibHomeScreen::~LibHomeScreen()
106 sd_event_unref(mploop);
108 if(sp_websock != NULL)
110 afb_wsj1_unref(sp_websock);
115 * This function is initializer
118 * - port [in] : This argument should be specified to the port number to be used for websocket
119 * - token [in] : This argument should be specified to the token to be used for websocket
125 * Use this constructor
128 int LibHomeScreen::init(const int port, const string& token)
131 if(port > 0 && token.size() > 0)
138 HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
141 ret = initialize_websocket();
144 HMI_ERROR("libhomescreen","Failed to initialize websocket");
147 HMI_DEBUG("libhomescreen","Initialized");
154 * This function register callback function for reply/event message from home screen
157 * - event_cb [in] : This argument should be specified to the callback for subscribed event
158 * - reply_cb [in] : This argument should be specified to the reply callback for call function
164 * Event callback is invoked by home screen for event you subscribed.
165 * If you would like to get event, please call subscribe function before/after this function
167 void LibHomeScreen::registerCallback(
168 void (*event_cb)(const std::string& event, struct json_object* event_contents),
169 void (*reply_cb)(struct json_object* reply_contents),
170 void (*hangup_cb)(void))
174 onHangup = hangup_cb;
177 int LibHomeScreen::initialize_websocket()
182 int ret = sd_event_default(&mploop);
185 HMI_ERROR("libhomescreen","Failed to create event loop");
189 /* Initialize interface from websocket */
190 minterface.on_hangup = _on_hangup_static;
191 minterface.on_call = _on_call_static;
192 minterface.on_event = _on_event_static;
193 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
194 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
195 if(sp_websock == NULL)
197 HMI_ERROR("libhomescreen","Failed to create websocket connection");
201 /* creates the evsrc */
202 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
208 sd_event_unref(mploop);
214 * Sending ShortCut Icon tapped event
216 * When HomeScreen shortcut area is tapped, sending a event
219 * - application_id [in] : Tapped application id (label)
222 * - Returns 0 on success or -1 in case of error.
224 int LibHomeScreen::tapShortcut(const char* application_id)
226 struct json_object* obj = json_object_new_object();
227 struct json_object* val = json_object_new_string("normal");
228 json_object_object_add(obj, "area", val);
230 return showWindow(application_id, obj);
234 * Sending onScreen message event
236 * Sending OnScreen message event to HomeScreen from applications
239 * - display_message [in] : message for display
242 * - Returns 0 on success or -1 in case of error.
244 int LibHomeScreen::onScreenMessage(const char* display_message)
251 struct json_object* j_obj = json_object_new_object();
252 struct json_object* val = json_object_new_string(display_message);
253 json_object_object_add(j_obj, "display_message", val);
254 return this->call("on_screen_message", j_obj);
258 * Sending onScreen reply event
260 * Sending OnScreen reply event to applications from HomeScreen
263 * - reply_message [in] : message for reply
266 * - Returns 0 on success or -1 in case of error.
268 int LibHomeScreen::onScreenReply(const char* reply_message)
275 struct json_object* j_obj = json_object_new_object();
276 struct json_object* val = json_object_new_string(reply_message);
277 json_object_object_add(j_obj, "reply_message", val);
278 return this->call("on_screen_reply", j_obj);
282 * Setting Event Handler
284 * Setting event handler for Homescreen
287 * - et [in] : event name
288 * - f [in] : event handler
294 * Don't release json_object by json_object_put in handler_func.
295 * The resource is released by libafbwsc library.
297 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
299 if (et > Event_Min && et < Event_Max) {
301 case Event_ShowWindow:
302 this->subscribe(LibHomeScreen::event_list[0]);
304 case Event_OnScreenMessage:
305 this->subscribe(LibHomeScreen::event_list[1]);
307 case Event_OnScreenReply:
308 this->subscribe(LibHomeScreen::event_list[2]);
310 case Event_HideWindow:
311 this->subscribe(LibHomeScreen::event_list[3]);
313 case Event_ReplyShowWindow:
314 this->subscribe(LibHomeScreen::event_list[4]);
316 case Event_ShowNotification:
317 this->subscribe(LibHomeScreen::event_list[5]);
319 case Event_ShowInformation:
320 this->subscribe(LibHomeScreen::event_list[6]);
322 case Event_AppListChanged:
323 this->subscribe(LibHomeScreen::event_list[7]);
327 this->handlers[et] = std::move(f);
332 * This function calls the API of HomeScreen via WebSocket
335 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
336 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
339 * - Returns 0 on success or -1 in case of error.
342 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
345 int LibHomeScreen::call(const string& verb, struct json_object* arg)
354 HMI_ERROR("libhomescreen","verb doesn't exit");
357 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
359 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
365 * This function calls the API of HomeScreen via WebSocket
366 * This function is overload function of "call"
369 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
370 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
373 * - Returns 0 on success or -1 in case of error.
376 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
379 int LibHomeScreen::call(const char* verb, struct json_object* arg)
386 if (!has_verb(string(verb)))
388 HMI_ERROR("libhomescreen","verb doesn't exit");
391 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
393 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
399 * Register callback function for each event
402 * - event_name [in] : This argument should be specified to the event name
405 * - Returns 0 on success or -1 in case of error.
408 * This function enables to get an event to your callback function.
411 int LibHomeScreen::subscribe(const string& event_name)
417 struct json_object* j_obj = json_object_new_object();
418 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
420 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
422 HMI_ERROR("libhomescreen","Failed to call verb");
428 * Unregister callback function for each event
431 * - event_name [in] : This argument should be specified to the event name
434 * - Returns 0 on success or -1 in case of error.
437 * This function disables to get an event to your callback function.
440 int LibHomeScreen::unsubscribe(const string& event_name)
446 struct json_object* j_obj = json_object_new_object();
447 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
449 int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
451 HMI_ERROR("libhomescreen","Failed to call verb");
457 * Sending show window event
459 * Call HomeScreen Service's showWindow verb to request display id's screen.
462 * - application_id [in] : This argument should be specified to the application's id.
463 * - json [in] : This argument should be specified to the json parameters.
466 * - Returns 0 on success or -1 in case of error.
469 int LibHomeScreen::showWindow(const char* application_id, json_object* json)
476 struct json_object* j_obj = json_object_new_object();
477 struct json_object* val = json_object_new_string(application_id);
478 json_object_object_add(j_obj, ApplicationId, val);
480 if (json == nullptr) {
481 struct json_object* j_json = json_object_new_object();
482 struct json_object* value = json_object_new_string("normal");
483 json_object_object_add(j_json, "area", value);
484 json_object_object_add(j_obj, "parameter", j_json);
487 json_object_object_add(j_obj, "parameter", json);
490 return this->call("showWindow", j_obj);
494 * Sending hide window event
496 * Call HomeScreen Service's hideWindow verb to release id's screen.
499 * - application_id [in] : This argument should be specified to the application's id.
502 * - Returns 0 on success or -1 in case of error.
505 int LibHomeScreen::hideWindow(const char* application_id)
512 struct json_object* j_obj = json_object_new_object();
513 struct json_object* val = json_object_new_string(application_id);
514 json_object_object_add(j_obj, ApplicationId, val);
516 return this->call("hideWindow", j_obj);
520 * Sending reply onscreen message event
522 * Call HomeScreen Service's replyShowWindow verb to reply onscreen message.
525 * - application_id [in] : This argument should be specified to the onscreen reply to applilcation id.
526 * - json [in] : This argument should be specified to the json parameters.
529 * - Returns 0 on success or -1 in case of error.
532 int LibHomeScreen::replyShowWindow(const char* application_id, json_object* json)
539 if (json == nullptr) {
540 HMI_WARNING("libhomescreen", "replyShowWindow`s parameter is null");
544 struct json_object* j_obj = json_object_new_object();
545 struct json_object* val = json_object_new_string(application_id);
546 json_object_object_add(j_obj, ApplicationId, val);
547 json_object_object_add(j_obj, "parameter", json);
549 return this->call("replyShowWindow", j_obj);
553 * Sending show notification event
555 * Call HomeScreen Service's notification verb to show notification on Status Bar.
558 * - json [in] : This argument should be specified to the json parameters.
561 * - Returns 0 on success or -1 in case of error.
564 int LibHomeScreen::showNotification(json_object* json)
571 return this->call("showNotification", json);
575 * Sending show information event
577 * Call HomeScreen Service's information verb to show notification on Information Bar.
580 * - json [in] : This argument should be specified to the json parameters.
583 * - Returns 0 on success or -1 in case of error.
586 int LibHomeScreen::showInformation(json_object* json)
593 return this->call("showInformation", json);
599 * Call HomeScreen Service's getRunnables verb to get runnalbes list.
605 * - Returns 0 on success or -1 in case of error.
608 int LibHomeScreen::getRunnables(void)
610 return this->call("getRunnables", nullptr);
614 /************* Callback Function *************/
616 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
618 HMI_DEBUG("libhomescreen","called");
619 if(onHangup != nullptr)
625 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
630 * event is like "homescreen/hvac"
631 * msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
633 event name : struct json_object obj = json_object_object_get(msg,"event")
635 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
637 HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
639 if (strstr(event, API) == NULL) {
643 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
644 struct json_object *json_data;
645 if(!json_object_object_get_ex(ev_contents, "data", &json_data)) {
646 HMI_ERROR("libhomescreen", "got ev_contents error.");
650 if(onEvent != nullptr)
652 const string ev(event);
653 onEvent(ev, ev_contents);
656 const char* event_type = nullptr;
657 struct json_object *json_event_type;
658 if(json_object_object_get_ex(json_data, "type", &json_event_type)) {
659 event_type = json_object_get_string(json_event_type);
662 HMI_WARNING("libhomescreen","event_type is null.");
666 if (strcasecmp(event_type, LibHomeScreen::event_list[0].c_str()) == 0) {
667 auto i = this->handlers.find(Event_ShowWindow);
668 if ( i != this->handlers.end() ) {
669 i->second(json_data);
672 else if (strcasecmp(event_type, LibHomeScreen::event_list[1].c_str()) == 0) {
673 auto i = this->handlers.find(Event_OnScreenMessage);
674 if ( i != this->handlers.end() ) {
675 i->second(json_data);
678 else if (strcasecmp(event_type, LibHomeScreen::event_list[2].c_str()) == 0) {
679 auto i = this->handlers.find(Event_OnScreenReply);
680 if ( i != this->handlers.end() ) {
681 i->second(json_data);
684 else if (strcasecmp(event_type, LibHomeScreen::event_list[3].c_str()) == 0) {
685 auto i = this->handlers.find(Event_HideWindow);
686 if ( i != this->handlers.end() ) {
687 i->second(json_data);
690 else if (strcasecmp(event_type, LibHomeScreen::event_list[4].c_str()) == 0) {
691 auto i = this->handlers.find(Event_ReplyShowWindow);
692 if ( i != this->handlers.end() ) {
693 i->second(json_data);
696 else if (strcasecmp(event_type, LibHomeScreen::event_list[5].c_str()) == 0) {
697 auto i = this->handlers.find(Event_ShowNotification);
698 if ( i != this->handlers.end() ) {
699 i->second(json_data);
702 else if (strcasecmp(event_type, LibHomeScreen::event_list[6].c_str()) == 0) {
703 auto i = this->handlers.find(Event_ShowInformation);
704 if ( i != this->handlers.end() ) {
705 i->second(json_data);
708 else if (strcasecmp(event_type, LibHomeScreen::event_list[7].c_str()) == 0) {
709 auto i = this->handlers.find(Event_AppListChanged);
710 if ( i != this->handlers.end() ) {
711 i->second(json_data);
717 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
718 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
720 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
722 HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
723 if(onReply != nullptr)
725 struct json_object* reply = afb_wsj1_msg_object_j(msg);
730 static bool has_verb(const string& verb)
732 HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
733 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())