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);
56 str = __connman_notifier_get_state();
57 connman_dbus_dict_append_basic(&dict, "State",
58 DBUS_TYPE_STRING, &str);
60 offlinemode = __connman_technology_get_offlinemode();
61 connman_dbus_dict_append_basic(&dict, "OfflineMode",
62 DBUS_TYPE_BOOLEAN, &offlinemode);
64 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
65 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
66 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
67 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
69 sessionmode = __connman_session_mode();
70 connman_dbus_dict_append_basic(&dict, "SessionMode",
74 connman_dbus_dict_close(&array, &dict);
79 static DBusMessage *set_property(DBusConnection *conn,
80 DBusMessage *msg, void *data)
82 DBusMessageIter iter, value;
88 if (dbus_message_iter_init(msg, &iter) == FALSE)
89 return __connman_error_invalid_arguments(msg);
91 dbus_message_iter_get_basic(&iter, &name);
92 dbus_message_iter_next(&iter);
93 dbus_message_iter_recurse(&iter, &value);
95 type = dbus_message_iter_get_arg_type(&value);
97 if (g_str_equal(name, "OfflineMode") == TRUE) {
98 connman_bool_t offlinemode;
100 if (type != DBUS_TYPE_BOOLEAN)
101 return __connman_error_invalid_arguments(msg);
103 dbus_message_iter_get_basic(&value, &offlinemode);
105 __connman_technology_set_offlinemode(offlinemode);
106 } else if (g_str_equal(name, "SessionMode") == TRUE) {
107 connman_bool_t sessionmode;
109 if (type != DBUS_TYPE_BOOLEAN)
110 return __connman_error_invalid_arguments(msg);
112 dbus_message_iter_get_basic(&value, &sessionmode);
114 if (session_mode_pending != NULL)
115 return __connman_error_in_progress(msg);
117 __connman_session_set_mode(sessionmode);
119 if (sessionmode == TRUE && connman_state_idle == FALSE) {
120 session_mode_pending = msg;
125 return __connman_error_invalid_property(msg);
127 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
130 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
132 __connman_technology_list_struct(iter);
135 static DBusMessage *get_technologies(DBusConnection *conn,
136 DBusMessage *msg, void *data)
142 reply = dbus_message_new_method_return(msg);
146 __connman_dbus_append_objpath_dict_array(reply,
147 append_technology_structs, NULL);
152 static DBusMessage *remove_provider(DBusConnection *conn,
153 DBusMessage *msg, void *data)
158 DBG("conn %p", conn);
160 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
163 err = __connman_provider_remove(path);
165 return __connman_error_failed(msg, -err);
167 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
170 static DBusConnection *connection = NULL;
172 static void session_mode_notify(void)
176 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
177 g_dbus_send_message(connection, reply);
179 dbus_message_unref(session_mode_pending);
180 session_mode_pending = NULL;
183 static void idle_state(connman_bool_t idle)
186 DBG("idle %d", idle);
188 connman_state_idle = idle;
190 if (connman_state_idle == FALSE || session_mode_pending == NULL)
193 session_mode_notify();
196 static struct connman_notifier technology_notifier = {
198 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
199 .idle_state = idle_state,
202 static void append_service_structs(DBusMessageIter *iter, void *user_data)
204 __connman_service_list_struct(iter);
207 static DBusMessage *get_services(DBusConnection *conn,
208 DBusMessage *msg, void *data)
212 reply = dbus_message_new_method_return(msg);
216 __connman_dbus_append_objpath_dict_array(reply,
217 append_service_structs, NULL);
222 static DBusMessage *connect_provider(DBusConnection *conn,
223 DBusMessage *msg, void *data)
227 DBG("conn %p", conn);
229 if (__connman_session_mode() == TRUE) {
230 connman_info("Session mode enabled: "
231 "direct provider connect disabled");
233 return __connman_error_failed(msg, -EINVAL);
236 err = __connman_provider_create_and_connect(msg);
238 if (err == -EINPROGRESS) {
239 connman_error("Invalid return code from connect");
243 return __connman_error_failed(msg, -err);
249 static DBusMessage *register_agent(DBusConnection *conn,
250 DBusMessage *msg, void *data)
252 const char *sender, *path;
255 DBG("conn %p", conn);
257 sender = dbus_message_get_sender(msg);
259 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
262 err = __connman_agent_register(sender, path);
264 return __connman_error_failed(msg, -err);
266 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
269 static DBusMessage *unregister_agent(DBusConnection *conn,
270 DBusMessage *msg, void *data)
272 const char *sender, *path;
275 DBG("conn %p", conn);
277 sender = dbus_message_get_sender(msg);
279 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
282 err = __connman_agent_unregister(sender, path);
284 return __connman_error_failed(msg, -err);
286 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
289 static DBusMessage *register_counter(DBusConnection *conn,
290 DBusMessage *msg, void *data)
292 const char *sender, *path;
293 unsigned int accuracy, period;
296 DBG("conn %p", conn);
298 sender = dbus_message_get_sender(msg);
300 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
301 DBUS_TYPE_UINT32, &accuracy,
302 DBUS_TYPE_UINT32, &period,
305 /* FIXME: add handling of accuracy parameter */
307 err = __connman_counter_register(sender, path, period);
309 return __connman_error_failed(msg, -err);
311 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
314 static DBusMessage *unregister_counter(DBusConnection *conn,
315 DBusMessage *msg, void *data)
317 const char *sender, *path;
320 DBG("conn %p", conn);
322 sender = dbus_message_get_sender(msg);
324 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
327 err = __connman_counter_unregister(sender, path);
329 return __connman_error_failed(msg, -err);
331 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
334 static DBusMessage *create_session(DBusConnection *conn,
335 DBusMessage *msg, void *data)
339 DBG("conn %p", conn);
341 err = __connman_session_create(msg);
343 return __connman_error_failed(msg, -err);
345 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
348 static DBusMessage *destroy_session(DBusConnection *conn,
349 DBusMessage *msg, void *data)
353 DBG("conn %p", conn);
355 err = __connman_session_destroy(msg);
357 return __connman_error_failed(msg, -err);
359 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
362 static DBusMessage *request_private_network(DBusConnection *conn,
363 DBusMessage *msg, void *data)
368 DBG("conn %p", conn);
370 sender = dbus_message_get_sender(msg);
372 err = __connman_private_network_request(msg, sender);
374 return __connman_error_failed(msg, -err);
379 static DBusMessage *release_private_network(DBusConnection *conn,
380 DBusMessage *msg, void *data)
385 DBG("conn %p", conn);
387 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
390 err = __connman_private_network_release(path);
392 return __connman_error_failed(msg, -err);
394 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
397 static GDBusMethodTable manager_methods[] = {
398 { "GetProperties", "", "a{sv}", get_properties },
399 { "SetProperty", "sv", "", set_property,
400 G_DBUS_METHOD_FLAG_ASYNC },
401 { "GetTechnologies", "", "a(oa{sv})", get_technologies },
402 { "RemoveProvider", "o", "", remove_provider },
403 { "GetServices", "", "a(oa{sv})", get_services },
404 { "ConnectProvider", "a{sv}", "o", connect_provider,
405 G_DBUS_METHOD_FLAG_ASYNC },
406 { "RegisterAgent", "o", "", register_agent },
407 { "UnregisterAgent", "o", "", unregister_agent },
408 { "RegisterCounter", "ouu", "", register_counter },
409 { "UnregisterCounter", "o", "", unregister_counter },
410 { "CreateSession", "a{sv}o", "o", create_session },
411 { "DestroySession", "o", "", destroy_session },
412 { "RequestPrivateNetwork", "", "oa{sv}h",
413 request_private_network,
414 G_DBUS_METHOD_FLAG_ASYNC },
415 { "ReleasePrivateNetwork", "o", "",
416 release_private_network },
420 static GDBusSignalTable manager_signals[] = {
421 { "PropertyChanged", "sv" },
422 { "TechnologyAdded", "oa{sv}" },
423 { "TechnologyRemoved", "o" },
424 { "ServicesAdded", "a(oa{sv})" },
425 { "ServicesRemoved", "a{o}" },
429 int __connman_manager_init(void)
433 connection = connman_dbus_get_connection();
434 if (connection == NULL)
437 if (connman_notifier_register(&technology_notifier) < 0)
438 connman_error("Failed to register technology notifier");
440 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
441 CONNMAN_MANAGER_INTERFACE,
443 manager_signals, NULL, NULL, NULL);
445 connman_state_idle = TRUE;
450 void __connman_manager_cleanup(void)
454 if (connection == NULL)
457 connman_notifier_unregister(&technology_notifier);
459 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
460 CONNMAN_MANAGER_INTERFACE);
462 dbus_connection_unref(connection);