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