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";
37 const std::vector<std::string> LibHomeScreen::api_list {
38 std::string("ping"), // debug do not use
39 std::string("tap_shortcut"), // HomeScreen Application only
40 std::string("on_screen_message"),
41 std::string("on_screen_reply"),
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"),
49 std::string("on_screen_reply"),
58 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
60 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
63 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
65 /* LibHomeScreen is not called from other process */
68 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
70 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
73 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
75 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
82 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");
141 * This function register callback function for reply/event message from home screen
144 * - event_cb [in] : This argument should be specified to the callback for subscribed event
145 * - reply_cb [in] : This argument should be specified to the reply callback for call function
151 * Event callback is invoked by home screen for event you subscribed.
152 * If you would like to get event, please call subscribe function before/after this function
154 void LibHomeScreen::registerCallback(
155 void (*event_cb)(const std::string& event, struct json_object* event_contents),
156 void (*reply_cb)(struct json_object* reply_contents),
157 void (*hangup_cb)(void))
161 onHangup = hangup_cb;
164 int LibHomeScreen::initialize_websocket()
169 int ret = sd_event_default(&mploop);
172 HMI_ERROR("libhomescreen","Failed to create event loop");
176 /* Initialize interface from websocket */
177 minterface.on_hangup = _on_hangup_static;
178 minterface.on_call = _on_call_static; /* Is this necessary? */
179 minterface.on_event = _on_event_static;
180 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
181 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
182 if(sp_websock == NULL)
184 HMI_ERROR("libhomescreen","Failed to create websocket connection");
188 /* creates the evsrc */
189 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
195 sd_event_unref(mploop);
201 * Sending ShortCut Icon tapped event
203 * When HomeScreen shortcut area is tapped, sending a event
206 * - application_name [in] : Tapped application name (label)
209 * - Returns 0 on success or -1 in case of error.
211 int LibHomeScreen::tapShortcut(const char* application_name)
218 struct json_object* j_obj = json_object_new_object();
219 struct json_object* val = json_object_new_string(application_name);
220 json_object_object_add(j_obj, "application_name", val);
221 return this->call("tap_shortcut", j_obj);
225 * Sending onScreen message event
227 * Sending OnScreen message event to HomeScreen from applications
230 * - display_message [in] : message for display
233 * - Returns 0 on success or -1 in case of error.
235 int LibHomeScreen::onScreenMessage(const char* display_message)
242 struct json_object* j_obj = json_object_new_object();
243 struct json_object* val = json_object_new_string(display_message);
244 json_object_object_add(j_obj, "display_message", val);
245 return this->call("on_screen_message", j_obj);
249 * Sending onScreen reply event
251 * Sending OnScreen reply event to applications from HomeScreen
254 * - reply_message [in] : message for reply
257 * - Returns 0 on success or -1 in case of error.
259 int LibHomeScreen::onScreenReply(const char* reply_message)
266 struct json_object* j_obj = json_object_new_object();
267 struct json_object* val = json_object_new_string(reply_message);
268 json_object_object_add(j_obj, "reply_message", val);
269 return this->call("on_screen_reply", j_obj);
273 * Setting Event Handler
275 * Setting event handler for Homescreen
278 * - et [in] : event name
279 * - f [in] : event handler
284 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
286 if (et >= 1 && et <= 3) {
288 case Event_TapShortcut:
289 this->subscribe(LibHomeScreen::event_list[0]);
291 case Event_OnScreenMessage:
292 this->subscribe(LibHomeScreen::event_list[1]);
294 case Event_OnScreenReply:
295 this->subscribe(LibHomeScreen::event_list[2]);
299 this->handlers[et] = std::move(f);
304 * This function calls the API of HomeScreen via WebSocket
307 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
308 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
311 * - Returns 0 on success or -1 in case of error.
314 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
317 int LibHomeScreen::call(const string& verb, struct json_object* arg)
326 HMI_ERROR("libhomescreen","verb doesn't exit");
329 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
331 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
337 * This function calls the API of HomeScreen via WebSocket
338 * This function is overload function of "call"
341 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
342 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
345 * - Returns 0 on success or -1 in case of error.
348 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
351 int LibHomeScreen::call(const char* verb, struct json_object* arg)
358 if (!has_verb(string(verb)))
360 HMI_ERROR("libhomescreen","verb doesn't exit");
363 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
365 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
371 * Register callback function for each event
374 * - event_name [in] : This argument should be specified to the event name
377 * - Returns 0 on success or -1 in case of error.
380 * This function enables to get an event to your callback function.
383 int LibHomeScreen::subscribe(const string& event_name)
389 struct json_object* j_obj = json_object_new_object();
390 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
392 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
394 HMI_ERROR("libhomescreen","Failed to call verb");
400 * Unregister callback function for each event
403 * - event_name [in] : This argument should be specified to the event name
406 * - Returns 0 on success or -1 in case of error.
409 * This function disables to get an event to your callback function.
412 int LibHomeScreen::unsubscribe(const string& event_name)
418 struct json_object* j_obj = json_object_new_object();
419 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
421 int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
423 HMI_ERROR("libhomescreen","Failed to call verb");
428 /************* Callback Function *************/
430 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
432 HMI_DEBUG("libhomescreen","called");
433 if(onHangup != nullptr)
439 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
444 * event is like "homescreen/tap_shortcut"
445 * msg is like {"event":"homescreen\/tap_shortcut","data":{"application_name":"hoge"},"jtype":"afb-event"}
447 event name : struct json_object obj = json_object_object_get(msg,"event")
449 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
451 HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
453 if (strstr(event, API) == NULL) {
457 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
458 struct json_object *json_data = json_object_object_get(ev_contents, "data");
460 if(onEvent != nullptr)
462 const string ev(event);
463 onEvent(ev, ev_contents);
466 const char* event_only = strchr(event, '/');
467 if (event_only != nullptr) {
468 event_only = event_only + 1;
471 if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
472 auto i = this->handlers.find(Event_TapShortcut);
473 if ( i != this->handlers.end() ) {
474 i->second(json_data);
477 else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
478 auto i = this->handlers.find(Event_OnScreenMessage);
479 if ( i != this->handlers.end() ) {
480 i->second(json_data);
483 else if (strcasecmp(event_only, LibHomeScreen::event_list[2].c_str()) == 0) {
484 auto i = this->handlers.find(Event_OnScreenReply);
485 if ( i != this->handlers.end() ) {
486 i->second(json_data);
490 json_object_put(ev_contents);
494 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
495 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
497 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
499 HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
500 if(onReply != nullptr)
502 struct json_object* reply = afb_wsj1_msg_object_j(msg);
505 json_object_put(reply);
509 static bool has_verb(const string& verb)
511 HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
512 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())