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-client.h"
18 #include "hs-helper.h"
19 #include "hmi-debug.h"
21 static const char _type[] = "type";
24 * HS_Client construction function
33 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
35 HMI_NOTICE("homescreen-service","called.");
36 my_event = afb_api_make_event(request->api, id.c_str());
40 * HS_Client destruction function
49 HS_Client::~HS_Client()
51 HMI_NOTICE("homescreen-service","called.");
52 afb_event_unref(my_event);
56 * push tap_shortcut event
65 int HS_Client::tap_shortcut(const char* appid)
67 if(!checkEvent(__FUNCTION__))
70 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
71 struct json_object* push_obj = json_object_new_object();
72 hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid,
74 afb_event_push(my_event, push_obj);
79 * push on_screen_message event
82 * - message: post message.
88 int HS_Client::on_screen_message(afb_req_t request, const char* message)
90 if(!checkEvent(__FUNCTION__))
93 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
94 struct json_object* push_obj = json_object_new_object();
95 hs_add_object_to_json_object_str( push_obj, 4, _display_message, message,
97 afb_event_push(my_event, push_obj);
102 * push on_screen_reply event
105 * - message: reply message.
111 int HS_Client::on_screen_reply(afb_req_t request, const char* message)
113 if(!checkEvent(__FUNCTION__))
116 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
117 struct json_object* push_obj = json_object_new_object();
118 hs_add_object_to_json_object_str( push_obj, 4, _reply_message, message,
119 _type, __FUNCTION__);
120 afb_event_push(my_event, push_obj);
128 * - event: homescreen event, tap_shortcut etc.
134 int HS_Client::subscribe(afb_req_t request, const char* event)
137 auto ip = event_list.find(std::string(event));
138 if(ip == event_list.end()) {
139 event_list[std::string(event)] = 0;
140 ret = afb_req_subscribe(request, my_event);
149 * - event: homescreen event, tap_shortcut etc.
155 int HS_Client::unsubscribe(afb_req_t request, const char* event)
158 event_list.erase(std::string(event));
159 if(event_list.empty()) {
160 ret = afb_req_unsubscribe(request, my_event);
166 * check if client subscribe event
169 * - event: homescreen event, tap_shortcut etc.
176 bool HS_Client::checkEvent(const char* event)
178 auto ip = event_list.find(std::string(event));
179 if(ip == event_list.end())