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, sessionmode;
40 reply = dbus_message_new_method_return(msg);
44 dbus_message_iter_init_append(reply, &array);
46 connman_dbus_dict_open(&array, &dict);
48 str = __connman_profile_active_path();
50 connman_dbus_dict_append_basic(&dict, "ActiveProfile",
51 DBUS_TYPE_OBJECT_PATH, &str);
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_profile_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_profile_set_offlinemode(offlinemode, TRUE);
121 __connman_profile_save_default();
122 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
125 dbus_message_iter_get_basic(&value, &str);
127 return __connman_error_not_supported(msg);
128 } else if (g_str_equal(name, "SessionMode") == TRUE) {
129 connman_bool_t sessionmode;
131 if (type != DBUS_TYPE_BOOLEAN)
132 return __connman_error_invalid_arguments(msg);
134 dbus_message_iter_get_basic(&value, &sessionmode);
136 __connman_session_set_mode(sessionmode);
138 return __connman_error_invalid_property(msg);
140 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
143 static DBusMessage *get_state(DBusConnection *conn,
144 DBusMessage *msg, void *data)
148 DBG("conn %p", conn);
150 str = __connman_notifier_get_state();
152 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
156 static DBusMessage *remove_provider(DBusConnection *conn,
157 DBusMessage *msg, void *data)
162 DBG("conn %p", conn);
164 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
167 err = __connman_provider_remove(path);
169 return __connman_error_failed(msg, -err);
171 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
174 static DBusMessage *request_scan(DBusConnection *conn,
175 DBusMessage *msg, void *data)
177 enum connman_service_type type;
181 DBG("conn %p", conn);
183 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
186 if (g_strcmp0(str, "") == 0)
187 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
188 else if (g_strcmp0(str, "wifi") == 0)
189 type = CONNMAN_SERVICE_TYPE_WIFI;
190 else if (g_strcmp0(str, "wimax") == 0)
191 type = CONNMAN_SERVICE_TYPE_WIMAX;
193 return __connman_error_invalid_arguments(msg);
195 err = __connman_element_request_scan(type);
197 if (err == -EINPROGRESS) {
198 connman_error("Invalid return code from scan");
202 return __connman_error_failed(msg, -err);
205 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
208 static DBusConnection *connection = NULL;
210 static enum connman_service_type technology_type;
211 static connman_bool_t technology_enabled;
212 static DBusMessage *technology_pending = NULL;
213 static guint technology_timeout = 0;
215 static void technology_reply(int error)
219 if (technology_timeout > 0) {
220 g_source_remove(technology_timeout);
221 technology_timeout = 0;
224 if (technology_pending != NULL) {
228 reply = __connman_error_failed(technology_pending,
231 g_dbus_send_message(connection, reply);
233 g_dbus_send_reply(connection, technology_pending,
236 dbus_message_unref(technology_pending);
237 technology_pending = NULL;
240 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
243 static gboolean technology_abort(gpointer user_data)
247 technology_timeout = 0;
249 technology_reply(ETIMEDOUT);
254 static void technology_notify(enum connman_service_type type,
255 connman_bool_t enabled)
257 DBG("type %d enabled %d", type, enabled);
259 if (type == technology_type && enabled == technology_enabled)
263 static struct connman_notifier technology_notifier = {
265 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
266 .service_enabled= technology_notify,
269 static DBusMessage *enable_technology(DBusConnection *conn,
270 DBusMessage *msg, void *data)
272 enum connman_service_type type;
276 DBG("conn %p", conn);
278 if (technology_pending != NULL)
279 return __connman_error_in_progress(msg);
281 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
284 if (g_strcmp0(str, "ethernet") == 0)
285 type = CONNMAN_SERVICE_TYPE_ETHERNET;
286 else if (g_strcmp0(str, "wifi") == 0)
287 type = CONNMAN_SERVICE_TYPE_WIFI;
288 else if (g_strcmp0(str, "wimax") == 0)
289 type = CONNMAN_SERVICE_TYPE_WIMAX;
290 else if (g_strcmp0(str, "bluetooth") == 0)
291 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
292 else if (g_strcmp0(str, "cellular") == 0)
293 type = CONNMAN_SERVICE_TYPE_CELLULAR;
295 return __connman_error_invalid_arguments(msg);
297 if (__connman_notifier_is_registered(type) == FALSE)
298 return __connman_error_not_registered(msg);
300 if (__connman_notifier_is_enabled(type) == TRUE)
301 return __connman_error_already_enabled(msg);
303 technology_type = type;
304 technology_enabled = TRUE;
305 technology_pending = dbus_message_ref(msg);
307 err = __connman_element_enable_technology(type);
308 if (err < 0 && err != -EINPROGRESS)
309 technology_reply(-err);
311 technology_timeout = g_timeout_add_seconds(15,
312 technology_abort, NULL);
317 static DBusMessage *disable_technology(DBusConnection *conn,
318 DBusMessage *msg, void *data)
320 enum connman_service_type type;
324 DBG("conn %p", conn);
326 if (technology_pending != NULL)
327 return __connman_error_in_progress(msg);
329 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
332 if (g_strcmp0(str, "ethernet") == 0)
333 type = CONNMAN_SERVICE_TYPE_ETHERNET;
334 else if (g_strcmp0(str, "wifi") == 0)
335 type = CONNMAN_SERVICE_TYPE_WIFI;
336 else if (g_strcmp0(str, "wimax") == 0)
337 type = CONNMAN_SERVICE_TYPE_WIMAX;
338 else if (g_strcmp0(str, "bluetooth") == 0)
339 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
340 else if (g_strcmp0(str, "cellular") == 0)
341 type = CONNMAN_SERVICE_TYPE_CELLULAR;
343 return __connman_error_invalid_arguments(msg);
345 if (__connman_notifier_is_registered(type) == FALSE)
346 return __connman_error_not_registered(msg);
348 if (__connman_notifier_is_enabled(type) == FALSE)
349 return __connman_error_already_disabled(msg);
351 technology_type = type;
352 technology_enabled = FALSE;
353 technology_pending = dbus_message_ref(msg);
355 err = __connman_element_disable_technology(type);
356 if (err < 0 && err != -EINPROGRESS)
357 technology_reply(-err);
359 technology_timeout = g_timeout_add_seconds(10,
360 technology_abort, NULL);
365 static DBusMessage *get_services(DBusConnection *conn,
366 DBusMessage *msg, void *data)
369 DBusMessageIter iter, array;
371 reply = dbus_message_new_method_return(msg);
375 dbus_message_iter_init_append(reply, &iter);
377 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
378 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
379 DBUS_TYPE_OBJECT_PATH_AS_STRING
380 DBUS_TYPE_ARRAY_AS_STRING
381 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
382 DBUS_TYPE_STRING_AS_STRING
383 DBUS_TYPE_VARIANT_AS_STRING
384 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
385 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
387 __connman_service_list_struct(&array);
389 dbus_message_iter_close_container(&iter, &array);
394 static DBusMessage *lookup_service(DBusConnection *conn,
395 DBusMessage *msg, void *data)
397 const char *pattern, *path;
400 DBG("conn %p", conn);
402 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
405 err = __connman_service_lookup(pattern, &path);
407 return __connman_error_failed(msg, -err);
409 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
413 static DBusMessage *connect_service(DBusConnection *conn,
414 DBusMessage *msg, void *data)
418 DBG("conn %p", conn);
420 if (__connman_session_mode() == TRUE) {
421 connman_info("Session mode enabled: "
422 "direct service connect disabled");
424 return __connman_error_failed(msg, -EINVAL);
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);
440 static DBusMessage *provision_service(DBusConnection *conn, DBusMessage *msg,
445 DBG("conn %p", conn);
447 err = __connman_service_provision(msg);
449 return __connman_error_failed(msg, -err);
454 static DBusMessage *connect_provider(DBusConnection *conn,
455 DBusMessage *msg, void *data)
459 DBG("conn %p", conn);
461 if (__connman_session_mode() == TRUE) {
462 connman_info("Session mode enabled: "
463 "direct provider connect disabled");
465 return __connman_error_failed(msg, -EINVAL);
468 err = __connman_provider_create_and_connect(msg);
470 if (err == -EINPROGRESS) {
471 connman_error("Invalid return code from connect");
475 return __connman_error_failed(msg, -err);
481 static DBusMessage *register_agent(DBusConnection *conn,
482 DBusMessage *msg, void *data)
484 const char *sender, *path;
487 DBG("conn %p", conn);
489 sender = dbus_message_get_sender(msg);
491 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
494 err = __connman_agent_register(sender, path);
496 return __connman_error_failed(msg, -err);
498 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
501 static DBusMessage *unregister_agent(DBusConnection *conn,
502 DBusMessage *msg, void *data)
504 const char *sender, *path;
507 DBG("conn %p", conn);
509 sender = dbus_message_get_sender(msg);
511 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
514 err = __connman_agent_unregister(sender, path);
516 return __connman_error_failed(msg, -err);
518 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
521 static DBusMessage *register_counter(DBusConnection *conn,
522 DBusMessage *msg, void *data)
524 const char *sender, *path;
525 unsigned int accuracy, period;
528 DBG("conn %p", conn);
530 sender = dbus_message_get_sender(msg);
532 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
533 DBUS_TYPE_UINT32, &accuracy,
534 DBUS_TYPE_UINT32, &period,
537 /* FIXME: add handling of accuracy parameter */
539 err = __connman_counter_register(sender, path, period);
541 return __connman_error_failed(msg, -err);
543 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
546 static DBusMessage *unregister_counter(DBusConnection *conn,
547 DBusMessage *msg, void *data)
549 const char *sender, *path;
552 DBG("conn %p", conn);
554 sender = dbus_message_get_sender(msg);
556 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
559 err = __connman_counter_unregister(sender, path);
561 return __connman_error_failed(msg, -err);
563 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
566 static DBusMessage *create_session(DBusConnection *conn,
567 DBusMessage *msg, void *data)
571 DBG("conn %p", conn);
573 err = __connman_session_create(msg);
575 return __connman_error_failed(msg, -err);
577 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
580 static DBusMessage *destroy_session(DBusConnection *conn,
581 DBusMessage *msg, void *data)
585 DBG("conn %p", conn);
587 err = __connman_session_destroy(msg);
589 return __connman_error_failed(msg, -err);
591 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
594 static DBusMessage *request_private_network(DBusConnection *conn,
595 DBusMessage *msg, void *data)
600 DBG("conn %p", conn);
602 sender = dbus_message_get_sender(msg);
604 err = __connman_private_network_request(msg, sender);
606 return __connman_error_failed(msg, -err);
611 static DBusMessage *release_private_network(DBusConnection *conn,
612 DBusMessage *msg, void *data)
617 DBG("conn %p", conn);
619 sender = dbus_message_get_sender(msg);
621 err = __connman_private_network_release(sender);
623 return __connman_error_failed(msg, -err);
625 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
628 static GDBusMethodTable manager_methods[] = {
629 { "GetProperties", "", "a{sv}", get_properties },
630 { "SetProperty", "sv", "", set_property },
631 { "GetState", "", "s", get_state },
632 { "RemoveProvider", "o", "", remove_provider },
633 { "RequestScan", "s", "", request_scan },
634 { "EnableTechnology", "s", "", enable_technology,
635 G_DBUS_METHOD_FLAG_ASYNC },
636 { "DisableTechnology", "s", "", disable_technology,
637 G_DBUS_METHOD_FLAG_ASYNC },
638 { "GetServices", "", "a(oa{sv})", get_services },
639 { "LookupService", "s", "o", lookup_service, },
640 { "ConnectService", "a{sv}", "o", connect_service,
641 G_DBUS_METHOD_FLAG_ASYNC },
642 { "ProvisionService", "s", "", provision_service,
643 G_DBUS_METHOD_FLAG_ASYNC },
644 { "ConnectProvider", "a{sv}", "o", connect_provider,
645 G_DBUS_METHOD_FLAG_ASYNC },
646 { "RegisterAgent", "o", "", register_agent },
647 { "UnregisterAgent", "o", "", unregister_agent },
648 { "RegisterCounter", "ouu", "", register_counter },
649 { "UnregisterCounter", "o", "", unregister_counter },
650 { "CreateSession", "a{sv}o", "o", create_session },
651 { "DestroySession", "o", "", destroy_session },
652 { "RequestPrivateNetwork", "", "ha{sv}",
653 request_private_network,
654 G_DBUS_METHOD_FLAG_ASYNC },
655 { "ReleasePrivateNetwork", "", "",
656 release_private_network },
660 static GDBusSignalTable manager_signals[] = {
661 { "PropertyChanged", "sv" },
662 { "StateChanged", "s" },
666 int __connman_manager_init(void)
670 connection = connman_dbus_get_connection();
671 if (connection == NULL)
674 if (connman_notifier_register(&technology_notifier) < 0)
675 connman_error("Failed to register technology notifier");
677 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
678 CONNMAN_MANAGER_INTERFACE,
680 manager_signals, NULL, NULL, NULL);
685 void __connman_manager_cleanup(void)
689 connman_notifier_unregister(&technology_notifier);
691 if (connection == NULL)
694 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
695 CONNMAN_MANAGER_INTERFACE);
697 dbus_connection_unref(connection);