summaryrefslogtreecommitdiffstats
path: root/binding/afm-geofence-binding.c
blob: 2e2f134826149344b45864f62329b6988f437f7c (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright (C) 2017 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.
 *
 * TODO: add support for geoclue binding location data
 */

#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#include <glib.h>
#include <glib-object.h>
#include <json-c/json.h>

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

#define ARRAY_SIZE(x)	(sizeof(x)/sizeof(void *))
#define SECS_TO_USECS(x) (x * 1000000)
#define USECS_TO_SECS(x) (x / 1000000)

static afb_event_t fence_event;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static GList *fences = NULL;

static long dwell_transition_in_usecs = SECS_TO_USECS(20);

/*
 * @name      - geofence name
 * @triggered - geofence is currently triggered
 * @timestamp - timestamp of last geofence 'entered' transition
 * @bbox      - struct of bounding box coordinates
 */
struct geofence {
	gchar *name;
	bool triggered;
	gint64 timestamp;
	struct {
		double min_latitude, max_latitude;
		double min_longitude, max_longitude;
	} bbox;
};

const char *points[] = {
	"min_latitude",
	"max_latitude",
	"min_longitude",
	"max_longitude",
};

static void subscribe(afb_req_t request)
{
	const char *value = afb_req_value(request, "value");

	if (value && !strcasecmp(value, "fence")) {
		afb_req_subscribe(request, fence_event);
		afb_req_success(request, NULL, NULL);
		return;
	}

	afb_req_fail(request, "failed", "invalid event");
}

static void unsubscribe(afb_req_t request)
{
	const char *value = afb_req_value(request, "value");

	if (value && !strcasecmp(value, "fence")) {
		afb_req_unsubscribe(request, fence_event);
		afb_req_success(request, NULL, NULL);
		return;
	}

	afb_req_fail(request, "failed", "invalid event");
}

static inline bool within_bounding_box(double latitude, double longitude,
				       struct geofence *fence)
{
	if (latitude < fence->bbox.min_latitude)
		return false;

	if (latitude > fence->bbox.max_latitude)
		return false;

	if (longitude < fence->bbox.min_longitude)
		return false;

	if (longitude > fence->bbox.max_longitude)
		return false;

	return true;
}

static int parse_bounding_box(const char *data, struct geofence *fence)
{
	json_object *jquery = json_tokener_parse(data);
	json_object *val = NULL;
	double *bbox = (double *) &fence->bbox;
	int ret = -EINVAL, i;

	if (jquery == NULL)
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(points); i++) {
		ret = json_object_object_get_ex(jquery, points[i], &val);
		if (!ret) {
			ret = -EINVAL;
			break;
		}

		*bbox++ = json_object_get_double(val);
	}

	json_object_put(jquery);

	if (fence->bbox.min_latitude > fence->bbox.max_latitude)
		return -EINVAL;

	if (fence->bbox.min_longitude > fence->bbox.max_longitude)
		return -EINVAL;

	return ret;
}

static void add_fence(afb_req_t request)
{
	const char *name = afb_req_value(request, "name");
	const char *bbox = afb_req_value(request, "bbox");
	struct geofence *fence;
	GList *l;
	int ret;

	if (!name) {
		afb_req_fail(request, "failed", "invalid name parameter");
		return;
	}

	if (!bbox) {
		afb_req_fail(request, "failed", "no bbox parameter found");
		return;
	}

	fence = g_try_malloc0(sizeof(struct geofence));
	if (fence == NULL) {
		afb_req_fail(request, "failed", "cannot allocate memory");
		return;
	}

	ret = parse_bounding_box(bbox, fence);
	if (ret < 0) {
		afb_req_fail(request, "failed", "invalid bounding box");
		g_free(fence);
		return;
	}

	pthread_mutex_lock(&mutex);

	for (l = fences; l; l = l->next) {
		struct geofence *g = l->data;

		if (g_strcmp0(g->name, name) == 0) {
			g_free(fence);
			pthread_mutex_unlock(&mutex);

			afb_req_fail(request, "failed", "fence with name exists");
			return;
		}
	}

	fence->name = g_strdup(name);
	fence->timestamp = -1;
	fences = g_list_append(fences, fence);

        pthread_mutex_unlock(&mutex);

	afb_req_success(request, NULL, NULL);
}

static void remove_fence(afb_req_t request)
{
	const char *name = afb_req_value(request, "name");
	GList *l;

	if (!name) {
		afb_req_fail(request, "failed", "invalid id parameter");
		return;
	}

	pthread_mutex_lock(&mutex);

	for (l = fences; l; l = l->next) {
		struct geofence *g = l->data;

		if (g_strcmp0(g->name, name) == 0) {
			fences = g_list_remove(fences, g);
			g_free(g->name);
			g_free(g);

			pthread_mutex_unlock(&mutex);

			afb_req_success(request, NULL, "removed fence");
			return;
		}
	}

	pthread_mutex_unlock(&mutex);

	afb_req_fail(request, "failed", "fence not found for removal");
}

