3 * neard - Near Field Communication manager
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 static DBusConnection *connection = NULL;
38 static GHashTable *ndef_app_hash;
40 struct near_ndef_agent {
47 static void ndef_agent_free(gpointer data)
50 struct near_ndef_agent *agent = data;
57 DBG("%s %s %s", agent->sender, agent->path, NFC_NDEF_AGENT_INTERFACE);
59 message = dbus_message_new_method_call(agent->sender, agent->path,
60 NFC_NDEF_AGENT_INTERFACE, "Release");
64 dbus_message_set_no_reply(message, TRUE);
66 g_dbus_send_message(connection, message);
68 g_dbus_remove_watch(connection, agent->watch);
70 g_free(agent->sender);
74 static void ndef_agent_disconnect(DBusConnection *conn, void *user_data)
76 struct near_ndef_agent *agent = user_data;
78 DBG("agent %s disconnected", agent->path);
80 g_hash_table_remove(ndef_app_hash, agent->record_type);
83 static void append_record_path(DBusMessageIter *iter, void *user_data)
85 GList *records = user_data, *list;
86 struct near_ndef_record *record;
89 for (list = records; list; list = list->next) {
92 path = __near_ndef_record_get_path(record);
96 dbus_message_iter_append_basic(iter,
97 DBUS_TYPE_STRING, &path);
101 static void ndef_agent_push_records(struct near_ndef_agent *agent,
104 DBusMessageIter iter, dict;
105 DBusMessage *message;
109 if (agent->sender == NULL || agent->path == NULL)
112 DBG("Sending NDEF to %s %s", agent->path, agent->sender);
114 message = dbus_message_new_method_call(agent->sender, agent->path,
115 NFC_NDEF_AGENT_INTERFACE,
120 dbus_message_iter_init_append(message, &iter);
122 near_dbus_dict_open(&iter, &dict);
123 near_dbus_dict_append_array(&dict, "Records",
124 DBUS_TYPE_STRING, append_record_path, records);
125 near_dbus_dict_close(&iter, &dict);
129 dbus_message_set_no_reply(message, TRUE);
131 g_dbus_send_message(connection, message);
134 void __near_agent_ndef_parse_records(GList *records)
137 struct near_ndef_record *record;
138 struct near_ndef_agent *agent;
143 for (list = records, agent = NULL; list; list = list->next) {
145 type = __near_ndef_record_get_type(record);
147 DBG("Looking for type %s", type);
149 agent = g_hash_table_lookup(ndef_app_hash, type);
157 ndef_agent_push_records(agent, records);
162 int __near_agent_ndef_register(const char *sender, const char *path,
163 const char *record_type)
165 struct near_ndef_agent *agent;
167 DBG("%s registers path %s for %s", sender, path, record_type);
169 if (g_hash_table_lookup(ndef_app_hash, record_type) != NULL)
172 agent = g_try_malloc0(sizeof(struct near_ndef_agent));
176 agent->sender = g_strdup(sender);
177 agent->path = g_strdup(path);
178 agent->record_type = g_strdup(record_type);
180 if (agent->sender == NULL || agent->path == NULL ||
181 agent->record_type == NULL) {
186 agent->watch = g_dbus_add_disconnect_watch(connection, sender,
187 ndef_agent_disconnect, agent, NULL);
188 g_hash_table_insert(ndef_app_hash, agent->record_type, agent);
193 int __near_agent_ndef_unregister(const char *sender, const char *path,
194 const char *record_type)
196 struct near_ndef_agent *agent;
198 DBG("sender %s path %s type %s", sender, path, record_type);
200 agent = g_hash_table_lookup(ndef_app_hash, record_type);
204 if (strcmp(agent->path, path) != 0 || strcmp(agent->sender, sender) != 0)
207 g_hash_table_remove(ndef_app_hash, record_type);
212 static guint handover_agent_watch = 0;
213 static gchar *handover_agent_path = NULL;
214 static gchar *handover_agent_sender = NULL;
216 static void handover_agent_free(void)
218 handover_agent_watch = 0;
220 g_free(handover_agent_sender);
221 handover_agent_sender = NULL;
223 g_free(handover_agent_path);
224 handover_agent_path = NULL;
227 static void handover_agent_disconnect(DBusConnection *conn, void *data)
229 DBG("data %p", data);
231 handover_agent_free();
234 int __near_agent_handover_register(const char *sender, const char *path)
236 DBG("sender %s path %s", sender, path);
238 if (handover_agent_path != NULL)
241 handover_agent_sender = g_strdup(sender);
242 handover_agent_path = g_strdup(path);
244 handover_agent_watch = g_dbus_add_disconnect_watch(connection, sender,
245 handover_agent_disconnect, NULL, NULL);
250 int __near_agent_handover_unregister(const char *sender, const char *path)
252 DBG("sender %s path %s", sender, path);
254 if (handover_agent_path == NULL)
257 if (handover_agent_watch > 0)
258 g_dbus_remove_watch(connection, handover_agent_watch);
260 handover_agent_free();
265 int __near_agent_init(void)
269 connection = near_dbus_get_connection();
270 if (connection == NULL)
273 ndef_app_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
274 g_free, ndef_agent_free);
279 void __near_agent_cleanup(void)
283 g_hash_table_destroy(ndef_app_hash);
284 ndef_app_hash = NULL;
286 handover_agent_free();
288 dbus_connection_unref(connection);