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 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_technology_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_technology_set_offlinemode(offlinemode);
120 } else if (g_str_equal(name, "SessionMode") == TRUE) {
121 connman_bool_t sessionmode;
123 if (type != DBUS_TYPE_BOOLEAN)
124 return __connman_error_invalid_arguments(msg);
126 dbus_message_iter_get_basic(&value, &sessionmode);
128 if (session_mode_pending != NULL)
129 return __connman_error_in_progress(msg);
131 __connman_session_set_mode(sessionmode);
133 if (connman_state_idle == FALSE) {
134 session_mode_pending = msg;
139 return __connman_error_invalid_property(msg);
141 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
144 static DBusMessage *get_state(DBusConnection *conn,
145 DBusMessage *msg, void *data)
149 DBG("conn %p", conn);
151 str = __connman_notifier_get_state();
153 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
157 static DBusMessage *remove_provider(DBusConnection *conn,
158 DBusMessage *msg, void *data)
163 DBG("conn %p", conn);
165 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
168 err = __connman_provider_remove(path);
170 return __connman_error_failed(msg, -err);
172 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
175 static DBusMessage *request_scan(DBusConnection *conn,
176 DBusMessage *msg, void *data)
178 enum connman_service_type type;
182 DBG("conn %p", conn);
184 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
187 if (g_strcmp0(str, "") == 0)
188 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
189 else if (g_strcmp0(str, "wifi") == 0)
190 type = CONNMAN_SERVICE_TYPE_WIFI;
191 else if (g_strcmp0(str, "wimax") == 0)
192 type = CONNMAN_SERVICE_TYPE_WIMAX;
194 return __connman_error_invalid_arguments(msg);
196 err = __connman_device_request_scan(type);
198 if (err == -EINPROGRESS) {
199 connman_error("Invalid return code from scan");
203 return __connman_error_failed(msg, -err);
206 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
209 static DBusConnection *connection = NULL;
211 static void session_mode_notify(void)
215 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
216 g_dbus_send_message(connection, reply);
218 dbus_message_unref(session_mode_pending);
219 session_mode_pending = NULL;
222 static void idle_state(connman_bool_t idle)
225 DBG("idle %d", idle);
227 connman_state_idle = idle;
229 if (connman_state_idle == FALSE || session_mode_pending == NULL)
232 session_mode_notify();
235 static struct connman_notifier technology_notifier = {
237 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
238 .idle_state = idle_state,
241 static DBusMessage *enable_technology(DBusConnection *conn,
242 DBusMessage *msg, void *data)
244 enum connman_service_type type;
247 DBG("conn %p", conn);
249 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
252 if (g_strcmp0(str, "ethernet") == 0)
253 type = CONNMAN_SERVICE_TYPE_ETHERNET;
254 else if (g_strcmp0(str, "wifi") == 0)
255 type = CONNMAN_SERVICE_TYPE_WIFI;
256 else if (g_strcmp0(str, "wimax") == 0)
257 type = CONNMAN_SERVICE_TYPE_WIMAX;
258 else if (g_strcmp0(str, "bluetooth") == 0)
259 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
260 else if (g_strcmp0(str, "cellular") == 0)
261 type = CONNMAN_SERVICE_TYPE_CELLULAR;
263 return __connman_error_invalid_arguments(msg);
265 if (__connman_notifier_is_registered(type) == FALSE)
266 return __connman_error_not_registered(msg);
268 if (__connman_notifier_is_enabled(type) == TRUE)
269 return __connman_error_already_enabled(msg);
271 __connman_technology_enable(type, msg);
276 static DBusMessage *disable_technology(DBusConnection *conn,
277 DBusMessage *msg, void *data)
279 enum connman_service_type type;
282 DBG("conn %p", conn);
284 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
287 if (g_strcmp0(str, "ethernet") == 0)
288 type = CONNMAN_SERVICE_TYPE_ETHERNET;
289 else if (g_strcmp0(str, "wifi") == 0)
290 type = CONNMAN_SERVICE_TYPE_WIFI;
291 else if (g_strcmp0(str, "wimax") == 0)
292 type = CONNMAN_SERVICE_TYPE_WIMAX;
293 else if (g_strcmp0(str, "bluetooth") == 0)
294 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
295 else if (g_strcmp0(str, "cellular") == 0)
296 type = CONNMAN_SERVICE_TYPE_CELLULAR;
298 return __connman_error_invalid_arguments(msg);
300 if (__connman_notifier_is_registered(type) == FALSE)
301 return __connman_error_not_registered(msg);
303 if (__connman_notifier_is_enabled(type) == FALSE)
304 return __connman_error_already_disabled(msg);
306 __connman_technology_disable(type, msg);
311 static DBusMessage *get_services(DBusConnection *conn,
312 DBusMessage *msg, void *data)
315 DBusMessageIter iter, array;
317 reply = dbus_message_new_method_return(msg);
321 dbus_message_iter_init_append(reply, &iter);
323 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
324 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
325 DBUS_TYPE_OBJECT_PATH_AS_STRING
326 DBUS_TYPE_ARRAY_AS_STRING
327 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
328 DBUS_TYPE_STRING_AS_STRING
329 DBUS_TYPE_VARIANT_AS_STRING
330 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
331 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
333 __connman_service_list_struct(&array);
335 dbus_message_iter_close_container(&iter, &array);
340 static DBusMessage *lookup_service(DBusConnection *conn,
341 DBusMessage *msg, void *data)
343 const char *pattern, *path;
346 DBG("conn %p", conn);
348 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
351 err = __connman_service_lookup(pattern, &path);
353 return __connman_error_failed(msg, -err);
355 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
359 static DBusMessage *connect_service(DBusConnection *conn,
360 DBusMessage *msg, void *data)
364 DBG("conn %p", conn);
366 if (__connman_session_mode() == TRUE) {
367 connman_info("Session mode enabled: "
368 "direct service connect disabled");
370 return __connman_error_failed(msg, -EINVAL);
373 err = __connman_service_create_and_connect(msg);
375 if (err == -EINPROGRESS) {
376 connman_error("Invalid return code from connect");
380 return __connman_error_failed(msg, -err);
386 static DBusMessage *provision_service(DBusConnection *conn, DBusMessage *msg,
391 DBG("conn %p", conn);
393 err = __connman_service_provision(msg);
395 return __connman_error_failed(msg, -err);
400 static DBusMessage *connect_provider(DBusConnection *conn,
401 DBusMessage *msg, void *data)
405 DBG("conn %p", conn);
407 if (__connman_session_mode() == TRUE) {
408 connman_info("Session mode enabled: "
409 "direct provider connect disabled");
411 return __connman_error_failed(msg, -EINVAL);
414 err = __connman_provider_create_and_connect(msg);
416 if (err == -EINPROGRESS) {
417 connman_error("Invalid return code from connect");
421 return __connman_error_failed(msg, -err);
427 static DBusMessage *register_agent(DBusConnection *conn,
428 DBusMessage *msg, void *data)
430 const char *sender, *path;
433 DBG("conn %p", conn);
435 sender = dbus_message_get_sender(msg);
437 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
440 err = __connman_agent_register(sender, path);
442 return __connman_error_failed(msg, -err);
444 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
447 static DBusMessage *unregister_agent(DBusConnection *conn,
448 DBusMessage *msg, void *data)
450 const char *sender, *path;
453 DBG("conn %p", conn);
455 sender = dbus_message_get_sender(msg);
457 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
460 err = __connman_agent_unregister(sender, path);
462 return __connman_error_failed(msg, -err);
464 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
467 static DBusMessage *register_counter(DBusConnection *conn,
468 DBusMessage *msg, void *data)
470 const char *sender, *path;
471 unsigned int accuracy, period;
474 DBG("conn %p", conn);
476 sender = dbus_message_get_sender(msg);
478 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
479 DBUS_TYPE_UINT32, &accuracy,
480 DBUS_TYPE_UINT32, &period,
483 /* FIXME: add handling of accuracy parameter */
485 err = __connman_counter_register(sender, path, period);
487 return __connman_error_failed(msg, -err);
489 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
492 static DBusMessage *unregister_counter(DBusConnection *conn,
493 DBusMessage *msg, void *data)
495 const char *sender, *path;
498 DBG("conn %p", conn);
500 sender = dbus_message_get_sender(msg);
502 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
505 err = __connman_counter_unregister(sender, path);
507 return __connman_error_failed(msg, -err);
509 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
512 static DBusMessage *create_session(DBusConnection *conn,
513 DBusMessage *msg, void *data)
517 DBG("conn %p", conn);
519 err = __connman_session_create(msg);
521 return __connman_error_failed(msg, -err);
523 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
526 static DBusMessage *destroy_session(DBusConnection *conn,
527 DBusMessage *msg, void *data)
531 DBG("conn %p", conn);
533 err = __connman_session_destroy(msg);
535 return __connman_error_failed(msg, -err);
537 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
540 static DBusMessage *request_private_network(DBusConnection *conn,
541 DBusMessage *msg, void *data)
546 DBG("conn %p", conn);
548 sender = dbus_message_get_sender(msg);
550 err = __connman_private_network_request(msg, sender);
552 return __connman_error_failed(msg, -err);
557 static DBusMessage *release_private_network(DBusConnection *conn,
558 DBusMessage *msg, void *data)
563 DBG("conn %p", conn);
565 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
568 err = __connman_private_network_release(path);
570 return __connman_error_failed(msg, -err);
572 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
575 static GDBusMethodTable manager_methods[] = {
576 { "GetProperties", "", "a{sv}", get_properties },
577 { "SetProperty", "sv", "", set_property,
578 G_DBUS_METHOD_FLAG_ASYNC },
579 { "GetState", "", "s", get_state },
580 { "RemoveProvider", "o", "", remove_provider },
581 { "RequestScan", "s", "", request_scan },
582 { "EnableTechnology", "s", "", enable_technology,
583 G_DBUS_METHOD_FLAG_ASYNC },
584 { "DisableTechnology", "s", "", disable_technology,
585 G_DBUS_METHOD_FLAG_ASYNC },
586 { "GetServices", "", "a(oa{sv})", get_services },
587 { "LookupService", "s", "o", lookup_service, },
588 { "ConnectService", "a{sv}", "o", connect_service,
589 G_DBUS_METHOD_FLAG_ASYNC },
590 { "ProvisionService", "s", "", provision_service,
591 G_DBUS_METHOD_FLAG_ASYNC },
592 { "ConnectProvider", "a{sv}", "o", connect_provider,
593 G_DBUS_METHOD_FLAG_ASYNC },
594 { "RegisterAgent", "o", "", register_agent },
595 { "UnregisterAgent", "o", "", unregister_agent },
596 { "RegisterCounter", "ouu", "", register_counter },
597 { "UnregisterCounter", "o", "", unregister_counter },
598 { "CreateSession", "a{sv}o", "o", create_session },
599 { "DestroySession", "o", "", destroy_session },
600 { "RequestPrivateNetwork", "", "oa{sv}h",
601 request_private_network,
602 G_DBUS_METHOD_FLAG_ASYNC },
603 { "ReleasePrivateNetwork", "o", "",
604 release_private_network },
608 static GDBusSignalTable manager_signals[] = {
609 { "PropertyChanged", "sv" },
610 { "StateChanged", "s" },
614 int __connman_manager_init(void)
618 connection = connman_dbus_get_connection();
619 if (connection == NULL)
622 if (connman_notifier_register(&technology_notifier) < 0)
623 connman_error("Failed to register technology notifier");
625 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
626 CONNMAN_MANAGER_INTERFACE,
628 manager_signals, NULL, NULL, NULL);
630 connman_state_idle = TRUE;
635 void __connman_manager_cleanup(void)
639 if (connection == NULL)
642 connman_notifier_unregister(&technology_notifier);
644 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
645 CONNMAN_MANAGER_INTERFACE);
647 dbus_connection_unref(connection);