summaryrefslogtreecommitdiff
path: root/lib/neardal_manager.c
blob: 8223ecb19321bcc06db45efcc823a55bc6c30750 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
 *     NEARDAL (Neard Abstraction Library)
 *
 *     Copyright 2012-2014 Intel Corporation. All rights reserved.
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License version 2
 *     as published by the Free Software Foundation.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software Foundation,
 *     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>

#include "neard_manager_proxy.h"
#include "neard_adapter_proxy.h"

#include "neardal.h"
#include "neardal_prv.h"

void neardal_adp_prv_cb_tag_found(OrgNeardTag *proxy,
			const gchar *arg_unnamed_arg0, void *user_data);
void neardal_adp_prv_cb_tag_lost(OrgNeardTag *proxy,
			const gchar *arg_unnamed_arg0, void *user_data);

TagProp *neardal_mgr_tag_search(const gchar *tag)
{
	char *adapter = NULL;
	AdpProp *adpProp = NULL;
	TagProp *tagProp = NULL;

	if (!(adapter = neardal_dirname(tag)))
		goto error;

	if (neardal_mgr_prv_get_adapter(adapter, &adpProp) != NEARDAL_SUCCESS) {
		NEARDAL_TRACE_ERR("Adapter %s not found\n", adapter);
		goto error;
	}

	if (neardal_adp_prv_get_tag(adpProp, (gchar *) tag, &tagProp)
			!= NEARDAL_SUCCESS) {
		NEARDAL_TRACE_ERR("Tag %s not found\n", tag);
		goto error;
	}
error:
	g_free(adapter);
	return tagProp;
}

TagProp *neardal_mgr_tag_search_by_record(const gchar *record)
{
	char *tag = NULL;
	TagProp *tagProp = NULL;

	if ((tag = neardal_dirname(record))) {
		tagProp = neardal_mgr_tag_search(tag);
		g_free(tag);
	} else
		NEARDAL_TRACE_ERR("No tag found for record=%s\n", record);

	return tagProp;
}

static void neardal_mgr_record_add(const gchar *record, GVariant *data)
{
	TagProp *tagProp = NULL;

	NEARDAL_TRACEF("Record: %s=%s\n", record, g_variant_print(data, TRUE));

	if (!(tagProp = neardal_mgr_tag_search_by_record(record)))
		return;

	g_datalist_set_data_full(&(neardalMgr.dbus_data), record,
		g_variant_ref(data), (GDestroyNotify) g_variant_unref);

	if (neardal_rcd_add((char *) record, tagProp) != NEARDAL_SUCCESS)
		g_datalist_remove_data(&(neardalMgr.dbus_data), record);
}

static void neardal_mgr_tag_add(const gchar *path, GVariant *tag)
{
	char *adapter = NULL;
	AdpProp *adpProp = NULL;

	NEARDAL_TRACEF("Tag: %s\n", g_variant_print(tag, TRUE));

	if (!g_variant_lookup(tag, "Adapter", "o", &adapter) ||
			neardal_mgr_prv_get_adapter(adapter, &adpProp) !=
				NEARDAL_SUCCESS) {
		NEARDAL_TRACE_ERR("Adapter not found\n");
		goto error;
	}

	NEARDAL_TRACEF("Adapter: %s\n", adapter);

	g_datalist_set_data_full(&(neardalMgr.dbus_data), path,
			g_variant_ref(tag), (GDestroyNotify) g_variant_unref);

	neardal_adp_prv_cb_tag_found(NULL, path, adpProp);
error:
	g_free(adapter);
}

static void neardal_mgr_interfaces_added(ObjectManager *om,
					const gchar *path, GVariant *interfaces)
{
	GVariant *v = NULL;

	NEARDAL_TRACEF("path=%s\n", path);
	NEARDAL_TRACEF("interfaces=%s\n", g_variant_print(interfaces, TRUE));

	if (g_variant_lookup(interfaces, "org.neard.Record", "*",
				(void *) &v)) {
		GVariant *record;
		if ((record = neardal_data_insert(path, "Record", v)))
			neardal_record_add(record);
		return;
	}

	if (g_variant_lookup(interfaces, "org.neard.Tag", "*", (void *) &v)) {
		neardal_mgr_tag_add(path, v);
		return;
	}

	NEARDAL_TRACE_ERR("Unsupported interface change: path=%s, "
		"interface=%s\n", path, g_variant_print(interfaces, TRUE));
}

