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 static const char _homescreen[] = "homescreen";
22 HS_ClientManager* HS_ClientManager::me = nullptr;
24 static void cbRemoveClientCtxt(void *data)
26 HS_ClientManager::instance()->removeClientCtxt(data);
30 * HS_ClientManager construction function
39 HS_ClientManager::HS_ClientManager()
50 * HS_ClientManager instance pointer
53 HS_ClientManager* HS_ClientManager::instance(void)
56 me = new HS_ClientManager();
62 * HS_ClientManager init function
71 int HS_ClientManager::init(void)
73 HMI_NOTICE("homescreen-service","called.");
77 * create client's afb_req_context
83 * HS_ClientCtxt pointer
86 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
88 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
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);
109 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
111 return (client_list[appid] = new HS_Client(req, appid));
124 void HS_ClientManager::removeClient(std::string appid)
126 delete client_list[appid];
127 client_list.erase(appid);
131 * remove Client from list
134 * - data: HS_ClientCtxt pointer
140 void HS_ClientManager::removeClientCtxt(void *data)
142 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
145 HMI_ERROR("homescreen-service", "data is nullptr");
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);
157 * handle homescreen request
160 * - request : the request
161 * - verb : the verb name
162 * - appid : to which application
169 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
171 HMI_NOTICE("homescreen-service","verb=[%s],appid=[%s].", verb, appid);
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);
180 auto ip = client_list.find(std::string(appid));
181 if(ip != client_list.end()) {
182 ret = ip->second->handleRequest(request, verb);
185 if(!strcasecmp(verb, "subscribe")) {
186 appid2ctxt[appid] = createClientCtxt(request, appid);
187 HS_Client* client = addClient(request, appid);
188 ret = client->handleRequest(request, "subscribe");
191 HMI_NOTICE("homescreen-service","not exist session");
192 ret = AFB_EVENT_BAD_REQUEST;