5 * Copyright (C) 2007-2013 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 #include <connman/agent.h>
34 static bool connman_state_idle;
35 static dbus_bool_t sessionmode;
37 static DBusMessage *get_properties(DBusConnection *conn,
38 DBusMessage *msg, void *data)
41 DBusMessageIter array, dict;
42 dbus_bool_t offlinemode;
45 dbus_bool_t autoconnectmode;
50 reply = dbus_message_new_method_return(msg);
54 dbus_message_iter_init_append(reply, &array);
56 connman_dbus_dict_open(&array, &dict);
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_basic(&dict, "SessionMode",
70 autoconnectmode = __connman_service_get_auto_connect_mode();
71 connman_dbus_dict_append_basic(&dict, "AutoConnectMode",
76 connman_dbus_dict_close(&array, &dict);
81 static DBusMessage *set_property(DBusConnection *conn,
82 DBusMessage *msg, void *data)
84 DBusMessageIter iter, value;
90 if (!dbus_message_iter_init(msg, &iter))
91 return __connman_error_invalid_arguments(msg);
93 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
94 return __connman_error_invalid_arguments(msg);
96 dbus_message_iter_get_basic(&iter, &name);
97 dbus_message_iter_next(&iter);
99 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
100 return __connman_error_invalid_arguments(msg);
102 dbus_message_iter_recurse(&iter, &value);
104 type = dbus_message_iter_get_arg_type(&value);
106 if (g_str_equal(name, "OfflineMode")) {
107 dbus_bool_t offlinemode;
109 if (type != DBUS_TYPE_BOOLEAN)
110 return __connman_error_invalid_arguments(msg);
112 dbus_message_iter_get_basic(&value, &offlinemode);
116 if (connman_dbus_get_connection_unix_user_sync(conn,
117 dbus_message_get_sender(msg),
119 DBG("Can not get unix user id!");
120 return __connman_error_permission_denied(msg);
123 if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
124 DBG("Not allow this user to turn on offlinemode now!");
125 return __connman_error_permission_denied(msg);
128 __connman_technology_set_offlinemode(offlinemode);
129 } else if (g_str_equal(name, "SessionMode")) {
131 if (type != DBUS_TYPE_BOOLEAN)
132 return __connman_error_invalid_arguments(msg);
134 dbus_message_iter_get_basic(&value, &sessionmode);
136 #if defined TIZEN_EXT
137 else if (g_str_equal(name, "AutoConnectMode") == TRUE) {
140 if (type != DBUS_TYPE_BOOLEAN)
141 return __connman_error_invalid_arguments(msg);
143 dbus_message_iter_get_basic(&value, &automode);
145 __connman_service_set_auto_connect_mode(automode);
149 return __connman_error_invalid_property(msg);
151 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
154 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
156 __connman_technology_list_struct(iter);
159 static DBusMessage *get_technologies(DBusConnection *conn,
160 DBusMessage *msg, void *data)
164 #if !defined TIZEN_EXT
168 reply = dbus_message_new_method_return(msg);
172 __connman_dbus_append_objpath_dict_array(reply,
173 append_technology_structs, NULL);
178 static DBusMessage *remove_provider(DBusConnection *conn,
179 DBusMessage *msg, void *data)
184 DBG("conn %p", conn);
186 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
189 err = __connman_provider_remove_by_path(path);
191 return __connman_error_failed(msg, -err);
193 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
196 static DBusConnection *connection = NULL;
198 static void idle_state(bool idle)
201 DBG("idle %d", idle);
203 connman_state_idle = idle;
205 if (!connman_state_idle)
209 static struct connman_notifier technology_notifier = {
211 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
212 .idle_state = idle_state,
215 static void append_service_structs(DBusMessageIter *iter, void *user_data)
217 __connman_service_list_struct(iter);
220 static DBusMessage *get_services(DBusConnection *conn,
221 DBusMessage *msg, void *data)
225 reply = dbus_message_new_method_return(msg);
229 __connman_dbus_append_objpath_dict_array(reply,
230 append_service_structs, NULL);
235 static void append_peer_structs(DBusMessageIter *iter, void *user_data)
237 __connman_peer_list_struct(iter);
240 static DBusMessage *get_peers(DBusConnection *conn,
241 DBusMessage *msg, void *data)
245 reply = dbus_message_new_method_return(msg);
249 __connman_dbus_append_objpath_dict_array(reply,
250 append_peer_structs, NULL);
254 static DBusMessage *connect_provider(DBusConnection *conn,
255 DBusMessage *msg, void *data)
259 DBG("conn %p", conn);
261 err = __connman_provider_create_and_connect(msg);
263 return __connman_error_failed(msg, -err);
268 static DBusMessage *register_agent(DBusConnection *conn,
269 DBusMessage *msg, void *data)
271 const char *sender, *path;
274 DBG("conn %p", conn);
276 sender = dbus_message_get_sender(msg);
278 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
281 err = connman_agent_register(sender, path);
283 return __connman_error_failed(msg, -err);
285 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
288 static DBusMessage *unregister_agent(DBusConnection *conn,
289 DBusMessage *msg, void *data)
291 const char *sender, *path;
294 DBG("conn %p", conn);
296 sender = dbus_message_get_sender(msg);
298 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
301 err = connman_agent_unregister(sender, path);
303 return __connman_error_failed(msg, -err);
305 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
308 static DBusMessage *register_counter(DBusConnection *conn,
309 DBusMessage *msg, void *data)
311 const char *sender, *path;
312 unsigned int accuracy, period;
315 DBG("conn %p", conn);
317 sender = dbus_message_get_sender(msg);
319 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
320 DBUS_TYPE_UINT32, &accuracy,
321 DBUS_TYPE_UINT32, &period,
324 /* FIXME: add handling of accuracy parameter */
326 err = __connman_counter_register(sender, path, period);
328 return __connman_error_failed(msg, -err);
330 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
333 static DBusMessage *unregister_counter(DBusConnection *conn,
334 DBusMessage *msg, void *data)
336 const char *sender, *path;
339 DBG("conn %p", conn);
341 sender = dbus_message_get_sender(msg);
343 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
346 err = __connman_counter_unregister(sender, path);
348 return __connman_error_failed(msg, -err);
350 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
353 static DBusMessage *create_session(DBusConnection *conn,
354 DBusMessage *msg, void *data)
358 DBG("conn %p", conn);
360 err = __connman_session_create(msg);
362 if (err == -EINPROGRESS)
365 return __connman_error_failed(msg, -err);
368 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
371 static DBusMessage *destroy_session(DBusConnection *conn,
372 DBusMessage *msg, void *data)
376 DBG("conn %p", conn);
378 err = __connman_session_destroy(msg);
380 return __connman_error_failed(msg, -err);
382 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
385 static DBusMessage *request_private_network(DBusConnection *conn,
386 DBusMessage *msg, void *data)
391 DBG("conn %p", conn);
393 sender = dbus_message_get_sender(msg);
395 err = __connman_private_network_request(msg, sender);
397 return __connman_error_failed(msg, -err);
402 static DBusMessage *release_private_network(DBusConnection *conn,
403 DBusMessage *msg, void *data)
408 DBG("conn %p", conn);
410 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
413 err = __connman_private_network_release(path);
415 return __connman_error_failed(msg, -err);
417 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
420 static int parse_peers_service_specs(DBusMessageIter *array,
421 const unsigned char **spec, int *spec_len,
422 const unsigned char **query, int *query_len,
425 *spec = *query = NULL;
426 *spec_len = *query_len = *version = 0;
428 while (dbus_message_iter_get_arg_type(array) ==
429 DBUS_TYPE_DICT_ENTRY) {
430 DBusMessageIter entry, inter, value;
433 dbus_message_iter_recurse(array, &entry);
434 dbus_message_iter_get_basic(&entry, &key);
436 dbus_message_iter_next(&entry);
438 dbus_message_iter_recurse(&entry, &inter);
440 if (!g_strcmp0(key, "BonjourResponse")) {
441 dbus_message_iter_recurse(&inter, &value);
442 dbus_message_iter_get_fixed_array(&value,
444 } else if (!g_strcmp0(key, "BonjourQuery")) {
445 dbus_message_iter_recurse(&inter, &value);
446 dbus_message_iter_get_fixed_array(&value,
448 } else if (!g_strcmp0(key, "UpnpService")) {
449 dbus_message_iter_get_basic(&inter, spec);
450 *spec_len = strlen((const char *)*spec)+1;
451 } else if (!g_strcmp0(key, "UpnpVersion")) {
452 dbus_message_iter_get_basic(&inter, version);
453 } else if (!g_strcmp0(key, "WiFiDisplayIEs")) {
457 dbus_message_iter_recurse(&inter, &value);
458 dbus_message_iter_get_fixed_array(&value,
463 dbus_message_iter_next(array);
466 if ((*query && !*spec && !*version) ||
467 (!*spec && !*query) || (!*spec && *version))
473 static DBusMessage *register_peer_service(DBusConnection *conn,
474 DBusMessage *msg, void *data)
476 const unsigned char *spec, *query;
477 DBusMessageIter iter, array;
478 int spec_len, query_len;
486 owner = dbus_message_get_sender(msg);
488 dbus_message_iter_init(msg, &iter);
489 dbus_message_iter_recurse(&iter, &array);
491 ret = parse_peers_service_specs(&array, &spec, &spec_len,
492 &query, &query_len, &version);
496 dbus_message_iter_next(&iter);
497 dbus_message_iter_get_basic(&iter, &master);
499 ret = __connman_peer_service_register(owner, msg, spec, spec_len,
500 query, query_len, version,master);
502 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
503 if (ret == -EINPROGRESS)
506 return __connman_error_failed(msg, -ret);
509 static DBusMessage *unregister_peer_service(DBusConnection *conn,
510 DBusMessage *msg, void *data)
512 const unsigned char *spec, *query;
513 DBusMessageIter iter, array;
514 int spec_len, query_len;
521 owner = dbus_message_get_sender(msg);
523 dbus_message_iter_init(msg, &iter);
524 dbus_message_iter_recurse(&iter, &array);
526 ret = parse_peers_service_specs(&array, &spec, &spec_len,
527 &query, &query_len, &version);
531 ret = __connman_peer_service_unregister(owner, spec, spec_len,
532 query, query_len, version);
534 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
536 return __connman_error_failed(msg, -ret);
540 static const GDBusMethodTable manager_methods[] = {
541 { GDBUS_METHOD("GetProperties",
542 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
544 { GDBUS_ASYNC_METHOD("SetProperty",
545 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
546 NULL, set_property) },
547 { GDBUS_METHOD("GetTechnologies",
548 NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
550 { GDBUS_DEPRECATED_METHOD("RemoveProvider",
551 GDBUS_ARGS({ "provider", "o" }), NULL,
553 { GDBUS_METHOD("GetServices",
554 NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
556 { GDBUS_METHOD("GetPeers",
557 NULL, GDBUS_ARGS({ "peers", "a(oa{sv})" }),
559 { GDBUS_DEPRECATED_ASYNC_METHOD("ConnectProvider",
560 GDBUS_ARGS({ "provider", "a{sv}" }),
561 GDBUS_ARGS({ "path", "o" }),
563 { GDBUS_METHOD("RegisterAgent",
564 GDBUS_ARGS({ "path", "o" }), NULL,
566 { GDBUS_METHOD("UnregisterAgent",
567 GDBUS_ARGS({ "path", "o" }), NULL,
569 { GDBUS_METHOD("RegisterCounter",
570 GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
572 NULL, register_counter) },
573 { GDBUS_METHOD("UnregisterCounter",
574 GDBUS_ARGS({ "path", "o" }), NULL,
575 unregister_counter) },
576 { GDBUS_ASYNC_METHOD("CreateSession",
577 GDBUS_ARGS({ "settings", "a{sv}" },
578 { "notifier", "o" }),
579 GDBUS_ARGS({ "session", "o" }),
581 { GDBUS_METHOD("DestroySession",
582 GDBUS_ARGS({ "session", "o" }), NULL,
584 { GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
585 NULL, GDBUS_ARGS({ "path", "o" },
586 { "settings", "a{sv}" },
588 request_private_network) },
589 { GDBUS_METHOD("ReleasePrivateNetwork",
590 GDBUS_ARGS({ "path", "o" }), NULL,
591 release_private_network) },
592 { GDBUS_ASYNC_METHOD("RegisterPeerService",
593 GDBUS_ARGS({ "specification", "a{sv}" },
594 { "master", "b" }), NULL,
595 register_peer_service) },
596 { GDBUS_METHOD("UnregisterPeerService",
597 GDBUS_ARGS({ "specification", "a{sv}" }), NULL,
598 unregister_peer_service) },
602 static const GDBusSignalTable manager_signals[] = {
603 { GDBUS_SIGNAL("PropertyChanged",
604 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
605 { GDBUS_SIGNAL("TechnologyAdded",
606 GDBUS_ARGS({ "path", "o" },
607 { "properties", "a{sv}" })) },
608 { GDBUS_SIGNAL("TechnologyRemoved",
609 GDBUS_ARGS({ "path", "o" })) },
610 { GDBUS_SIGNAL("ServicesChanged",
611 GDBUS_ARGS({ "changed", "a(oa{sv})" },
612 { "removed", "ao" })) },
613 { GDBUS_SIGNAL("PeersChanged",
614 GDBUS_ARGS({ "changed", "a(oa{sv})" },
615 { "removed", "ao" })) },
619 int __connman_manager_init(void)
623 connection = connman_dbus_get_connection();
627 if (connman_notifier_register(&technology_notifier) < 0)
628 connman_error("Failed to register technology notifier");
630 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
631 CONNMAN_MANAGER_INTERFACE,
633 manager_signals, NULL, NULL, NULL);
635 connman_state_idle = true;
640 void __connman_manager_cleanup(void)
647 connman_notifier_unregister(&technology_notifier);
649 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
650 CONNMAN_MANAGER_INTERFACE);
652 dbus_connection_unref(connection);