Change log macro to AFB_XXX
[apps/agl-service-homescreen.git] / src / homescreen.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20 #include <memory>
21 #include <algorithm>
22 #include "hs-helper.h"
23 #include "hs-clientmanager.h"
24 #include "hs-appinfo.h"
25
26
27 const char _error[] = "error";
28 const char _application_id[] = "application_id";
29 const char _display_message[] = "display_message";
30 const char _reply_message[] = "reply_message";
31 const char _keyData[] = "data";
32 const char _keyId[] = "id";
33
34 struct hs_instance {
35   HS_ClientManager *client_manager;   // the connection session manager
36   HS_AppInfo *app_info;               // application info
37
38   hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {}
39   int init(afb_api_t api);
40 };
41
42 /**
43  * init function
44  *
45  * #### Parameters
46  * - api : the api serving the request
47  *
48  * #### Return
49  * 0 : init success
50  * 1 : init fail
51  *
52  */
53 int hs_instance::init(afb_api_t api)
54 {
55     if(client_manager == nullptr) {
56         AFB_ERROR("client_manager is nullptr.");
57         return -1;
58     }
59     client_manager->init();
60
61     if(app_info == nullptr) {
62         AFB_ERROR("app_info is nullptr.");
63         return -1;
64     }
65     app_info->init(api);
66
67     return 0;
68 }
69
70 static struct hs_instance *g_hs_instance;
71
72 /*
73 ********** Method of HomeScreen Service (API) **********
74 */
75
76 static void pingSample(afb_req_t request)
77 {
78    static int pingcount = 0;
79    afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
80    AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
81    pingcount++;
82 }
83
84 /**
85  * tap_shortcut notify for homescreen
86  * When Shortcut area is tapped,  notify these applciations
87  *
88  * #### Parameters
89  * Request key
90  * - application_id   : application id
91  *
92  * #### Return
93  * None
94  *
95  */
96 static void tap_shortcut (afb_req_t request)
97 {
98     int ret = 0;
99     const char* value = afb_req_value(request, _application_id);
100     if (value) {
101         AFB_INFO("request appid = %s.", value);
102         ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
103         if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
104             std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
105             HS_AfmMainProxy afm_proxy;
106             afm_proxy.start(request, id);
107             ret = 0;
108         }
109     }
110     else {
111         ret = AFB_EVENT_BAD_REQUEST;
112     }
113
114     if (ret) {
115         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
116     }
117     else {
118         struct json_object *res = json_object_new_object();
119         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
120           _error,  ret);
121         afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
122     }
123 }
124
125 /**
126  * HomeScreen OnScreen message
127  *
128  * #### Parameters
129  * Request key
130  * - display_message   : message for display
131  *
132  * #### Return
133  * None
134  *
135  */
136 static void on_screen_message (afb_req_t request)
137 {
138     int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
139     if (ret) {
140         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
141     }
142     else {
143         struct json_object *res = json_object_new_object();
144         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
145           _error,  ret);
146         afb_req_success(request, res, "afb_event_push event [on_screen_message]");
147     }
148 }
149
150 /**
151  * HomeScreen OnScreen Reply
152  *
153  * #### Parameters
154  * Request key
155  * - reply_message   : message for reply
156  *
157  * #### Return
158  * None
159  *
160  */
161 static void on_screen_reply (afb_req_t request)
162 {
163     int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
164     if (ret) {
165         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
166     }
167     else {
168         struct json_object *res = json_object_new_object();
169         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
170           _error,  ret);
171         afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
172     }
173 }
174
175 /**
176  * Subscribe event
177  *
178  * #### Parameters
179  *  - event  : Event name. Event list is written in libhomescreen.cpp
180  *
181  * #### Return
182  * None
183  *
184  */
185 static void subscribe(afb_req_t request)
186 {
187     int ret = 0;
188     std::string req_appid = std::move(get_application_id(request));
189     if(!req_appid.empty()) {
190         ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
191     }
192     else {
193         ret = AFB_EVENT_BAD_REQUEST;
194     }
195
196     if(ret) {
197         afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
198     }
199     else {
200         struct json_object *res = json_object_new_object();
201         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
202             _error, ret);
203         afb_req_success_f(request, res, "homescreen binder subscribe.");
204     }
205 }
206
207 /**
208  * Unsubscribe event
209  *
210  * #### Parameters
211  *  - event  : Event name. Event list is written in libhomescreen.cpp
212  *
213  * #### Return
214  * None
215  *
216  */
217 static void unsubscribe(afb_req_t request)
218 {
219     int ret = 0;
220     std::string req_appid = std::move(get_application_id(request));
221     if(!req_appid.empty()) {
222         ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
223     }
224     else {
225         ret = AFB_EVENT_BAD_REQUEST;
226     }
227
228     if(ret) {
229         afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
230     }
231     else {
232         struct json_object *res = json_object_new_object();
233         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
234             _error, ret);
235         afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
236     }
237 }
238
239 /**
240  * showWindow event
241  *
242  * #### Parameters
243  *  - request : the request
244  *
245  * #### Return
246  * None
247  *
248  */
249 static void showWindow(afb_req_t request)
250 {
251     int ret = 0;
252     const char* value = afb_req_value(request, _application_id);
253     if (value) {
254         ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
255         if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
256             std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
257             HS_AfmMainProxy afm_proxy;
258             afm_proxy.start(request, id);
259             ret = 0;
260         }
261     }
262     else {
263         ret = AFB_EVENT_BAD_REQUEST;
264     }
265
266     if (ret) {
267         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
268     }
269     else {
270         struct json_object *res = json_object_new_object();
271         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
272           _error,  ret);
273         afb_req_success(request, res, "afb_event_push event [showWindow]");
274     }
275 }
276
277 /**
278  * hideWindow event
279  *
280  * #### Parameters
281  *  - request : the request
282  *
283  * #### Return
284  * None
285  *
286  */
287 static void hideWindow(afb_req_t request)
288 {
289     int ret = 0;
290     const char* value = afb_req_value(request, _application_id);
291     if (value) {
292         ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
293     }
294     else {
295         ret = AFB_EVENT_BAD_REQUEST;
296     }
297
298     if (ret) {
299         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
300     }
301     else {
302         struct json_object *res = json_object_new_object();
303         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
304           _error,  ret);
305         afb_req_success(request, res, "afb_event_push event [hideWindow]");
306     }
307 }
308
309 /**
310  * replyShowWindow event
311  *
312  * #### Parameters
313  *  - request : the request
314  *
315  * #### Return
316  *  None
317  *
318  */
319 static void replyShowWindow(afb_req_t request)
320 {
321     int ret = 0;
322     const char* value = afb_req_value(request, _application_id);
323     if (value) {
324         ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
325     }
326     else {
327         ret = AFB_EVENT_BAD_REQUEST;
328     }
329
330     if (ret) {
331         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
332     }
333     else {
334         struct json_object *res = json_object_new_object();
335         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
336           _error,  ret);
337         afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
338     }
339 }
340
341 /**
342  * showNotification event
343  *
344  * the contents to homescreen which display at top area.
345  *
346  * #### Parameters
347  *  - request : the request
348  *
349  * #### Return
350  * None
351  *
352  */
353 static void showNotification(afb_req_t request)
354 {
355     int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
356     if (ret) {
357         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
358     }
359     else {
360         struct json_object *res = json_object_new_object();
361         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
362           _error,  ret);
363         afb_req_success(request, res, "afb_event_push event [showNotification]");
364     }
365 }
366
367 /**
368  * showInformation event
369  *
370  * the contents to homescreen which display at bottom area.
371  *
372  * #### Parameters
373  *  - request : the request
374  *
375  * #### Return
376  * None
377  *
378  */
379 static void showInformation(afb_req_t request)
380 {
381     int ret = g_hs_instance->client_manager->handleRequest(request,  __FUNCTION__, "homescreen");
382     if (ret) {
383         afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
384     }
385     else {
386         struct json_object *res = json_object_new_object();
387         hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
388           _error,  ret);
389         afb_req_success(request, res, "afb_event_push event [showInformation]");
390     }
391 }
392
393 /**
394  * get runnables list
395  *
396  * #### Parameters
397  *  - request : the request
398  *
399  * #### Return
400  * None
401  *
402  */
403 static void getRunnables(afb_req_t request)
404 {
405     struct json_object* j_runnable = json_object_new_array();
406     g_hs_instance->app_info->getRunnables(&j_runnable);
407
408     /*create response json object*/
409     struct json_object *res = json_object_new_object();
410     hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
411     json_object_object_add(res, _keyData, j_runnable);
412     afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
413 }
414
415 /*
416  * array of the verbs exported to afb-daemon
417  */
418 static const afb_verb_t verbs[]= {
419     /* VERB'S NAME                 FUNCTION TO CALL                  */
420     { .verb="ping",              .callback=pingSample             },
421     { .verb="tap_shortcut",      .callback=tap_shortcut           },
422     { .verb="showWindow",        .callback=showWindow             },
423     { .verb="hideWindow",        .callback=hideWindow             },
424     { .verb="replyShowWindow",   .callback=replyShowWindow        },
425     { .verb="on_screen_message", .callback=on_screen_message      },
426     { .verb="on_screen_reply",   .callback=on_screen_reply        },
427     { .verb="subscribe",         .callback=subscribe              },
428     { .verb="unsubscribe",       .callback=unsubscribe            },
429     { .verb="showNotification",  .callback=showNotification       },
430     { .verb="showInformation",   .callback=showInformation        },
431     { .verb="getRunnables",      .callback=getRunnables           },
432     {NULL } /* marker for end of the array */
433 };
434
435 /**
436  * homescreen binding preinit function
437  *
438  * #### Parameters
439  *  - api : the api serving the request
440  *
441  * #### Return
442  * None
443  *
444  */
445 static int preinit(afb_api_t api)
446 {
447    AFB_DEBUG("binding preinit (was register)");
448    return 0;
449 }
450
451 /**
452  * homescreen binding init function
453  *
454  * #### Parameters
455  *  - api : the api serving the request
456  *
457  * #### Return
458  * None
459  *
460  */
461 static int init(afb_api_t api)
462 {
463     AFB_DEBUG("binding init");
464
465     if(g_hs_instance != nullptr) {
466         AFB_WARNING( "g_hs_instance isn't null.");
467         delete g_hs_instance->client_manager;
468         delete g_hs_instance->app_info;
469         delete g_hs_instance;
470         g_hs_instance = nullptr;
471     }
472     g_hs_instance = new hs_instance();
473     if(g_hs_instance == nullptr) {
474         return -1;
475     }
476
477     return g_hs_instance->init(api);
478 }
479
480 /**
481  * homescreen binding event function
482  *
483  * #### Parameters
484  *  - api : the api serving the request
485  *  - event  : event name
486  *  - object : event json object
487  *
488  * #### Return
489  * None
490  *
491  */
492 static void onevent(afb_api_t api, const char *event, struct json_object *object)
493 {
494     AFB_DEBUG("on_event %s", event);
495     g_hs_instance->app_info->onEvent(api, event, object);
496 }
497
498 const afb_binding_t afbBindingExport = {
499     .api = "homescreen",
500     .specification = NULL,
501     .info = NULL,
502     .verbs = verbs,
503     .preinit = preinit,
504     .init = init,
505     .onevent = onevent
506 };