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("subscribe"),
42 std::string("unsubscribe")
45 const std::vector<std::string> LibHomeScreen::event_list {
46 std::string("tap_shortcut"),
47 std::string("on_screen_message"),
56 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
58 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
61 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
63 /* LibHomeScreen is not called from other process */
66 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
68 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
71 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
73 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
80 LibHomeScreen::LibHomeScreen()
87 LibHomeScreen::~LibHomeScreen()
91 sd_event_unref(mploop);
93 if(sp_websock != NULL)
95 afb_wsj1_unref(sp_websock);
100 * This function is initializer
103 * - port [in] : This argument should be specified to the port number to be used for websocket
104 * - token [in] : This argument should be specified to the token to be used for websocket
110 * Use this constructor
113 int LibHomeScreen::init(const int port, const string& token)
116 if(port > 0 && token.size() > 0)
123 HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
126 ret = initialize_websocket();
129 HMI_ERROR("libhomescreen","Failed to initialize websocket");
132 HMI_DEBUG("libhomescreen","Initialized");
139 * This function register callback function for reply/event message from home screen
142 * - event_cb [in] : This argument should be specified to the callback for subscribed event
143 * - reply_cb [in] : This argument should be specified to the reply callback for call function
149 * Event callback is invoked by home screen for event you subscribed.
150 * If you would like to get event, please call subscribe function before/after this function
152 void LibHomeScreen::registerCallback(
153 void (*event_cb)(const std::string& event, struct json_object* event_contents),
154 void (*reply_cb)(struct json_object* reply_contents),
155 void (*hangup_cb)(void))
159 onHangup = hangup_cb;
162 int LibHomeScreen::initialize_websocket()
167 int ret = sd_event_default(&mploop);
170 HMI_ERROR("libhomescreen","Failed to create event loop");
174 /* Initialize interface from websocket */
175 minterface.on_hangup = _on_hangup_static;
176 minterface.on_call = _on_call_static; /* Is this necessary? */
177 minterface.on_event = _on_event_static;
178 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
179 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
180 if(sp_websock == NULL)
182 HMI_ERROR("libhomescreen","Failed to create websocket connection");
186 /* creates the evsrc */
187 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
193 sd_event_unref(mploop);
199 * Sending ShortCut Icon tapped event
201 * When HomeScreen shortcut area is tapped, sending a event
204 * - application_name [in] : Tapped application name (label)
207 * - Returns 0 on success or -1 in case of error.
209 int LibHomeScreen::tapShortcut(const char* application_name)
216 struct json_object* j_obj = json_object_new_object();
217 struct json_object* val = json_object_new_string(application_name);
218 json_object_object_add(j_obj, "application_name", val);
219 return this->call("tap_shortcut", j_obj);
223 * Sending onScreen message event
225 * Sending OnScreen message event to HomeScreen from applications
228 * - display_message [in] : message for display
231 * - Returns 0 on success or -1 in case of error.
233 int LibHomeScreen::onScreenMessage(const char* display_message)
240 struct json_object* j_obj = json_object_new_object();
241 struct json_object* val = json_object_new_string(display_message);
242 json_object_object_add(j_obj, "display_message", val);
243 return this->call("on_screen_message", j_obj);
247 * Setting Event Handler
249 * Setting event handler for Homescreen
252 * - et [in] : event name
253 * - f [in] : event handler
258 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
260 if (et >= 1 && et <= 2) {
262 case Event_TapShortcut:
263 this->subscribe(LibHomeScreen::event_list[0]);
265 case Event_OnScreenMessage:
266 this->subscribe(LibHomeScreen::event_list[1]);
270 this->handlers[et] = std::move(f);
275 * This function calls the API of HomeScreen via WebSocket
278 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
279 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
282 * - Returns 0 on success or -1 in case of error.
285 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
288 int LibHomeScreen::call(const string& verb, struct json_object* arg)
297 HMI_ERROR("libhomescreen","verb doesn't exit");
300 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
302 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
308 * This function calls the API of HomeScreen via WebSocket
309 * This function is overload function of "call"
312 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
313 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
316 * - Returns 0 on success or -1 in case of error.
319 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
322 int LibHomeScreen::call(const char* verb, struct json_object* arg)
329 if (!has_verb(string(verb)))
331 HMI_ERROR("libhomescreen","verb doesn't exit");
334 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
336 HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
342 * Register callback function for each event
345 * - event_name [in] : This argument should be specified to the event name
348 * - Returns 0 on success or -1 in case of error.
351 * This function enables to get an event to your callback function.
354 int LibHomeScreen::subscribe(const string& event_name)
360 struct json_object* j_obj = json_object_new_object();
361 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
363 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
365 HMI_ERROR("libhomescreen","Failed to call verb");
371 * Unregister 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 disables to get an event to your callback function.
383 int LibHomeScreen::unsubscribe(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, "unsubscribe", j_obj, _on_reply_static, this);
394 HMI_ERROR("libhomescreen","Failed to call verb");
399 /************* Callback Function *************/
401 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
403 HMI_DEBUG("libhomescreen","called");
404 if(onHangup != nullptr)
410 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
415 * event is like "homescreen/tap_shortcut"
416 * msg is like {"event":"homescreen\/tap_shortcut","data":{"application_name":"hoge"},"jtype":"afb-event"}
418 event name : struct json_object obj = json_object_object_get(msg,"event")
420 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
422 HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
424 if (strstr(event, API) == NULL) {
428 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
429 struct json_object *json_data = json_object_object_get(ev_contents, "data");
431 if(onEvent != nullptr)
433 const string ev(event);
434 onEvent(ev, ev_contents);
437 const char* event_only = strchr(event, '/');
438 if (event_only != nullptr) {
439 event_only = event_only + 1;
442 if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
443 auto i = this->handlers.find(Event_TapShortcut);
445 struct json_object *json_application_name = json_object_object_get(json_data, "application_name");
446 const char* application_name = json_object_get_string(json_application_name);
448 if ( i != this->handlers.end() ) {
449 i->second(application_name);
452 else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
454 auto i = this->handlers.find(Event_OnScreenMessage);
456 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
457 const char* display_message = json_object_get_string(json_display_message);
459 if ( i != this->handlers.end() ) {
460 i->second(display_message);
465 json_object_put(ev_contents);
469 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
470 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
472 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
474 HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
475 if(onReply != nullptr)
477 struct json_object* reply = afb_wsj1_msg_object_j(msg);
480 json_object_put(reply);
484 static bool has_verb(const string& verb)
486 HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
487 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())