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 if (__connman_element_count(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION) > 0)
72 connman_dbus_dict_append_basic(&dict, "State",
73 DBUS_TYPE_STRING, &str);
75 offlinemode = __connman_profile_get_offlinemode();
76 connman_dbus_dict_append_basic(&dict, "OfflineMode",
77 DBUS_TYPE_BOOLEAN, &offlinemode);
79 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
80 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
81 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
82 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
83 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
84 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
86 str = __connman_service_default();
88 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
89 DBUS_TYPE_STRING, &str);
91 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
92 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
93 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
94 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
96 connman_dbus_dict_close(&array, &dict);
101 static DBusMessage *set_property(DBusConnection *conn,
102 DBusMessage *msg, void *data)
104 DBusMessageIter iter, value;
108 DBG("conn %p", conn);
110 if (dbus_message_iter_init(msg, &iter) == FALSE)
111 return __connman_error_invalid_arguments(msg);
113 dbus_message_iter_get_basic(&iter, &name);
114 dbus_message_iter_next(&iter);
115 dbus_message_iter_recurse(&iter, &value);
117 if (__connman_security_check_privilege(msg,
118 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
119 return __connman_error_permission_denied(msg);
121 type = dbus_message_iter_get_arg_type(&value);
123 if (g_str_equal(name, "OfflineMode") == TRUE) {
124 connman_bool_t offlinemode;
126 if (type != DBUS_TYPE_BOOLEAN)
127 return __connman_error_invalid_arguments(msg);
129 dbus_message_iter_get_basic(&value, &offlinemode);
131 __connman_profile_set_offlinemode(offlinemode);
133 __connman_profile_save_default();
134 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
137 dbus_message_iter_get_basic(&value, &str);
139 return __connman_error_not_supported(msg);
141 return __connman_error_invalid_property(msg);
143 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
146 static DBusMessage *get_state(DBusConnection *conn,
147 DBusMessage *msg, void *data)
151 DBG("conn %p", conn);
153 if (__connman_security_check_privilege(msg,
154 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
155 return __connman_error_permission_denied(msg);
157 if (__connman_element_count(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION) > 0)
162 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
166 static DBusMessage *create_profile(DBusConnection *conn,
167 DBusMessage *msg, void *data)
169 const char *name, *path;
172 DBG("conn %p", conn);
174 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
177 if (__connman_security_check_privilege(msg,
178 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
179 return __connman_error_permission_denied(msg);
181 err = __connman_profile_create(name, &path);
183 return __connman_error_failed(msg, -err);
185 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
189 static DBusMessage *remove_profile(DBusConnection *conn,
190 DBusMessage *msg, void *data)
195 DBG("conn %p", conn);
197 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
200 if (__connman_security_check_privilege(msg,
201 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
202 return __connman_error_permission_denied(msg);
204 err = __connman_profile_remove(path);
206 return __connman_error_failed(msg, -err);
208 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
211 static DBusMessage *remove_provider(DBusConnection *conn,
212 DBusMessage *msg, void *data)
217 DBG("conn %p", conn);
219 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &path,
222 if (__connman_security_check_privilege(msg,
223 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
224 return __connman_error_permission_denied(msg);
226 err = __connman_provider_remove(path);
228 return __connman_error_failed(msg, -err);
230 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
233 static DBusMessage *request_scan(DBusConnection *conn,
234 DBusMessage *msg, void *data)
236 enum connman_service_type type;
240 DBG("conn %p", conn);
242 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
245 if (g_strcmp0(str, "") == 0)
246 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
247 else if (g_strcmp0(str, "wifi") == 0)
248 type = CONNMAN_SERVICE_TYPE_WIFI;
249 else if (g_strcmp0(str, "wimax") == 0)
250 type = CONNMAN_SERVICE_TYPE_WIMAX;
252 return __connman_error_invalid_arguments(msg);
254 err = __connman_element_request_scan(type);
256 if (err == -EINPROGRESS) {
257 connman_error("Invalid return code from scan");
261 return __connman_error_failed(msg, -err);
264 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
267 static DBusConnection *connection = NULL;
269 static enum connman_service_type technology_type;
270 static connman_bool_t technology_enabled;
271 static DBusMessage *technology_pending = NULL;
272 static guint technology_timeout = 0;
274 static void technology_reply(int error)
278 if (technology_timeout > 0) {
279 g_source_remove(technology_timeout);
280 technology_timeout = 0;
283 if (technology_pending != NULL) {
287 reply = __connman_error_failed(technology_pending,
290 g_dbus_send_message(connection, reply);
292 g_dbus_send_reply(connection, technology_pending,
295 dbus_message_unref(technology_pending);
296 technology_pending = NULL;
299 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
302 static gboolean technology_abort(gpointer user_data)
306 technology_timeout = 0;
308 technology_reply(ETIMEDOUT);
313 static void technology_notify(enum connman_service_type type,
314 connman_bool_t enabled)
316 DBG("type %d enabled %d", type, enabled);
318 if (type == technology_type && enabled == technology_enabled)
322 static struct connman_notifier technology_notifier = {
324 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
325 .service_enabled= technology_notify,
328 static DBusMessage *enable_technology(DBusConnection *conn,
329 DBusMessage *msg, void *data)
331 enum connman_service_type type;
335 DBG("conn %p", conn);
337 if (technology_pending != NULL)
338 return __connman_error_in_progress(msg);
340 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
343 if (g_strcmp0(str, "ethernet") == 0)
344 type = CONNMAN_SERVICE_TYPE_ETHERNET;
345 else if (g_strcmp0(str, "wifi") == 0)
346 type = CONNMAN_SERVICE_TYPE_WIFI;
347 else if (g_strcmp0(str, "wimax") == 0)
348 type = CONNMAN_SERVICE_TYPE_WIMAX;
349 else if (g_strcmp0(str, "bluetooth") == 0)
350 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
351 else if (g_strcmp0(str, "cellular") == 0)
352 type = CONNMAN_SERVICE_TYPE_CELLULAR;
354 return __connman_error_invalid_arguments(msg);
356 if (__connman_notifier_is_enabled(type) == TRUE)
357 return __connman_error_already_enabled(msg);
359 technology_type = type;
360 technology_enabled = TRUE;
361 technology_pending = dbus_message_ref(msg);
363 err = __connman_element_enable_technology(type);
364 if (err < 0 && err != -EINPROGRESS)
365 technology_reply(-err);
367 technology_timeout = g_timeout_add_seconds(15,
368 technology_abort, NULL);
373 static DBusMessage *disable_technology(DBusConnection *conn,
374 DBusMessage *msg, void *data)
376 enum connman_service_type type;
380 DBG("conn %p", conn);
382 if (technology_pending != NULL)
383 return __connman_error_in_progress(msg);
385 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
388 if (g_strcmp0(str, "ethernet") == 0)
389 type = CONNMAN_SERVICE_TYPE_ETHERNET;
390 else if (g_strcmp0(str, "wifi") == 0)
391 type = CONNMAN_SERVICE_TYPE_WIFI;
392 else if (g_strcmp0(str, "wimax") == 0)
393 type = CONNMAN_SERVICE_TYPE_WIMAX;
394 else if (g_strcmp0(str, "bluetooth") == 0)
395 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
396 else if (g_strcmp0(str, "cellular") == 0)
397 type = CONNMAN_SERVICE_TYPE_CELLULAR;
399 return __connman_error_invalid_arguments(msg);
401 if (__connman_notifier_is_enabled(type) == FALSE)
402 return __connman_error_already_disabled(msg);
404 technology_type = type;
405 technology_enabled = FALSE;
406 technology_pending = dbus_message_ref(msg);
408 err = __connman_element_disable_technology(type);
409 if (err < 0 && err != -EINPROGRESS)
410 technology_reply(-err);
412 technology_timeout = g_timeout_add_seconds(10,
413 technology_abort, NULL);
418 static DBusMessage *connect_service(DBusConnection *conn,
419 DBusMessage *msg, void *data)
423 DBG("conn %p", conn);
425 if (__connman_security_check_privilege(msg,
426 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
427 return __connman_error_permission_denied(msg);
429 err = __connman_service_create_and_connect(msg);
431 if (err == -EINPROGRESS) {
432 connman_error("Invalid return code from connect");
436 return __connman_error_failed(msg, -err);
443 static DBusMessage *connect_provider(DBusConnection *conn,
444 DBusMessage *msg, void *data)
448 DBG("conn %p", conn);
450 if (__connman_security_check_privilege(msg,
451 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
452 return __connman_error_permission_denied(msg);
454 err = __connman_provider_create_and_connect(msg);
456 if (err == -EINPROGRESS) {
457 connman_error("Invalid return code from connect");
461 return __connman_error_failed(msg, -err);
467 static DBusMessage *register_agent(DBusConnection *conn,
468 DBusMessage *msg, void *data)
470 const char *sender, *path;
473 DBG("conn %p", conn);
475 sender = dbus_message_get_sender(msg);
477 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
480 err = __connman_agent_register(sender, path);
482 return __connman_error_failed(msg, -err);
484 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
487 static DBusMessage *unregister_agent(DBusConnection *conn,
488 DBusMessage *msg, void *data)
490 const char *sender, *path;
493 DBG("conn %p", conn);
495 sender = dbus_message_get_sender(msg);
497 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
500 err = __connman_agent_unregister(sender, path);
502 return __connman_error_failed(msg, -err);
504 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
507 static DBusMessage *register_counter(DBusConnection *conn,
508 DBusMessage *msg, void *data)
510 const char *sender, *path;
511 unsigned int interval;
514 DBG("conn %p", conn);
516 sender = dbus_message_get_sender(msg);
518 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
519 DBUS_TYPE_UINT32, &interval,
522 err = __connman_counter_register(sender, path, interval);
524 return __connman_error_failed(msg, -err);
526 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
529 static DBusMessage *unregister_counter(DBusConnection *conn,
530 DBusMessage *msg, void *data)
532 const char *sender, *path;
535 DBG("conn %p", conn);
537 sender = dbus_message_get_sender(msg);
539 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
542 err = __connman_counter_unregister(sender, path);
544 return __connman_error_failed(msg, -err);
546 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
549 static GDBusMethodTable manager_methods[] = {
550 { "GetProperties", "", "a{sv}", get_properties },
551 { "SetProperty", "sv", "", set_property },
552 { "GetState", "", "s", get_state },
553 { "CreateProfile", "s", "o", create_profile },
554 { "RemoveProfile", "o", "", remove_profile },
555 { "RemoveProvider", "s", "", remove_provider },
556 { "RequestScan", "s", "", request_scan },
557 { "EnableTechnology", "s", "", enable_technology,
558 G_DBUS_METHOD_FLAG_ASYNC },
559 { "DisableTechnology", "s", "", disable_technology,
560 G_DBUS_METHOD_FLAG_ASYNC },
561 { "ConnectService", "a{sv}", "o", connect_service,
562 G_DBUS_METHOD_FLAG_ASYNC },
563 { "ConnectProvider", "a{sv}", "o", connect_provider,
564 G_DBUS_METHOD_FLAG_ASYNC },
565 { "RegisterAgent", "o", "", register_agent },
566 { "UnregisterAgent", "o", "", unregister_agent },
567 { "RegisterCounter", "ou", "", register_counter },
568 { "UnregisterCounter", "o", "", unregister_counter },
572 static GDBusSignalTable manager_signals[] = {
573 { "PropertyChanged", "sv" },
574 { "StateChanged", "s" },
578 static DBusMessage *nm_sleep(DBusConnection *conn,
579 DBusMessage *msg, void *data)
583 DBG("conn %p", conn);
585 reply = dbus_message_new_method_return(msg);
589 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
594 static DBusMessage *nm_wake(DBusConnection *conn,
595 DBusMessage *msg, void *data)
599 DBG("conn %p", conn);
601 reply = dbus_message_new_method_return(msg);
605 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
611 NM_STATE_UNKNOWN = 0,
615 NM_STATE_DISCONNECTED
618 static DBusMessage *nm_state(DBusConnection *conn,
619 DBusMessage *msg, void *data)
624 DBG("conn %p", conn);
626 reply = dbus_message_new_method_return(msg);
630 if (__connman_element_count(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION) > 0)
631 state = NM_STATE_CONNECTED;
633 state = NM_STATE_DISCONNECTED;
635 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
641 static GDBusMethodTable nm_methods[] = {
642 { "sleep", "", "", nm_sleep },
643 { "wake", "", "", nm_wake },
644 { "state", "", "u", nm_state },
648 static gboolean nm_compat = FALSE;
650 int __connman_manager_init(gboolean compat)
654 connection = connman_dbus_get_connection();
655 if (connection == NULL)
658 if (connman_notifier_register(&technology_notifier) < 0)
659 connman_error("Failed to register technology notifier");
661 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
662 CONNMAN_MANAGER_INTERFACE,
664 manager_signals, NULL, NULL, NULL);
666 if (compat == TRUE) {
667 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
668 nm_methods, NULL, NULL, NULL, NULL);
676 void __connman_manager_cleanup(void)
680 connman_notifier_unregister(&technology_notifier);
682 if (connection == NULL)
685 if (nm_compat == TRUE) {
686 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
689 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
690 CONNMAN_MANAGER_INTERFACE);
692 dbus_connection_unref(connection);