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