5 * Copyright (C) 2007-2013 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
31 #include <connman/agent.h>
32 #include <connman/setting.h>
36 #define agent_ref(agent) \
37 agent_ref_debug(agent, __FILE__, __LINE__, __func__)
38 #define agent_unref(agent) \
39 agent_unref_debug(agent, __FILE__, __LINE__, __func__)
41 static DBusConnection *connection = NULL;
42 static GHashTable *agent_hash = NULL;
43 static struct connman_agent *default_agent = NULL;
45 struct connman_agent {
49 struct connman_agent_request *pending;
50 GList *queue; /* queued requests for this agent */
54 struct connman_agent_request {
58 DBusPendingCall *call;
60 agent_queue_cb callback;
61 struct connman_agent_driver *driver;
64 static GSList *driver_list = NULL;
66 void *connman_agent_get_info(const char *dbus_sender, const char **sender,
69 struct connman_agent *agent;
72 agent = default_agent;
74 agent = g_hash_table_lookup(agent_hash, dbus_sender);
76 agent = default_agent;
81 *sender = agent->owner;
94 static void agent_request_free(struct connman_agent_request *request)
99 if (request->user_context) {
100 if (request->driver && request->driver->context_unref)
101 request->driver->context_unref(request->user_context);
105 dbus_message_unref(request->msg);
108 dbus_pending_call_cancel(request->call);
109 dbus_pending_call_unref(request->call);
115 static void agent_finalize_pending(struct connman_agent *agent,
118 struct connman_agent_request *pending = agent->pending;
120 agent->pending = NULL;
121 pending->callback(reply, pending->user_data);
122 agent_request_free(pending);
126 static void agent_receive_message(DBusPendingCall *call, void *user_data);
128 static int agent_send_next_request(struct connman_agent *agent)
136 agent->pending = agent->queue->data;
137 agent->queue = g_list_remove(agent->queue, agent->pending);
139 if (!agent->pending->msg)
142 if (!dbus_connection_send_with_reply(connection, agent->pending->msg,
143 &agent->pending->call,
144 agent->pending->timeout))
147 if (!agent->pending->call)
150 if (!dbus_pending_call_set_notify(agent->pending->call,
151 agent_receive_message,
155 dbus_message_unref(agent->pending->msg);
156 agent->pending->msg = NULL;
160 agent_finalize_pending(agent, NULL);
164 static int send_cancel_request(struct connman_agent *agent,
165 struct connman_agent_request *request)
167 DBusMessage *message;
169 DBG("send cancel req to %s %s", agent->owner, agent->path);
171 message = dbus_message_new_method_call(agent->owner,
173 request->driver->interface,
176 connman_error("Couldn't allocate D-Bus message");
180 g_dbus_send_message(connection, message);
184 static void agent_receive_message(DBusPendingCall *call, void *user_data)
186 struct connman_agent *agent = user_data;
190 DBG("agent %p req %p", agent, agent->pending);
192 reply = dbus_pending_call_steal_reply(call);
193 dbus_pending_call_unref(call);
194 agent->pending->call = NULL;
196 if (dbus_message_is_error(reply,
197 "org.freedesktop.DBus.Error.Timeout") ||
198 dbus_message_is_error(reply,
199 "org.freedesktop.DBus.Error.TimedOut")) {
200 send_cancel_request(agent, agent->pending);
203 agent_finalize_pending(agent, reply);
204 dbus_message_unref(reply);
206 err = agent_send_next_request(agent);
207 if (err < 0 && err != -EBUSY)
208 DBG("send next request failed (%s/%d)", strerror(-err), -err);
211 static struct connman_agent_driver *get_driver(void)
213 return g_slist_nth_data(driver_list, 0);
216 int connman_agent_queue_message(void *user_context,
217 DBusMessage *msg, int timeout,
218 agent_queue_cb callback, void *user_data,
221 struct connman_agent_request *queue_data;
222 struct connman_agent_driver *driver;
223 struct connman_agent *agent = agent_data;
226 if (!user_context || !callback)
229 queue_data = g_new0(struct connman_agent_request, 1);
233 driver = get_driver();
234 DBG("driver %p", driver);
236 if (driver && driver->context_ref) {
237 queue_data->user_context = driver->context_ref(user_context);
238 queue_data->driver = driver;
240 queue_data->user_context = user_context;
242 queue_data->msg = dbus_message_ref(msg);
243 queue_data->timeout = timeout;
244 queue_data->callback = callback;
245 queue_data->user_data = user_data;
246 agent->queue = g_list_append(agent->queue, queue_data);
248 err = agent_send_next_request(agent);
249 if (err < 0 && err != -EBUSY)
250 DBG("send next request failed (%s/%d)", strerror(-err), -err);
255 static void set_default_agent(void)
257 struct connman_agent *agent = NULL;
264 g_hash_table_iter_init(&iter, agent_hash);
265 if (g_hash_table_iter_next(&iter, &key, &value))
269 DBG("default agent set to %s %s", agent->owner, agent->path);
271 DBG("default agent cleared");
273 default_agent = agent;
276 static void agent_disconnect(DBusConnection *conn, void *user_data)
278 struct connman_agent *agent = user_data;
280 DBG("agent %s disconnected", agent->owner);
282 if (agent->watch > 0) {
283 g_dbus_remove_watch(conn, agent->watch);
287 g_hash_table_remove(agent_hash, agent->owner);
290 static struct connman_agent *agent_ref_debug(struct connman_agent *agent,
291 const char *file, int line, const char *caller)
293 DBG("%p ref %d by %s:%d:%s()", agent, agent->refcount + 1,
296 __sync_fetch_and_add(&agent->refcount, 1);
301 static struct connman_agent *agent_create(const char *name, const char *path)
303 struct connman_agent *agent;
305 agent = g_new0(struct connman_agent, 1);
307 agent->owner = g_strdup(name);
308 agent->path = g_strdup(path);
310 agent->watch = g_dbus_add_disconnect_watch(connection,
311 name, agent_disconnect,
314 return agent_ref(agent);
317 int connman_agent_register(const char *sender, const char *path)
319 struct connman_agent *agent;
321 DBG("sender %s path %s", sender, path);
323 agent = g_hash_table_lookup(agent_hash, sender);
327 agent = agent_create(sender, path);
331 DBG("agent %s", agent->owner);
333 g_hash_table_replace(agent_hash, agent->owner, agent);
341 struct report_error_data {
343 report_error_cb_t callback;
347 static void report_error_reply(DBusMessage *reply, void *user_data)
349 struct report_error_data *report_error = user_data;
351 const char *dbus_err;
356 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
357 dbus_err = dbus_message_get_error_name(reply);
360 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
364 report_error->callback(report_error->user_context, retry,
365 report_error->user_data);
367 g_free(report_error);
370 int connman_agent_report_error_full(void *user_context, const char *path,
371 const char *method, const char *error,
372 report_error_cb_t callback,
373 const char *dbus_sender, void *user_data)
375 DBusMessage *message;
376 DBusMessageIter iter;
377 struct report_error_data *report_error;
378 struct connman_agent *agent;
381 agent = connman_agent_get_info(dbus_sender, NULL, NULL);
383 DBG("agent %p sender %s context %p path %s", agent,
384 dbus_sender, user_context, agent ? agent->path : "-");
386 if (!user_context || !agent || !agent->path || !error || !callback)
389 message = dbus_message_new_method_call(agent->owner, agent->path,
390 CONNMAN_AGENT_INTERFACE, method);
394 dbus_message_iter_init_append(message, &iter);
396 dbus_message_iter_append_basic(&iter,
397 DBUS_TYPE_OBJECT_PATH, &path);
398 dbus_message_iter_append_basic(&iter,
399 DBUS_TYPE_STRING, &error);
401 report_error = g_try_new0(struct report_error_data, 1);
403 dbus_message_unref(message);
407 report_error->user_context = user_context;
408 report_error->callback = callback;
409 report_error->user_data = user_data;
411 err = connman_agent_queue_message(user_context, message,
412 connman_timeout_input_request(),
413 report_error_reply, report_error,
415 if (err < 0 && err != -EBUSY) {
416 DBG("error %d sending error request", err);
417 g_free(report_error);
418 dbus_message_unref(message);
422 dbus_message_unref(message);
427 int connman_agent_report_error(void *user_context, const char *path,
429 report_error_cb_t callback,
430 const char *dbus_sender, void *user_data)
432 return connman_agent_report_error_full(user_context, path,
433 "ReportError", error, callback, dbus_sender,
437 static gint compare_priority(gconstpointer a, gconstpointer b)
439 const struct connman_agent_driver *driver1 = a;
440 const struct connman_agent_driver *driver2 = b;
442 return driver2->priority - driver1->priority;
446 * connman_agent_driver_register:
447 * @driver: Agent driver definition
449 * Register a new agent driver
451 * Returns: %0 on success
453 int connman_agent_driver_register(struct connman_agent_driver *driver)
455 DBG("Registering driver %p name %s", driver, driver->name);
457 driver_list = g_slist_insert_sorted(driver_list, driver,
463 static void release_driver(void)
465 connman_agent_driver_unregister(get_driver());
468 static void cancel_all_requests(struct connman_agent *agent)
472 DBG("request %p pending %p", agent->pending, agent->queue);
474 if (agent->pending) {
475 if (agent->pending->call)
476 send_cancel_request(agent, agent->pending);
478 agent_finalize_pending(agent, NULL);
481 for (list = agent->queue; list; list = list->next) {
482 struct connman_agent_request *request = list->data;
487 request->callback(NULL, request->user_data);
488 agent_request_free(request);
491 g_list_free(agent->queue);
495 void connman_agent_cancel(void *user_context)
501 DBG("context %p", user_context);
503 g_hash_table_iter_init(&iter, agent_hash);
504 while (g_hash_table_iter_next(&iter, &key, &value)) {
506 struct connman_agent *agent = value;
509 * Cancel all the pending requests to a given agent and service
513 struct connman_agent_request *request = list->data;
517 if (request && request->user_context &&
518 request->user_context ==
520 DBG("cancel pending %p", request);
522 request->callback(NULL, request->user_data);
524 agent_request_free(request);
526 agent->queue = g_list_delete_link(agent->queue,
534 * If there is a request from client to a given service,
535 * we need to cancel it.
537 if (agent->pending && agent->pending->user_context &&
538 agent->pending->user_context == user_context) {
539 DBG("cancel request %p", agent->pending);
541 if (agent->pending->call)
542 send_cancel_request(agent, agent->pending);
544 agent_finalize_pending(agent, NULL);
546 err = agent_send_next_request(agent);
547 if (err < 0 && err != -EBUSY)
548 DBG("send next request failed (%s/%d)",
549 strerror(-err), -err);
554 static void agent_unref_debug(struct connman_agent *agent,
555 const char *file, int line, const char *caller)
557 DBG("%p ref %d by %s:%d:%s()", agent, agent->refcount - 1,
560 if (__sync_fetch_and_sub(&agent->refcount, 1) != 1)
563 cancel_all_requests(agent);
565 g_free(agent->owner);
568 if (agent == default_agent) {
569 default_agent = NULL;
576 static void agent_release(struct connman_agent *agent, const char *interface)
578 DBusMessage *message;
580 DBG("release agent %s %s", agent->owner, agent->path);
582 message = dbus_message_new_method_call(agent->owner, agent->path,
583 interface, "Release");
584 if (message == NULL) {
585 connman_error("Couldn't allocate D-Bus message");
589 dbus_message_set_no_reply(message, TRUE);
590 g_dbus_send_message(connection, message);
593 static void release_agents(void)
598 g_hash_table_iter_init(&iter, agent_hash);
599 while (g_hash_table_iter_next(&iter, &key, &value))
600 agent_release(value, get_driver()->interface);
604 * connman_agent_driver_unregister:
605 * @driver: Agent driver definition
607 * Remove a previously registered agent driver
609 void connman_agent_driver_unregister(struct connman_agent_driver *driver)
616 DBG("Unregistering driver %p name %s", driver, driver->name);
620 for (list = driver_list; list; list = list->next) {
621 if (driver != list->data)
624 g_hash_table_remove_all(agent_hash);
628 driver_list = g_slist_remove(driver_list, driver);
631 static void agent_destroy(gpointer data)
633 struct connman_agent *agent = data;
635 DBG("agent %s req %p", agent->owner, agent->pending);
637 if (agent->watch > 0) {
638 g_dbus_remove_watch(connection, agent->watch);
645 int connman_agent_unregister(const char *sender, const char *path)
647 DBG("sender %s path %s", sender, path);
649 if (!g_hash_table_remove(agent_hash, sender))
655 int __connman_agent_init(void)
659 connection = connman_dbus_get_connection();
663 agent_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
664 NULL, agent_destroy);
671 void __connman_agent_cleanup(void)
678 g_hash_table_destroy(agent_hash);
682 dbus_connection_unref(connection);