5 * Copyright (C) 2007-2009 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 connman_bool_t global_offlinemode = FALSE;
32 static void append_profiles(DBusMessageIter *dict)
34 DBusMessageIter entry, value, iter;
35 const char *key = "Profiles";
37 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
40 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
42 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
43 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
46 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
47 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
48 __connman_profile_list(&iter);
49 dbus_message_iter_close_container(&value, &iter);
51 dbus_message_iter_close_container(&entry, &value);
53 dbus_message_iter_close_container(dict, &entry);
56 static void append_services(DBusMessageIter *dict)
58 DBusMessageIter entry, value, iter;
59 const char *key = "Services";
61 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
64 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
66 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
67 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
70 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
71 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
72 __connman_service_list(&iter);
73 dbus_message_iter_close_container(&value, &iter);
75 dbus_message_iter_close_container(&entry, &value);
77 dbus_message_iter_close_container(dict, &entry);
80 static void append_devices(DBusMessageIter *dict)
82 DBusMessageIter entry, value, iter;
83 const char *key = "Devices";
85 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
88 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
90 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
91 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
94 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
95 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
96 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
97 dbus_message_iter_close_container(&value, &iter);
99 dbus_message_iter_close_container(&entry, &value);
101 dbus_message_iter_close_container(dict, &entry);
104 static void append_connections(DBusMessageIter *dict)
106 DBusMessageIter entry, value, iter;
107 const char *key = "Connections";
109 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
112 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
114 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
115 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
118 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
119 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
120 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION, &iter);
121 dbus_message_iter_close_container(&value, &iter);
123 dbus_message_iter_close_container(&entry, &value);
125 dbus_message_iter_close_container(dict, &entry);
128 static void append_technologies(gboolean powered, DBusMessageIter *dict)
130 DBusMessageIter entry, value, iter;
134 key = "EnabledTechnologies";
136 key = "Technologies";
138 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
141 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
143 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
144 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
147 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
148 DBUS_TYPE_STRING_AS_STRING, &iter);
149 __connman_notifier_device_type_list(powered, &iter);
150 dbus_message_iter_close_container(&value, &iter);
152 dbus_message_iter_close_container(&entry, &value);
154 dbus_message_iter_close_container(dict, &entry);
157 static DBusMessage *get_properties(DBusConnection *conn,
158 DBusMessage *msg, void *data)
161 DBusMessageIter array, dict;
164 DBG("conn %p", conn);
166 if (__connman_security_check_privilege(msg,
167 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
168 return __connman_error_permission_denied(msg);
170 reply = dbus_message_new_method_return(msg);
174 dbus_message_iter_init_append(reply, &array);
176 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
177 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
178 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
179 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
181 str = __connman_profile_active_path();
183 connman_dbus_dict_append_variant(&dict, "ActiveProfile",
184 DBUS_TYPE_OBJECT_PATH, &str);
186 append_profiles(&dict);
187 append_services(&dict);
189 append_devices(&dict);
190 append_connections(&dict);
192 if (__connman_element_count(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION) > 0)
197 connman_dbus_dict_append_variant(&dict, "State",
198 DBUS_TYPE_STRING, &str);
200 connman_dbus_dict_append_variant(&dict, "OfflineMode",
201 DBUS_TYPE_BOOLEAN, &global_offlinemode);
203 append_technologies(FALSE, &dict);
204 append_technologies(TRUE, &dict);
206 dbus_message_iter_close_container(&array, &dict);
211 static DBusMessage *set_property(DBusConnection *conn,
212 DBusMessage *msg, void *data)
214 DBusMessageIter iter, value;
217 DBG("conn %p", conn);
219 if (dbus_message_iter_init(msg, &iter) == FALSE)
220 return __connman_error_invalid_arguments(msg);
222 dbus_message_iter_get_basic(&iter, &name);
223 dbus_message_iter_next(&iter);
224 dbus_message_iter_recurse(&iter, &value);
226 if (__connman_security_check_privilege(msg,
227 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
228 return __connman_error_permission_denied(msg);
230 if (g_str_equal(name, "OfflineMode") == TRUE) {
231 connman_bool_t offlinemode;
233 dbus_message_iter_get_basic(&value, &offlinemode);
235 if (global_offlinemode == offlinemode)
236 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
238 global_offlinemode = offlinemode;
240 __connman_device_set_offlinemode(offlinemode);
241 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
244 dbus_message_iter_get_basic(&value, &str);
246 return __connman_error_not_supported(msg);
248 return __connman_error_invalid_property(msg);
250 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
253 static DBusMessage *add_profile(DBusConnection *conn,
254 DBusMessage *msg, void *data)
258 DBG("conn %p", conn);
260 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
263 return __connman_error_not_supported(msg);
266 static DBusMessage *remove_profile(DBusConnection *conn,
267 DBusMessage *msg, void *data)
271 DBG("conn %p", conn);
273 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
276 return __connman_error_not_supported(msg);
279 static DBusMessage *request_scan(DBusConnection *conn,
280 DBusMessage *msg, void *data)
282 enum connman_device_type type;
286 DBG("conn %p", conn);
288 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
291 if (g_strcmp0(str, "") == 0)
292 type = CONNMAN_DEVICE_TYPE_UNKNOWN;
293 else if (g_strcmp0(str, "wifi") == 0)
294 type = CONNMAN_DEVICE_TYPE_WIFI;
295 else if (g_strcmp0(str, "wimax") == 0)
296 type = CONNMAN_DEVICE_TYPE_WIMAX;
298 return __connman_error_invalid_arguments(msg);
300 err = __connman_element_request_scan(type);
302 if (err == -EINPROGRESS) {
303 connman_error("Invalid return code from scan");
307 return __connman_error_failed(msg, -err);
310 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
313 static DBusMessage *connect_service(DBusConnection *conn,
314 DBusMessage *msg, void *data)
318 DBG("conn %p", conn);
320 if (__connman_security_check_privilege(msg,
321 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
322 return __connman_error_permission_denied(msg);
324 err = __connman_service_create_and_connect(msg);
326 if (err == -EINPROGRESS) {
327 connman_error("Invalid return code from connect");
331 return __connman_error_failed(msg, -err);
337 static DBusMessage *register_agent(DBusConnection *conn,
338 DBusMessage *msg, void *data)
341 const char *sender, *path;
343 DBG("conn %p", conn);
345 sender = dbus_message_get_sender(msg);
347 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
350 reply = dbus_message_new_method_return(msg);
354 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
356 __connman_agent_register(sender, path);
361 static DBusMessage *unregister_agent(DBusConnection *conn,
362 DBusMessage *msg, void *data)
365 const char *sender, *path;
367 DBG("conn %p", conn);
369 sender = dbus_message_get_sender(msg);
371 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
374 reply = dbus_message_new_method_return(msg);
378 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
380 __connman_agent_unregister(sender, path);
385 static GDBusMethodTable manager_methods[] = {
386 { "GetProperties", "", "a{sv}", get_properties },
387 { "SetProperty", "sv", "", set_property },
388 { "AddProfile", "s", "o", add_profile },
389 { "RemoveProfile", "o", "", remove_profile },
390 { "RequestScan", "s", "", request_scan },
391 { "ConnectService", "a{sv}", "o", connect_service,
392 G_DBUS_METHOD_FLAG_ASYNC },
393 { "RegisterAgent", "o", "", register_agent },
394 { "UnregisterAgent", "o", "", unregister_agent },
398 static GDBusSignalTable manager_signals[] = {
399 { "PropertyChanged", "sv" },
400 { "StateChanged", "s" },
404 static DBusMessage *nm_sleep(DBusConnection *conn,
405 DBusMessage *msg, void *data)
409 DBG("conn %p", conn);
411 reply = dbus_message_new_method_return(msg);
415 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
420 static DBusMessage *nm_wake(DBusConnection *conn,
421 DBusMessage *msg, void *data)
425 DBG("conn %p", conn);
427 reply = dbus_message_new_method_return(msg);
431 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
437 NM_STATE_UNKNOWN = 0,
441 NM_STATE_DISCONNECTED
444 static DBusMessage *nm_state(DBusConnection *conn,
445 DBusMessage *msg, void *data)
450 DBG("conn %p", conn);
452 reply = dbus_message_new_method_return(msg);
456 state = NM_STATE_DISCONNECTED;
458 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
464 static GDBusMethodTable nm_methods[] = {
465 { "sleep", "", "", nm_sleep },
466 { "wake", "", "", nm_wake },
467 { "state", "", "u", nm_state },
471 static DBusConnection *connection = NULL;
472 static gboolean nm_compat = FALSE;
474 int __connman_manager_init(DBusConnection *conn, gboolean compat)
476 DBG("conn %p", conn);
478 connection = dbus_connection_ref(conn);
479 if (connection == NULL)
482 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
483 CONNMAN_MANAGER_INTERFACE,
485 manager_signals, NULL, NULL, NULL);
487 if (compat == TRUE) {
488 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
489 nm_methods, NULL, NULL, NULL, NULL);
497 void __connman_manager_cleanup(void)
499 DBG("conn %p", connection);
501 if (nm_compat == TRUE) {
502 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
505 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
506 CONNMAN_MANAGER_INTERFACE);
508 dbus_connection_unref(connection);