static void list_fences(afb_req_t request)
{
	json_object *jresp = json_object_new_object();
	GList *l;

	pthread_mutex_lock(&mutex);

	for (l = fences; l; l = l->next)
	{
		json_object *json_bbox = json_object_new_object();
		json_object *item = json_object_new_object();
		struct geofence *g = l->data;
		double *bbox = (double *) &g->bbox;
		int i;

		for (i = 0; i < ARRAY_SIZE(points); i++) {
			json_object_object_add(json_bbox, points[i],
				       json_object_new_double(*bbox++));
		}

		json_object_object_add(item, "within",
			json_object_new_boolean(g->triggered));
		json_object_object_add(item, "dwell",
			json_object_new_boolean(g->triggered && g->timestamp == -1));
		json_object_object_add(item, "bbox", json_bbox);
		json_object_object_add(jresp, g->name, item);
	}

	pthread_mutex_unlock(&mutex);

	afb_req_success(request, jresp, "list of geofences");
}

static int init(afb_api_t api)
{
	json_object *response, *query;
	int ret;

	ret = afb_daemon_require_api("gps", 1);
	if (ret < 0) {
		AFB_ERROR("Cannot request gps service");
		return ret;
	}

	query = json_object_new_object();
	json_object_object_add(query, "value", json_object_new_string("location"));

	ret = afb_service_call_sync("gps", "subscribe", query, &response, NULL, NULL);
	json_object_put(response);

	if (ret < 0) {
		AFB_ERROR("Cannot subscribe to gps service");
		return ret;
	}

	fence_event = afb_daemon_make_event("fence");

	return 0;
}

static inline bool dwell_transition_state(struct geofence *g)
{
	// zero dwell_transition_in_usecs means disabled
	if (dwell_transition_in_usecs == 0)
		return false;

	if (g->timestamp == -1)
		return false;

	if ((g_get_monotonic_time() - g->timestamp) < dwell_transition_in_usecs)
		return false;

	return true;
}

static void dwell_transition(afb_req_t request)
{
	const char *value = afb_req_value(request, "value");
	json_object *jresp = NULL;

	pthread_mutex_lock(&mutex);

	if (value) {
		long seconds = strtol(value, NULL, 10);
		if (seconds < 0) {
			afb_req_fail(request, "failed", "invalid input");
			pthread_mutex_unlock(&mutex);
			return;
		}

		dwell_transition_in_usecs = SECS_TO_USECS(seconds);
	}

	jresp = json_object_new_object();
	json_object_object_add(jresp, "seconds",
		json_object_new_int64(USECS_TO_SECS(dwell_transition_in_usecs)));

	pthread_mutex_unlock(&mutex);

	afb_req_success(request, jresp,
			"loitering time in seconds to enter dwell transition");
}

static void onevent(afb_api_t api, const char *event, struct json_object *object)
{
	json_object *val = NULL;
	double latitude, longitude;
	GList *l;
	int ret;

	if (g_strcmp0(event, "gps/location"))
		return;

	ret = json_object_object_get_ex(object, "latitude", &val);
	if (!ret)
		return;
	latitude = json_object_get_double(val);

	ret = json_object_object_get_ex(object, "longitude", &val);
	if (!ret)
		return;
	longitude = json_object_get_double(val);

	pthread_mutex_lock(&mutex);

	for (l = fences; l; l = l->next) {
		struct geofence *g = (struct geofence *) l->data;
		struct json_object *jresp;
		bool current = within_bounding_box(latitude, longitude, g);

		if (current == g->triggered && !dwell_transition_state(g))
			continue;

		jresp = json_object_new_object();

		json_object_object_add(jresp, "name",
			json_object_new_string(g->name));

		if (current && g->triggered) {
			json_object_object_add(jresp, "state",
				json_object_new_string("dwell"));
			g->timestamp = -1;
		} else if (current) {
			json_object_object_add(jresp, "state",
				json_object_new_string("entered"));
			g->timestamp = g_get_monotonic_time();
		} else {
			json_object_object_add(jresp, "state",
				json_object_new_string("exited"));
			g->timestamp = -1;
		}

		g->triggered = current;

		afb_event_push(fence_event, jresp);
	}

	pthread_mutex_unlock(&mutex);
}

static const struct afb_verb_v3 binding_verbs[] = {
	{ .verb = "subscribe",    .callback = subscribe,    .info = "Subscribe to geofence events" },
	{ .verb = "unsubscribe",  .callback = unsubscribe,  .info = "Unsubscribe to geofence events" },
	{ .verb = "add_fence",    .callback = add_fence,    .info = "Add geofence" },
	{ .verb = "remove_fence", .callback = remove_fence, .info = "Remove geofence" },
	{ .verb = "list_fences",  .callback = list_fences,  .info = "List all curent geofences" },
	{ .verb = "dwell_transition",   .callback = dwell_transition,   .info = "Set/get dwell transition delay" },
	{ }
};

/*
 * binder API description
 */
const struct afb_binding_v3 afbBindingV3 = {
	.api		= "geofence",
	.specification	= "Geofence service API",
	.verbs		= binding_verbs,
	.init		= init,
	.onevent	= onevent,
};