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);
63 connman_dbus_dict_append_array(&dict, "Technologies",
64 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
66 str = __connman_notifier_get_state();
67 connman_dbus_dict_append_basic(&dict, "State",
68 DBUS_TYPE_STRING, &str);
70 offlinemode = __connman_profile_get_offlinemode();
71 connman_dbus_dict_append_basic(&dict, "OfflineMode",
72 DBUS_TYPE_BOOLEAN, &offlinemode);
74 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
75 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
76 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
77 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
78 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
79 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
81 str = __connman_service_default();
83 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
84 DBUS_TYPE_STRING, &str);
86 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
87 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
88 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
89 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
91 connman_dbus_dict_close(&array, &dict);
96 static DBusMessage *set_property(DBusConnection *conn,
97 DBusMessage *msg, void *data)
99 DBusMessageIter iter, value;
103 DBG("conn %p", conn);
105 if (dbus_message_iter_init(msg, &iter) == FALSE)
106 return __connman_error_invalid_arguments(msg);
108 dbus_message_iter_get_basic(&iter, &name);
109 dbus_message_iter_next(&iter);
110 dbus_message_iter_recurse(&iter, &value);
112 if (__connman_security_check_privilege(msg,
113 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
114 return __connman_error_permission_denied(msg);
116 type = dbus_message_iter_get_arg_type(&value);
118 if (g_str_equal(name, "OfflineMode") == TRUE) {
119 connman_bool_t offlinemode;
121 if (type != DBUS_TYPE_BOOLEAN)
122 return __connman_error_invalid_arguments(msg);
124 dbus_message_iter_get_basic(&value, &offlinemode);
126 __connman_profile_set_offlinemode(offlinemode);
128 __connman_profile_save_default();
129 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
132 dbus_message_iter_get_basic(&value, &str);
134 return __connman_error_not_supported(msg);
136 return __connman_error_invalid_property(msg);
138 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
141 static DBusMessage *get_state(DBusConnection *conn,
142 DBusMessage *msg, void *data)
146 DBG("conn %p", conn);
148 if (__connman_security_check_privilege(msg,
149 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
150 return __connman_error_permission_denied(msg);
152 str = __connman_notifier_get_state();
154 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
158 static DBusMessage *create_profile(DBusConnection *conn,
159 DBusMessage *msg, void *data)
161 const char *name, *path;
164 DBG("conn %p", conn);
166 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
169 if (__connman_security_check_privilege(msg,
170 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
171 return __connman_error_permission_denied(msg);
173 err = __connman_profile_create(name, &path);
175 return __connman_error_failed(msg, -err);
177 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
181 static DBusMessage *remove_profile(DBusConnection *conn,
182 DBusMessage *msg, void *data)
187 DBG("conn %p", conn);
189 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
192 if (__connman_security_check_privilege(msg,
193 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
194 return __connman_error_permission_denied(msg);
196 err = __connman_profile_remove(path);
198 return __connman_error_failed(msg, -err);
200 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
203 static DBusMessage *remove_provider(DBusConnection *conn,
204 DBusMessage *msg, void *data)
209 DBG("conn %p", conn);
211 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &path,
214 if (__connman_security_check_privilege(msg,
215 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
216 return __connman_error_permission_denied(msg);
218 err = __connman_provider_remove(path);
220 return __connman_error_failed(msg, -err);
222 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
225 static DBusMessage *request_scan(DBusConnection *conn,
226 DBusMessage *msg, void *data)
228 enum connman_service_type type;
232 DBG("conn %p", conn);
234 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
237 if (g_strcmp0(str, "") == 0)
238 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
239 else if (g_strcmp0(str, "wifi") == 0)
240 type = CONNMAN_SERVICE_TYPE_WIFI;
241 else if (g_strcmp0(str, "wimax") == 0)
242 type = CONNMAN_SERVICE_TYPE_WIMAX;
244 return __connman_error_invalid_arguments(msg);
246 err = __connman_element_request_scan(type);
248 if (err == -EINPROGRESS) {
249 connman_error("Invalid return code from scan");
253 return __connman_error_failed(msg, -err);
256 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
259 static DBusConnection *connection = NULL;
261 static enum connman_service_type technology_type;
262 static connman_bool_t technology_enabled;
263 static DBusMessage *technology_pending = NULL;
264 static guint technology_timeout = 0;
266 static void technology_reply(int error)
270 if (technology_timeout > 0) {
271 g_source_remove(technology_timeout);
272 technology_timeout = 0;
275 if (technology_pending != NULL) {
279 reply = __connman_error_failed(technology_pending,
282 g_dbus_send_message(connection, reply);
284 g_dbus_send_reply(connection, technology_pending,
287 dbus_message_unref(technology_pending);
288 technology_pending = NULL;
291 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
294 static gboolean technology_abort(gpointer user_data)
298 technology_timeout = 0;
300 technology_reply(ETIMEDOUT);
305 static void technology_notify(enum connman_service_type type,
306 connman_bool_t enabled)
308 DBG("type %d enabled %d", type, enabled);
310 if (type == technology_type && enabled == technology_enabled)
314 static struct connman_notifier technology_notifier = {
316 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
317 .service_enabled= technology_notify,
320 static DBusMessage *enable_technology(DBusConnection *conn,
321 DBusMessage *msg, void *data)
323 enum connman_service_type type;
327 DBG("conn %p", conn);
329 if (technology_pending != NULL)
330 return __connman_error_in_progress(msg);
332 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
335 if (g_strcmp0(str, "ethernet") == 0)
336 type = CONNMAN_SERVICE_TYPE_ETHERNET;
337 else if (g_strcmp0(str, "wifi") == 0)
338 type = CONNMAN_SERVICE_TYPE_WIFI;
339 else if (g_strcmp0(str, "wimax") == 0)
340 type = CONNMAN_SERVICE_TYPE_WIMAX;
341 else if (g_strcmp0(str, "bluetooth") == 0)
342 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
343 else if (g_strcmp0(str, "cellular") == 0)
344 type = CONNMAN_SERVICE_TYPE_CELLULAR;
346 return __connman_error_invalid_arguments(msg);
348 if (__connman_notifier_is_registered(type) == FALSE)
349 return __connman_error_not_registered(msg);
351 if (__connman_notifier_is_enabled(type) == TRUE)
352 return __connman_error_already_enabled(msg);
354 technology_type = type;
355 technology_enabled = TRUE;
356 technology_pending = dbus_message_ref(msg);
358 err = __connman_element_enable_technology(type);
359 if (err < 0 && err != -EINPROGRESS)
360 technology_reply(-err);
362 technology_timeout = g_timeout_add_seconds(15,
363 technology_abort, NULL);
368 static DBusMessage *disable_technology(DBusConnection *conn,
369 DBusMessage *msg, void *data)
371 enum connman_service_type type;
375 DBG("conn %p", conn);
377 if (technology_pending != NULL)
378 return __connman_error_in_progress(msg);
380 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
383 if (g_strcmp0(str, "ethernet") == 0)
384 type = CONNMAN_SERVICE_TYPE_ETHERNET;
385 else if (g_strcmp0(str, "wifi") == 0)
386 type = CONNMAN_SERVICE_TYPE_WIFI;
387 else if (g_strcmp0(str, "wimax") == 0)
388 type = CONNMAN_SERVICE_TYPE_WIMAX;
389 else if (g_strcmp0(str, "bluetooth") == 0)
390 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
391 else if (g_strcmp0(str, "cellular") == 0)
392 type = CONNMAN_SERVICE_TYPE_CELLULAR;
394 return __connman_error_invalid_arguments(msg);
396 if (__connman_notifier_is_registered(type) == FALSE)
397 return __connman_error_not_registered(msg);
399 if (__connman_notifier_is_enabled(type) == FALSE)
400 return __connman_error_already_disabled(msg);
402 technology_type = type;
403 technology_enabled = FALSE;
404 technology_pending = dbus_message_ref(msg);
406 err = __connman_element_disable_technology(type);
407 if (err < 0 && err != -EINPROGRESS)
408 technology_reply(-err);
410 technology_timeout = g_timeout_add_seconds(10,
411 technology_abort, NULL);
416 static DBusMessage *connect_service(DBusConnection *conn,
417 DBusMessage *msg, void *data)
421 DBG("conn %p", conn);
423 if (__connman_security_check_privilege(msg,
424 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
425 return __connman_error_permission_denied(msg);
427 err = __connman_service_create_and_connect(msg);
429 if (err == -EINPROGRESS) {
430 connman_error("Invalid return code from connect");
434 return __connman_error_failed(msg, -err);
441 static DBusMessage *connect_provider(DBusConnection *conn,
442 DBusMessage *msg, void *data)
446 DBG("conn %p", conn);
448 if (__connman_security_check_privilege(msg,
449 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
450 return __connman_error_permission_denied(msg);
452 err = __connman_provider_create_and_connect(msg);
454 if (err == -EINPROGRESS) {
455 connman_error("Invalid return code from connect");
459 return __connman_error_failed(msg, -err);
465 static DBusMessage *register_agent(DBusConnection *conn,
466 DBusMessage *msg, void *data)
468 const char *sender, *path;
471 DBG("conn %p", conn);
473 sender = dbus_message_get_sender(msg);
475 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
478 err = __connman_agent_register(sender, path);
480 return __connman_error_failed(msg, -err);
482 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
485 static DBusMessage *unregister_agent(DBusConnection *conn,
486 DBusMessage *msg, void *data)
488 const char *sender, *path;
491 DBG("conn %p", conn);
493 sender = dbus_message_get_sender(msg);
495 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
498 err = __connman_agent_unregister(sender, path);
500 return __connman_error_failed(msg, -err);
502 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
505 static DBusMessage *register_counter(DBusConnection *conn,
506 DBusMessage *msg, void *data)
508 const char *sender, *path;
509 unsigned int interval;
512 DBG("conn %p", conn);
514 sender = dbus_message_get_sender(msg);
516 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
517 DBUS_TYPE_UINT32, &interval,
520 err = __connman_counter_register(sender, path, interval);
522 return __connman_error_failed(msg, -err);
524 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
527 static DBusMessage *unregister_counter(DBusConnection *conn,
528 DBusMessage *msg, void *data)
530 const char *sender, *path;
533 DBG("conn %p", conn);
535 sender = dbus_message_get_sender(msg);
537 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
540 err = __connman_counter_unregister(sender, path);
542 return __connman_error_failed(msg, -err);
544 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
547 static DBusMessage *request_session(DBusConnection *conn,
548 DBusMessage *msg, void *data)
550 const char *bearer, *sender, *service_path;
551 struct connman_service *service;
553 DBG("conn %p", conn);
555 sender = dbus_message_get_sender(msg);
557 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &bearer,
560 service = __connman_session_request(bearer, sender);
562 return __connman_error_failed(msg, EINVAL);
564 service_path = __connman_service_get_path(service);
566 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &service_path,
570 static DBusMessage *release_session(DBusConnection *conn,
571 DBusMessage *msg, void *data)
576 DBG("conn %p", conn);
578 sender = dbus_message_get_sender(msg);
580 err = __connman_session_release(sender);
582 return __connman_error_failed(msg, -err);
584 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
587 static GDBusMethodTable manager_methods[] = {
588 { "GetProperties", "", "a{sv}", get_properties },
589 { "SetProperty", "sv", "", set_property },
590 { "GetState", "", "s", get_state },
591 { "CreateProfile", "s", "o", create_profile },
592 { "RemoveProfile", "o", "", remove_profile },
593 { "RemoveProvider", "s", "", remove_provider },
594 { "RequestScan", "s", "", request_scan },
595 { "EnableTechnology", "s", "", enable_technology,
596 G_DBUS_METHOD_FLAG_ASYNC },
597 { "DisableTechnology", "s", "", disable_technology,
598 G_DBUS_METHOD_FLAG_ASYNC },
599 { "ConnectService", "a{sv}", "o", connect_service,
600 G_DBUS_METHOD_FLAG_ASYNC },
601 { "ConnectProvider", "a{sv}", "o", connect_provider,
602 G_DBUS_METHOD_FLAG_ASYNC },
603 { "RegisterAgent", "o", "", register_agent },
604 { "UnregisterAgent", "o", "", unregister_agent },
605 { "RegisterCounter", "ou", "", register_counter },
606 { "UnregisterCounter", "o", "", unregister_counter },
607 { "RequestSession", "s", "o", request_session },
608 { "ReleaseSession", "s", "", release_session },
612 static GDBusSignalTable manager_signals[] = {
613 { "PropertyChanged", "sv" },
614 { "StateChanged", "s" },
618 static DBusMessage *nm_sleep(DBusConnection *conn,
619 DBusMessage *msg, void *data)
623 DBG("conn %p", conn);
625 reply = dbus_message_new_method_return(msg);
629 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
634 static DBusMessage *nm_wake(DBusConnection *conn,
635 DBusMessage *msg, void *data)
639 DBG("conn %p", conn);
641 reply = dbus_message_new_method_return(msg);
645 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
651 NM_STATE_UNKNOWN = 0,
655 NM_STATE_DISCONNECTED
658 static DBusMessage *nm_state(DBusConnection *conn,
659 DBusMessage *msg, void *data)
664 DBG("conn %p", conn);
666 reply = dbus_message_new_method_return(msg);
670 if (__connman_notifier_count_connected() > 0)
671 state = NM_STATE_CONNECTED;
673 state = NM_STATE_DISCONNECTED;
675 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
681 static GDBusMethodTable nm_methods[] = {
682 { "sleep", "", "", nm_sleep },
683 { "wake", "", "", nm_wake },
684 { "state", "", "u", nm_state },
688 static gboolean nm_compat = FALSE;
690 int __connman_manager_init(gboolean compat)
694 connection = connman_dbus_get_connection();
695 if (connection == NULL)
698 if (connman_notifier_register(&technology_notifier) < 0)
699 connman_error("Failed to register technology notifier");
701 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
702 CONNMAN_MANAGER_INTERFACE,
704 manager_signals, NULL, NULL, NULL);
706 if (compat == TRUE) {
707 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
708 nm_methods, NULL, NULL, NULL, NULL);
716 void __connman_manager_cleanup(void)
720 connman_notifier_unregister(&technology_notifier);
722 if (connection == NULL)
725 if (nm_compat == TRUE) {
726 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
729 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
730 CONNMAN_MANAGER_INTERFACE);
732 dbus_connection_unref(connection);