summaryrefslogtreecommitdiffstats
path: root/binding/afm-nfc-binding.c
blob: 1037227ab88bab9a849b73091287ffec31417717 (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
/*
 * Copyright (C) 2018 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 NFC p2p transactions
 */

#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>
#include <neardal/neardal.h>
#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>
#include "afm-nfc-common.h"

#define STR(X) STR1(X)
#define STR1(X) #X

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static afb_event_t presence_event;

static void __attribute__((unused)) dbg_dump_tag_records(neardal_tag *tag)
{
	char **recs = tag->records;
	int i;

	if (!recs) {
		AFB_API_DEBUG(afbBindingV3root, "tag empty!");
		return;
	}

	for (i = 0; recs[i] != NULL; i++)
		AFB_API_DEBUG(afbBindingV3root, "tag record[n]: %s", recs[i]);

	return;
}


static void __attribute__((unused)) dbg_dump_record_content(neardal_record record)
{
#define DBG_RECORD(__x) if (record.__x)  \
	AFB_API_DEBUG(afbBindingV3root, "record %s=%s", STR(__x), (record.__x))

	DBG_RECORD(authentication);
	DBG_RECORD(representation);
	DBG_RECORD(passphrase);
	DBG_RECORD(encryption);
	DBG_RECORD(encoding);
	DBG_RECORD(language);
	DBG_RECORD(action);
	DBG_RECORD(mime);
	DBG_RECORD(type);
	DBG_RECORD(ssid);
	DBG_RECORD(uri);
}

static void record_found(const char *tag_name, void *ptr)
{
	json_object *jresp, *jdict, *jtemp;
	nfc_binding_data *data = ptr;
	neardal_record *record;
	GVariant *value, *v = NULL;
	GVariantIter iter;
	char *s = NULL;
	int ret;

	ret = neardal_get_record_properties(tag_name, &record);
	if (ret != NEARDAL_SUCCESS) {
		AFB_API_ERROR(afbBindingV3root,
			      "read record properties for %s tag, err:0x%x (%s)",
			      tag_name, ret, neardal_error_get_text(ret));
		return;
	}

	value = neardal_record_to_g_variant(record);
	jresp = json_object_new_object();
	jdict = json_object_new_object();

	g_variant_iter_init(&iter, value);
	json_object_object_add(jresp,
			       "status", json_object_new_string("detected"));

	while (g_variant_iter_loop(&iter, "{sv}", &s, &v)) {
		gchar *str = g_variant_print(v, 0);

		str[strlen(str) - 1] = '\0';
		AFB_API_DEBUG(afbBindingV3root,
			      "%s tag, record %s= %s", tag_name, s, str);

		json_object_object_add(jdict, s,
                                       json_object_new_string(str + 1));
		g_free(str);
	}

	neardal_free_record(record);

	/*
	 * get record dictionary and look for 'Representation' field which should
	 * contain the uid value the identity agent needs.
	 *
	 */
	if (json_object_object_get_ex(jdict, "Representation", &jtemp)) {
		const char *uid = json_object_get_string(jtemp);

		pthread_mutex_lock(&mutex);
		data->jresp = jresp;
		json_object_object_add(jresp, "uid", json_object_new_string(uid));
		pthread_mutex_unlock(&mutex);

		afb_event_push(presence_event, jresp);
		AFB_API_DEBUG(afbBindingV3root,
			      "sent presence event, record content %s, tag %s",
			      uid, tag_name);
	}

	return;
}

static void tag_found(const char *tag_name, void *ptr)
{
	neardal_tag *tag;
	int ret;

	ret = neardal_get_tag_properties(tag_name, &tag);
	if (ret != NEARDAL_SUCCESS) {
		AFB_API_ERROR(afbBindingV3root,
			      "read tag %s properties, err:0x%x (%s)",
			      tag_name, ret, neardal_error_get_text(ret));
		return;
	}

	dbg_dump_tag_records(tag);
	neardal_free_tag(tag);

	return;
}

static void tag_removed(const char *tag_name, void *ptr)
{
	nfc_binding_data *data = ptr;
	json_object *jresp;

	pthread_mutex_lock(&mutex);
	if (data->jresp) {
		json_object_put(data->jresp);
		data->jresp = NULL;
	}
	pthread_mutex_unlock(&mutex);

	jresp = json_object_new_object();
	json_object_object_add(jresp, "status",
			       json_object_new_string("removed"));
	afb_event_push(presence_event, jresp);

	AFB_API_DEBUG(afbBindingV3root, "%s tag removed, quit loop", tag_name);
	g_main_loop_quit(data->loop);
}

static void *poll_adapter(void *ptr)
{
	nfc_binding_data *data = ptr;
	int ret;

	data->loop = g_main_loop_new(NULL, FALSE);

	neardal_set_cb_tag_found(tag_found, ptr);
	neardal_set_cb_tag_lost(tag_removed, ptr);
	neardal_set_cb_record_found(record_found, ptr);

	ret = neardal_set_adapter_property(data->adapter,
					   NEARD_ADP_PROP_POWERED,
					   GINT_TO_POINTER(1));
	if (ret != NEARDAL_SUCCESS) {
		AFB_API_DEBUG(afbBindingV3root,
			      "failed to power %s adapter on: ret=0x%x (%s)",
			      data->adapter, ret, neardal_error_get_text(ret));

		goto out;
	}

	for (;;) {
		ret = neardal_start_poll(data->adapter);

		if ((ret != NEARDAL_SUCCESS) &&
		    (ret != NEARDAL_ERROR_POLLING_ALREADY_ACTIVE))
			break;

		g_main_loop_run(data->loop);
	}

	AFB_API_DEBUG(afbBindingV3root, "exiting polling loop");

out:
	g_free(data->adapter);
	free(data);

	return NULL;
}

static int get_adapter(nfc_binding_data *data, unsigned int id)
{
	char **adapters = NULL;
	int num_adapters, ret;

	ret = neardal_get_adapters(&adapters, &num_adapters);
	if (ret != NEARDAL_SUCCESS) {
		AFB_API_DEBUG(afbBindingV3root,
			      "failed to find adapters ret=0x%x (%s)",
			      ret, neardal_error_get_text(ret));
		return -EIO;
	}

	if (id > num_adapters - 1) {
		AFB_API_DEBUG(afbBindingV3root,
			      "adapter out of range (%d - %d)",
			      id, num_adapters);
		ret = -EINVAL;
		goto out;
	}

	data->adapter = g_strdup(adapters[id]);

out:
	neardal_free_array(&adapters);
	return ret;
}

static int init(afb_api_t api)
{
	nfc_binding_data *data;
	pthread_t thread_id;
	int ret;

	data = malloc(sizeof(nfc_binding_data));
	if (!data)
		return -ENOMEM;

	presence_event = afb_api_make_event(api, "presence");
	if (!afb_event_is_valid(presence_event)) {
		AFB_API_ERROR(api, "Failed to create event");
		free(data);
		return -EINVAL;
	}

	/* get the first adapter */
	ret = get_adapter(data, 0);
	if (ret != NEARDAL_SUCCESS) {
		free(data);
		return 0;  /* ignore error if no adapter */
	}

	AFB_API_DEBUG(api, " %s adapter found", data->adapter);

	afb_api_set_userdata(api, data);
	ret = pthread_create(&thread_id, NULL, poll_adapter, data);
	if (ret) {
		AFB_API_ERROR(api, "polling pthread creation failed");
		g_free(data->adapter);
		free(data);
	}

	return 0;
}

static void subscribe(afb_req_t request)
{
        if (afb_req_subscribe(request, presence_event) < 0) {
                AFB_REQ_ERROR(request, "subscribe to presence_event failed");
                afb_req_reply(request, NULL, "failed", "Invalid event");

		return;
        }

	afb_req_reply(request, NULL, NULL, NULL);
}

static void unsubscribe(afb_req_t request)
{
        if (afb_req_unsubscribe(request, presence_event) < 0) {
                AFB_REQ_ERROR(request, "unsubscribe to presence_event failed");
                afb_req_reply(request, NULL, "failed", "Invalid event");

		return;
	}

	afb_req_reply(request, NULL, NULL, NULL);
}

static const afb_verb_t binding_verbs[] = {
        { .verb = "subscribe",   .callback = subscribe, 
	  .info = "Subscribe to NFC events" },
        { .verb = "unsubscribe", .callback = unsubscribe, 
	  .info = "Unsubscribe to NFC events" },
        { .verb = NULL }
};

/*
 * binder API description
 */
const afb_binding_t afbBindingExport = {
        .api            = "nfc",
        .specification  = "NFC service API",
        .info           = "AGL nfc service",
        .verbs          = binding_verbs,
        .preinit        = NULL,
        .init           = init,
        .onevent        = NULL,
        .noconcurrency  = 0
};