static void neardal_mgr_record_remove(const gchar *record)
{
	TagProp *tagProp;
	GVariant *v;
	GList *l;

	NEARDAL_TRACEF("record=%s\n", record);

	if (!(tagProp = neardal_mgr_tag_search_by_record(record))) {
		NEARDAL_TRACE_ERR("No tag found for record=%s\n", record);
		return;
	}

	for (l = tagProp->rcdList; l != NULL; l = l->next) {
		RcdProp *rcdProp = l->data;
		if (g_strcmp0(rcdProp->name, record) != 0)
			continue;

		NEARDAL_TRACEF("Record found: %s\n", rcdProp->name);

		neardal_rcd_remove(rcdProp);

		v = g_datalist_get_data(&(neardalMgr.dbus_data), record);

		NEARDAL_ASSERT(v != NULL);

		g_datalist_remove_data(&(neardalMgr.dbus_data), record);

		tagProp->rcdLen--;
	}
}

static void neardal_mgr_tag_remove(const gchar *tag)
{
	GVariant *v = g_datalist_get_data(&(neardalMgr.dbus_data), tag);
	char *adapter = NULL;
	AdpProp *adpProp = NULL;

	if (v == NULL) {
		NEARDAL_TRACE_ERR("Tag %s not found\n", tag);
		return;
	}

	NEARDAL_TRACEF("Tag's objects: %s\n", g_variant_print(v, TRUE));

	if (!g_variant_lookup(v, "Adapter", "o", &adapter) ||
			neardal_mgr_prv_get_adapter(adapter, &adpProp)
				!= NEARDAL_SUCCESS)
		return;

	NEARDAL_TRACEF("Adapter: %s=%p\n", adapter, (void *) adpProp);

	neardal_adp_prv_cb_tag_lost(NULL, tag, adpProp);

	g_datalist_remove_data(&(neardalMgr.dbus_data), tag);

	g_free(adapter);
}

static void neardal_mgr_interfaces_removed(ObjectManager *om,
						const gchar *path,
						const gchar *const *interfaces)
{
	char *s = g_strjoinv("' '", (gchar **)interfaces);
	int i = 0;

	NEARDAL_TRACEF("path=%s\n", path);
	NEARDAL_TRACEF("interfaces='%s'\n", s);

	g_free(s);

	while ((s = (char *) interfaces[i++])) {
		if (strcmp(s, "org.neard.Record") == 0) {
			GVariant *record;
			if ((record = neardal_data_search(path))) {
				neardal_record_remove(record);
				neardal_data_remove(record);
			}
			continue;
		}

		if (strcmp(s, "org.neard.Tag") == 0) {
			neardal_mgr_tag_remove(path);
			continue;
		}

		if (strcmp(s, "org.freedesktop.DBus.Properties") == 0)
			continue;
		if (strcmp(s, "org.freedesktop.DBus.Introspectable") == 0)
			continue;

		NEARDAL_TRACE_ERR("Unsupported interface change: "
					"path=%s, data=%s\n", path, s);
	}
}

/*****************************************************************************
 * neardal_mgr_prv_cb_property_changed: Callback called when a NFC Manager
 * Property is changed
 ****************************************************************************/
static void neardal_mgr_prv_cb_property_changed(OrgNeardManager *proxy,
						const gchar *arg_unnamed_arg0,
						GVariant *arg_unnamed_arg1,
						void        *user_data)
{
	NEARDAL_TRACEIN();

	(void) proxy; /* remove warning */
	(void) arg_unnamed_arg1; /* remove warning */
	(void) user_data; /* remove warning */
	NEARDAL_ASSERT(arg_unnamed_arg0 != NULL);

	NEARDAL_TRACEF("arg_unnamed_arg0='%s'\n", arg_unnamed_arg0);
	/* Adapters List ignored... */
}

/*****************************************************************************
 * neardal_mgr_prv_cb_adapter_added: Callback called when a NFC adapter is
 * added
 ****************************************************************************/
