5 * Copyright (C) 2007-2010 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
32 connman_bool_t connman_state_idle;
33 DBusMessage *session_mode_pending = NULL;
35 static DBusMessage *get_properties(DBusConnection *conn,
36 DBusMessage *msg, void *data)
39 DBusMessageIter array, dict;
40 connman_bool_t offlinemode, sessionmode;
45 reply = dbus_message_new_method_return(msg);
49 dbus_message_iter_init_append(reply, &array);
51 connman_dbus_dict_open(&array, &dict);
53 connman_dbus_dict_append_array(&dict, "Services",
54 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
55 connman_dbus_dict_append_array(&dict, "Technologies",
56 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
58 str = __connman_notifier_get_state();
59 connman_dbus_dict_append_basic(&dict, "State",
60 DBUS_TYPE_STRING, &str);
62 offlinemode = __connman_technology_get_offlinemode();
63 connman_dbus_dict_append_basic(&dict, "OfflineMode",
64 DBUS_TYPE_BOOLEAN, &offlinemode);
66 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
67 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
68 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
69 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
70 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
71 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
73 str = __connman_service_default();
75 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
76 DBUS_TYPE_STRING, &str);
78 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
79 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
80 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
81 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
83 sessionmode = __connman_session_mode();
84 connman_dbus_dict_append_basic(&dict, "SessionMode",
88 connman_dbus_dict_close(&array, &dict);
93 static DBusMessage *set_property(DBusConnection *conn,
94 DBusMessage *msg, void *data)
96 DBusMessageIter iter, value;
100 DBG("conn %p", conn);
102 if (dbus_message_iter_init(msg, &iter) == FALSE)
103 return __connman_error_invalid_arguments(msg);
105 dbus_message_iter_get_basic(&iter, &name);
106 dbus_message_iter_next(&iter);
107 dbus_message_iter_recurse(&iter, &value);
109 type = dbus_message_iter_get_arg_type(&value);
111 if (g_str_equal(name, "OfflineMode") == TRUE) {
112 connman_bool_t offlinemode;
114 if (type != DBUS_TYPE_BOOLEAN)
115 return __connman_error_invalid_arguments(msg);
117 dbus_message_iter_get_basic(&value, &offlinemode);
119 __connman_technology_set_offlinemode(offlinemode);
120 } else if (g_str_equal(name, "SessionMode") == TRUE) {
121 connman_bool_t sessionmode;
123 if (type != DBUS_TYPE_BOOLEAN)
124 return __connman_error_invalid_arguments(msg);
126 dbus_message_iter_get_basic(&value, &sessionmode);
128 if (session_mode_pending != NULL)
129 return __connman_error_in_progress(msg);
131 __connman_session_set_mode(sessionmode);
133 if (sessionmode == TRUE && connman_state_idle == FALSE) {
134 session_mode_pending = msg;
139 return __connman_error_invalid_property(msg);
141 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
144 static DBusMessage *get_state(DBusConnection *conn,
145 DBusMessage *msg, void *data)
149 DBG("conn %p", conn);
151 str = __connman_notifier_get_state();
153 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
157 static DBusMessage *remove_provider(DBusConnection *conn,
158 DBusMessage *msg, void *data)
163 DBG("conn %p", conn);
165 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
168 err = __connman_provider_remove(path);
170 return __connman_error_failed(msg, -err);
172 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
175 static DBusMessage *request_scan(DBusConnection *conn,
176 DBusMessage *msg, void *data)
178 enum connman_service_type type;
182 DBG("conn %p", conn);
184 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
187 if (g_strcmp0(str, "") == 0)
188 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
189 else if (g_strcmp0(str, "wifi") == 0)
190 type = CONNMAN_SERVICE_TYPE_WIFI;
191 else if (g_strcmp0(str, "wimax") == 0)
192 type = CONNMAN_SERVICE_TYPE_WIMAX;
194 return __connman_error_invalid_arguments(msg);
196 err = __connman_device_request_scan(type);
198 if (err == -EINPROGRESS) {
199 connman_error("Invalid return code from scan");
203 return __connman_error_failed(msg, -err);
206 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
209 static DBusConnection *connection = NULL;
211 static void session_mode_notify(void)
215 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
216 g_dbus_send_message(connection, reply);
218 dbus_message_unref(session_mode_pending);
219 session_mode_pending = NULL;
222 static void idle_state(connman_bool_t idle)
225 DBG("idle %d", idle);
227 connman_state_idle = idle;
229 if (connman_state_idle == FALSE || session_mode_pending == NULL)
232 session_mode_notify();
235 static struct connman_notifier technology_notifier = {
237 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
238 .idle_state = idle_state,
241 static DBusMessage *enable_technology(DBusConnection *conn,
242 DBusMessage *msg, void *data)
244 enum connman_service_type type;
247 DBG("conn %p", conn);
249 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
252 if (g_strcmp0(str, "ethernet") == 0)
253 type = CONNMAN_SERVICE_TYPE_ETHERNET;
254 else if (g_strcmp0(str, "wifi") == 0)
255 type = CONNMAN_SERVICE_TYPE_WIFI;
256 else if (g_strcmp0(str, "wimax") == 0)
257 type = CONNMAN_SERVICE_TYPE_WIMAX;
258 else if (g_strcmp0(str, "bluetooth") == 0)
259 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
260 else if (g_strcmp0(str, "cellular") == 0)
261 type = CONNMAN_SERVICE_TYPE_CELLULAR;
263 return __connman_error_invalid_arguments(msg);
265 if (__connman_notifier_is_registered(type) == FALSE)
266 return __connman_error_not_registered(msg);
268 if (__connman_notifier_is_enabled(type) == TRUE)
269 return __connman_error_already_enabled(msg);
271 __connman_technology_enable(type, msg);
276 static DBusMessage *disable_technology(DBusConnection *conn,
277 DBusMessage *msg, void *data)
279 enum connman_service_type type;
282 DBG("conn %p", conn);
284 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
287 if (g_strcmp0(str, "ethernet") == 0)
288 type = CONNMAN_SERVICE_TYPE_ETHERNET;
289 else if (g_strcmp0(str, "wifi") == 0)
290 type = CONNMAN_SERVICE_TYPE_WIFI;
291 else if (g_strcmp0(str, "wimax") == 0)
292 type = CONNMAN_SERVICE_TYPE_WIMAX;
293 else if (g_strcmp0(str, "bluetooth") == 0)
294 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
295 else if (g_strcmp0(str, "cellular") == 0)
296 type = CONNMAN_SERVICE_TYPE_CELLULAR;
298 return __connman_error_invalid_arguments(msg);
300 if (__connman_notifier_is_registered(type) == FALSE)
301 return __connman_error_not_registered(msg);
303 if (__connman_notifier_is_enabled(type) == FALSE)
304 return __connman_error_already_disabled(msg);
306 __connman_technology_disable(type, msg);
311 static DBusMessage *get_services(DBusConnection *conn,
312 DBusMessage *msg, void *data)
315 DBusMessageIter iter, array;
317 reply = dbus_message_new_method_return(msg);
321 dbus_message_iter_init_append(reply, &iter);
323 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
324 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
325 DBUS_TYPE_OBJECT_PATH_AS_STRING
326 DBUS_TYPE_ARRAY_AS_STRING
327 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
328 DBUS_TYPE_STRING_AS_STRING
329 DBUS_TYPE_VARIANT_AS_STRING
330 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
331 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
333 __connman_service_list_struct(&array);
335 dbus_message_iter_close_container(&iter, &array);
340 static DBusMessage *provision_service(DBusConnection *conn, DBusMessage *msg,
345 DBG("conn %p", conn);
347 err = __connman_service_provision(msg);
349 return __connman_error_failed(msg, -err);
354 static DBusMessage *connect_provider(DBusConnection *conn,
355 DBusMessage *msg, void *data)
359 DBG("conn %p", conn);
361 if (__connman_session_mode() == TRUE) {
362 connman_info("Session mode enabled: "
363 "direct provider connect disabled");
365 return __connman_error_failed(msg, -EINVAL);
368 err = __connman_provider_create_and_connect(msg);
370 if (err == -EINPROGRESS) {
371 connman_error("Invalid return code from connect");
375 return __connman_error_failed(msg, -err);
381 static DBusMessage *register_agent(DBusConnection *conn,
382 DBusMessage *msg, void *data)
384 const char *sender, *path;
387 DBG("conn %p", conn);
389 sender = dbus_message_get_sender(msg);
391 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
394 err = __connman_agent_register(sender, path);
396 return __connman_error_failed(msg, -err);
398 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
401 static DBusMessage *unregister_agent(DBusConnection *conn,
402 DBusMessage *msg, void *data)
404 const char *sender, *path;
407 DBG("conn %p", conn);
409 sender = dbus_message_get_sender(msg);
411 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
414 err = __connman_agent_unregister(sender, path);
416 return __connman_error_failed(msg, -err);
418 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
421 static DBusMessage *register_counter(DBusConnection *conn,
422 DBusMessage *msg, void *data)
424 const char *sender, *path;
425 unsigned int accuracy, period;
428 DBG("conn %p", conn);
430 sender = dbus_message_get_sender(msg);
432 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
433 DBUS_TYPE_UINT32, &accuracy,
434 DBUS_TYPE_UINT32, &period,
437 /* FIXME: add handling of accuracy parameter */
439 err = __connman_counter_register(sender, path, period);
441 return __connman_error_failed(msg, -err);
443 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
446 static DBusMessage *unregister_counter(DBusConnection *conn,
447 DBusMessage *msg, void *data)
449 const char *sender, *path;
452 DBG("conn %p", conn);
454 sender = dbus_message_get_sender(msg);
456 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
459 err = __connman_counter_unregister(sender, path);
461 return __connman_error_failed(msg, -err);
463 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
466 static DBusMessage *create_session(DBusConnection *conn,
467 DBusMessage *msg, void *data)
471 DBG("conn %p", conn);
473 err = __connman_session_create(msg);
475 return __connman_error_failed(msg, -err);
477 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
480 static DBusMessage *destroy_session(DBusConnection *conn,
481 DBusMessage *msg, void *data)
485 DBG("conn %p", conn);
487 err = __connman_session_destroy(msg);
489 return __connman_error_failed(msg, -err);
491 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
494 static DBusMessage *request_private_network(DBusConnection *conn,
495 DBusMessage *msg, void *data)
500 DBG("conn %p", conn);
502 sender = dbus_message_get_sender(msg);
504 err = __connman_private_network_request(msg, sender);
506 return __connman_error_failed(msg, -err);
511 static DBusMessage *release_private_network(DBusConnection *conn,
512 DBusMessage *msg, void *data)
517 DBG("conn %p", conn);
519 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
522 err = __connman_private_network_release(path);
524 return __connman_error_failed(msg, -err);
526 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
529 static GDBusMethodTable manager_methods[] = {
530 { "GetProperties", "", "a{sv}", get_properties },
531 { "SetProperty", "sv", "", set_property,
532 G_DBUS_METHOD_FLAG_ASYNC },
533 { "GetState", "", "s", get_state },
534 { "RemoveProvider", "o", "", remove_provider },
535 { "RequestScan", "s", "", request_scan },
536 { "EnableTechnology", "s", "", enable_technology,
537 G_DBUS_METHOD_FLAG_ASYNC },
538 { "DisableTechnology", "s", "", disable_technology,
539 G_DBUS_METHOD_FLAG_ASYNC },
540 { "GetServices", "", "a(oa{sv})", get_services },
541 { "ProvisionService", "s", "", provision_service,
542 G_DBUS_METHOD_FLAG_ASYNC },
543 { "ConnectProvider", "a{sv}", "o", connect_provider,
544 G_DBUS_METHOD_FLAG_ASYNC },
545 { "RegisterAgent", "o", "", register_agent },
546 { "UnregisterAgent", "o", "", unregister_agent },
547 { "RegisterCounter", "ouu", "", register_counter },
548 { "UnregisterCounter", "o", "", unregister_counter },
549 { "CreateSession", "a{sv}o", "o", create_session },
550 { "DestroySession", "o", "", destroy_session },
551 { "RequestPrivateNetwork", "", "oa{sv}h",
552 request_private_network,
553 G_DBUS_METHOD_FLAG_ASYNC },
554 { "ReleasePrivateNetwork", "o", "",
555 release_private_network },
559 static GDBusSignalTable manager_signals[] = {
560 { "PropertyChanged", "sv" },
561 { "StateChanged", "s" },
565 int __connman_manager_init(void)
569 connection = connman_dbus_get_connection();
570 if (connection == NULL)
573 if (connman_notifier_register(&technology_notifier) < 0)
574 connman_error("Failed to register technology notifier");
576 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
577 CONNMAN_MANAGER_INTERFACE,
579 manager_signals, NULL, NULL, NULL);
581 connman_state_idle = TRUE;
586 void __connman_manager_cleanup(void)
590 if (connection == NULL)
593 connman_notifier_unregister(&technology_notifier);
595 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
596 CONNMAN_MANAGER_INTERFACE);
598 dbus_connection_unref(connection);