807b068de094cf2b0fe7cecbc3d885d36d0d6dea
[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(struct afb_req request, std::string id) : my_id(id)
34 {
35     HMI_NOTICE("homescreen-service","called.");
36     my_event = afb_daemon_make_event(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     return afb_event_push(my_event, push_obj);
75 }
76
77 /**
78  * push on_screen_message event
79  *
80  * #### Parameters
81  *  - message: post message.
82  *
83  * #### Return
84  * result
85  *
86  */
87 int HS_Client::on_screen_message(struct afb_req request, const char* message)
88 {
89     if(!checkEvent(__FUNCTION__))
90         return 0;
91
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,
95     _type, __FUNCTION__);
96     return afb_event_push(my_event, push_obj);
97 }
98
99 /**
100  * push on_screen_reply event
101  *
102  * #### Parameters
103  *  - message: reply message.
104  *
105  * #### Return
106  * result
107  *
108  */
109 int HS_Client::on_screen_reply(struct afb_req request, const char* message)
110 {
111     if(!checkEvent(__FUNCTION__))
112         return 0;
113
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);
119 }
120
121 /**
122  * subscribe event
123  *
124  * #### Parameters
125  *  - event: homescreen event, tap_shortcut etc.
126  *
127  * #### Return
128  * result
129  *
130  */
131 int HS_Client::subscribe(struct afb_req request, const char* event)
132 {
133     int ret = 0;
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);
138     }
139     return ret;
140 }
141
142 /**
143  * unsubscribe event
144  *
145  * #### Parameters
146  *  - event: homescreen event, tap_shortcut etc.
147  *
148  * #### Return
149  * result
150  *
151  */
152 int HS_Client::unsubscribe(struct afb_req request, const char* event)
153 {
154     int ret = 0;
155     event_list.erase(std::string(event));
156     if(event_list.empty()) {
157         ret = afb_req_unsubscribe(request, my_event);
158     }
159     return ret;
160 }
161
162 /**
163  * check if client subscribe event
164  *
165  * #### Parameters
166  *  - event: homescreen event, tap_shortcut etc.
167  *
168  * #### Return
169  * true: found
170  * false: not found
171  *
172  */
173 bool HS_Client::checkEvent(const char* event)
174 {
175     auto ip = event_list.find(std::string(event));
176     if(ip == event_list.end())
177         return false;
178     else
179         return true;
180 }