6ac4439950ed6ad174c57d30f3b61c65ec3c0fd5
[apps/agl-service-homescreen.git] / src / hs-proxy.cpp
1 /*
2  * Copyright (c) 2019 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 #include "hs-proxy.h"
18 #include "hmi-debug.h"
19
20 static const char _windowmanager[] = "windowmanager";
21 static const char _event[] = "event";
22
23
24 /**
25  * the callback function
26  *
27  * #### Parameters
28  *  - closure : the user defined closure pointer 'closure'
29  *  - object : a JSON object returned (can be NULL)
30  *  - error : a string not NULL in case of error but NULL on success
31  *  - info : a string handling some info (can be NULL)
32  *  - api : the api
33  *
34  * #### Return
35  *  None
36  *
37  */
38 static void api_callback(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api)
39 {
40     HMI_NOTICE("homescreen-service","asynchronous call, error=%s, info=%s, object=%s.", error, info, json_object_get_string(object));
41     callback_func* f = (callback_func*)closure;
42     if(f != nullptr)
43         (*f)(api, object);
44 }
45
46 /**
47  * call api asynchronous
48  *
49  * #### Parameters
50  *  - api : the api serving the request
51  *  - service : the api name of service
52  *  - verb : the verb of service
53  *  - args : parameter
54  *
55  * #### Return
56  *  None
57  *
58  */
59 static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args, callback_func *f = nullptr)
60 {
61     HMI_NOTICE("homescreen-service","service=%s verb=%s, args=%s.", service, verb, json_object_get_string(args));
62     afb_api_call(api, service, verb, args, api_callback, (void*)f);
63 }
64
65 /**
66  * call api synchronous
67  *
68  * #### Parameters
69  *  - api : the api serving the request
70  *  - service : the api name of service
71  *  - verb : the verb of afm-main
72  *  - args : parameter
73  *  - object : return the details of application
74  *
75  * #### Return
76  *  0 : success
77  *  1 : fail
78  *
79  */
80 static int api_call_sync(afb_api_t api, const char *service, const char *verb, struct json_object *args, struct json_object **object)
81 {
82     char *error = nullptr, *info = nullptr;
83     int ret = afb_api_call_sync(api, service, verb, args, object, &error, &info);
84     HMI_NOTICE("homescreen-service","synchronous call, error=%s, info=%s.", error, info);
85     return ret;
86 }
87
88 /**
89  * subscribe windowmanager event
90  *
91  * #### Parameters
92  *  - api : the api serving the request
93  *  - event : windowmanager event
94  *
95  * #### Return
96  *  None
97  *
98  */
99 void HS_WmProxy::subscribe(afb_api_t api, EventType event)
100 {
101     struct json_object* push_obj = json_object_new_object();
102     json_object_object_add(push_obj, _event, json_object_new_int(event));
103     api_call(api, _windowmanager, "wm_subscribe", push_obj);
104 }