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 str = __connman_profile_active_path();
55 connman_dbus_dict_append_basic(&dict, "ActiveProfile",
56 DBUS_TYPE_OBJECT_PATH, &str);
58 connman_dbus_dict_append_array(&dict, "Services",
59 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
60 connman_dbus_dict_append_array(&dict, "Technologies",
61 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
63 str = __connman_notifier_get_state();
64 connman_dbus_dict_append_basic(&dict, "State",
65 DBUS_TYPE_STRING, &str);
67 offlinemode = __connman_profile_get_offlinemode();
68 connman_dbus_dict_append_basic(&dict, "OfflineMode",
69 DBUS_TYPE_BOOLEAN, &offlinemode);
71 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
72 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
73 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
74 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
75 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
76 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
78 str = __connman_service_default();
80 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
81 DBUS_TYPE_STRING, &str);
83 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
84 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
85 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
86 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
88 sessionmode = __connman_session_mode();
89 connman_dbus_dict_append_basic(&dict, "SessionMode",
93 connman_dbus_dict_close(&array, &dict);
98 static DBusMessage *set_property(DBusConnection *conn,
99 DBusMessage *msg, void *data)
101 DBusMessageIter iter, value;
105 DBG("conn %p", conn);
107 if (dbus_message_iter_init(msg, &iter) == FALSE)
108 return __connman_error_invalid_arguments(msg);
110 dbus_message_iter_get_basic(&iter, &name);
111 dbus_message_iter_next(&iter);
112 dbus_message_iter_recurse(&iter, &value);
114 type = dbus_message_iter_get_arg_type(&value);
116 if (g_str_equal(name, "OfflineMode") == TRUE) {
117 connman_bool_t offlinemode;
119 if (type != DBUS_TYPE_BOOLEAN)
120 return __connman_error_invalid_arguments(msg);
122 dbus_message_iter_get_basic(&value, &offlinemode);
124 __connman_profile_set_offlinemode(offlinemode, TRUE);
126 __connman_profile_save_default();
127 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
130 dbus_message_iter_get_basic(&value, &str);
132 return __connman_error_not_supported(msg);
133 } else if (g_str_equal(name, "SessionMode") == TRUE) {
134 connman_bool_t sessionmode;
136 if (type != DBUS_TYPE_BOOLEAN)
137 return __connman_error_invalid_arguments(msg);
139 dbus_message_iter_get_basic(&value, &sessionmode);
141 if (session_mode_pending != NULL)
142 return __connman_error_in_progress(msg);
144 __connman_session_set_mode(sessionmode);
146 if (connman_state_idle == FALSE) {
147 session_mode_pending = msg;
152 return __connman_error_invalid_property(msg);
154 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
157 static DBusMessage *get_state(DBusConnection *conn,
158 DBusMessage *msg, void *data)
162 DBG("conn %p", conn);
164 str = __connman_notifier_get_state();
166 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
170 static DBusMessage *remove_provider(DBusConnection *conn,
171 DBusMessage *msg, void *data)
176 DBG("conn %p", conn);
178 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
181 err = __connman_provider_remove(path);
183 return __connman_error_failed(msg, -err);
185 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
188 static DBusMessage *request_scan(DBusConnection *conn,
189 DBusMessage *msg, void *data)
191 enum connman_service_type type;
195 DBG("conn %p", conn);
197 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
200 if (g_strcmp0(str, "") == 0)
201 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
202 else if (g_strcmp0(str, "wifi") == 0)
203 type = CONNMAN_SERVICE_TYPE_WIFI;
204 else if (g_strcmp0(str, "wimax") == 0)
205 type = CONNMAN_SERVICE_TYPE_WIMAX;
207 return __connman_error_invalid_arguments(msg);
209 err = __connman_device_request_scan(type);
211 if (err == -EINPROGRESS) {
212 connman_error("Invalid return code from scan");
216 return __connman_error_failed(msg, -err);
219 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
222 static DBusConnection *connection = NULL;
224 static enum connman_service_type technology_type;
225 static connman_bool_t technology_enabled;
226 static DBusMessage *technology_pending = NULL;
227 static guint technology_timeout = 0;
229 static void technology_reply(int error)
233 if (technology_timeout > 0) {
234 g_source_remove(technology_timeout);
235 technology_timeout = 0;
238 if (technology_pending != NULL) {
242 reply = __connman_error_failed(technology_pending,
245 g_dbus_send_message(connection, reply);
247 g_dbus_send_reply(connection, technology_pending,
250 dbus_message_unref(technology_pending);
251 technology_pending = NULL;
254 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
257 static gboolean technology_abort(gpointer user_data)
261 technology_timeout = 0;
263 technology_reply(ETIMEDOUT);
268 static void technology_notify(enum connman_service_type type,
269 connman_bool_t enabled)
271 DBG("type %d enabled %d", type, enabled);
273 if (type == technology_type && enabled == technology_enabled)
277 static void session_mode_notify(void)
281 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
282 g_dbus_send_message(connection, reply);
284 dbus_message_unref(session_mode_pending);
285 session_mode_pending = NULL;
288 static void idle_state(connman_bool_t idle)
291 DBG("idle %d", idle);
293 connman_state_idle = idle;
295 if (connman_state_idle == FALSE || session_mode_pending == NULL)
298 session_mode_notify();
301 static struct connman_notifier technology_notifier = {
303 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
304 .service_enabled= technology_notify,
305 .idle_state = idle_state,
308 static DBusMessage *enable_technology(DBusConnection *conn,
309 DBusMessage *msg, void *data)
311 enum connman_service_type type;
315 DBG("conn %p", conn);
317 if (technology_pending != NULL)
318 return __connman_error_in_progress(msg);
320 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
323 if (g_strcmp0(str, "ethernet") == 0)
324 type = CONNMAN_SERVICE_TYPE_ETHERNET;
325 else if (g_strcmp0(str, "wifi") == 0)
326 type = CONNMAN_SERVICE_TYPE_WIFI;
327 else if (g_strcmp0(str, "wimax") == 0)
328 type = CONNMAN_SERVICE_TYPE_WIMAX;
329 else if (g_strcmp0(str, "bluetooth") == 0)
330 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
331 else if (g_strcmp0(str, "cellular") == 0)
332 type = CONNMAN_SERVICE_TYPE_CELLULAR;
334 return __connman_error_invalid_arguments(msg);
336 if (__connman_notifier_is_registered(type) == FALSE)
337 return __connman_error_not_registered(msg);
339 if (__connman_notifier_is_enabled(type) == TRUE)
340 return __connman_error_already_enabled(msg);
342 technology_type = type;
343 technology_enabled = TRUE;
344 technology_pending = dbus_message_ref(msg);
346 err = __connman_device_enable_technology(type);
347 if (err < 0 && err != -EINPROGRESS)
348 technology_reply(-err);
350 technology_timeout = g_timeout_add_seconds(15,
351 technology_abort, NULL);
356 static DBusMessage *disable_technology(DBusConnection *conn,
357 DBusMessage *msg, void *data)
359 enum connman_service_type type;
363 DBG("conn %p", conn);
365 if (technology_pending != NULL)
366 return __connman_error_in_progress(msg);
368 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
371 if (g_strcmp0(str, "ethernet") == 0)
372 type = CONNMAN_SERVICE_TYPE_ETHERNET;
373 else if (g_strcmp0(str, "wifi") == 0)
374 type = CONNMAN_SERVICE_TYPE_WIFI;
375 else if (g_strcmp0(str, "wimax") == 0)
376 type = CONNMAN_SERVICE_TYPE_WIMAX;
377 else if (g_strcmp0(str, "bluetooth") == 0)
378 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
379 else if (g_strcmp0(str, "cellular") == 0)
380 type = CONNMAN_SERVICE_TYPE_CELLULAR;
382 return __connman_error_invalid_arguments(msg);
384 if (__connman_notifier_is_registered(type) == FALSE)
385 return __connman_error_not_registered(msg);
387 if (__connman_notifier_is_enabled(type) == FALSE)
388 return __connman_error_already_disabled(msg);
390 technology_type = type;
391 technology_enabled = FALSE;
392 technology_pending = dbus_message_ref(msg);
394 err = __connman_device_disable_technology(type);
395 if (err < 0 && err != -EINPROGRESS)
396 technology_reply(-err);
398 technology_timeout = g_timeout_add_seconds(10,
399 technology_abort, NULL);
404 static DBusMessage *get_services(DBusConnection *conn,
405 DBusMessage *msg, void *data)
408 DBusMessageIter iter, array;
410 reply = dbus_message_new_method_return(msg);
414 dbus_message_iter_init_append(reply, &iter);
416 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
417 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
418 DBUS_TYPE_OBJECT_PATH_AS_STRING
419 DBUS_TYPE_ARRAY_AS_STRING
420 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
421 DBUS_TYPE_STRING_AS_STRING
422 DBUS_TYPE_VARIANT_AS_STRING
423 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
424 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
426 __connman_service_list_struct(&array);
428 dbus_message_iter_close_container(&iter, &array);
433 static DBusMessage *lookup_service(DBusConnection *conn,
434 DBusMessage *msg, void *data)
436 const char *pattern, *path;
439 DBG("conn %p", conn);
441 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
444 err = __connman_service_lookup(pattern, &path);
446 return __connman_error_failed(msg, -err);
448 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
452 static DBusMessage *connect_service(DBusConnection *conn,
453 DBusMessage *msg, void *data)
457 DBG("conn %p", conn);
459 if (__connman_session_mode() == TRUE) {
460 connman_info("Session mode enabled: "
461 "direct service connect disabled");
463 return __connman_error_failed(msg, -EINVAL);
466 err = __connman_service_create_and_connect(msg);
468 if (err == -EINPROGRESS) {
469 connman_error("Invalid return code from connect");
473 return __connman_error_failed(msg, -err);
479 static DBusMessage *provision_service(DBusConnection *conn, DBusMessage *msg,
484 DBG("conn %p", conn);
486 err = __connman_service_provision(msg);
488 return __connman_error_failed(msg, -err);
493 static DBusMessage *connect_provider(DBusConnection *conn,
494 DBusMessage *msg, void *data)
498 DBG("conn %p", conn);
500 if (__connman_session_mode() == TRUE) {
501 connman_info("Session mode enabled: "
502 "direct provider connect disabled");
504 return __connman_error_failed(msg, -EINVAL);
507 err = __connman_provider_create_and_connect(msg);
509 if (err == -EINPROGRESS) {
510 connman_error("Invalid return code from connect");
514 return __connman_error_failed(msg, -err);
520 static DBusMessage *register_agent(DBusConnection *conn,
521 DBusMessage *msg, void *data)
523 const char *sender, *path;
526 DBG("conn %p", conn);
528 sender = dbus_message_get_sender(msg);
530 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
533 err = __connman_agent_register(sender, path);
535 return __connman_error_failed(msg, -err);
537 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
540 static DBusMessage *unregister_agent(DBusConnection *conn,
541 DBusMessage *msg, void *data)
543 const char *sender, *path;
546 DBG("conn %p", conn);
548 sender = dbus_message_get_sender(msg);
550 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
553 err = __connman_agent_unregister(sender, path);
555 return __connman_error_failed(msg, -err);
557 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
560 static DBusMessage *register_counter(DBusConnection *conn,
561 DBusMessage *msg, void *data)
563 const char *sender, *path;
564 unsigned int accuracy, period;
567 DBG("conn %p", conn);
569 sender = dbus_message_get_sender(msg);
571 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
572 DBUS_TYPE_UINT32, &accuracy,
573 DBUS_TYPE_UINT32, &period,
576 /* FIXME: add handling of accuracy parameter */
578 err = __connman_counter_register(sender, path, period);
580 return __connman_error_failed(msg, -err);
582 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
585 static DBusMessage *unregister_counter(DBusConnection *conn,
586 DBusMessage *msg, void *data)
588 const char *sender, *path;
591 DBG("conn %p", conn);
593 sender = dbus_message_get_sender(msg);
595 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
598 err = __connman_counter_unregister(sender, path);
600 return __connman_error_failed(msg, -err);
602 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
605 static DBusMessage *create_session(DBusConnection *conn,
606 DBusMessage *msg, void *data)
610 DBG("conn %p", conn);
612 err = __connman_session_create(msg);
614 return __connman_error_failed(msg, -err);
616 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
619 static DBusMessage *destroy_session(DBusConnection *conn,
620 DBusMessage *msg, void *data)
624 DBG("conn %p", conn);
626 err = __connman_session_destroy(msg);
628 return __connman_error_failed(msg, -err);
630 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
633 static DBusMessage *request_private_network(DBusConnection *conn,
634 DBusMessage *msg, void *data)
639 DBG("conn %p", conn);
641 sender = dbus_message_get_sender(msg);
643 err = __connman_private_network_request(msg, sender);
645 return __connman_error_failed(msg, -err);
650 static DBusMessage *release_private_network(DBusConnection *conn,
651 DBusMessage *msg, void *data)
656 DBG("conn %p", conn);
658 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
661 err = __connman_private_network_release(path);
663 return __connman_error_failed(msg, -err);
665 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
668 static GDBusMethodTable manager_methods[] = {
669 { "GetProperties", "", "a{sv}", get_properties },
670 { "SetProperty", "sv", "", set_property,
671 G_DBUS_METHOD_FLAG_ASYNC },
672 { "GetState", "", "s", get_state },
673 { "RemoveProvider", "o", "", remove_provider },
674 { "RequestScan", "s", "", request_scan },
675 { "EnableTechnology", "s", "", enable_technology,
676 G_DBUS_METHOD_FLAG_ASYNC },
677 { "DisableTechnology", "s", "", disable_technology,
678 G_DBUS_METHOD_FLAG_ASYNC },
679 { "GetServices", "", "a(oa{sv})", get_services },
680 { "LookupService", "s", "o", lookup_service, },
681 { "ConnectService", "a{sv}", "o", connect_service,
682 G_DBUS_METHOD_FLAG_ASYNC },
683 { "ProvisionService", "s", "", provision_service,
684 G_DBUS_METHOD_FLAG_ASYNC },
685 { "ConnectProvider", "a{sv}", "o", connect_provider,
686 G_DBUS_METHOD_FLAG_ASYNC },
687 { "RegisterAgent", "o", "", register_agent },
688 { "UnregisterAgent", "o", "", unregister_agent },
689 { "RegisterCounter", "ouu", "", register_counter },
690 { "UnregisterCounter", "o", "", unregister_counter },
691 { "CreateSession", "a{sv}o", "o", create_session },
692 { "DestroySession", "o", "", destroy_session },
693 { "RequestPrivateNetwork", "", "oa{sv}h",
694 request_private_network,
695 G_DBUS_METHOD_FLAG_ASYNC },
696 { "ReleasePrivateNetwork", "o", "",
697 release_private_network },
701 static GDBusSignalTable manager_signals[] = {
702 { "PropertyChanged", "sv" },
703 { "StateChanged", "s" },
707 int __connman_manager_init(void)
711 connection = connman_dbus_get_connection();
712 if (connection == NULL)
715 if (connman_notifier_register(&technology_notifier) < 0)
716 connman_error("Failed to register technology notifier");
718 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
719 CONNMAN_MANAGER_INTERFACE,
721 manager_signals, NULL, NULL, NULL);
723 connman_state_idle = TRUE;
728 void __connman_manager_cleanup(void)
732 connman_notifier_unregister(&technology_notifier);
734 if (connection == NULL)
737 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
738 CONNMAN_MANAGER_INTERFACE);
740 dbus_connection_unref(connection);