aboutsummaryrefslogtreecommitdiffstats
path: root/binding/navigation-api.c
blob: 1774e29e92cc1758a63a3cef078d45480153f16f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright (C) 2019 Konsulko Group
 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <json-c/json.h>

#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>

#include "navigation-api.h"

afb_api_t g_api;

static const char *vshl_capabilities_events[] = {
	"setDestination",
	"cancelNavigation",
	NULL,
};

struct navigation_state *navigation_get_userdata(void)
{
	return afb_api_get_userdata(g_api);
}

static afb_event_t get_event_from_value(struct navigation_state *ns,
			const char *value)
{
	if (!g_strcmp0(value, "status"))
		return ns->status_event;

	if (!g_strcmp0(value, "position"))
		return ns->position_event;

	if (!g_strcmp0(value, "waypoints"))
		return ns->waypoints_event;

	return NULL;
}

static json_object **get_storage_from_value(struct navigation_state *ns,
			const char *value)
{
	if (!g_strcmp0(value, "status"))
		return &ns->status_storage;

	if (!g_strcmp0(value, "position"))
		return &ns->position_storage;

	if (!g_strcmp0(value, "waypoints"))
		return &ns->waypoints_storage;

	return NULL;
}

static void navigation_subscribe_unsubscribe(afb_req_t request,
		gboolean unsub)
{
	struct navigation_state *ns = navigation_get_userdata();
	json_object *jresp = json_object_new_object();
	const char *value;
	afb_event_t event;
	int rc;

	value = afb_req_value(request, "value");
	if (!value) {
		afb_req_fail_f(request, "failed", "Missing \"value\" event");
		return;
	}

	event = get_event_from_value(ns, value);
	if (!event) {
		afb_req_fail_f(request, "failed", "Bad \"value\" event \"%s\"",
				value);
		return;
	}

	if (!unsub) {
		json_object *storage;
		rc = afb_req_subscribe(request, event);

		g_rw_lock_reader_lock(&ns->rw_lock);
		storage = *get_storage_from_value(ns, value);
		if (storage) {
			// increment reference counter, and send out cached value
			json_object_get(storage);
			afb_event_push(event, storage);
		}
		g_rw_lock_reader_unlock(&ns->rw_lock);
	} else {
		rc = afb_req_unsubscribe(request, event);
	}
	if (rc != 0) {
		afb_req_fail_f(request, "failed",
					"%s error on \"value\" event \"%s\"",
					!unsub ? "subscribe" : "unsubscribe",
					value);
		return;
	}

	afb_req_success_f(request, jresp, "Navigation %s to event \"%s\"",
			!unsub ? "subscribed" : "unsubscribed",
			value);
}

static void subscribe(afb_req_t request)
{
	navigation_subscribe_unsubscribe(request, FALSE);
}

static void unsubscribe(afb_req_t request)
{
	navigation_subscribe_unsubscribe(request, TRUE);
}

static void broadcast(json_object *jresp, const char *name, gboolean cache)
{
	struct navigation_state *ns = navigation_get_userdata();
	afb_event_t event = get_event_from_value(ns, name);
	json_object *tmp = NULL;

	if (json_object_deep_copy(jresp, (json_object **) &tmp, NULL))
		return;

	if (cache) {
		json_object **storage;

		g_rw_lock_writer_lock(&ns->rw_lock);

		storage = get_storage_from_value(ns, name);

		if (*storage)
			json_object_put(*storage);
		*storage = NULL;

		// increment reference for storage
		json_object_get(tmp);
		*storage = tmp;

		// increment reference for event
		json_object_get(tmp);
		afb_event_push(event, tmp);

		g_rw_lock_writer_unlock(&ns->rw_lock);

		return;
	}

	afb_event_push(event, tmp);
}

static void broadcast_status(afb_req_t request)
{
	json_object *jresp = afb_req_json(request);
	broadcast(jresp, "status", TRUE);

	afb_req_success(request, NULL, "Broadcast status send");

	// NOTE: If the Alexa SDK API for pushing local navigation
	//       updates gets exposed, send update to vshl-capabilities
	//       here.
}

static void broadcast_position(afb_req_t request)
{
	const char *position = afb_req_value(request, "position");
	gboolean cache = FALSE;

	// only send out a car position event on subscribe
	if (position && !g_strcmp0(position, "car"))
		cache = TRUE;

	json_object *jresp = afb_req_json(request);
	broadcast(jresp, "position", cache);

	afb_req_success(request, NULL, "Broadcast position send");

	// NOTE: If the Alexa SDK API for pushing local navigation
	//       updates gets exposed, send update to vshl-capabilities
	//       here.
}

static void broadcast_waypoints(afb_req_t request)
{
	json_object *jresp = afb_req_json(request);
	broadcast(jresp, "waypoints", TRUE);

	afb_req_success(request, NULL, "Broadcast waypoints send");

	// NOTE: If the Alexa SDK API for pushing local navigation
	//       updates gets exposed, send update to vshl-capabilities
	//       here.
}