static void neardal_mgr_prv_cb_adapter_added(OrgNeardManager *proxy,
					     const gchar *arg_unnamed_arg0,
					     void        *user_data)
{
	errorCode_t	err = NEARDAL_SUCCESS;

	NEARDAL_TRACEIN();
	(void) proxy; /* remove warning */
	(void) user_data; /* remove warning */

	NEARDAL_ASSERT(arg_unnamed_arg0 != NULL);
	
	err = neardal_adp_add((char *) arg_unnamed_arg0);
	if (err != NEARDAL_SUCCESS)
		return;

	NEARDAL_TRACEF("NEARDAL LIB adapterList contains %d elements\n",
		      g_list_length(neardalMgr.prop.adpList));
}

/*****************************************************************************
 * neardal_mgr_prv_cb_adapter_removed: Callback called when a NFC adapter
 * is removed
 ****************************************************************************/
static void neardal_mgr_prv_cb_adapter_removed(OrgNeardManager *proxy,
					       const gchar *arg_unnamed_arg0,
					       void *user_data)
{
	GList	*node	= NULL;

	NEARDAL_TRACEIN();
	(void) proxy; /* remove warning */
	(void) user_data; /* remove warning */

	NEARDAL_ASSERT(arg_unnamed_arg0 != NULL);

	node = g_list_first(neardalMgr.prop.adpList);
	if (node == NULL) {
		NEARDAL_TRACE_ERR("NFC adapter not found! (%s)\n",
				  arg_unnamed_arg0);
		return;
	}

	/* Invoke client cb 'adapter removed' */
	if (neardalMgr.cb.adp_removed != NULL)
		(neardalMgr.cb.adp_removed)((char *) arg_unnamed_arg0,
					 neardalMgr.cb.adp_removed_ud);

	neardal_adp_remove(((AdpProp *)node->data));

	NEARDAL_TRACEF("NEARDAL LIB adapterList contains %d elements\n",
		      g_list_length(neardalMgr.prop.adpList));
}

static void neardal_mgr_adapters_parse(GVariant *v, char ***adps, gsize *nadps)
{
	char *s = NULL;
	GVariantIter iter;

	*adps = g_new0(char *, g_variant_n_children(v) + 1);
	*nadps = 0;

	g_variant_iter_init(&iter, v);

	while (g_variant_iter_loop(&iter, "{o*}", &s, NULL)) {
		if (g_str_has_prefix(s, "/org/neard/nfc")) {
			NEARDAL_TRACEF("Found adapter: %s\n", s);
			(*adps)[(*nadps)++] = s;
			s = NULL;
		}
	}

	if (!*nadps) {
		g_free(*adps);
		return;
	}

	NEARDAL_TRACEF("Found %lu adapter(s)\n", *nadps);
}

/*****************************************************************************
 * neardal_mgr_prv_get_all_adapters: Check if neard has an adapter
 ****************************************************************************/
static errorCode_t neardal_mgr_prv_get_all_adapters(gchar ***adpArray,
						    gsize *len)
{
	errorCode_t	err		= NEARDAL_ERROR_NO_ADAPTER;

	NEARDAL_ASSERT_RET(adpArray != NULL, NEARDAL_ERROR_INVALID_PARAMETER);

	if (object_manager_call_get_managed_objects_sync(neardalMgr.dbus_om,
			&neardalMgr.dbus_objs, NULL, &neardalMgr.gerror)) {
		NEARDAL_TRACEF("Reading:\n%s\n",
				g_variant_print(neardalMgr.dbus_objs, TRUE));
		NEARDAL_TRACEF("Parsing neard adapters...\n");

		neardal_mgr_adapters_parse(neardalMgr.dbus_objs, adpArray, len);

		err = *len ? NEARDAL_SUCCESS : NEARDAL_ERROR_NO_ADAPTER;

	} else {
		err = NEARDAL_ERROR_DBUS_CANNOT_INVOKE_METHOD;
		NEARDAL_TRACE_ERR("%d:%s\n", neardalMgr.gerror->code,
				 neardalMgr.gerror->message);
		neardal_tools_prv_free_gerror(&neardalMgr.gerror);
	}

	return err;
}


/*****************************************************************************
 * neardal_mgr_prv_get_adapter: Get NFC Adapter from name
 ****************************************************************************/
