d9ea285598946cba34158b642a3cd508a3de9ae0
[apps/agl-service-navigation.git] / binding / navigation-api.c
1 /*
2  * Copyright (C) 2019 Konsulko Group
3  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/time.h>
24 #include <time.h>
25
26 #include <glib.h>
27 #include <glib/gstdio.h>
28 #include <gio/gio.h>
29 #include <json-c/json.h>
30
31 #define AFB_BINDING_VERSION 3
32 #include <afb/afb-binding.h>
33
34 #include "navigation-api.h"
35
36 struct navigation_state *navigation_get_userdata(afb_req_t request) {
37         afb_api_t api = afb_req_get_api(request);
38         return afb_api_get_userdata(api);
39 }
40
41 static afb_event_t get_event_from_value(struct navigation_state *ns,
42                         const char *value)
43 {
44         if (!g_strcmp0(value, "status"))
45                 return ns->status_event;
46
47         if (!g_strcmp0(value, "position"))
48                 return ns->position_event;
49
50         if (!g_strcmp0(value, "waypoints"))
51                 return ns->waypoints_event;
52
53         return NULL;
54 }
55
56 static json_object **get_storage_from_value(struct navigation_state *ns,
57                         const char *value)
58 {
59         if (!g_strcmp0(value, "status"))
60                 return &ns->status_storage;
61
62         if (!g_strcmp0(value, "position"))
63                 return &ns->position_storage;
64
65         if (!g_strcmp0(value, "waypoints"))
66                 return &ns->waypoints_storage;
67
68         return NULL;
69 }
70
71 static void navigation_subscribe_unsubscribe(afb_req_t request,
72                 gboolean unsub)
73 {
74         struct navigation_state *ns = navigation_get_userdata(request);
75         json_object *jresp = json_object_new_object();
76         const char *value;
77         afb_event_t event;
78         int rc;
79
80         value = afb_req_value(request, "value");
81         if (!value) {
82                 afb_req_fail_f(request, "failed", "Missing \"value\" event");
83                 return;
84         }
85
86         event = get_event_from_value(ns, value);
87         if (!event) {
88                 afb_req_fail_f(request, "failed", "Bad \"value\" event \"%s\"",
89                                 value);
90                 return;
91         }
92
93         if (!unsub) {
94                 json_object *storage;
95                 rc = afb_req_subscribe(request, event);
96
97                 g_rw_lock_reader_lock(&ns->rw_lock);
98                 storage = *get_storage_from_value(ns, value);
99                 if (storage) {
100                         // increment reference counter, and send out cached value
101                         json_object_get(storage);
102                         afb_event_push(event, storage);
103                 }
104                 g_rw_lock_reader_unlock(&ns->rw_lock);
105         } else {
106                 rc = afb_req_unsubscribe(request, event);
107         }
108         if (rc != 0) {
109                 afb_req_fail_f(request, "failed",
110                                         "%s error on \"value\" event \"%s\"",
111                                         !unsub ? "subscribe" : "unsubscribe",
112                                         value);
113                 return;
114         }
115
116         afb_req_success_f(request, jresp, "Navigation %s to event \"%s\"",
117                         !unsub ? "subscribed" : "unsubscribed",
118                         value);
119 }
120
121 static void subscribe(afb_req_t request)
122 {
123         navigation_subscribe_unsubscribe(request, FALSE);
124 }
125
126 static void unsubscribe(afb_req_t request)
127 {
128         navigation_subscribe_unsubscribe(request, TRUE);
129 }
130
131 static void broadcast(afb_req_t request, const char *name, gboolean cache)
132 {
133         struct navigation_state *ns = navigation_get_userdata(request);
134         afb_event_t event = get_event_from_value(ns, name);
135         json_object *jresp = afb_req_json(request);
136
137         if (cache) {
138                 json_object **storage = get_storage_from_value(ns, name);
139
140                 g_rw_lock_writer_lock(&ns->rw_lock);
141
142                 if (*storage)
143                         json_object_put(*storage);
144
145                 // increment reference for storage
146                 json_object_get(jresp);
147                 *storage = jresp;
148
149                 g_rw_lock_writer_unlock(&ns->rw_lock);
150         }
151
152         // increment reference for event
153         json_object_get(jresp);
154         afb_event_push(event, jresp);
155 }
156
157 static void broadcast_status(afb_req_t request)
158 {
159         broadcast(request, "status", TRUE);
160
161         afb_req_success(request, NULL, "Broadcast status send");
162 }
163
164 static void broadcast_position(afb_req_t request)
165 {
166         const char *position = afb_req_value(request, "position");
167         gboolean cache = FALSE;
168
169         // only send out a car position event on subscribe
170         if (position && !g_strcmp0(position, "car"))
171                 cache = TRUE;
172
173         broadcast(request, "position", cache);
174
175         afb_req_success(request, NULL, "Broadcast position send");
176 }
177
178 static void broadcast_waypoints(afb_req_t request)
179 {
180         broadcast(request, "waypoints", TRUE);
181
182         afb_req_success(request, NULL, "Broadcast waypoints send");
183 }
184
185 static int init(afb_api_t api)
186 {
187         struct navigation_state *ns;
188
189         ns = g_try_malloc0(sizeof(*ns));
190         if (!ns) {
191                 AFB_ERROR("out of memory allocating navigation state");
192                 return -ENOMEM;
193         }
194
195         ns->status_event = afb_daemon_make_event("status");
196         ns->position_event = afb_daemon_make_event("position");
197         ns->waypoints_event = afb_daemon_make_event("waypoints");
198
199         if (!afb_event_is_valid(ns->status_event) ||
200             !afb_event_is_valid(ns->position_event) ||
201             !afb_event_is_valid(ns->waypoints_event)) {
202                 AFB_ERROR("Cannot create events");
203                 return -EINVAL;
204         }
205
206         afb_api_set_userdata(api, ns);
207
208         g_rw_lock_init(&ns->rw_lock);
209
210         return 0;
211 }
212
213 static const afb_verb_t binding_verbs[] = {
214         {
215                 .verb = "subscribe",
216                 .callback = subscribe,
217                 .info = "Subscribe to event"
218         }, {
219                 .verb = "unsubscribe",
220                 .callback = unsubscribe,
221                 .info = "Unsubscribe to event"
222         }, {
223                 .verb = "broadcast_status",
224                 .callback = broadcast_status,
225                 .info = "Allows clients to broadcast status events"
226         }, {
227                 .verb = "broadcast_position",
228                 .callback = broadcast_position,
229                 .info = "Broadcast out position event"
230         }, {
231                 .verb = "broadcast_waypoints",
232                 .callback = broadcast_waypoints,
233                 .info = "Broadcast out waypoint event"
234         },
235         {}
236 };
237
238 /*
239  * description of the binding for afb-daemon
240  */
241 const afb_binding_t afbBindingV3 = {
242         .api = "navigation",
243         .verbs = binding_verbs,
244         .init = init,
245 };