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.
22 #include <unordered_map>
24 #include "hs-helper.h"
25 #include "hs-clientmanager.h"
26 #include "hs-appinfo.h"
29 const char _error[] = "error";
30 const char _application_id[] = "application_id";
31 const char _display_message[] = "display_message";
32 const char _reply_message[] = "reply_message";
33 const char _keyData[] = "data";
34 const char _keyId[] = "id";
35 const char _keyHandshake[] = "handshake";
36 const char _keyTimes[] = "times";
37 const char _keySleep[] = "sleep";
38 const char _hs_conf_json[] = "hs-conf.json";
39 const char _lastmode_json[] = "lastmode.json";
42 struct json_object *hs_conf;
43 struct json_object *lastmode;
45 static struct hs_config g_hs_config;
47 const char _wm_event[] = "windowmanager/screenUpdated";
48 static void screenUpdateCb(void *closure, const char *event, struct json_object* obj, afb_api_t api)
50 AFB_WARNING("windowmanager/screenUpdated callback. obj=%s.", json_object_to_json_string(obj));
54 HS_ClientManager *client_manager; // the connection session manager
55 HS_AppInfo *app_info; // application info
57 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {}
58 int init(afb_api_t api);
59 void setEventHook(const char *event, const event_hook_func f);
60 void onEvent(afb_api_t api, const char *event, struct json_object *object);
62 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
69 * - api : the api serving the request
76 int hs_instance::init(afb_api_t api)
78 if(client_manager == nullptr) {
79 AFB_ERROR("FATAL ERROR: client_manager is nullptr.");
82 client_manager->init();
84 if(app_info == nullptr) {
85 AFB_ERROR("FATAL ERROR: app_info is nullptr.");
90 struct json_object *handshake_obj;
91 if(json_object_object_get_ex(g_hs_config.hs_conf, _keyHandshake, &handshake_obj) == 0) {
92 AFB_ERROR("get handshake failed.");
96 struct json_object *times_obj, *sleep_obj;
97 json_object_object_get_ex(handshake_obj, _keyTimes, ×_obj);
98 json_object_object_get_ex(handshake_obj, _keySleep, &sleep_obj);
99 AFB_WARNING("get handshake times=%d, sleep=%d", json_object_get_int(times_obj), json_object_get_int(sleep_obj));
109 * - event : event name
110 * - f : hook function
115 void hs_instance::setEventHook(const char *event, const event_hook_func f)
117 if(event == nullptr || f == nullptr) {
118 AFB_WARNING("argument is null.");
122 std::string ev(event);
123 auto it = event_hook_list.find(ev);
124 if(it != event_hook_list.end()) {
125 it->second.push_back(f);
128 std::list<event_hook_func> l;
130 event_hook_list[ev] = std::move(l);
138 * - api : the api serving the request
139 * - event : event name
140 * - object : event json object
145 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
147 std::string ev(event);
148 auto it = event_hook_list.find(ev);
149 if(it != event_hook_list.end()) {
150 for(auto &ref : it->second) {
151 if(ref(api, event, object))
157 static struct hs_instance *g_hs_instance;
163 * - event : event name
164 * - f : hook function pointer
169 void setEventHook(const char *event, const event_hook_func f)
171 if(g_hs_instance == nullptr) {
172 AFB_ERROR("g_hs_instance is null.");
176 g_hs_instance->setEventHook(event, f);
180 ********** Method of HomeScreen Service (API) **********
183 static void pingSample(afb_req_t request)
185 static int pingcount = 0;
186 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
187 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
192 * tap_shortcut notify for homescreen
193 * When Shortcut area is tapped, notify these applciations
197 * - application_id : application id
203 static void tap_shortcut (afb_req_t request)
205 AFB_DEBUG("called.");
207 const char* value = afb_req_value(request, _application_id);
209 AFB_INFO("request appid = %s.", value);
210 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
211 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
212 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
213 HS_AfmMainProxy afm_proxy;
214 afm_proxy.start(request, id);
219 ret = AFB_EVENT_BAD_REQUEST;
223 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
226 struct json_object *res = json_object_new_object();
227 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
229 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
234 * HomeScreen OnScreen message
238 * - display_message : message for display
244 static void on_screen_message (afb_req_t request)
246 AFB_DEBUG("called.");
247 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
249 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
252 struct json_object *res = json_object_new_object();
253 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
255 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
260 * HomeScreen OnScreen Reply
264 * - reply_message : message for reply
270 static void on_screen_reply (afb_req_t request)
272 AFB_DEBUG("called.");
273 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
275 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
278 struct json_object *res = json_object_new_object();
279 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
281 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
289 * - event : Event name. Event list is written in libhomescreen.cpp
295 static void subscribe(afb_req_t request)
297 AFB_DEBUG("called.");
299 std::string req_appid = std::move(get_application_id(request));
300 if(!req_appid.empty()) {
301 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
304 ret = AFB_EVENT_BAD_REQUEST;
308 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
311 struct json_object *res = json_object_new_object();
312 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
314 afb_req_success_f(request, res, "homescreen binder subscribe.");
322 * - event : Event name. Event list is written in libhomescreen.cpp
328 static void unsubscribe(afb_req_t request)
330 AFB_DEBUG("called.");
332 std::string req_appid = std::move(get_application_id(request));
333 if(!req_appid.empty()) {
334 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
337 ret = AFB_EVENT_BAD_REQUEST;
341 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
344 struct json_object *res = json_object_new_object();
345 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
347 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
355 * - request : the request
361 static void showWindow(afb_req_t request)
363 AFB_DEBUG("called.");
365 const char* value = afb_req_value(request, _application_id);
367 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
368 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
369 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
370 HS_AfmMainProxy afm_proxy;
371 afm_proxy.start(request, id);
376 ret = AFB_EVENT_BAD_REQUEST;
380 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
383 struct json_object *res = json_object_new_object();
384 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
386 afb_req_success(request, res, "afb_event_push event [showWindow]");
394 * - request : the request
400 static void hideWindow(afb_req_t request)
402 AFB_DEBUG("called.");
404 const char* value = afb_req_value(request, _application_id);
406 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
409 ret = AFB_EVENT_BAD_REQUEST;
413 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
416 struct json_object *res = json_object_new_object();
417 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
419 afb_req_success(request, res, "afb_event_push event [hideWindow]");
424 * replyShowWindow event
427 * - request : the request
433 static void replyShowWindow(afb_req_t request)
435 AFB_DEBUG("called.");
437 const char* value = afb_req_value(request, _application_id);
439 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
442 ret = AFB_EVENT_BAD_REQUEST;
446 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
449 struct json_object *res = json_object_new_object();
450 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
452 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
457 * showNotification event
459 * the contents to homescreen which display at top area.
462 * - request : the request
468 static void showNotification(afb_req_t request)
470 AFB_DEBUG("called.");
471 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
473 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
476 struct json_object *res = json_object_new_object();
477 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
479 afb_req_success(request, res, "afb_event_push event [showNotification]");
484 * showInformation event
486 * the contents to homescreen which display at bottom area.
489 * - request : the request
495 static void showInformation(afb_req_t request)
497 AFB_DEBUG("called.");
498 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
500 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
503 struct json_object *res = json_object_new_object();
504 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
506 afb_req_success(request, res, "afb_event_push event [showInformation]");
514 * - request : the request
520 static void getRunnables(afb_req_t request)
522 AFB_DEBUG("called.");
523 struct json_object* j_runnable = json_object_new_array();
524 g_hs_instance->app_info->getRunnables(&j_runnable);
526 /*create response json object*/
527 struct json_object *res = json_object_new_object();
528 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
529 json_object_object_add(res, _keyData, j_runnable);
530 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
534 * array of the verbs exported to afb-daemon
536 static const afb_verb_t verbs[]= {
537 /* VERB'S NAME FUNCTION TO CALL */
538 { .verb="ping", .callback=pingSample },
539 { .verb="tap_shortcut", .callback=tap_shortcut },
540 { .verb="showWindow", .callback=showWindow },
541 { .verb="hideWindow", .callback=hideWindow },
542 { .verb="replyShowWindow", .callback=replyShowWindow },
543 { .verb="on_screen_message", .callback=on_screen_message },
544 { .verb="on_screen_reply", .callback=on_screen_reply },
545 { .verb="subscribe", .callback=subscribe },
546 { .verb="unsubscribe", .callback=unsubscribe },
547 { .verb="showNotification", .callback=showNotification },
548 { .verb="showInformation", .callback=showInformation },
549 { .verb="getRunnables", .callback=getRunnables },
550 {NULL } /* marker for end of the array */
554 * homescreen binding preinit function
557 * - api : the api serving the request
563 static int preinit(afb_api_t api)
565 AFB_DEBUG("binding preinit (was register)");
566 auto rootdir = std::string(getenv("AFM_APP_INSTALL_DIR"));
567 auto path = rootdir + "/etc/" + _hs_conf_json;
568 if(readJsonFile(path.c_str(), &g_hs_config.hs_conf) < 0) {
569 AFB_ERROR("read %s failed.", _hs_conf_json);
573 path = rootdir + "/etc/" + _lastmode_json;
574 if(readJsonFile(path.c_str(), &g_hs_config.lastmode) < 0) {
575 AFB_ERROR("read %s failed.", _lastmode_json);
582 * homescreen binding init function
585 * - api : the api serving the request
591 static int init(afb_api_t api)
593 AFB_DEBUG("binding init");
595 if(g_hs_instance != nullptr) {
596 AFB_WARNING( "g_hs_instance isn't null.");
597 delete g_hs_instance->client_manager;
598 delete g_hs_instance->app_info;
599 delete g_hs_instance;
600 g_hs_instance = nullptr;
602 g_hs_instance = new hs_instance();
603 if(g_hs_instance == nullptr) {
604 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
608 return g_hs_instance->init(api);
612 * homescreen binding event function
615 * - api : the api serving the request
616 * - event : event name
617 * - object : event json object
623 static void onevent(afb_api_t api, const char *event, struct json_object *object)
625 AFB_INFO("on_event %s", event);
626 g_hs_instance->onEvent(api, event, object);
629 const afb_binding_t afbBindingExport = {
631 .specification = NULL,