errorCode_t neardal_mgr_prv_get_adapter(gchar *adpName, AdpProp **adpProp)
{
	errorCode_t	err	= NEARDAL_ERROR_NO_ADAPTER;
	guint		len	= 0;
	AdpProp		*adapter;
	GList		*tmpList;

	tmpList = neardalMgr.prop.adpList;
	while (len < g_list_length(tmpList)) {
		adapter = g_list_nth_data(tmpList, len);
		if (adapter != NULL) {
			if (neardal_tools_prv_cmp_path(adapter->name,
							adpName)) {
				if (adpProp != NULL)
					*adpProp = adapter;
				err = NEARDAL_SUCCESS;
				break;
			}
		}
		len++;
	}

	return err;
}

/*****************************************************************************
 * neardal_mgr_prv_get_adapter_from_proxy: Get NFC Adapter from proxy
 ****************************************************************************/
errorCode_t neardal_mgr_prv_get_adapter_from_proxy(OrgNeardAdapter *adpProxy,
						   AdpProp **adpProp)
{
	errorCode_t	err	= NEARDAL_ERROR_NO_ADAPTER;
	guint		len = 0;
	AdpProp		*adapter;
	GList		*tmpList;

	NEARDAL_ASSERT_RET(adpProp != NULL, NEARDAL_ERROR_INVALID_PARAMETER);

	tmpList = neardalMgr.prop.adpList;
	while (len < g_list_length(tmpList)) {
		adapter = g_list_nth_data(tmpList, len);
		if (adapter != NULL) {
			if (adapter->proxy == adpProxy) {
				*adpProp = adapter;
				err = NEARDAL_SUCCESS;
				break;
			}
		}
		len++;
	}

	return err;
}

/*****************************************************************************
 * neardal_mgr_create: Get Neard Manager Properties = NFC Adapters list.
 * Create a DBus proxy for the first one NFC adapter if present
 * Register Neard Manager signals ('PropertyChanged')
 ****************************************************************************/
errorCode_t neardal_mgr_create(void)
{
	errorCode_t	err;
	gchar		**adpArray = NULL;
	gsize		adpArrayLen;
	char		*adpName;
	guint		len;

	NEARDAL_TRACEIN();
	if (neardalMgr.proxy != NULL) {
		g_signal_handlers_disconnect_by_func(neardalMgr.proxy,
			NEARDAL_G_CALLBACK(neardal_mgr_prv_cb_property_changed),
							NULL);
		g_signal_handlers_disconnect_by_func(neardalMgr.proxy,
			NEARDAL_G_CALLBACK(neardal_mgr_prv_cb_adapter_added),
							NULL);
		g_signal_handlers_disconnect_by_func(neardalMgr.proxy,
			NEARDAL_G_CALLBACK(neardal_mgr_prv_cb_adapter_removed),
							NULL);
		g_object_unref(neardalMgr.proxy);
		neardalMgr.proxy = NULL;
	}

	neardalMgr.proxy = org_neard_manager_proxy_new_sync(neardalMgr.conn,
					G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
							NEARD_DBUS_SERVICE,
							NEARD_MGR_PATH,
							NULL, /* GCancellable */
							&neardalMgr.gerror);

	if (neardalMgr.gerror != NULL) {
		NEARDAL_TRACE_ERR(
			"Unable to create Neard Manager Proxy (%d:%s)\n",
				 neardalMgr.gerror->code,
				neardalMgr.gerror->message);
		neardal_tools_prv_free_gerror(&neardalMgr.gerror);
		return NEARDAL_ERROR_DBUS_CANNOT_CREATE_PROXY;
	}

	g_datalist_init(&(neardalMgr.dbus_data));

	if (neardalMgr.dbus_om != NULL) {
		g_signal_handlers_disconnect_by_func(neardalMgr.dbus_om,
			NEARDAL_G_CALLBACK(neardal_mgr_interfaces_added), NULL);

		g_signal_handlers_disconnect_by_func(neardalMgr.dbus_om,
			NEARDAL_G_CALLBACK(neardal_mgr_interfaces_removed),
							NULL);
	}

	neardalMgr.gerror = NULL;

	neardalMgr.dbus_om = object_manager_proxy_new_sync(neardalMgr.conn, 0,
				NEARD_DBUS_SERVICE, NEARD_MGR_PATH, NULL,
				&neardalMgr.gerror);
	if (neardalMgr.gerror) {
		NEARDAL_TRACE_ERR("Error creating ObjectManager proxy: %s\n",
					neardalMgr.gerror->message);
		neardal_tools_prv_free_gerror(&neardalMgr.gerror);
		g_object_unref(neardalMgr.proxy);
		neardalMgr.proxy = NULL;
		return NEARDAL_ERROR_DBUS_CANNOT_CREATE_PROXY;
	}

	/* Get and store NFC adapters (is present) */
	err = neardal_mgr_prv_get_all_adapters(&adpArray, &adpArrayLen);
	if (adpArray != NULL && adpArrayLen > 0) {
		len = 0;
		while (len < adpArrayLen && err == NEARDAL_SUCCESS) {
			adpName =  adpArray[len++];
			err = neardal_adp_add(adpName);
		}
		g_strfreev(adpArray);
	}

	/* Register for manager signals 'PropertyChanged(String,Variant)' */
	NEARDAL_TRACEF("Register Neard-Manager Signal 'PropertyChanged'\n");
	g_signal_connect(neardalMgr.proxy,
			 NEARD_MGR_SIG_PROPCHANGED,
			 G_CALLBACK(neardal_mgr_prv_cb_property_changed),
			 NULL);

	/* Register for manager signals 'AdapterAdded(ObjectPath)' */
	NEARDAL_TRACEF("Register Neard-Manager Signal 'AdapterAdded'\n");
	g_signal_connect(neardalMgr.proxy,
			 NEARD_MGR_SIG_ADP_ADDED,
			 G_CALLBACK(neardal_mgr_prv_cb_adapter_added),
			 NULL);

	/* Register for manager signals 'AdapterRemoved(ObjectPath)' */
	NEARDAL_TRACEF("Register Neard-Manager Signal 'AdapterRemoved'\n");
	g_signal_connect(neardalMgr.proxy,
			 NEARD_MGR_SIG_ADP_RM,
			 G_CALLBACK(neardal_mgr_prv_cb_adapter_removed),
			 NULL);

	g_signal_connect(neardalMgr.dbus_om, "interfaces-added",
		G_CALLBACK(neardal_mgr_interfaces_added), NULL);

	g_signal_connect(neardalMgr.dbus_om, "interfaces-removed",
		G_CALLBACK(neardal_mgr_interfaces_removed), NULL);

	return err;
}

