merge vui
[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
19 const char _afm_main[] = "afm-main";
20 const char _windowmanager[] = "windowmanager";
21 const char _event[] = "event";
22 const char _vshl_capabilities[] = "vshl-capabilities";
23 const char _actions[] = "actions";
24
25 /**
26  * the callback function
27  *
28  * #### Parameters
29  *  - closure : the user defined closure pointer 'closure'
30  *  - object : a JSON object returned (can be NULL)
31  *  - error : a string not NULL in case of error but NULL on success
32  *  - info : a string handling some info (can be NULL)
33  *  - api : the api
34  *
35  * #### Return
36  *  None
37  *
38  */
39 static void api_callback(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api)
40 {
41     AFB_INFO("asynchronous call, error=%s, info=%s, object=%s.", error, info, json_object_get_string(object));
42     if(closure != nullptr) {
43         reinterpret_cast<api_cb_func>(closure)(object, error, info);
44     }
45 }
46
47 /**
48  * call api asynchronous
49  *
50  * #### Parameters
51  *  - api : the api serving the request
52  *  - service : the api name of service
53  *  - verb : the verb of service
54  *  - args : parameter
55  *
56  * #### Return
57  *  None
58  *
59  */
60 static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args, api_cb_func f = nullptr)
61 {
62     AFB_INFO("service=%s verb=%s, args=%s.", service, verb, json_object_get_string(args));
63     afb_api_call(api, service, verb, args, api_callback, (void*)f);
64 }
65
66 /**
67  * call api synchronous
68  *
69  * #### Parameters
70  *  - api : the api serving the request
71  *  - service : the api name of service
72  *  - verb : the verb of afm-main
73  *  - args : parameter
74  *  - object : return the details of application
75  *
76  * #### Return
77  *  0 : success
78  *  1 : fail
79  *
80  */
81 static int api_call_sync(afb_api_t api, const char *service, const char *verb, struct json_object *args, struct json_object **object)
82 {
83     char *error = nullptr, *info = nullptr;
84     int ret = afb_api_call_sync(api, service, verb, args, object, &error, &info);
85     AFB_INFO("synchronous call, error=%s, info=%s.", error, info);
86     return ret;
87 }
88
89 /* -------------------------------------HS_AfmMainProxy------------------------------------------ */
90
91 /**
92  * get runnables application list
93  *
94  * #### Parameters
95  *  - api : the api serving the request
96  *  - object : return the details of appid
97  *
98  * #### Return
99  *  0 : success
100  *  1 : fail
101  *
102  */
103 int HS_AfmMainProxy::runnables(afb_api_t api, struct json_object **object)
104 {
105     return api_call_sync(api, _afm_main, __FUNCTION__, nullptr, object);
106 }
107
108 /**
109  * get details of application
110  *
111  * #### Parameters
112  *  - api : the api serving the request
113  *  - id : the id to get details,liked "dashboard@0.1"
114  *  - object : return the details of application
115  *
116  * #### Return
117  *  0 : success
118  *  1 : fail
119  *
120  */
121 int HS_AfmMainProxy::detail(afb_api_t api, const std::string &id, struct json_object **object)
122 {
123     struct json_object *args = json_object_new_string(id.c_str());
124     return api_call_sync(api, _afm_main, __FUNCTION__, args, object);
125 }
126
127 /**
128  * start application
129  *
130  * #### Parameters
131  *  - api : the api
132  *  - id : the application id liked "dashboard@0.1"
133  *
134  * #### Return
135  *  None
136  *
137  */
138 void HS_AfmMainProxy::start(afb_api_t api, const std::string &id)
139 {
140     struct json_object *args = json_object_new_string(id.c_str());
141     api_call(api, _afm_main, __FUNCTION__, args);
142 }
143
144 /* -------------------------------------HS_WmProxy------------------------------------------ */
145
146 /**
147  * subscribe windowmanager event
148  *
149  * #### Parameters
150  *  - api : the api serving the request
151  *  - event : windowmanager event
152  *  - f : callback function
153  *
154  * #### Return
155  *  None
156  *
157  */
158 void HS_WmProxy::subscribe(afb_api_t api, EventType event, api_cb_func f)
159 {
160     struct json_object* push_obj = json_object_new_object();
161     json_object_object_add(push_obj, _event, json_object_new_int(event));
162     api_call(api, _windowmanager, "wm_subscribe", push_obj, f);
163 }
164
165 /* -------------------------------------HS_VshlCapabilitiesProxy------------------------------------------ */
166
167 /**
168  * subscribe event
169  *
170  * #### Parameters
171  *  - api : the api
172  *  - module : module name
173  *  - ev_list : event list
174  *
175  * #### Return
176  *  None
177  *
178  */
179 void HS_VshlCapabilitiesProxy::subscribe(afb_api_t api, const std::string &module, const std::list<std::string> &ev_list)
180 {
181     if(ev_list.empty()) {
182         return;
183     }
184
185     struct json_object *arr_obj = json_object_new_array();
186     for(auto &it : ev_list) {
187         json_object_array_add(arr_obj, json_object_new_string(it.c_str()));
188     }
189     struct json_object *args = json_object_new_object();
190     json_object_object_add(args, _actions, arr_obj);
191
192     std::string verb = module + '/' + __FUNCTION__;
193     api_call(api, _vshl_capabilities, verb.c_str(), args);
194 }