static void handle_setDestination_event(struct json_object *object)
{
	json_object *jdest = NULL;
	json_object_object_get_ex(object, "destination", &jdest);
	if(!jdest) {
		AFB_WARNING("setDestination event missing destination element");
		return;
	}

	json_object *jcoord = NULL;
	json_object_object_get_ex(jdest, "coordinate", &jcoord);
	if(!jcoord) {
		AFB_WARNING("setDestination event missing coordinate element");
		return;
	}

	json_object *jlat = NULL;
	json_object_object_get_ex(jcoord, "latitudeInDegrees", &jlat);
	if(!jlat)
		return;
	errno = 0;
	double lat = json_object_get_double(jlat);
	if(errno != 0)
		return;

	json_object *jlon = NULL;
	json_object_object_get_ex(jcoord, "longitudeInDegrees", &jlon);
	if(!jlon)
		return;
	double lon = json_object_get_double(jlon);
	if(errno != 0)
		return;

	json_object *jobj = json_object_new_object();
	json_object *jpoints = json_object_new_array();
	json_object *jpoint = json_object_new_object();
	jlat = json_object_new_double(lat);
	jlon = json_object_new_double(lon);
	json_object_object_add(jpoint, "latitude", jlat);
	json_object_object_add(jpoint, "longitude", jlon);
	json_object_array_add(jpoints, jpoint);
	json_object_object_add(jobj, "points", jpoints);
	broadcast(jobj, "waypoints", TRUE);
}

static void handle_cancelNavigation_event(struct json_object *object)
{
	json_object *jobj = json_object_new_object();
	json_object *jstate = json_object_new_string("stop");
	json_object_object_add(jobj, "state", jstate);
	broadcast(jobj, "status", TRUE);
}

static void onevent(afb_api_t api, const char *event, struct json_object *object)
{
	if(!event)
		return;

	if(strcmp(event, "vshl-capabilities/setDestination") == 0) {
		handle_setDestination_event(object);
	} else if(strcmp(event, "vshl-capabilities/cancelNavigation") == 0) {
		handle_cancelNavigation_event(object);
	} else {
		AFB_WARNING("Unhandled vshl-capabilities event");
	}
}

static int init(afb_api_t api)
{
	struct navigation_state *ns;
	int rc;

	ns = g_try_malloc0(sizeof(*ns));
	if (!ns) {
		AFB_ERROR("out of memory allocating navigation state");
		return -ENOMEM;
	}

	rc = afb_daemon_require_api("vshl-capabilities", 1);
	if (!rc) {
		const char **tmp = vshl_capabilities_events;
		json_object *args = json_object_new_object();
		json_object *actions = json_object_new_array();

		while (*tmp) {
			json_object_array_add(actions, json_object_new_string(*tmp++));
		}
		json_object_object_add(args, "actions", actions);
		if(json_object_array_length(actions)) {
			rc = afb_api_call_sync(api, "vshl-capabilities", "navigation/subscribe",
					       args, NULL, NULL, NULL);
			if(rc != 0)
				AFB_WARNING("afb_api_call_sync returned %d", rc);
		} else {
			json_object_put(args);
		}
	} else {
		AFB_WARNING("unable to initialize vshl-capabilities binding");
	}

	ns->status_event = afb_daemon_make_event("status");
	ns->position_event = afb_daemon_make_event("position");
	ns->waypoints_event = afb_daemon_make_event("waypoints");

	if (!afb_event_is_valid(ns->status_event) ||
	    !afb_event_is_valid(ns->position_event) ||
	    !afb_event_is_valid(ns->waypoints_event)) {
		AFB_ERROR("Cannot create events");
		return -EINVAL;
	}

	afb_api_set_userdata(api, ns);
	g_api = api;

	g_rw_lock_init(&ns->rw_lock);

	return 0;
}

static const afb_verb_t binding_verbs[] = {
	{
		.verb = "subscribe",
		.callback = subscribe,
		.info = "Subscribe to event"
	}, {
		.verb = "unsubscribe",
		.callback = unsubscribe,
		.info = "Unsubscribe to event"
	}, {
		.verb = "broadcast_status",
		.callback = broadcast_status,
		.info = "Allows clients to broadcast status events"
	}, {
		.verb = "broadcast_position",
		.callback = broadcast_position,
		.info = "Broadcast out position event"
	}, {
		.verb = "broadcast_waypoints",
		.callback = broadcast_waypoints,
		.info = "Broadcast out waypoint event"
	},
	{}
};

/*
 * description of the binding for afb-daemon
 */
const afb_binding_t afbBindingV3 = {
	.api = "navigation",
	.verbs = binding_verbs,
	.onevent = onevent,
	.init = init,
};