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>
32 #define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
33 #define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
37 static void _DLOG(const char* func, const int line, const char* log, ...);
38 static void _ELOG(const char* func, const int line, const char* log, ...);
39 static bool has_verb(const string& verb);
40 static const char API[] = "homescreen";
42 const std::vector<std::string> LibHomeScreen::api_list {
43 std::string("ping"), // debug do not use
44 std::string("tap_shortcut"), // HomeScreen Application only
45 std::string("on_screen_message"),
46 std::string("subscribe"),
47 std::string("unsubscribe")
50 const std::vector<std::string> LibHomeScreen::event_list {
51 std::string("tap_shortcut"),
52 std::string("on_screen_message"),
61 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
63 static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
66 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
68 /* LibHomeScreen is not called from other process */
71 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
73 static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
76 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
78 static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
85 LibHomeScreen::LibHomeScreen()
93 LibHomeScreen::~LibHomeScreen()
97 sd_event_unref(mploop);
99 if(sp_websock != NULL)
101 afb_wsj1_unref(sp_websock);
106 * This function is initializer
109 * - port [in] : This argument should be specified to the port number to be used for websocket
110 * - token [in] : This argument should be specified to the token to be used for websocket
116 * Use this constructor
119 int LibHomeScreen::init(const int port, const string& token)
122 if(port > 0 && token.size() > 0)
129 ELOG("port and token should be > 0, Initial port and token uses.");
132 ret = initialize_websocket();
135 ELOG("Failed to initialize websocket");
141 this->runEventloop();
147 * This function register callback function for reply/event message from home screen
150 * - event_cb [in] : This argument should be specified to the callback for subscribed event
151 * - reply_cb [in] : This argument should be specified to the reply callback for call function
157 * Event callback is invoked by home screen for event you subscribed.
158 * If you would like to get event, please call subscribe function before/after this function
160 void LibHomeScreen::registerCallback(
161 void (*event_cb)(const std::string& event, struct json_object* event_contents),
162 void (*reply_cb)(struct json_object* reply_contents),
163 void (*hangup_cb)(void))
167 onHangup = hangup_cb;
170 int LibHomeScreen::initialize_websocket()
175 int ret = sd_event_default(&mploop);
178 ELOG("Failed to create event loop");
182 /* Initialize interface from websocket */
183 minterface.on_hangup = _on_hangup_static;
184 minterface.on_call = _on_call_static; /* Is this necessary? */
185 minterface.on_event = _on_event_static;
186 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
187 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
188 if(sp_websock == NULL)
190 ELOG("Failed to create websocket connection");
194 /* creates the evsrc */
195 //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
201 sd_event_unref(mploop);
206 static void *event_loop_run(void *args)
208 struct sd_event* loop = (struct sd_event*)(args);
209 DLOG("start eventloop");
211 sd_event_run(loop, 30000000);
215 * This function start receiving the reply/event message from home screen
221 * - Returns thread_id on success or -1 in case of error.
226 int LibHomeScreen::runEventloop()
228 if(mploop && sp_websock)
231 int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
234 ELOG("Cannot run eventloop due to error:%d", errno);
242 ELOG("Connecting is not established yet");
248 * ショートカットアイコンがタップされたイベントの発行
250 * HomeScreenアプリケーションにて各アプリアイコンがタップされたときにイベントを発行する
253 * - application_name [in] : タップされたアプリケーションの名前(label)
256 * - Returns 0 on success or -1 in case of error.
258 int LibHomeScreen::tapShortcut(const char* application_name)
265 struct json_object* j_obj = json_object_new_object();
266 struct json_object* val = json_object_new_string(application_name);
267 json_object_object_add(j_obj, "application_name", val);
268 return this->call("tap_shortcut", j_obj);
272 * HomeScreenアプリに表示するメッセージイベントの発行
274 * 各アプリからHomeScreenアプリケーションのOnScreenに表示するイベントを発行する
277 * - display_message [in] : 表示するメッセージ
280 * - Returns 0 on success or -1 in case of error.
282 int LibHomeScreen::onScreenMessage(const char* display_message)
289 struct json_object* j_obj = json_object_new_object();
290 struct json_object* val = json_object_new_string(display_message);
291 json_object_object_add(j_obj, "display_message", val);
292 return this->call("on_screen_message", j_obj);
298 * 各アプリからHomeScreenアプリケーションのOnScreenに表示するイベントを発行する
301 * - et [in] : 対象のイベント
302 * - f [in] : イベントハンドラ
307 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
309 if (et >= 1 && et <= 2) {
311 case Event_TapShortcut:
312 this->subscribe(LibHomeScreen::event_list[0]);
314 case Event_OnScreenMessage:
315 this->subscribe(LibHomeScreen::event_list[1]);
319 this->handlers[et] = std::move(f);
324 * This function calls the API of HomeScreen via WebSocket
327 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
328 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
331 * - Returns 0 on success or -1 in case of error.
334 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
337 int LibHomeScreen::call(const string& verb, struct json_object* arg)
346 ELOG("verb doesn't exit");
349 ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
351 ELOG("Failed to call verb:%s",verb.c_str());
357 * This function calls the API of HomeScreen via WebSocket
358 * This function is overload function of "call"
361 * - verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
362 * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
365 * - Returns 0 on success or -1 in case of error.
368 * To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.
371 int LibHomeScreen::call(const char* verb, struct json_object* arg)
378 if (!has_verb(string(verb)))
380 ELOG("verb doesn't exit");
383 ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
385 ELOG("Failed to call verb:%s",verb);
391 * Register callback function for each event
394 * - event_name [in] : This argument should be specified to the event name
397 * - Returns 0 on success or -1 in case of error.
400 * This function enables to get an event to your callback function.
403 int LibHomeScreen::subscribe(const string& event_name)
409 struct json_object* j_obj = json_object_new_object();
410 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
412 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
414 ELOG("Failed to call verb:%s",__FUNCTION__);
420 * Unregister callback function for each event
423 * - event_name [in] : This argument should be specified to the event name
426 * - Returns 0 on success or -1 in case of error.
429 * This function disables to get an event to your callback function.
432 int LibHomeScreen::unsubscribe(const string& event_name)
438 struct json_object* j_obj = json_object_new_object();
439 json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
441 int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
443 ELOG("Failed to call verb:%s",__FUNCTION__);
448 /************* Callback Function *************/
450 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
452 DLOG("%s called", __FUNCTION__);
453 if(onHangup != nullptr)
459 void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
464 * event is like "homescreen/tap_shortcut"
465 * msg is like {"event":"homescreen\/tap_shortcut","data":{"application_name":"hoge"},"jtype":"afb-event"}
467 event name : struct json_object obj = json_object_object_get(msg,"event")
469 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
471 cout << "[libhomescreen on_event]: " << event << " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
473 if (strstr(event, API) == NULL) {
477 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
478 struct json_object *json_data = json_object_object_get(ev_contents, "data");
480 if(onEvent != nullptr)
482 const string ev(event);
483 onEvent(ev, ev_contents);
486 const char* event_only = strchr(event, '/');
487 if (event_only != nullptr) {
488 event_only = event_only + 1;
491 if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
492 auto i = this->handlers.find(Event_TapShortcut);
494 struct json_object *json_application_name = json_object_object_get(json_data, "application_name");
495 const char* application_name = json_object_get_string(json_application_name);
497 if ( i != this->handlers.end() ) {
498 i->second(application_name);
501 else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
503 auto i = this->handlers.find(Event_OnScreenMessage);
505 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
506 const char* display_message = json_object_get_string(json_display_message);
508 if ( i != this->handlers.end() ) {
509 i->second(display_message);
514 json_object_put(ev_contents);
518 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
519 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
521 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
523 cout << "[libhomescreen on_reply]: " << " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
524 if(onReply != nullptr)
526 struct json_object* reply = afb_wsj1_msg_object_j(msg);
529 json_object_put(reply);
533 /* Internal Function in libhomescreen */
535 static void _ELOG(const char* func, const int line, const char* log, ...)
540 if (log == NULL || vasprintf(&message, log, args) < 0)
542 cout << "[libhomescreen ERROR]" << func << "(" << line << "):" << message << endl;
547 static void _DLOG(const char* func, const int line, const char* log, ...)
552 if (log == NULL || vasprintf(&message, log, args) < 0)
554 cout << "[libhomescreen DEBUG]" << func << "(" << line << "):" << message << endl;
559 static bool has_verb(const string& verb)
561 DLOG("verb is %s", verb.c_str());
562 if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())