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
30 static DBusMessage *get_properties(DBusConnection *conn,
31 DBusMessage *msg, void *data)
34 DBusMessageIter array, dict;
35 connman_bool_t offlinemode;
40 if (__connman_security_check_privilege(msg,
41 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
42 return __connman_error_permission_denied(msg);
44 reply = dbus_message_new_method_return(msg);
48 dbus_message_iter_init_append(reply, &array);
50 connman_dbus_dict_open(&array, &dict);
52 str = __connman_profile_active_path();
54 connman_dbus_dict_append_basic(&dict, "ActiveProfile",
55 DBUS_TYPE_OBJECT_PATH, &str);
57 connman_dbus_dict_append_array(&dict, "Profiles",
58 DBUS_TYPE_OBJECT_PATH, __connman_profile_list, NULL);
59 connman_dbus_dict_append_array(&dict, "Services",
60 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
61 connman_dbus_dict_append_array(&dict, "Providers",
62 DBUS_TYPE_OBJECT_PATH, __connman_provider_list, NULL);
64 connman_dbus_dict_append_array(&dict, "Devices",
65 DBUS_TYPE_OBJECT_PATH, __connman_device_list, NULL);
67 str = __connman_notifier_get_state();
68 connman_dbus_dict_append_basic(&dict, "State",
69 DBUS_TYPE_STRING, &str);
71 offlinemode = __connman_profile_get_offlinemode();
72 connman_dbus_dict_append_basic(&dict, "OfflineMode",
73 DBUS_TYPE_BOOLEAN, &offlinemode);
75 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
76 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
77 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
78 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
79 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
80 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
82 str = __connman_service_default();
84 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
85 DBUS_TYPE_STRING, &str);
87 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
88 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
89 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
90 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
92 connman_dbus_dict_close(&array, &dict);
97 static DBusMessage *set_property(DBusConnection *conn,
98 DBusMessage *msg, void *data)
100 DBusMessageIter iter, value;
104 DBG("conn %p", conn);
106 if (dbus_message_iter_init(msg, &iter) == FALSE)
107 return __connman_error_invalid_arguments(msg);
109 dbus_message_iter_get_basic(&iter, &name);
110 dbus_message_iter_next(&iter);
111 dbus_message_iter_recurse(&iter, &value);
113 if (__connman_security_check_privilege(msg,
114 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
115 return __connman_error_permission_denied(msg);
117 type = dbus_message_iter_get_arg_type(&value);
119 if (g_str_equal(name, "OfflineMode") == TRUE) {
120 connman_bool_t offlinemode;
122 if (type != DBUS_TYPE_BOOLEAN)
123 return __connman_error_invalid_arguments(msg);
125 dbus_message_iter_get_basic(&value, &offlinemode);
127 __connman_profile_set_offlinemode(offlinemode);
129 __connman_profile_save_default();
130 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
133 dbus_message_iter_get_basic(&value, &str);
135 return __connman_error_not_supported(msg);
137 return __connman_error_invalid_property(msg);
139 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
142 static DBusMessage *get_state(DBusConnection *conn,
143 DBusMessage *msg, void *data)
147 DBG("conn %p", conn);
149 if (__connman_security_check_privilege(msg,
150 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
151 return __connman_error_permission_denied(msg);
153 str = __connman_notifier_get_state();
155 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
159 static DBusMessage *create_profile(DBusConnection *conn,
160 DBusMessage *msg, void *data)
162 const char *name, *path;
165 DBG("conn %p", conn);
167 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
170 if (__connman_security_check_privilege(msg,
171 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
172 return __connman_error_permission_denied(msg);
174 err = __connman_profile_create(name, &path);
176 return __connman_error_failed(msg, -err);
178 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
182 static DBusMessage *remove_profile(DBusConnection *conn,
183 DBusMessage *msg, void *data)
188 DBG("conn %p", conn);
190 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
193 if (__connman_security_check_privilege(msg,
194 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
195 return __connman_error_permission_denied(msg);
197 err = __connman_profile_remove(path);
199 return __connman_error_failed(msg, -err);
201 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
204 static DBusMessage *remove_provider(DBusConnection *conn,
205 DBusMessage *msg, void *data)
210 DBG("conn %p", conn);
212 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &path,
215 if (__connman_security_check_privilege(msg,
216 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
217 return __connman_error_permission_denied(msg);
219 err = __connman_provider_remove(path);
221 return __connman_error_failed(msg, -err);
223 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
226 static DBusMessage *request_scan(DBusConnection *conn,
227 DBusMessage *msg, void *data)
229 enum connman_service_type type;
233 DBG("conn %p", conn);
235 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
238 if (g_strcmp0(str, "") == 0)
239 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
240 else if (g_strcmp0(str, "wifi") == 0)
241 type = CONNMAN_SERVICE_TYPE_WIFI;
242 else if (g_strcmp0(str, "wimax") == 0)
243 type = CONNMAN_SERVICE_TYPE_WIMAX;
245 return __connman_error_invalid_arguments(msg);
247 err = __connman_element_request_scan(type);
249 if (err == -EINPROGRESS) {
250 connman_error("Invalid return code from scan");
254 return __connman_error_failed(msg, -err);
257 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
260 static DBusConnection *connection = NULL;
262 static enum connman_service_type technology_type;
263 static connman_bool_t technology_enabled;
264 static DBusMessage *technology_pending = NULL;
265 static guint technology_timeout = 0;
267 static void technology_reply(int error)
271 if (technology_timeout > 0) {
272 g_source_remove(technology_timeout);
273 technology_timeout = 0;
276 if (technology_pending != NULL) {
280 reply = __connman_error_failed(technology_pending,
283 g_dbus_send_message(connection, reply);
285 g_dbus_send_reply(connection, technology_pending,
288 dbus_message_unref(technology_pending);
289 technology_pending = NULL;
292 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
295 static gboolean technology_abort(gpointer user_data)
299 technology_timeout = 0;
301 technology_reply(ETIMEDOUT);
306 static void technology_notify(enum connman_service_type type,
307 connman_bool_t enabled)
309 DBG("type %d enabled %d", type, enabled);
311 if (type == technology_type && enabled == technology_enabled)
315 static struct connman_notifier technology_notifier = {
317 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
318 .service_enabled= technology_notify,
321 static DBusMessage *enable_technology(DBusConnection *conn,
322 DBusMessage *msg, void *data)
324 enum connman_service_type type;
328 DBG("conn %p", conn);
330 if (technology_pending != NULL)
331 return __connman_error_in_progress(msg);
333 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
336 if (g_strcmp0(str, "ethernet") == 0)
337 type = CONNMAN_SERVICE_TYPE_ETHERNET;
338 else if (g_strcmp0(str, "wifi") == 0)
339 type = CONNMAN_SERVICE_TYPE_WIFI;
340 else if (g_strcmp0(str, "wimax") == 0)
341 type = CONNMAN_SERVICE_TYPE_WIMAX;
342 else if (g_strcmp0(str, "bluetooth") == 0)
343 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
344 else if (g_strcmp0(str, "cellular") == 0)
345 type = CONNMAN_SERVICE_TYPE_CELLULAR;
347 return __connman_error_invalid_arguments(msg);
349 if (__connman_notifier_is_enabled(type) == TRUE)
350 return __connman_error_already_enabled(msg);
352 technology_type = type;
353 technology_enabled = TRUE;
354 technology_pending = dbus_message_ref(msg);
356 err = __connman_element_enable_technology(type);
357 if (err < 0 && err != -EINPROGRESS)
358 technology_reply(-err);
360 technology_timeout = g_timeout_add_seconds(15,
361 technology_abort, NULL);
366 static DBusMessage *disable_technology(DBusConnection *conn,
367 DBusMessage *msg, void *data)
369 enum connman_service_type type;
373 DBG("conn %p", conn);
375 if (technology_pending != NULL)
376 return __connman_error_in_progress(msg);
378 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
381 if (g_strcmp0(str, "ethernet") == 0)
382 type = CONNMAN_SERVICE_TYPE_ETHERNET;
383 else if (g_strcmp0(str, "wifi") == 0)
384 type = CONNMAN_SERVICE_TYPE_WIFI;
385 else if (g_strcmp0(str, "wimax") == 0)
386 type = CONNMAN_SERVICE_TYPE_WIMAX;
387 else if (g_strcmp0(str, "bluetooth") == 0)
388 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
389 else if (g_strcmp0(str, "cellular") == 0)
390 type = CONNMAN_SERVICE_TYPE_CELLULAR;
392 return __connman_error_invalid_arguments(msg);
394 if (__connman_notifier_is_enabled(type) == FALSE)
395 return __connman_error_already_disabled(msg);
397 technology_type = type;
398 technology_enabled = FALSE;
399 technology_pending = dbus_message_ref(msg);
401 err = __connman_element_disable_technology(type);
402 if (err < 0 && err != -EINPROGRESS)
403 technology_reply(-err);
405 technology_timeout = g_timeout_add_seconds(10,
406 technology_abort, NULL);
411 static DBusMessage *connect_service(DBusConnection *conn,
412 DBusMessage *msg, void *data)
416 DBG("conn %p", conn);
418 if (__connman_security_check_privilege(msg,
419 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
420 return __connman_error_permission_denied(msg);
422 err = __connman_service_create_and_connect(msg);
424 if (err == -EINPROGRESS) {
425 connman_error("Invalid return code from connect");
429 return __connman_error_failed(msg, -err);
436 static DBusMessage *connect_provider(DBusConnection *conn,
437 DBusMessage *msg, void *data)
441 DBG("conn %p", conn);
443 if (__connman_security_check_privilege(msg,
444 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
445 return __connman_error_permission_denied(msg);
447 err = __connman_provider_create_and_connect(msg);
449 if (err == -EINPROGRESS) {
450 connman_error("Invalid return code from connect");
454 return __connman_error_failed(msg, -err);
460 static DBusMessage *register_agent(DBusConnection *conn,
461 DBusMessage *msg, void *data)
463 const char *sender, *path;
466 DBG("conn %p", conn);
468 sender = dbus_message_get_sender(msg);
470 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
473 err = __connman_agent_register(sender, path);
475 return __connman_error_failed(msg, -err);
477 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
480 static DBusMessage *unregister_agent(DBusConnection *conn,
481 DBusMessage *msg, void *data)
483 const char *sender, *path;
486 DBG("conn %p", conn);
488 sender = dbus_message_get_sender(msg);
490 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
493 err = __connman_agent_unregister(sender, path);
495 return __connman_error_failed(msg, -err);
497 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
500 static DBusMessage *register_counter(DBusConnection *conn,
501 DBusMessage *msg, void *data)
503 const char *sender, *path;
504 unsigned int interval;
507 DBG("conn %p", conn);
509 sender = dbus_message_get_sender(msg);
511 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
512 DBUS_TYPE_UINT32, &interval,
515 err = __connman_counter_register(sender, path, interval);
517 return __connman_error_failed(msg, -err);
519 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
522 static DBusMessage *unregister_counter(DBusConnection *conn,
523 DBusMessage *msg, void *data)
525 const char *sender, *path;
528 DBG("conn %p", conn);
530 sender = dbus_message_get_sender(msg);
532 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
535 err = __connman_counter_unregister(sender, path);
537 return __connman_error_failed(msg, -err);
539 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
542 static GDBusMethodTable manager_methods[] = {
543 { "GetProperties", "", "a{sv}", get_properties },
544 { "SetProperty", "sv", "", set_property },
545 { "GetState", "", "s", get_state },
546 { "CreateProfile", "s", "o", create_profile },
547 { "RemoveProfile", "o", "", remove_profile },
548 { "RemoveProvider", "s", "", remove_provider },
549 { "RequestScan", "s", "", request_scan },
550 { "EnableTechnology", "s", "", enable_technology,
551 G_DBUS_METHOD_FLAG_ASYNC },
552 { "DisableTechnology", "s", "", disable_technology,
553 G_DBUS_METHOD_FLAG_ASYNC },
554 { "ConnectService", "a{sv}", "o", connect_service,
555 G_DBUS_METHOD_FLAG_ASYNC },
556 { "ConnectProvider", "a{sv}", "o", connect_provider,
557 G_DBUS_METHOD_FLAG_ASYNC },
558 { "RegisterAgent", "o", "", register_agent },
559 { "UnregisterAgent", "o", "", unregister_agent },
560 { "RegisterCounter", "ou", "", register_counter },
561 { "UnregisterCounter", "o", "", unregister_counter },
565 static GDBusSignalTable manager_signals[] = {
566 { "PropertyChanged", "sv" },
567 { "StateChanged", "s" },
571 static DBusMessage *nm_sleep(DBusConnection *conn,
572 DBusMessage *msg, void *data)
576 DBG("conn %p", conn);
578 reply = dbus_message_new_method_return(msg);
582 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
587 static DBusMessage *nm_wake(DBusConnection *conn,
588 DBusMessage *msg, void *data)
592 DBG("conn %p", conn);
594 reply = dbus_message_new_method_return(msg);
598 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
604 NM_STATE_UNKNOWN = 0,
608 NM_STATE_DISCONNECTED
611 static DBusMessage *nm_state(DBusConnection *conn,
612 DBusMessage *msg, void *data)
617 DBG("conn %p", conn);
619 reply = dbus_message_new_method_return(msg);
623 if (__connman_notifier_count_connected() > 0)
624 state = NM_STATE_CONNECTED;
626 state = NM_STATE_DISCONNECTED;
628 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
634 static GDBusMethodTable nm_methods[] = {
635 { "sleep", "", "", nm_sleep },
636 { "wake", "", "", nm_wake },
637 { "state", "", "u", nm_state },
641 static gboolean nm_compat = FALSE;
643 int __connman_manager_init(gboolean compat)
647 connection = connman_dbus_get_connection();
648 if (connection == NULL)
651 if (connman_notifier_register(&technology_notifier) < 0)
652 connman_error("Failed to register technology notifier");
654 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
655 CONNMAN_MANAGER_INTERFACE,
657 manager_signals, NULL, NULL, NULL);
659 if (compat == TRUE) {
660 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
661 nm_methods, NULL, NULL, NULL, NULL);
669 void __connman_manager_cleanup(void)
673 connman_notifier_unregister(&technology_notifier);
675 if (connection == NULL)
678 if (nm_compat == TRUE) {
679 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
682 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
683 CONNMAN_MANAGER_INTERFACE);
685 dbus_connection_unref(connection);