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>
30 #include <libhomescreen.hpp>
31 #include "hmi-debug.h"
35 static bool has_verb(const string& verb);
36 static const char API[] = "homescreen";
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("subscribe"),
43 std::string("unsubscribe")
46 const std::vector<std::string> LibHomeScreen::event_list {
47 std::string("tap_shortcut"),
48 std::string("on_screen_message"),
57 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
59 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
62 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
64 /* LibHomeScreen is not called from other process */
67 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
69 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
72 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
74 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
81 LibHomeScreen::LibHomeScreen()
89 LibHomeScreen::~LibHomeScreen()
93 sd_event_unref(mploop);
95 if(sp_websock != NULL)
97 afb_wsj1_unref(sp_websock);
102 * This function is initializer
105 * - port [in] : This argument should be specified to the port number to be used for websocket
106 * - token [in] : This argument should be specified to the token to be used for websocket
112 * Use this constructor
115 int LibHomeScreen::init(const int port, const string& token)
118 if(port > 0 && token.size() > 0)
125 HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
128 ret = initialize_websocket();
131 HMI_ERROR("libhomescreen","Failed to initialize websocket");
134 HMI_DEBUG("libhomescreen","Initialized");
137 this->runEventloop();
143 * This function register callback function for reply/event message from home screen
146 * - event_cb [in] : This argument should be specified to the callback for subscribed event
147 * - reply_cb [in] : This argument should be specified to the reply callback for call function
153 * Event callback is invoked by home screen for event you subscribed.
154 * If you would like to get event, please call subscribe function before/after this function
156 void LibHomeScreen::registerCallback(
157 void (*event_cb)(const std::string& event, struct json_object* event_contents),
158 void (*reply_cb)(struct json_object* reply_contents),
159 void (*hangup_cb)(void))
163 onHangup = hangup_cb;
166 int LibHomeScreen::initialize_websocket()
171 int ret = sd_event_default(&mploop);
174 HMI_ERROR("libhomescreen","Failed to create event loop");
178 /* Initialize interface from websocket */
179 minterface.on_hangup = _on_hangup_static;
180 minterface.on_call = _on_call_static; /* Is this necessary? */
181 minterface.on_event = _on_event_static;
182 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
183 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
184 if(sp_websock == NULL)
186 HMI_ERROR("libhomescreen","Failed to create websocket connection");
190 /* creates the evsrc */
191 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
197 sd_event_unref(mploop);
202 static void *event_loop_run(void *args)
204 struct sd_event* loop = (struct sd_event*)(args);
205 HMI_DEBUG("libhomescreen","start eventloop");
207 sd_event_run(loop, 30000000);
211 * This function start receiving the reply/event message from home screen
217 * - Returns thread_id on success or -1 in case of error.
222 int LibHomeScreen::runEventloop()
224 if(mploop && sp_websock)
227 int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
230 HMI_ERROR("libhomescreen","Cannot run eventloop due to error:%d", errno);
238 HMI_ERROR("libhomescreen","Connecting is not established yet");
244 * Sending ShortCut Icon tapped event
246 * When HomeScreen shortcut area is tapped, sending a event
249 * - application_name [in] : Tapped application name (label)
252 * - Returns 0 on success or -1 in case of error.
254 int LibHomeScreen::tapShortcut(const char* application_name)
261 struct json_object* j_obj = json_object_new_object();
262 struct json_object* val = json_object_new_string(application_name);
263 json_object_object_add(j_obj, "application_name", val);
264 return this->call("tap_shortcut", j_obj);
268 * Sending onScreen message event
270 * Sending OnScreen message event to HomeScreen from applications
273 * - display_message [in] : message for display
276 * - Returns 0 on success or -1 in case of error.
278 int LibHomeScreen::onScreenMessage(const char* display_message)
285 struct json_object* j_obj = json_object_new_object();
286 struct json_object* val = json_object_new_string(display_message);
287 json_object_object_add(j_obj, "display_message", val);
288 return this->call("on_screen_message", j_obj);
292 * Setting Event Handler
294 * Setting event handler for Homescreen
297 * - et [in] : event name
298 * - f [in] : event handler
303 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
305 if (et >= 1 && et <= 2) {
307 case Event_TapShortcut:
308 this->subscribe(LibHomeScreen::event_list[0]);
310 case Event_OnScreenMessage:
311 this->subscribe(LibHomeScreen::event_list[1]);
315 this->handlers[et] = std::move(f);
320 * This function calls the API of HomeScreen via WebSocket
323 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
324 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
327 * - Returns 0 on success or -1 in case of error.
330 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
333 int LibHomeScreen::call(const string& verb, struct json_object* arg)
342 HMI_ERROR("libhomescreen","verb doesn't exit");
345 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
347 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
353 * This function calls the API of HomeScreen via WebSocket
354 * This function is overload function of "call"
357 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
358 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
361 * - Returns 0 on success or -1 in case of error.
364 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
367 int LibHomeScreen::call(const char* verb, struct json_object* arg)
374 if (!has_verb(string(verb)))
376 HMI_ERROR("libhomescreen","verb doesn't exit");
379 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
381 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
387 * Register callback function for each event
390 * - event_name [in] : This argument should be specified to the event name
393 * - Returns 0 on success or -1 in case of error.
396 * This function enables to get an event to your callback function.
399 int LibHomeScreen::subscribe(const string& event_name)
405 struct json_object* j_obj = json_object_new_object();
406 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
408 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
410 HMI_ERROR("libhomescreen","Failed to call verb");
416 * Unregister callback function for each event
419 * - event_name [in] : This argument should be specified to the event name
422 * - Returns 0 on success or -1 in case of error.
425 * This function disables to get an event to your callback function.
428 int LibHomeScreen::unsubscribe(const string& event_name)
434 struct json_object* j_obj = json_object_new_object();
435 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
437 int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
439 HMI_ERROR("libhomescreen","Failed to call verb");
444 /************* Callback Function *************/
446 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
448 HMI_DEBUG("libhomescreen","called");
449 if(onHangup != nullptr)
455 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
460 * event is like "homescreen/tap_shortcut"
461 * msg is like {"event":"homescreen\/tap_shortcut","data":{"application_name":"hoge"},"jtype":"afb-event"}
463 event name : struct json_object obj = json_object_object_get(msg,"event")
465 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
467 HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
469 if (strstr(event, API) == NULL) {
473 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
474 struct json_object *json_data = json_object_object_get(ev_contents, "data");
476 if(onEvent != nullptr)
478 const string ev(event);
479 onEvent(ev, ev_contents);
482 const char* event_only = strchr(event, '/');
483 if (event_only != nullptr) {
484 event_only = event_only + 1;
487 if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
488 auto i = this->handlers.find(Event_TapShortcut);
490 struct json_object *json_application_name = json_object_object_get(json_data, "application_name");
491 const char* application_name = json_object_get_string(json_application_name);
493 if ( i != this->handlers.end() ) {
494 i->second(application_name);
497 else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
499 auto i = this->handlers.find(Event_OnScreenMessage);
501 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
502 const char* display_message = json_object_get_string(json_display_message);
504 if ( i != this->handlers.end() ) {
505 i->second(display_message);
510 json_object_put(ev_contents);
514 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
515 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
517 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
519 HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
520 if(onReply != nullptr)
522 struct json_object* reply = afb_wsj1_msg_object_j(msg);
525 json_object_put(reply);
529 static bool has_verb(const string& verb)
531 HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
532 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())