try to use afb_api_event_handler_add
[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
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     AFB_INFO("asynchronous call, error=%s, info=%s, object=%s.", error, info, json_object_get_string(object));
41 }
42
43 /**
44  * call api asynchronous
45  *
46  * #### Parameters
47  *  - api : the api serving the request
48  *  - service : the api name of service
49  *  - verb : the verb of service
50  *  - args : parameter
51  *
52  * #### Return
53  *  None
54  *
55  */
56 static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args)
57 {
58     AFB_INFO("service=%s verb=%s, args=%s.", service, verb, json_object_get_string(args));
59     afb_api_call(api, service, verb, args, api_callback, nullptr);
60 }
61
62 /**
63  * call api synchronous
64  *
65  * #### Parameters
66  *  - api : the api serving the request
67  *  - service : the api name of service
68  *  - verb : the verb of afm-main
69  *  - args : parameter
70  *  - object : return the details of application
71  *
72  * #### Return
73  *  0 : success
74  *  1 : fail
75  *
76  */
77 static int api_call_sync(afb_api_t api, const char *service, const char *verb, struct json_object *args, struct json_object **object)
78 {
79     char *error = nullptr, *info = nullptr;
80     int ret = afb_api_call_sync(api, service, verb, args, object, &error, &info);
81     AFB_INFO("synchronous call, error=%s, info=%s.", error, info);
82     return ret;
83 }
84
85 /**
86  * get runnables application list
87  *
88  * #### Parameters
89  *  - api : the api serving the request
90  *  - object : return the details of appid
91  *
92  * #### Return
93  *  0 : success
94  *  1 : fail
95  *
96  */
97 int HS_AfmMainProxy::runnables(afb_api_t api, struct json_object **object)
98 {
99     return api_call_sync(api, _afm_main, __FUNCTION__, nullptr, object);
100 }
101
102 /**
103  * get details of application
104  *
105  * #### Parameters
106  *  - api : the api serving the request
107  *  - id : the id to get details,liked "dashboard@0.1"
108  *  - object : return the details of application
109  *
110  * #### Return
111  *  0 : success
112  *  1 : fail
113  *
114  */
115 int HS_AfmMainProxy::detail(afb_api_t api, const std::string &id, struct json_object **object)
116 {
117     struct json_object *args = json_object_new_string(id.c_str());
118     return api_call_sync(api, _afm_main, __FUNCTION__, args, object);
119 }
120
121 /**
122  * start application
123  *
124  * #### Parameters
125  *  - request : the request
126  *  - id : the application id liked "dashboard@0.1"
127  *
128  * #### Return
129  *  None
130  *
131  */
132 void HS_AfmMainProxy::start(afb_req_t request, const std::string &id)
133 {
134     struct json_object *args = json_object_new_string(id.c_str());
135     api_call(request->api, _afm_main, __FUNCTION__, args);
136 }
137
138 /**
139  * subscribe windowmanager event
140  *
141  * #### Parameters
142  *  - api : the api serving the request
143  *  - event : windowmanager event
144  *
145  * #### Return
146  *  None
147  *
148  */
149 void HS_WmProxy::subscribe(afb_api_t api, EventType event)
150 {
151     struct json_object* push_obj = json_object_new_object();
152     json_object_object_add(push_obj, _event, json_object_new_int(event));
153     api_call(api, _windowmanager, "wm_subscribe", push_obj);
154 }