2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "hs-clientmanager.h"
18 #include "hmi-debug.h"
20 HS_ClientManager* HS_ClientManager::me = nullptr;
22 static void cbRemoveClientCtxt(void *data)
24 HS_ClientManager::instance()->removeClientCtxt(data);
28 * HS_ClientManager construction function
37 HS_ClientManager::HS_ClientManager()
48 * HS_ClientManager instance pointer
51 HS_ClientManager* HS_ClientManager::instance(void)
54 me = new HS_ClientManager();
60 * HS_ClientManager init function
69 int HS_ClientManager::init(void)
71 HMI_NOTICE("homescreen-service","called.");
75 * create client's afb_req_context
81 * HS_ClientCtxt pointer
84 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
86 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
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);
107 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
109 return (client_list[appid] = new HS_Client(req, appid));
122 void HS_ClientManager::removeClient(std::string appid)
124 delete client_list[appid];
125 client_list.erase(appid);
129 * remove Client from list
132 * - data: HS_ClientCtxt pointer
138 void HS_ClientManager::removeClientCtxt(void *data)
140 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
143 HMI_ERROR("homescreen-service", "data is nullptr");
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);
158 * - request: the request to bindings
164 int HS_ClientManager::tap_shortcut(afb_req_t request)
167 const char* value = afb_req_value(request, _application_name);
169 HMI_NOTICE("homescreen-service","request params = %s.", value);
170 // first step get appid from appname, next step change appname to appid
171 std::string appid(value);
172 std::transform(appid.begin(), appid.end(), appid.begin(), ::tolower);
173 std::lock_guard<std::mutex> lock(this->mtx);
174 auto ip = client_list.find(appid);
175 if(ip != client_list.end()) {
176 ip->second->tap_shortcut(value);
180 HMI_NOTICE("homescreen-service","Please input application_name");
181 ret = AFB_EVENT_BAD_REQUEST;
190 * - request: the request to bindings
196 int HS_ClientManager::on_screen_message(afb_req_t request)
199 const char* value = afb_req_value(request, _display_message);
201 HMI_NOTICE("homescreen-service","request params = %s.", value);
202 std::lock_guard<std::mutex> lock(this->mtx);
203 for(auto m : client_list) {
204 m.second->on_screen_message(request, value);
208 HMI_NOTICE("homescreen-service","Please input display_message");
209 ret = AFB_EVENT_BAD_REQUEST;
218 * - request: the request to bindings
224 int HS_ClientManager::on_screen_reply(afb_req_t request)
227 const char* value = afb_req_value(request, _reply_message);
229 HMI_NOTICE("homescreen-service","request params = %s.", value);
230 std::lock_guard<std::mutex> lock(this->mtx);
231 for(auto m : client_list) {
232 m.second->on_screen_reply(request, value);
236 HMI_NOTICE("homescreen-service","Please input reply_message");
237 ret = AFB_EVENT_BAD_REQUEST;
246 * - request: the request to bindings
252 int HS_ClientManager::subscribe(afb_req_t request)
255 const char *value = afb_req_value(request, "event");
256 HMI_NOTICE("homescreen-service","value is %s", value);
258 std::string appid(afb_req_get_application_id(request));
259 std::transform(appid.begin(), appid.end(), appid.begin(), ::tolower);
260 std::lock_guard<std::mutex> lock(this->mtx);
262 HS_Client* client = nullptr;
263 auto ip = client_list.find(appid);
264 if(ip != client_list.end()) {
265 client = client_list[appid];
268 appid2ctxt[appid] = createClientCtxt(request, appid);
269 client = addClient(request, appid);
272 if(client->subscribe(request, value) != 0) {
273 HMI_NOTICE("homescreen-service","subscribe failed");
274 ret = AFB_REQ_SUBSCRIBE_ERROR;
278 HMI_NOTICE("homescreen-service","Please input event name");
279 ret = AFB_EVENT_BAD_REQUEST;
288 * - request: the request to bindings
294 int HS_ClientManager::unsubscribe(afb_req_t request)
296 const char *value = afb_req_value(request, "event");
297 HMI_NOTICE("homescreen-service","value is %s", value);
300 std::string appid(afb_req_get_application_id(request));
301 std::transform(appid.begin(), appid.end(), appid.begin(), ::tolower);
302 std::lock_guard<std::mutex> lock(this->mtx);
304 auto ip = client_list.find(appid);
305 if(ip != client_list.end()
306 && ip->second->unsubscribe(request, value) != 0) {
307 HMI_NOTICE("homescreen-service","unsubscribe failed");
308 ret = AFB_REQ_UNSUBSCRIBE_ERROR;
312 HMI_NOTICE("homescreen-service","Please input event name");
313 ret = AFB_EVENT_BAD_REQUEST;