use appid instead of appname in "tap_shortcut"
[apps/agl-service-homescreen.git] / src / hs-clientmanager.cpp
1 /*
2  * Copyright (c) 2018 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 #include <algorithm>
17 #include "hs-clientmanager.h"
18 #include "hmi-debug.h"
19
20 HS_ClientManager* HS_ClientManager::me = nullptr;
21
22 static void cbRemoveClientCtxt(void *data)
23 {
24     HS_ClientManager::instance()->removeClientCtxt(data);
25 }
26
27 /**
28  * HS_ClientManager construction function
29  *
30  * #### Parameters
31  *  - Nothing
32  *
33  * #### Return
34  * None
35  *
36  */
37 HS_ClientManager::HS_ClientManager()
38 {
39 }
40
41 /**
42  * get instance
43  *
44  * #### Parameters
45  *  - Nothing
46  *
47  * #### Return
48  * HS_ClientManager instance pointer
49  *
50  */
51 HS_ClientManager* HS_ClientManager::instance(void)
52 {
53     if(me == nullptr)
54         me = new HS_ClientManager();
55
56     return me;
57 }
58
59 /**
60  * HS_ClientManager init function
61  *
62  * #### Parameters
63  *  - Nothing
64  *
65  * #### Return
66  * init result
67  *
68  */
69 int HS_ClientManager::init(void)
70 {
71     HMI_NOTICE("homescreen-service","called.");
72 }
73
74 /**
75  * create client's afb_req_context
76  *
77  * #### Parameters
78  *  - appid: app's id
79  *
80  * #### Return
81  * HS_ClientCtxt pointer
82  *
83  */
84 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
85 {
86     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
87     if (!ctxt)
88     {
89         HMI_NOTICE("homescreen-service", "create new session for %s", appid.c_str());
90         HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
91         afb_req_session_set_LOA(req, 1);
92         afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
93     }
94     return ctxt;
95 }
96
97 /**
98  * add Client
99  *
100  * #### Parameters
101  *  - ctxt: app's id
102  *
103  * #### Return
104  * HS_Client pointer
105  *
106  */
107 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
108 {
109     return (client_list[appid] = new HS_Client(req, appid));
110 }
111
112 /**
113  * remove Client
114  *
115  * #### Parameters
116  *  - appid: app's id
117  *
118  * #### Return
119  * None
120  *
121  */
122 void HS_ClientManager::removeClient(std::string appid)
123 {
124     delete client_list[appid];
125     client_list.erase(appid);
126 }
127
128 /**
129  * remove Client from list
130  *
131  * #### Parameters
132  *  - data: HS_ClientCtxt pointer
133  *
134  * #### Return
135  * None
136  *
137  */
138 void HS_ClientManager::removeClientCtxt(void *data)
139 {
140     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
141     if(ctxt == nullptr)
142     {
143         HMI_ERROR("homescreen-service", "data is nullptr");
144         return;
145     }
146
147     HMI_NOTICE("homescreen-service", "remove app %s", ctxt->id.c_str());
148     std::lock_guard<std::mutex> lock(this->mtx);
149     removeClient(ctxt->id);
150     delete appid2ctxt[ctxt->id];
151     appid2ctxt.erase(ctxt->id);
152 }
153
154 /**
155  * tap_shortcut
156  *
157  * #### Parameters
158  *  - request: the request to bindings
159  *
160  * #### Return
161  * result
162  *
163  */
164 int HS_ClientManager::tap_shortcut(afb_req_t request)
165 {
166     int ret = 0;
167     const char* value = afb_req_value(request, _application_id);
168     if (value) {
169         HMI_NOTICE("homescreen-service","request params = %s.", value);
170         std::lock_guard<std::mutex> lock(this->mtx);
171         auto ip = client_list.find(std::string(value));
172         if(ip != client_list.end()) {
173             ip->second->tap_shortcut(value);
174         }
175     }
176     else {
177         HMI_NOTICE("homescreen-service","Please input application_id");
178         ret = AFB_EVENT_BAD_REQUEST;
179     }
180     return ret;
181 }
182
183 /**
184  * on_screen_message
185  *
186  * #### Parameters
187  *  - request: the request to bindings
188  *
189  * #### Return
190  * result
191  *
192  */
193 int HS_ClientManager::on_screen_message(afb_req_t request)
194 {
195     int ret = 0;
196     const char* value = afb_req_value(request, _display_message);
197     if (value) {
198         HMI_NOTICE("homescreen-service","request params = %s.", value);
199         std::lock_guard<std::mutex> lock(this->mtx);
200         for(auto m : client_list) {
201             m.second->on_screen_message(request, value);
202         }
203     }
204     else {
205         HMI_NOTICE("homescreen-service","Please input display_message");
206         ret = AFB_EVENT_BAD_REQUEST;
207     }
208     return ret;
209 }
210
211 /**
212  * on_screen_reply
213  *
214  * #### Parameters
215  *  - request: the request to bindings
216  *
217  * #### Return
218  * result
219  *
220  */
221 int HS_ClientManager::on_screen_reply(afb_req_t request)
222 {
223     int ret = 0;
224     const char* value = afb_req_value(request, _reply_message);
225     if (value) {
226       HMI_NOTICE("homescreen-service","request params = %s.", value);
227       std::lock_guard<std::mutex> lock(this->mtx);
228       for(auto m : client_list) {
229         m.second->on_screen_reply(request, value);
230       }
231     }
232     else {
233         HMI_NOTICE("homescreen-service","Please input reply_message");
234         ret = AFB_EVENT_BAD_REQUEST;
235     }
236     return ret;
237 }
238
239 /**
240  * subscribe
241  *
242  * #### Parameters
243  *  - request: the request to bindings
244  *
245  * #### Return
246  * result
247  *
248  */
249 int HS_ClientManager::subscribe(afb_req_t request)
250 {
251     int ret = 0;
252     const char *value = afb_req_value(request, "event");
253     HMI_NOTICE("homescreen-service","value is %s", value);
254     if(value) {
255         std::string appid(afb_req_get_application_id(request));
256         std::lock_guard<std::mutex> lock(this->mtx);
257
258         HS_Client* client = nullptr;
259         auto ip = client_list.find(appid);
260         if(ip != client_list.end()) {
261             client = client_list[appid];
262         }
263         else {
264             appid2ctxt[appid] = createClientCtxt(request, appid);
265             client = addClient(request, appid);
266         }
267
268         if(client->subscribe(request, value) != 0) {
269             HMI_NOTICE("homescreen-service","subscribe failed");
270             ret = AFB_REQ_SUBSCRIBE_ERROR;
271         }
272     }
273     else {
274         HMI_NOTICE("homescreen-service","Please input event name");
275         ret = AFB_EVENT_BAD_REQUEST;
276     }
277     return ret;
278 }
279
280 /**
281  * unsubscribe
282  *
283  * #### Parameters
284  *  - request: the request to bindings
285  *
286  * #### Return
287  * result
288  *
289  */
290 int HS_ClientManager::unsubscribe(afb_req_t request)
291 {
292     const char *value = afb_req_value(request, "event");
293     HMI_NOTICE("homescreen-service","value is %s", value);
294     int ret = 0;
295     if(value) {
296         std::string appid(afb_req_get_application_id(request));
297         std::lock_guard<std::mutex> lock(this->mtx);
298
299         auto ip = client_list.find(appid);
300         if(ip != client_list.end()
301         && ip->second->unsubscribe(request, value) != 0) {
302             HMI_NOTICE("homescreen-service","unsubscribe failed");
303             ret = AFB_REQ_UNSUBSCRIBE_ERROR;
304         }
305     }
306     else {
307         HMI_NOTICE("homescreen-service","Please input event name");
308         ret = AFB_EVENT_BAD_REQUEST;
309     }
310     return ret;
311 }