HomeScreenBinding
homescreen.cpp
Go to the documentation of this file.
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 "hmi-debug.h"
24 #include "hs-clientmanager.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 
33 
34 /*
35 ********** Method of HomeScreen Service (API) **********
36 */
37 
38 static void pingSample(afb_req_t request)
39 {
40  static int pingcount = 0;
41  afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
42  HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
43  pingcount++;
44 }
45 
58 static void tap_shortcut (afb_req_t request)
59 {
60  HMI_NOTICE("homescreen-service","called.");
61  int ret = g_client_manager->tap_shortcut(request);
62  if (ret != 0) {
63  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
64  return;
65  }
66 
67  // response to HomeScreen
68  struct json_object *res = json_object_new_object();
69  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
70  _error, ret);
71  afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
72 }
73 
85 static void on_screen_message (afb_req_t request)
86 {
87  HMI_NOTICE("homescreen-service","called.");
88 
89  int ret = g_client_manager->on_screen_message(request);
90  if (ret != 0) {
91  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
92  return;
93  }
94 
95  // response to HomeScreen
96  struct json_object *res = json_object_new_object();
97  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
98  _error, ret);
99  afb_req_success(request, res, "afb_event_push event [on_screen_message]");
100 }
101 
113 static void on_screen_reply (afb_req_t request)
114 {
115  HMI_NOTICE("homescreen-service","called.");
116 
117  int ret = g_client_manager->on_screen_reply(request);
118  if (ret != 0) {
119  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
120  return;
121  }
122 
123  // response to HomeScreen
124  struct json_object *res = json_object_new_object();
125  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
126  _error, ret);
127  afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
128 }
129 
140 static void subscribe(afb_req_t request)
141 {
142  HMI_NOTICE("homescreen-service","called.");
143 
144  int ret = g_client_manager->subscribe(request);
145  if(ret) {
146  afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
147  return;
148  }
149 
150  /*create response json object*/
151  struct json_object *res = json_object_new_object();
152  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
153  _error, ret);
154  afb_req_success_f(request, res, "homescreen binder subscribe.");
155 }
156 
167 static void unsubscribe(afb_req_t request)
168 {
169  HMI_NOTICE("homescreen-service","called.");
170 
171  int ret = g_client_manager->unsubscribe(request);
172  if(ret) {
173  afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
174  return;
175  }
176 
177  /*create response json object*/
178  struct json_object *res = json_object_new_object();
179  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
180  _error, ret);
181  afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
182 }
183 
194 static void showWindow(afb_req_t request)
195 {
196  HMI_NOTICE("homescreen-service","called.");
197 
198  int ret = g_client_manager->showWindow(request);
199  if (ret != 0) {
200  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
201  return;
202  }
203 
204  // response to HomeScreen
205  struct json_object *res = json_object_new_object();
206  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
207  _error, ret);
208  afb_req_success(request, res, "afb_event_push event [showWindow]");
209 }
210 
221 static void hideWindow(afb_req_t request)
222 {
223  HMI_NOTICE("homescreen-service","called.");
224 
225  int ret = g_client_manager->hideWindow(request);
226  if (ret != 0) {
227  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
228  return;
229  }
230 
231  // response to HomeScreen
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(request, res, "afb_event_push event [hideWindow]");
236 }
237 
248 static void replyShowWindow(afb_req_t request)
249 {
250  HMI_NOTICE("homescreen-service","called.");
251 
252  int ret = g_client_manager->replyShowWindow(request);
253  if (ret != 0) {
254  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
255  return;
256  }
257 
258  // response to HomeScreen
259  struct json_object *res = json_object_new_object();
260  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
261  _error, ret);
262  afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
263 }
264 
277 static void showNotification(afb_req_t request)
278 {
279  HMI_NOTICE("homescreen-service","called.");
280 
281  int ret = g_client_manager->showNotification(request);
282  if (ret != 0) {
283  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
284  return;
285  }
286 
287  // response to Application
288  struct json_object *res = json_object_new_object();
289  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
290  _error, ret);
291  afb_req_success(request, res, "afb_event_push event [showNotification]");
292 }
293 
306 static void showInformation(afb_req_t request)
307 {
308  HMI_NOTICE("homescreen-service","called.");
309 
310  int ret = g_client_manager->showInformation(request);
311  if (ret != 0) {
312  afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
313  return;
314  }
315 
316  // response to Application
317  struct json_object *res = json_object_new_object();
318  hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
319  _error, ret);
320  afb_req_success(request, res, "afb_event_push event [showInformation]");
321 }
322 
323 /*
324  * array of the verbs exported to afb-daemon
325  */
326 static const afb_verb_t verbs[]= {
327  /* VERB'S NAME FUNCTION TO CALL */
328  { .verb="ping", .callback=pingSample },
329  { .verb="tap_shortcut", .callback=tap_shortcut },
330  { .verb="showWindow", .callback=showWindow },
331  { .verb="hideWindow", .callback=hideWindow },
332  { .verb="replyShowWindow", .callback=replyShowWindow },
333  { .verb="on_screen_message", .callback=on_screen_message },
334  { .verb="on_screen_reply", .callback=on_screen_reply },
335  { .verb="subscribe", .callback=subscribe },
336  { .verb="unsubscribe", .callback=unsubscribe },
337  { .verb="showNotification", .callback=showNotification },
338  { .verb="showInformation", .callback=showInformation },
339  {NULL } /* marker for end of the array */
340 };
341 
352 static int preinit(afb_api_t api)
353 {
354  HMI_NOTICE("homescreen-service","binding preinit (was register)");
355  return 0;
356 }
357 
368 static int init(afb_api_t api)
369 {
370  HMI_NOTICE("homescreen-service","binding init");
371 
372  g_client_manager->init();
373 
374  return 0;
375 }
376 
389 static void onevent(afb_api_t api, const char *event, struct json_object *object)
390 {
391  HMI_NOTICE("homescreen-service","on_event %s", event);
392 }
393 
394 const afb_binding_t afbBindingExport = {
395  .api = "homescreen",
396  .specification = NULL,
397  .info = NULL,
398  .verbs = verbs,
399  .preinit = preinit,
400  .init = init,
401  .onevent = onevent
402 };
int hideWindow(afb_req_t request)
static void showWindow(afb_req_t request)
Definition: homescreen.cpp:194
static void on_screen_reply(afb_req_t request)
Definition: homescreen.cpp:113
int subscribe(afb_req_t request)
int tap_shortcut(afb_req_t request)
static void on_screen_message(afb_req_t request)
Definition: homescreen.cpp:85
const afb_binding_t afbBindingExport
Definition: homescreen.cpp:394
static void subscribe(afb_req_t request)
Definition: homescreen.cpp:140
const char _reply_message[]
Definition: homescreen.cpp:30
static void unsubscribe(afb_req_t request)
Definition: homescreen.cpp:167
int on_screen_reply(afb_req_t request)
#define HMI_NOTICE(prefix, args,...)
Definition: hmi-debug.h:40
static const afb_verb_t verbs[]
Definition: homescreen.cpp:326
int showWindow(afb_req_t request)
static void showInformation(afb_req_t request)
Definition: homescreen.cpp:306
static void pingSample(afb_req_t request)
Definition: homescreen.cpp:38
static void onevent(afb_api_t api, const char *event, struct json_object *object)
Definition: homescreen.cpp:389
int unsubscribe(afb_req_t request)
const char _error[]
Definition: homescreen.cpp:27
int replyShowWindow(afb_req_t request)
static void hideWindow(afb_req_t request)
Definition: homescreen.cpp:221
static HS_ClientManager * g_client_manager
Definition: homescreen.cpp:32
const char _display_message[]
Definition: homescreen.cpp:29
static HS_ClientManager * instance(void)
int showInformation(afb_req_t request)
static void replyShowWindow(afb_req_t request)
Definition: homescreen.cpp:248
static void showNotification(afb_req_t request)
Definition: homescreen.cpp:277
void hs_add_object_to_json_object_func(struct json_object *j_obj, const char *verb_name, int count,...)
Definition: hs-helper.cpp:207
static int preinit(afb_api_t api)
Definition: homescreen.cpp:352
int on_screen_message(afb_req_t request)
const char _application_id[]
Definition: homescreen.cpp:28
int showNotification(afb_req_t request)
static void tap_shortcut(afb_req_t request)
Definition: homescreen.cpp:58
static int init(afb_api_t api)
Definition: homescreen.cpp:368