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_id);
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);
177 HMI_NOTICE("homescreen-service","Please input application_id");
178 ret = AFB_EVENT_BAD_REQUEST;
187 * - request: the request to bindings
193 int HS_ClientManager::on_screen_message(afb_req_t request)
196 const char* value = afb_req_value(request, _display_message);
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);
205 HMI_NOTICE("homescreen-service","Please input display_message");
206 ret = AFB_EVENT_BAD_REQUEST;
215 * - request: the request to bindings
221 int HS_ClientManager::on_screen_reply(afb_req_t request)
224 const char* value = afb_req_value(request, _reply_message);
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);
233 HMI_NOTICE("homescreen-service","Please input reply_message");
234 ret = AFB_EVENT_BAD_REQUEST;
243 * - request: the request to bindings
249 int HS_ClientManager::subscribe(afb_req_t request)
252 const char *value = afb_req_value(request, "event");
253 HMI_NOTICE("homescreen-service","value is %s", value);
255 std::string appid(afb_req_get_application_id(request));
256 std::lock_guard<std::mutex> lock(this->mtx);
258 HS_Client* client = nullptr;
259 auto ip = client_list.find(appid);
260 if(ip != client_list.end()) {
261 client = client_list[appid];
264 appid2ctxt[appid] = createClientCtxt(request, appid);
265 client = addClient(request, appid);
268 if(client->subscribe(request, value) != 0) {
269 HMI_NOTICE("homescreen-service","subscribe failed");
270 ret = AFB_REQ_SUBSCRIBE_ERROR;
274 HMI_NOTICE("homescreen-service","Please input event name");
275 ret = AFB_EVENT_BAD_REQUEST;
284 * - request: the request to bindings
290 int HS_ClientManager::unsubscribe(afb_req_t request)
292 const char *value = afb_req_value(request, "event");
293 HMI_NOTICE("homescreen-service","value is %s", value);
296 std::string appid(afb_req_get_application_id(request));
297 std::lock_guard<std::mutex> lock(this->mtx);
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;
307 HMI_NOTICE("homescreen-service","Please input event name");
308 ret = AFB_EVENT_BAD_REQUEST;