c683c668864380b49f8c2063e1204a04b5d7d28b
[apps/agl-service-homescreen.git] / src / hs-client.cpp
1 /*
2  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "hs-client.h"
18 #include "hs-helper.h"
19 #include "hmi-debug.h"
20
21 static const char _type[] = "type";
22
23 /**
24  * HS_Client construction function
25  *
26  * #### Parameters
27  *  - id: app's id
28  *
29  * #### Return
30  * None
31  *
32  */
33 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
34 {
35     HMI_NOTICE("homescreen-service","called.");
36     my_event = afb_api_make_event(request->api, id.c_str());
37 }
38
39 /**
40  * HS_Client destruction function
41  *
42  * #### Parameters
43  *  - null
44  *
45  * #### Return
46  * None
47  *
48  */
49 HS_Client::~HS_Client()
50 {
51     HMI_NOTICE("homescreen-service","called.");
52     afb_event_unref(my_event);
53 }
54
55 /**
56  * push tap_shortcut event
57  *
58  * #### Parameters
59  *  - appname: app's name.
60  *
61  * #### Return
62  * result
63  *
64  */
65 int HS_Client::tap_shortcut(const char* appname)
66 {
67     if(!checkEvent(__FUNCTION__))
68         return 0;
69
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,
73     _type, __FUNCTION__);
74     afb_event_push(my_event, push_obj);
75     return 0;
76 }
77
78 /**
79  * push on_screen_message event
80  *
81  * #### Parameters
82  *  - message: post message.
83  *
84  * #### Return
85  * result
86  *
87  */
88 int HS_Client::on_screen_message(afb_req_t request, const char* message)
89 {
90     if(!checkEvent(__FUNCTION__))
91         return 0;
92
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,
96     _type, __FUNCTION__);
97     afb_event_push(my_event, push_obj);
98     return 0;
99 }
100
101 /**
102  * push on_screen_reply event
103  *
104  * #### Parameters
105  *  - message: reply message.
106  *
107  * #### Return
108  * result
109  *
110  */
111 int HS_Client::on_screen_reply(afb_req_t request, const char* message)
112 {
113     if(!checkEvent(__FUNCTION__))
114         return 0;
115
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);
121     return 0;
122 }
123
124 /**
125  * subscribe event
126  *
127  * #### Parameters
128  *  - event: homescreen event, tap_shortcut etc.
129  *
130  * #### Return
131  * result
132  *
133  */
134 int HS_Client::subscribe(afb_req_t request, const char* event)
135 {
136     int ret = 0;
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);
141     }
142     return ret;
143 }
144
145 /**
146  * unsubscribe event
147  *
148  * #### Parameters
149  *  - event: homescreen event, tap_shortcut etc.
150  *
151  * #### Return
152  * result
153  *
154  */
155 int HS_Client::unsubscribe(afb_req_t request, const char* event)
156 {
157     int ret = 0;
158     event_list.erase(std::string(event));
159     if(event_list.empty()) {
160         ret = afb_req_unsubscribe(request, my_event);
161     }
162     return ret;
163 }
164
165 /**
166  * check if client subscribe event
167  *
168  * #### Parameters
169  *  - event: homescreen event, tap_shortcut etc.
170  *
171  * #### Return
172  * true: found
173  * false: not found
174  *
175  */
176 bool HS_Client::checkEvent(const char* event)
177 {
178     auto ip = event_list.find(std::string(event));
179     if(ip == event_list.end())
180         return false;
181     else
182         return true;
183 }