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;
168 const char *interface = NULL;
170 if (request && request->driver)
171 interface = request->driver->interface;
173 DBG("send cancel req to %s %s iface %s", agent->owner, agent->path,
176 message = dbus_message_new_method_call(agent->owner,
181 connman_error("Couldn't allocate D-Bus message");
185 g_dbus_send_message(connection, message);
189 static void agent_receive_message(DBusPendingCall *call, void *user_data)
191 struct connman_agent *agent = user_data;
195 DBG("agent %p req %p", agent, agent->pending);
197 reply = dbus_pending_call_steal_reply(call);
198 dbus_pending_call_unref(call);
199 agent->pending->call = NULL;
201 if (dbus_message_is_error(reply,
202 "org.freedesktop.DBus.Error.Timeout") ||
203 dbus_message_is_error(reply,
204 "org.freedesktop.DBus.Error.TimedOut")) {
205 send_cancel_request(agent, agent->pending);
208 agent_finalize_pending(agent, reply);
209 dbus_message_unref(reply);
211 err = agent_send_next_request(agent);
212 if (err < 0 && err != -EBUSY)
213 DBG("send next request failed (%s/%d)", strerror(-err), -err);
216 static struct connman_agent_driver *get_driver(void)
218 return g_slist_nth_data(driver_list, 0);
221 int connman_agent_queue_message(void *user_context,
222 DBusMessage *msg, int timeout,
223 agent_queue_cb callback, void *user_data,
226 struct connman_agent_request *queue_data;
227 struct connman_agent_driver *driver;
228 struct connman_agent *agent = agent_data;
231 if (!user_context || !callback)
234 queue_data = g_new0(struct connman_agent_request, 1);
238 driver = get_driver();
239 DBG("driver %p", driver);
241 if (driver && driver->context_ref)
242 queue_data->user_context = driver->context_ref(user_context);
244 queue_data->user_context = user_context;
246 queue_data->driver = driver;
247 queue_data->msg = dbus_message_ref(msg);
248 queue_data->timeout = timeout;
249 queue_data->callback = callback;
250 queue_data->user_data = user_data;
251 agent->queue = g_list_append(agent->queue, queue_data);
253 err = agent_send_next_request(agent);
254 if (err < 0 && err != -EBUSY)
255 DBG("send next request failed (%s/%d)", strerror(-err), -err);
260 static void set_default_agent(void)
262 struct connman_agent *agent = NULL;
269 g_hash_table_iter_init(&iter, agent_hash);
270 if (g_hash_table_iter_next(&iter, &key, &value))
274 DBG("default agent set to %s %s", agent->owner, agent->path);
276 DBG("default agent cleared");
278 default_agent = agent;
281 static void agent_disconnect(DBusConnection *conn, void *user_data)
283 struct connman_agent *agent = user_data;
285 DBG("agent %s disconnected", agent->owner);
287 if (agent->watch > 0) {
288 g_dbus_remove_watch(conn, agent->watch);
292 g_hash_table_remove(agent_hash, agent->owner);
295 static struct connman_agent *agent_ref_debug(struct connman_agent *agent,
296 const char *file, int line, const char *caller)
298 DBG("%p ref %d by %s:%d:%s()", agent, agent->refcount + 1,
301 __sync_fetch_and_add(&agent->refcount, 1);
306 static struct connman_agent *agent_create(const char *name, const char *path)
308 struct connman_agent *agent;
310 agent = g_new0(struct connman_agent, 1);
312 agent->owner = g_strdup(name);
313 agent->path = g_strdup(path);
315 agent->watch = g_dbus_add_disconnect_watch(connection,
316 name, agent_disconnect,
319 return agent_ref(agent);
322 int connman_agent_register(const char *sender, const char *path)
324 struct connman_agent *agent;
326 DBG("sender %s path %s", sender, path);
328 agent = g_hash_table_lookup(agent_hash, sender);
332 agent = agent_create(sender, path);
336 DBG("agent %s", agent->owner);
338 g_hash_table_replace(agent_hash, agent->owner, agent);
346 struct report_error_data {
348 report_error_cb_t callback;
352 static void report_error_reply(DBusMessage *reply, void *user_data)
354 struct report_error_data *report_error = user_data;
356 const char *dbus_err;
361 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
362 dbus_err = dbus_message_get_error_name(reply);
365 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
369 report_error->callback(report_error->user_context, retry,
370 report_error->user_data);
372 g_free(report_error);
375 int connman_agent_report_error_full(void *user_context, const char *path,
376 const char *method, const char *error,
377 report_error_cb_t callback,
378 const char *dbus_sender, void *user_data)
380 DBusMessage *message;
381 DBusMessageIter iter;
382 struct report_error_data *report_error;
383 struct connman_agent *agent;
386 agent = connman_agent_get_info(dbus_sender, NULL, NULL);
388 DBG("agent %p sender %s context %p path %s", agent,
389 dbus_sender, user_context, agent ? agent->path : "-");
391 if (!user_context || !agent || !agent->path || !error || !callback)
394 message = dbus_message_new_method_call(agent->owner, agent->path,
395 CONNMAN_AGENT_INTERFACE, method);
399 dbus_message_iter_init_append(message, &iter);
401 dbus_message_iter_append_basic(&iter,
402 DBUS_TYPE_OBJECT_PATH, &path);
403 dbus_message_iter_append_basic(&iter,
404 DBUS_TYPE_STRING, &error);
406 report_error = g_try_new0(struct report_error_data, 1);
408 dbus_message_unref(message);
412 report_error->user_context = user_context;
413 report_error->callback = callback;
414 report_error->user_data = user_data;
416 err = connman_agent_queue_message(user_context, message,
417 connman_timeout_input_request(),
418 report_error_reply, report_error,
420 if (err < 0 && err != -EBUSY) {
421 DBG("error %d sending error request", err);
422 g_free(report_error);
423 dbus_message_unref(message);
427 dbus_message_unref(message);
432 int connman_agent_report_error(void *user_context, const char *path,
434 report_error_cb_t callback,
435 const char *dbus_sender, void *user_data)
437 return connman_agent_report_error_full(user_context, path,
438 "ReportError", error, callback, dbus_sender,
442 static gint compare_priority(gconstpointer a, gconstpointer b)
444 const struct connman_agent_driver *driver1 = a;
445 const struct connman_agent_driver *driver2 = b;
447 return driver2->priority - driver1->priority;
451 * connman_agent_driver_register:
452 * @driver: Agent driver definition
454 * Register a new agent driver
456 * Returns: %0 on success
458 int connman_agent_driver_register(struct connman_agent_driver *driver)
460 DBG("Registering driver %p name %s", driver, driver->name);
462 driver_list = g_slist_insert_sorted(driver_list, driver,
468 static void release_driver(void)
470 connman_agent_driver_unregister(get_driver());
473 static void cancel_all_requests(struct connman_agent *agent)
477 DBG("request %p pending %p", agent->pending, agent->queue);
479 if (agent->pending) {
480 if (agent->pending->call)
481 send_cancel_request(agent, agent->pending);
483 agent_finalize_pending(agent, NULL);
486 for (list = agent->queue; list; list = list->next) {
487 struct connman_agent_request *request = list->data;
492 request->callback(NULL, request->user_data);
493 agent_request_free(request);
496 g_list_free(agent->queue);
500 void connman_agent_cancel(void *user_context)
506 DBG("context %p", user_context);
508 g_hash_table_iter_init(&iter, agent_hash);
509 while (g_hash_table_iter_next(&iter, &key, &value)) {
511 struct connman_agent *agent = value;
514 * Cancel all the pending requests to a given agent and service
518 struct connman_agent_request *request = list->data;
522 if (request && request->user_context &&
523 request->user_context ==
525 DBG("cancel pending %p", request);
527 agent->queue = g_list_delete_link(agent->queue,
530 request->callback(NULL, request->user_data);
532 agent_request_free(request);
539 * If there is a request from client to a given service,
540 * we need to cancel it.
542 if (agent->pending && agent->pending->user_context &&
543 agent->pending->user_context == user_context) {
544 DBG("cancel request %p", agent->pending);
546 if (agent->pending->call)
547 send_cancel_request(agent, agent->pending);
549 agent_finalize_pending(agent, NULL);
551 err = agent_send_next_request(agent);
552 if (err < 0 && err != -EBUSY)
553 DBG("send next request failed (%s/%d)",
554 strerror(-err), -err);
559 static void agent_unref_debug(struct connman_agent *agent,
560 const char *file, int line, const char *caller)
562 DBG("%p ref %d by %s:%d:%s()", agent, agent->refcount - 1,
565 if (__sync_fetch_and_sub(&agent->refcount, 1) != 1)
568 cancel_all_requests(agent);
570 g_free(agent->owner);
573 if (agent == default_agent) {
574 default_agent = NULL;
581 static void agent_release(struct connman_agent *agent, const char *interface)
583 DBusMessage *message;
585 DBG("release agent %s %s", agent->owner, agent->path);
587 message = dbus_message_new_method_call(agent->owner, agent->path,
588 interface, "Release");
590 connman_error("Couldn't allocate D-Bus message");
594 dbus_message_set_no_reply(message, TRUE);
595 g_dbus_send_message(connection, message);
598 static void release_agents(void)
603 g_hash_table_iter_init(&iter, agent_hash);
604 while (g_hash_table_iter_next(&iter, &key, &value))
605 agent_release(value, get_driver()->interface);
609 * connman_agent_driver_unregister:
610 * @driver: Agent driver definition
612 * Remove a previously registered agent driver
614 void connman_agent_driver_unregister(struct connman_agent_driver *driver)
621 DBG("Unregistering driver %p name %s", driver, driver->name);
625 for (list = driver_list; list; list = list->next) {
626 if (driver != list->data)
629 g_hash_table_remove_all(agent_hash);
633 driver_list = g_slist_remove(driver_list, driver);
636 static void agent_destroy(gpointer data)
638 struct connman_agent *agent = data;
640 DBG("agent %s req %p", agent->owner, agent->pending);
642 if (agent->watch > 0) {
643 g_dbus_remove_watch(connection, agent->watch);
650 int connman_agent_unregister(const char *sender, const char *path)
652 DBG("sender %s path %s", sender, path);
654 if (!g_hash_table_remove(agent_hash, sender))
660 int __connman_agent_init(void)
664 connection = connman_dbus_get_connection();
668 agent_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
669 NULL, agent_destroy);
676 void __connman_agent_cleanup(void)
683 g_hash_table_destroy(agent_hash);
687 dbus_connection_unref(connection);