Start app and get runnables list by homescreen
[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 static const char _homescreen[] = "homescreen";
21
22 HS_ClientManager* HS_ClientManager::me = nullptr;
23
24 static void cbRemoveClientCtxt(void *data)
25 {
26     HS_ClientManager::instance()->removeClientCtxt(data);
27 }
28
29 /**
30  * HS_ClientManager construction function
31  *
32  * #### Parameters
33  *  - Nothing
34  *
35  * #### Return
36  * None
37  *
38  */
39 HS_ClientManager::HS_ClientManager()
40 {
41 }
42
43 /**
44  * get instance
45  *
46  * #### Parameters
47  *  - Nothing
48  *
49  * #### Return
50  * HS_ClientManager instance pointer
51  *
52  */
53 HS_ClientManager* HS_ClientManager::instance(void)
54 {
55     if(me == nullptr)
56         me = new HS_ClientManager();
57
58     return me;
59 }
60
61 /**
62  * HS_ClientManager init function
63  *
64  * #### Parameters
65  *  - Nothing
66  *
67  * #### Return
68  * init result
69  *
70  */
71 int HS_ClientManager::init(void)
72 {
73     HMI_NOTICE("homescreen-service","called.");
74 }
75
76 /**
77  * create client's afb_req_context
78  *
79  * #### Parameters
80  *  - appid: app's id
81  *
82  * #### Return
83  * HS_ClientCtxt pointer
84  *
85  */
86 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
87 {
88     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
89     if (!ctxt)
90     {
91         HMI_NOTICE("homescreen-service", "create new session for %s", appid.c_str());
92         HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
93         afb_req_session_set_LOA(req, 1);
94         afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
95     }
96     return ctxt;
97 }
98
99 /**
100  * add Client
101  *
102  * #### Parameters
103  *  - ctxt: app's id
104  *
105  * #### Return
106  * HS_Client pointer
107  *
108  */
109 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
110 {
111     return (client_list[appid] = new HS_Client(req, appid));
112 }
113
114 /**
115  * remove Client
116  *
117  * #### Parameters
118  *  - appid: app's id
119  *
120  * #### Return
121  * None
122  *
123  */
124 void HS_ClientManager::removeClient(std::string appid)
125 {
126     delete client_list[appid];
127     client_list.erase(appid);
128 }
129
130 /**
131  * remove Client from list
132  *
133  * #### Parameters
134  *  - data: HS_ClientCtxt pointer
135  *
136  * #### Return
137  * None
138  *
139  */
140 void HS_ClientManager::removeClientCtxt(void *data)
141 {
142     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
143     if(ctxt == nullptr)
144     {
145         HMI_ERROR("homescreen-service", "data is nullptr");
146         return;
147     }
148
149     HMI_NOTICE("homescreen-service", "remove app %s", ctxt->id.c_str());
150     std::lock_guard<std::mutex> lock(this->mtx);
151     removeClient(ctxt->id);
152     delete appid2ctxt[ctxt->id];
153     appid2ctxt.erase(ctxt->id);
154 }
155
156 /**
157  * handle homescreen request
158  *
159  * #### Parameters
160  *  - request : the request
161  *  - verb : the verb name
162  *  - appid : to which application
163  *
164  * #### Return
165  * 0 : success
166  * others : fail
167  *
168  */
169 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
170 {
171     HMI_NOTICE("homescreen-service","verb=[%s],appid=[%s].", verb, appid);
172     int ret = 0;
173     std::lock_guard<std::mutex> lock(this->mtx);
174     if(appid == nullptr) {
175         for(auto m : client_list) {
176             m.second->handleRequest(request, verb);
177         }
178     }
179     else {
180         auto ip = client_list.find(std::string(appid));
181         if(ip != client_list.end()) {
182             ret = ip->second->handleRequest(request, verb);
183         }
184         else {
185             if(!strcasecmp(verb, "subscribe")) {
186                 appid2ctxt[appid] = createClientCtxt(request, appid);
187                 HS_Client* client = addClient(request, appid);
188                 ret = client->handleRequest(request, "subscribe");
189             }
190             else {
191                 HMI_NOTICE("homescreen-service","not exist session");
192                 ret = AFB_REQ_NOT_STARTED_APPLICATION;
193             }
194         }
195     }
196     return ret;
197 }
198
199 /**
200  * push event
201  *
202  * #### Parameters
203  *  - event : the event want to push
204  *  - param : the parameter contents of event
205  *  - appid : the destination application's id
206  *
207  * #### Return
208  * 0 : success
209  * others : fail
210  *
211  */
212 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
213 {
214     if(event == nullptr) {
215         HMI_ERROR("homescreen-service","event name is null.");
216         return -1;
217     }
218
219     std::lock_guard<std::mutex> lock(this->mtx);
220     if(appid.empty()) { // broadcast event to clients who subscribed this event
221         for(auto m : client_list) {
222             m.second->pushEvent(event, param);
223         }
224     }
225     else {  // push event to specific client
226         auto ip = client_list.find(appid);
227         if(ip != client_list.end()) {
228             ip->second->pushEvent(event, param);
229         }
230     }
231
232     return 0;
233 }