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);
160 * - request: the request to bindings
166 int HS_ClientManager::tap_shortcut(afb_req_t request)
169 const char* value = afb_req_value(request, _application_id);
171 HMI_NOTICE("homescreen-service","request params = %s.", value);
172 std::lock_guard<std::mutex> lock(this->mtx);
173 auto ip = client_list.find(std::string(value));
174 if(ip != client_list.end()) {
175 ip->second->tap_shortcut(value);
179 HMI_NOTICE("homescreen-service","Please input application_id");
180 ret = AFB_EVENT_BAD_REQUEST;
189 * - request: the request to bindings
195 int HS_ClientManager::on_screen_message(afb_req_t request)
198 const char* value = afb_req_value(request, _display_message);
200 HMI_NOTICE("homescreen-service","request params = %s.", value);
201 std::lock_guard<std::mutex> lock(this->mtx);
202 for(auto m : client_list) {
203 m.second->on_screen_message(request, value);
207 HMI_NOTICE("homescreen-service","Please input display_message");
208 ret = AFB_EVENT_BAD_REQUEST;
217 * - request: the request to bindings
223 int HS_ClientManager::on_screen_reply(afb_req_t request)
226 const char* value = afb_req_value(request, _reply_message);
228 HMI_NOTICE("homescreen-service","request params = %s.", value);
229 std::lock_guard<std::mutex> lock(this->mtx);
230 for(auto m : client_list) {
231 m.second->on_screen_reply(request, value);
235 HMI_NOTICE("homescreen-service","Please input reply_message");
236 ret = AFB_EVENT_BAD_REQUEST;
245 * - request: the request to bindings
251 int HS_ClientManager::subscribe(afb_req_t request)
254 const char *value = afb_req_value(request, "event");
255 HMI_NOTICE("homescreen-service","value is %s", value);
257 std::string appid =std::move(get_application_id(request));
259 HMI_NOTICE("homescreen-service","can't get application identifier");
260 return AFB_REQ_GETAPPLICATIONID_ERROR;
263 std::lock_guard<std::mutex> lock(this->mtx);
264 HS_Client* client = nullptr;
265 auto ip = client_list.find(appid);
266 if(ip != client_list.end()) {
267 client = client_list[appid];
270 appid2ctxt[appid] = createClientCtxt(request, appid);
271 client = addClient(request, appid);
274 if(client->subscribe(request, value) != 0) {
275 HMI_NOTICE("homescreen-service","subscribe failed");
276 ret = AFB_REQ_SUBSCRIBE_ERROR;
280 HMI_NOTICE("homescreen-service","Please input event name");
281 ret = AFB_EVENT_BAD_REQUEST;
290 * - request: the request to bindings
296 int HS_ClientManager::unsubscribe(afb_req_t request)
298 const char *value = afb_req_value(request, "event");
299 HMI_NOTICE("homescreen-service","value is %s", value);
302 std::string appid = std::move(get_application_id(request));
304 HMI_NOTICE("homescreen-service","can't get application identifier");
305 return AFB_REQ_GETAPPLICATIONID_ERROR;
308 std::lock_guard<std::mutex> lock(this->mtx);
309 auto ip = client_list.find(appid);
310 if(ip != client_list.end()
311 && ip->second->unsubscribe(request, value) != 0) {
312 HMI_NOTICE("homescreen-service","unsubscribe failed");
313 ret = AFB_REQ_UNSUBSCRIBE_ERROR;
317 HMI_NOTICE("homescreen-service","Please input event name");
318 ret = AFB_EVENT_BAD_REQUEST;
327 * - request : the request
334 int HS_ClientManager::showWindow(afb_req_t request)
337 const char* value = afb_req_value(request, _application_id);
339 HMI_NOTICE("homescreen-service","request params = %s.", value);
340 std::lock_guard<std::mutex> lock(this->mtx);
341 auto ip = client_list.find(std::string(value));
342 if(ip != client_list.end()) {
343 ret = ip->second->showWindow(request, value);
347 HMI_NOTICE("homescreen-service","Please input application_id");
348 ret = AFB_EVENT_BAD_REQUEST;
357 * - request : the request
364 int HS_ClientManager::hideWindow(afb_req_t request)
367 const char* value = afb_req_value(request, _application_id);
369 HMI_NOTICE("homescreen-service","request params = %s.", value);
370 std::lock_guard<std::mutex> lock(this->mtx);
371 auto ip = client_list.find(std::string(value));
372 if(ip != client_list.end()) {
373 ret = ip->second->hideWindow(request);
377 HMI_NOTICE("homescreen-service","Please input application_id");
378 ret = AFB_EVENT_BAD_REQUEST;
384 * replyShowWindow event
387 * - request : the request
394 int HS_ClientManager::replyShowWindow(afb_req_t request)
397 const char* value = afb_req_value(request, _application_id);
399 HMI_NOTICE("homescreen-service","request params = %s.", value);
400 std::lock_guard<std::mutex> lock(this->mtx);
401 auto ip = client_list.find(std::string(value));
402 if(ip != client_list.end()) {
403 ret = ip->second->replyShowWindow(request, value);
407 HMI_NOTICE("homescreen-service","Please input application_id");
408 ret = AFB_EVENT_BAD_REQUEST;
414 * showNotification event
417 * - request : the request
424 int HS_ClientManager::showNotification(afb_req_t request)
427 std::lock_guard<std::mutex> lock(this->mtx);
428 auto ip = client_list.find(_homescreen);
429 if(ip != client_list.end()) {
430 ret = ip->second->showNotification(request);
433 HMI_NOTICE("homescreen-service","not exist sessiion with homescreen");
434 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
441 * showInformation event
444 * - request : the request
451 int HS_ClientManager::showInformation(afb_req_t request)
454 std::lock_guard<std::mutex> lock(this->mtx);
455 auto ip = client_list.find(_homescreen);
456 if(ip != client_list.end()) {
457 ret = ip->second->showInformation(request);
460 HMI_NOTICE("homescreen-service","not exist sessiion with homescreen");
461 ret = AFB_REQ_SHOWINFORMATION_ERROR;
471 * - event : the event want to push
472 * - param : the parameter contents of event
473 * - appid : the destination application's id
480 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
482 if(event == nullptr) {
483 HMI_ERROR("homescreen-service","event name is null.");
487 if(appid.empty()) { // broadcast event to clients who subscribed this event
488 std::lock_guard<std::mutex> lock(this->mtx);
489 for(auto m : client_list) {
490 m.second->pushEvent(event, param);
493 else { // push event to specific client
494 std::lock_guard<std::mutex> lock(this->mtx);
495 auto ip = client_list.find(appid);
496 if(ip != client_list.end()) {
497 ip->second->pushEvent(event, param);
504 int HS_ClientManager::handleRequest(const char *verb, afb_req_t request)
506 HMI_NOTICE("homescreen-service","verb = %s", verb);
507 int ret = AFB_EVENT_BAD_REQUEST;
508 auto ip = func_list.find(std::string(verb));
509 if(ip != func_list.end()) {
510 HMI_NOTICE("homescreen-service","verb found");
511 ret = (this->*(ip->second))(request);