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