summaryrefslogtreecommitdiff
path: root/src/agent.c
blob: a5309c5aa17caffe89801873cb392efb6a6f28ae (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
/*
 *
 *  neard - Near Field Communication manager
 *
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <glib.h>

#include <gdbus.h>

#include "near.h"

static DBusConnection *connection = NULL;
static GHashTable *ndef_app_hash;

struct near_ndef_agent {
	char *sender;
	char *path;
	char *record_type;
	guint watch;
};

static void ndef_agent_free(gpointer data)
{
	DBusMessage *message;
	struct near_ndef_agent *agent = data;

	DBG("");

	if (agent == NULL)
		return;

	DBG("%s %s %s", agent->sender, agent->path, NFC_NDEF_AGENT_INTERFACE);

	message = dbus_message_new_method_call(agent->sender, agent->path,
					NFC_NDEF_AGENT_INTERFACE, "Release");
	if (message == NULL)
		return;

	dbus_message_set_no_reply(message, TRUE);

	g_dbus_send_message(connection, message);

	g_dbus_remove_watch(connection, agent->watch);

	g_free(agent->sender);
	g_free(agent->path);
}

static void ndef_agent_disconnect(DBusConnection *conn, void *user_data)
{
	struct near_ndef_agent *agent = user_data;

	DBG("agent %s disconnected", agent->path);

	g_hash_table_remove(ndef_app_hash, agent->record_type);
}

static void append_record_path(DBusMessageIter *iter, void *user_data)
{
	GList *records = user_data, *list;
	struct near_ndef_record *record;
	char *path;

	for (list = records; list; list = list->next) {
		record = list->data;

		path = __near_ndef_record_get_path(record);
		if (path == NULL)
			continue;

		dbus_message_iter_append_basic(iter,
					DBUS_TYPE_STRING, &path);
	}
}

static void ndef_agent_push_records(struct near_ndef_agent *agent,
							GList *records)
{
	DBusMessageIter iter, dict;
	DBusMessage *message;

	DBG("");

	if (agent->sender == NULL || agent->path == NULL)
		return;

	DBG("Sending NDEF to %s %s", agent->path, agent->sender);

	message = dbus_message_new_method_call(agent->sender, agent->path,
					NFC_NDEF_AGENT_INTERFACE,
					"GetNDEF");
	if (message == NULL)
		return;

	dbus_message_iter_init_append(message, &iter);

	near_dbus_dict_open(&iter, &dict);
	near_dbus_dict_append_array(&dict, "Records",
				DBUS_TYPE_STRING, append_record_path, records);
	near_dbus_dict_close(&iter, &dict);

	DBG("sending...");

	dbus_message_set_no_reply(message, TRUE);

	g_dbus_send_message(connection, message);
}

void __near_agent_ndef_parse_records(GList *records)
{
	GList *list;
	struct near_ndef_record *record;
	struct near_ndef_agent *agent;
	char *type;

	DBG("");

	for (list = records, agent = NULL; list; list = list->next) {
		record = list->data;
		type  = __near_ndef_record_get_type(record);

		DBG("Looking for type %s", type);

		agent = g_hash_table_lookup(ndef_app_hash, type);
		if (agent != NULL)
			break;
	}

	if (agent == NULL)
		return;

	ndef_agent_push_records(agent, records);

	return;
}

int __near_agent_ndef_register(const char *sender, const char *path,
						const char *record_type)
{
	struct near_ndef_agent *agent;

	DBG("%s registers path %s for %s", sender, path, record_type);

	if (g_hash_table_lookup(ndef_app_hash, record_type) != NULL)
		return -EEXIST;

	agent = g_try_malloc0(sizeof(struct near_ndef_agent));
	if (agent == NULL)
		return -ENOMEM;

	agent->sender = g_strdup(sender);
	agent->path = g_strdup(path);
	agent->record_type = g_strdup(record_type);
	
	if (agent->sender == NULL || agent->path == NULL ||
	    agent->record_type == NULL) {
		g_free(agent);
		return -ENOMEM;
	}

	agent->watch = g_dbus_add_disconnect_watch(connection, sender,
						ndef_agent_disconnect, agent, NULL);
	g_hash_table_insert(ndef_app_hash, agent->record_type, agent);

	return 0;
}

int __near_agent_ndef_unregister(const char *sender, const char *path,
						const char *record_type)
{
	struct near_ndef_agent *agent;

	DBG("sender %s path %s type %s", sender, path, record_type);

	agent = g_hash_table_lookup(ndef_app_hash, record_type);
	if (agent == NULL)
		return -EINVAL;

	if (strcmp(agent->path, path) != 0 || strcmp(agent->sender, sender) != 0)
		return -EINVAL;

	g_hash_table_remove(ndef_app_hash, record_type);

	return 0;
}

static guint handover_agent_watch = 0;
static gchar *handover_agent_path = NULL;
static gchar *handover_agent_sender = NULL;

static void handover_agent_free(void)
{
	if (handover_agent_watch > 0) {
		g_dbus_remove_watch(connection, handover_agent_watch);
		handover_agent_watch = 0;
	}

	g_free(handover_agent_sender);
	handover_agent_sender = NULL;

	g_free(handover_agent_path);
	handover_agent_path = NULL;
}

static void handover_agent_disconnect(DBusConnection *conn, void *data)
{
	DBG("data %p", data);

	handover_agent_watch = 0;

	handover_agent_free();
}

static void handover_agent_release(void)
{
	DBusMessage *message;

	if (handover_agent_watch == 0)
		return;

	message = dbus_message_new_method_call(handover_agent_sender,
						handover_agent_path,
						"org.neard.HandoverAgent",
						"Release");
	if (message != NULL)
		g_dbus_send_message(connection, message);

	handover_agent_free();
}

int __near_agent_handover_register(const char *sender, const char *path)
{
	DBG("sender %s path %s", sender, path);

	if (handover_agent_path != NULL)
		return -EEXIST;

	handover_agent_watch = g_dbus_add_disconnect_watch(connection, sender,
					handover_agent_disconnect, NULL, NULL);
	if (handover_agent_watch == 0)
		return -ENOMEM;

	handover_agent_sender = g_strdup(sender);
	handover_agent_path = g_strdup(path);

	return 0;
}

int __near_agent_handover_unregister(const char *sender, const char *path)
{
	DBG("sender %s path %s", sender, path);

	if (handover_agent_path == NULL)
		return -ESRCH;

	handover_agent_free();

	return 0;
}

int __near_agent_init(void)
{
	DBG("");

	connection = near_dbus_get_connection();
	if (connection == NULL)
		return -1;

	ndef_app_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
						g_free, ndef_agent_free);

	return 0;
}

void __near_agent_cleanup(void)
{
	DBG("");

	g_hash_table_destroy(ndef_app_hash);
	ndef_app_hash = NULL;

	handover_agent_release();

	dbus_connection_unref(connection);
}