5 * Copyright (C) 2007-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
30 #include <connman/agent.h>
34 static connman_bool_t connman_state_idle;
35 static DBusMessage *session_mode_pending = NULL;
37 static DBusMessage *get_properties(DBusConnection *conn,
38 DBusMessage *msg, void *data)
41 DBusMessageIter array, dict;
42 connman_bool_t offlinemode, sessionmode;
47 reply = dbus_message_new_method_return(msg);
51 dbus_message_iter_init_append(reply, &array);
53 connman_dbus_dict_open(&array, &dict);
55 str = __connman_notifier_get_state();
56 connman_dbus_dict_append_basic(&dict, "State",
57 DBUS_TYPE_STRING, &str);
59 offlinemode = __connman_technology_get_offlinemode();
60 connman_dbus_dict_append_basic(&dict, "OfflineMode",
61 DBUS_TYPE_BOOLEAN, &offlinemode);
63 sessionmode = __connman_session_mode();
64 connman_dbus_dict_append_basic(&dict, "SessionMode",
68 connman_dbus_dict_close(&array, &dict);
73 static DBusMessage *set_property(DBusConnection *conn,
74 DBusMessage *msg, void *data)
76 DBusMessageIter iter, value;
82 if (dbus_message_iter_init(msg, &iter) == FALSE)
83 return __connman_error_invalid_arguments(msg);
85 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
86 return __connman_error_invalid_arguments(msg);
88 dbus_message_iter_get_basic(&iter, &name);
89 dbus_message_iter_next(&iter);
91 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
92 return __connman_error_invalid_arguments(msg);
94 dbus_message_iter_recurse(&iter, &value);
96 type = dbus_message_iter_get_arg_type(&value);
98 if (g_str_equal(name, "OfflineMode") == TRUE) {
99 connman_bool_t offlinemode;
101 if (type != DBUS_TYPE_BOOLEAN)
102 return __connman_error_invalid_arguments(msg);
104 dbus_message_iter_get_basic(&value, &offlinemode);
106 __connman_technology_set_offlinemode(offlinemode);
107 } else if (g_str_equal(name, "SessionMode") == TRUE) {
108 connman_bool_t sessionmode;
110 if (type != DBUS_TYPE_BOOLEAN)
111 return __connman_error_invalid_arguments(msg);
113 dbus_message_iter_get_basic(&value, &sessionmode);
115 if (session_mode_pending != NULL)
116 return __connman_error_in_progress(msg);
118 __connman_session_set_mode(sessionmode);
120 if (sessionmode == TRUE && connman_state_idle == FALSE) {
121 session_mode_pending = dbus_message_ref(msg);
126 return __connman_error_invalid_property(msg);
128 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
131 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
133 __connman_technology_list_struct(iter);
136 static DBusMessage *get_technologies(DBusConnection *conn,
137 DBusMessage *msg, void *data)
143 reply = dbus_message_new_method_return(msg);
147 __connman_dbus_append_objpath_dict_array(reply,
148 append_technology_structs, NULL);
153 static DBusMessage *remove_provider(DBusConnection *conn,
154 DBusMessage *msg, void *data)
159 DBG("conn %p", conn);
161 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
164 err = __connman_provider_remove(path);
166 return __connman_error_failed(msg, -err);
168 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
171 static DBusConnection *connection = NULL;
173 static void session_mode_notify(void)
177 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
178 g_dbus_send_message(connection, reply);
180 dbus_message_unref(session_mode_pending);
181 session_mode_pending = NULL;
184 static void idle_state(connman_bool_t idle)
187 DBG("idle %d", idle);
189 connman_state_idle = idle;
191 if (connman_state_idle == FALSE || session_mode_pending == NULL)
194 session_mode_notify();
197 static struct connman_notifier technology_notifier = {
199 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
200 .idle_state = idle_state,
203 static void append_service_structs(DBusMessageIter *iter, void *user_data)
205 __connman_service_list_struct(iter);
208 static DBusMessage *get_services(DBusConnection *conn,
209 DBusMessage *msg, void *data)
213 reply = dbus_message_new_method_return(msg);
217 __connman_dbus_append_objpath_dict_array(reply,
218 append_service_structs, NULL);
223 static DBusMessage *connect_provider(DBusConnection *conn,
224 DBusMessage *msg, void *data)
228 DBG("conn %p", conn);
230 if (__connman_session_mode() == TRUE) {
231 connman_info("Session mode enabled: "
232 "direct provider connect disabled");
234 return __connman_error_failed(msg, EINVAL);
237 err = __connman_provider_create_and_connect(msg);
239 if (err == -EINPROGRESS) {
240 connman_error("Invalid return code from connect");
244 return __connman_error_failed(msg, -err);
250 static DBusMessage *register_agent(DBusConnection *conn,
251 DBusMessage *msg, void *data)
253 const char *sender, *path;
256 DBG("conn %p", conn);
258 sender = dbus_message_get_sender(msg);
260 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
263 err = connman_agent_register(sender, path);
265 return __connman_error_failed(msg, -err);
267 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
270 static DBusMessage *unregister_agent(DBusConnection *conn,
271 DBusMessage *msg, void *data)
273 const char *sender, *path;
276 DBG("conn %p", conn);
278 sender = dbus_message_get_sender(msg);
280 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
283 err = connman_agent_unregister(sender, path);
285 return __connman_error_failed(msg, -err);
287 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
290 static DBusMessage *register_counter(DBusConnection *conn,
291 DBusMessage *msg, void *data)
293 const char *sender, *path;
294 unsigned int accuracy, period;
297 DBG("conn %p", conn);
299 sender = dbus_message_get_sender(msg);
301 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
302 DBUS_TYPE_UINT32, &accuracy,
303 DBUS_TYPE_UINT32, &period,
306 /* FIXME: add handling of accuracy parameter */
308 err = __connman_counter_register(sender, path, period);
310 return __connman_error_failed(msg, -err);
312 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
315 static DBusMessage *unregister_counter(DBusConnection *conn,
316 DBusMessage *msg, void *data)
318 const char *sender, *path;
321 DBG("conn %p", conn);
323 sender = dbus_message_get_sender(msg);
325 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
328 err = __connman_counter_unregister(sender, path);
330 return __connman_error_failed(msg, -err);
332 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
335 static DBusMessage *create_session(DBusConnection *conn,
336 DBusMessage *msg, void *data)
340 DBG("conn %p", conn);
342 err = __connman_session_create(msg);
344 if (err == -EINPROGRESS)
347 return __connman_error_failed(msg, -err);
350 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
353 static DBusMessage *destroy_session(DBusConnection *conn,
354 DBusMessage *msg, void *data)
358 DBG("conn %p", conn);
360 err = __connman_session_destroy(msg);
362 return __connman_error_failed(msg, -err);
364 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
367 static DBusMessage *request_private_network(DBusConnection *conn,
368 DBusMessage *msg, void *data)
373 DBG("conn %p", conn);
375 sender = dbus_message_get_sender(msg);
377 err = __connman_private_network_request(msg, sender);
379 return __connman_error_failed(msg, -err);
384 static DBusMessage *release_private_network(DBusConnection *conn,
385 DBusMessage *msg, void *data)
390 DBG("conn %p", conn);
392 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
395 err = __connman_private_network_release(path);
397 return __connman_error_failed(msg, -err);
399 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
402 static const GDBusMethodTable manager_methods[] = {
403 { GDBUS_METHOD("GetProperties",
404 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
406 { GDBUS_ASYNC_METHOD("SetProperty",
407 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
408 NULL, set_property) },
409 { GDBUS_METHOD("GetTechnologies",
410 NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
412 { GDBUS_DEPRECATED_METHOD("RemoveProvider",
413 GDBUS_ARGS({ "provider", "o" }), NULL,
415 { GDBUS_METHOD("GetServices",
416 NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
418 { GDBUS_DEPRECATED_ASYNC_METHOD("ConnectProvider",
419 GDBUS_ARGS({ "provider", "a{sv}" }),
420 GDBUS_ARGS({ "path", "o" }),
422 { GDBUS_METHOD("RegisterAgent",
423 GDBUS_ARGS({ "path", "o" }), NULL,
425 { GDBUS_METHOD("UnregisterAgent",
426 GDBUS_ARGS({ "path", "o" }), NULL,
428 { GDBUS_METHOD("RegisterCounter",
429 GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
431 NULL, register_counter) },
432 { GDBUS_METHOD("UnregisterCounter",
433 GDBUS_ARGS({ "path", "o" }), NULL,
434 unregister_counter) },
435 { GDBUS_ASYNC_METHOD("CreateSession",
436 GDBUS_ARGS({ "settings", "a{sv}" },
437 { "notifier", "o" }),
438 GDBUS_ARGS({ "session", "o" }),
440 { GDBUS_METHOD("DestroySession",
441 GDBUS_ARGS({ "session", "o" }), NULL,
443 { GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
444 NULL, GDBUS_ARGS({ "path", "o" },
445 { "settings", "a{sv}" },
447 request_private_network) },
448 { GDBUS_METHOD("ReleasePrivateNetwork",
449 GDBUS_ARGS({ "path", "o" }), NULL,
450 release_private_network) },
454 static const GDBusSignalTable manager_signals[] = {
455 { GDBUS_SIGNAL("PropertyChanged",
456 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
457 { GDBUS_SIGNAL("TechnologyAdded",
458 GDBUS_ARGS({ "path", "o" },
459 { "properties", "a{sv}" })) },
460 { GDBUS_SIGNAL("TechnologyRemoved",
461 GDBUS_ARGS({ "path", "o" })) },
462 { GDBUS_SIGNAL("ServicesChanged",
463 GDBUS_ARGS({ "changed", "a(oa{sv})" },
464 { "removed", "ao" })) },
468 int __connman_manager_init(void)
472 connection = connman_dbus_get_connection();
473 if (connection == NULL)
476 if (connman_notifier_register(&technology_notifier) < 0)
477 connman_error("Failed to register technology notifier");
479 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
480 CONNMAN_MANAGER_INTERFACE,
482 manager_signals, NULL, NULL, NULL);
484 connman_state_idle = TRUE;
489 void __connman_manager_cleanup(void)
493 if (connection == NULL)
496 if (session_mode_pending != NULL)
497 dbus_message_unref(session_mode_pending);
499 connman_notifier_unregister(&technology_notifier);
501 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
502 CONNMAN_MANAGER_INTERFACE);
504 dbus_connection_unref(connection);