/*****************************************************************************
 * neardal_mgr_destroy: unref DBus proxy, disconnect Neard Manager signals
 ****************************************************************************/
void neardal_mgr_destroy(void)
{
	GList	*node;
	GList	**tmpList;

	NEARDAL_TRACEIN();
	/* Remove all adapters */
	tmpList = &neardalMgr.prop.adpList;
	while (g_list_length((*tmpList))) {
		node = g_list_first((*tmpList));
		neardal_adp_remove(((AdpProp *)node->data));
	}
	neardalMgr.prop.adpList = (*tmpList);

	if (neardalMgr.proxy == NULL)
		return;

	g_signal_handlers_disconnect_by_func(neardalMgr.proxy,
			NEARDAL_G_CALLBACK(neardal_mgr_prv_cb_property_changed),
						NULL);
	g_signal_handlers_disconnect_by_func(neardalMgr.proxy,
			NEARDAL_G_CALLBACK(neardal_mgr_prv_cb_adapter_added),
						NULL);
	g_signal_handlers_disconnect_by_func(neardalMgr.proxy,
			NEARDAL_G_CALLBACK(neardal_mgr_prv_cb_adapter_removed),
						NULL);
	g_object_unref(neardalMgr.proxy);
	neardalMgr.proxy = NULL;

	g_signal_handlers_disconnect_by_func(neardalMgr.dbus_om,
		NEARDAL_G_CALLBACK(neardal_mgr_interfaces_added), NULL);

	g_signal_handlers_disconnect_by_func(neardalMgr.dbus_om,
		NEARDAL_G_CALLBACK(neardal_mgr_interfaces_removed), NULL);

	g_datalist_clear(&(neardalMgr.dbus_data));
	neardalMgr.dbus_data = NULL;

	g_variant_unref(neardalMgr.dbus_objs);
	neardalMgr.dbus_objs = NULL;

	g_object_unref(neardalMgr.dbus_om);
	neardalMgr.dbus_om = NULL;
}