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
59 * - appname: app's name.
65 int HS_Client::tap_shortcut(const char* appname)
67 if(!checkEvent(__FUNCTION__))
70 HMI_NOTICE("homescreen-service","%s application_name = %s.", __FUNCTION__, appname);
71 struct json_object* push_obj = json_object_new_object();
72 hs_add_object_to_json_object_str( push_obj, 4, _application_name, appname,
74 return afb_event_push(my_event, push_obj);
78 * push on_screen_message event
81 * - message: post message.
87 int HS_Client::on_screen_message(afb_req_t request, const char* message)
89 if(!checkEvent(__FUNCTION__))
92 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
93 struct json_object* push_obj = json_object_new_object();
94 hs_add_object_to_json_object_str( push_obj, 4, _display_message, message,
96 return afb_event_push(my_event, push_obj);
100 * push on_screen_reply event
103 * - message: reply message.
109 int HS_Client::on_screen_reply(afb_req_t request, const char* message)
111 if(!checkEvent(__FUNCTION__))
114 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
115 struct json_object* push_obj = json_object_new_object();
116 hs_add_object_to_json_object_str( push_obj, 4, _reply_message, message,
117 _type, __FUNCTION__);
118 return afb_event_push(my_event, push_obj);
125 * - event: homescreen event, tap_shortcut etc.
131 int HS_Client::subscribe(afb_req_t request, const char* event)
134 auto ip = event_list.find(std::string(event));
135 if(ip == event_list.end()) {
136 event_list[std::string(event)] = 0;
137 ret = afb_req_subscribe(request, my_event);
146 * - event: homescreen event, tap_shortcut etc.
152 int HS_Client::unsubscribe(afb_req_t request, const char* event)
155 event_list.erase(std::string(event));
156 if(event_list.empty()) {
157 ret = afb_req_unsubscribe(request, my_event);
163 * check if client subscribe event
166 * - event: homescreen event, tap_shortcut etc.
173 bool HS_Client::checkEvent(const char* event)
175 auto ip = event_list.find(std::string(event));
176 if(ip == event_list.end())