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);
114 __connman_technology_set_offlinemode(offlinemode);
115 } else if (g_str_equal(name, "SessionMode")) {
117 if (type != DBUS_TYPE_BOOLEAN)
118 return __connman_error_invalid_arguments(msg);
120 dbus_message_iter_get_basic(&value, &sessionmode);
121 #if defined TIZEN_EXT
122 } else if (g_str_equal(name, "AutoConnectMode") == TRUE) {
125 if (type != DBUS_TYPE_BOOLEAN)
126 return __connman_error_invalid_arguments(msg);
128 dbus_message_iter_get_basic(&value, &automode);
130 __connman_service_set_auto_connect_mode(automode);
133 return __connman_error_invalid_property(msg);
135 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
138 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
140 __connman_technology_list_struct(iter);
143 static DBusMessage *get_technologies(DBusConnection *conn,
144 DBusMessage *msg, void *data)
148 #if !defined TIZEN_EXT
152 reply = dbus_message_new_method_return(msg);
156 __connman_dbus_append_objpath_dict_array(reply,
157 append_technology_structs, NULL);
162 static DBusMessage *remove_provider(DBusConnection *conn,
163 DBusMessage *msg, void *data)
168 DBG("conn %p", conn);
170 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
173 err = __connman_provider_remove_by_path(path);
175 return __connman_error_failed(msg, -err);
177 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
180 static DBusConnection *connection = NULL;
182 static void idle_state(bool idle)
185 DBG("idle %d", idle);
187 connman_state_idle = idle;
189 if (!connman_state_idle)
193 static const struct connman_notifier technology_notifier = {
195 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
196 .idle_state = idle_state,
199 static void append_service_structs(DBusMessageIter *iter, void *user_data)
201 __connman_service_list_struct(iter);
204 static DBusMessage *get_services(DBusConnection *conn,
205 DBusMessage *msg, void *data)
209 reply = dbus_message_new_method_return(msg);
213 __connman_dbus_append_objpath_dict_array(reply,
214 append_service_structs, NULL);
219 static void append_peer_structs(DBusMessageIter *iter, void *user_data)
221 __connman_peer_list_struct(iter);
224 static DBusMessage *get_peers(DBusConnection *conn,
225 DBusMessage *msg, void *data)
229 reply = dbus_message_new_method_return(msg);
233 __connman_dbus_append_objpath_dict_array(reply,
234 append_peer_structs, NULL);
238 static DBusMessage *get_tethering_clients(DBusConnection *conn,
239 DBusMessage *msg, void *data)
242 DBusMessageIter iter, array;
244 reply = dbus_message_new_method_return(msg);
248 dbus_message_iter_init_append(reply, &iter);
250 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
251 DBUS_TYPE_STRING_AS_STRING, &array);
253 __connman_tethering_list_clients(&array);
255 dbus_message_iter_close_container(&iter, &array);
259 static DBusMessage *connect_provider(DBusConnection *conn,
260 DBusMessage *msg, void *data)
264 DBG("conn %p", conn);
266 err = __connman_provider_create_and_connect(msg);
268 return __connman_error_failed(msg, -err);
273 static DBusMessage *register_agent(DBusConnection *conn,
274 DBusMessage *msg, void *data)
276 const char *sender, *path;
279 DBG("conn %p", conn);
281 sender = dbus_message_get_sender(msg);
283 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
286 err = connman_agent_register(sender, path);
288 return __connman_error_failed(msg, -err);
290 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
293 static DBusMessage *unregister_agent(DBusConnection *conn,
294 DBusMessage *msg, void *data)
296 const char *sender, *path;
299 DBG("conn %p", conn);
301 sender = dbus_message_get_sender(msg);
303 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
306 err = connman_agent_unregister(sender, path);
308 return __connman_error_failed(msg, -err);
310 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
313 static DBusMessage *register_counter(DBusConnection *conn,
314 DBusMessage *msg, void *data)
316 const char *sender, *path;
317 unsigned int accuracy, period;
320 DBG("conn %p", conn);
322 sender = dbus_message_get_sender(msg);
324 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
325 DBUS_TYPE_UINT32, &accuracy,
326 DBUS_TYPE_UINT32, &period,
329 /* FIXME: add handling of accuracy parameter */
331 err = __connman_counter_register(sender, path, period);
333 return __connman_error_failed(msg, -err);
335 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
338 static DBusMessage *unregister_counter(DBusConnection *conn,
339 DBusMessage *msg, void *data)
341 const char *sender, *path;
344 DBG("conn %p", conn);
346 sender = dbus_message_get_sender(msg);
348 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
351 err = __connman_counter_unregister(sender, path);
353 return __connman_error_failed(msg, -err);
355 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
358 static DBusMessage *create_session(DBusConnection *conn,
359 DBusMessage *msg, void *data)
363 DBG("conn %p", conn);
365 err = __connman_session_create(msg);
367 if (err == -EINPROGRESS)
370 return __connman_error_failed(msg, -err);
373 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
376 static DBusMessage *destroy_session(DBusConnection *conn,
377 DBusMessage *msg, void *data)
381 DBG("conn %p", conn);
383 err = __connman_session_destroy(msg);
385 return __connman_error_failed(msg, -err);
387 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
390 static DBusMessage *request_private_network(DBusConnection *conn,
391 DBusMessage *msg, void *data)
396 DBG("conn %p", conn);
398 sender = dbus_message_get_sender(msg);
400 err = __connman_private_network_request(msg, sender);
402 return __connman_error_failed(msg, -err);
407 static DBusMessage *release_private_network(DBusConnection *conn,
408 DBusMessage *msg, void *data)
413 DBG("conn %p", conn);
415 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
418 err = __connman_private_network_release(path);
420 return __connman_error_failed(msg, -err);
422 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
425 static int parse_peers_service_specs(DBusMessageIter *array,
426 const unsigned char **spec, int *spec_len,
427 const unsigned char **query, int *query_len,
430 *spec = *query = NULL;
431 *spec_len = *query_len = *version = 0;
433 while (dbus_message_iter_get_arg_type(array) ==
434 DBUS_TYPE_DICT_ENTRY) {
435 DBusMessageIter entry, inter, value;
438 dbus_message_iter_recurse(array, &entry);
439 dbus_message_iter_get_basic(&entry, &key);
441 dbus_message_iter_next(&entry);
443 dbus_message_iter_recurse(&entry, &inter);
445 if (!g_strcmp0(key, "BonjourResponse")) {
446 dbus_message_iter_recurse(&inter, &value);
447 dbus_message_iter_get_fixed_array(&value,
449 } else if (!g_strcmp0(key, "BonjourQuery")) {
450 dbus_message_iter_recurse(&inter, &value);
451 dbus_message_iter_get_fixed_array(&value,
453 } else if (!g_strcmp0(key, "UpnpService")) {
454 dbus_message_iter_get_basic(&inter, spec);
455 *spec_len = strlen((const char *)*spec)+1;
456 } else if (!g_strcmp0(key, "UpnpVersion")) {
457 dbus_message_iter_get_basic(&inter, version);
458 } else if (!g_strcmp0(key, "WiFiDisplayIEs")) {
462 dbus_message_iter_recurse(&inter, &value);
463 dbus_message_iter_get_fixed_array(&value,
468 dbus_message_iter_next(array);
471 if ((*query && !*spec && !*version) ||
472 (!*spec && !*query) || (!*spec && *version))
478 static DBusMessage *register_peer_service(DBusConnection *conn,
479 DBusMessage *msg, void *data)
481 const unsigned char *spec, *query;
482 DBusMessageIter iter, array;
483 int spec_len, query_len;
491 owner = dbus_message_get_sender(msg);
493 dbus_message_iter_init(msg, &iter);
494 dbus_message_iter_recurse(&iter, &array);
496 ret = parse_peers_service_specs(&array, &spec, &spec_len,
497 &query, &query_len, &version);
501 dbus_message_iter_next(&iter);
502 dbus_message_iter_get_basic(&iter, &master);
504 ret = __connman_peer_service_register(owner, msg, spec, spec_len,
505 query, query_len, version,master);
507 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
508 if (ret == -EINPROGRESS)
511 return __connman_error_failed(msg, -ret);
514 static DBusMessage *unregister_peer_service(DBusConnection *conn,
515 DBusMessage *msg, void *data)
517 const unsigned char *spec, *query;
518 DBusMessageIter iter, array;
519 int spec_len, query_len;
526 owner = dbus_message_get_sender(msg);
528 dbus_message_iter_init(msg, &iter);
529 dbus_message_iter_recurse(&iter, &array);
531 ret = parse_peers_service_specs(&array, &spec, &spec_len,
532 &query, &query_len, &version);
536 ret = __connman_peer_service_unregister(owner, spec, spec_len,
537 query, query_len, version);
539 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
541 return __connman_error_failed(msg, -ret);
545 #if defined TIZEN_EXT_WIFI_MESH
546 static void append_mesh_peer_structs(DBusMessageIter *iter, void *user_data)
548 __connman_mesh_peer_list_struct(iter);
551 static DBusMessage *get_mesh_peers(DBusConnection *conn,
552 DBusMessage *msg, void *data)
556 reply = dbus_message_new_method_return(msg);
560 __connman_dbus_append_objpath_dict_array(reply,
561 append_mesh_peer_structs, NULL);
565 static DBusMessage *get_connected_mesh_peers(DBusConnection *conn,
566 DBusMessage *msg, void *data)
569 DBusMessageIter iter, array;
571 reply = dbus_message_new_method_return(msg);
575 dbus_message_iter_init_append(reply, &iter);
576 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
577 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
578 DBUS_TYPE_ARRAY_AS_STRING
579 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
580 DBUS_TYPE_STRING_AS_STRING
581 DBUS_TYPE_VARIANT_AS_STRING
582 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
583 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
585 __connman_mesh_connected_peer_list_struct(&array);
586 dbus_message_iter_close_container(&iter, &array);
590 static DBusMessage *get_disconnected_mesh_peers(DBusConnection *conn,
591 DBusMessage *msg, void *data)
594 DBusMessageIter iter, array;
596 reply = dbus_message_new_method_return(msg);
600 dbus_message_iter_init_append(reply, &iter);
601 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
602 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
603 DBUS_TYPE_ARRAY_AS_STRING
604 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
605 DBUS_TYPE_STRING_AS_STRING
606 DBUS_TYPE_VARIANT_AS_STRING
607 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
608 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
610 __connman_mesh_disconnected_peer_list_struct(&array);
611 dbus_message_iter_close_container(&iter, &array);
615 static DBusMessage *mesh_add_peer(DBusConnection *conn,
616 DBusMessage *msg, void *user_data)
621 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &addr,
624 DBG("Address %s", addr);
626 err = __connman_mesh_change_peer_status(msg, addr, CONNMAN_MESH_PEER_ADD);
628 return __connman_error_failed(msg, -err);
633 static DBusMessage *mesh_remove_peer(DBusConnection *conn,
634 DBusMessage *msg, void *user_data)
639 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &addr,
642 DBG("Address %s", addr);
644 err = __connman_mesh_change_peer_status(msg, addr,
645 CONNMAN_MESH_PEER_REMOVE);
647 return __connman_error_failed(msg, -err);
653 static const GDBusMethodTable manager_methods[] = {
654 { GDBUS_METHOD("GetProperties",
655 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
657 { GDBUS_ASYNC_METHOD("SetProperty",
658 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
659 NULL, set_property) },
660 { GDBUS_METHOD("GetTechnologies",
661 NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
663 { GDBUS_DEPRECATED_METHOD("RemoveProvider",
664 GDBUS_ARGS({ "provider", "o" }), NULL,
666 { GDBUS_METHOD("GetServices",
667 NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
669 { GDBUS_METHOD("GetPeers",
670 NULL, GDBUS_ARGS({ "peers", "a(oa{sv})" }),
672 { GDBUS_METHOD("GetTetheringClients",
673 NULL, GDBUS_ARGS({ "tethering_clients", "as" }),
674 get_tethering_clients) },
675 { GDBUS_DEPRECATED_ASYNC_METHOD("ConnectProvider",
676 GDBUS_ARGS({ "provider", "a{sv}" }),
677 GDBUS_ARGS({ "path", "o" }),
679 { GDBUS_METHOD("RegisterAgent",
680 GDBUS_ARGS({ "path", "o" }), NULL,
682 { GDBUS_METHOD("UnregisterAgent",
683 GDBUS_ARGS({ "path", "o" }), NULL,
685 { GDBUS_METHOD("RegisterCounter",
686 GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
688 NULL, register_counter) },
689 { GDBUS_METHOD("UnregisterCounter",
690 GDBUS_ARGS({ "path", "o" }), NULL,
691 unregister_counter) },
692 { GDBUS_ASYNC_METHOD("CreateSession",
693 GDBUS_ARGS({ "settings", "a{sv}" },
694 { "notifier", "o" }),
695 GDBUS_ARGS({ "session", "o" }),
697 { GDBUS_METHOD("DestroySession",
698 GDBUS_ARGS({ "session", "o" }), NULL,
700 { GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
701 NULL, GDBUS_ARGS({ "path", "o" },
702 { "settings", "a{sv}" },
704 request_private_network) },
705 { GDBUS_METHOD("ReleasePrivateNetwork",
706 GDBUS_ARGS({ "path", "o" }), NULL,
707 release_private_network) },
708 { GDBUS_ASYNC_METHOD("RegisterPeerService",
709 GDBUS_ARGS({ "specification", "a{sv}" },
710 { "master", "b" }), NULL,
711 register_peer_service) },
712 { GDBUS_METHOD("UnregisterPeerService",
713 GDBUS_ARGS({ "specification", "a{sv}" }), NULL,
714 unregister_peer_service) },
715 #if defined TIZEN_EXT_WIFI_MESH
716 { GDBUS_METHOD("GetMeshPeers",
717 NULL, GDBUS_ARGS({ "peers", "a(oa{sv})" }),
719 { GDBUS_METHOD("GetConnectedMeshPeers",
720 NULL, GDBUS_ARGS({ "peers", "a(a{sv})" }),
721 get_connected_mesh_peers) },
722 { GDBUS_METHOD("GetDisconnectedMeshPeers",
723 NULL, GDBUS_ARGS({ "peers", "a(a{sv})" }),
724 get_disconnected_mesh_peers) },
725 { GDBUS_ASYNC_METHOD("MeshAddPeer", GDBUS_ARGS({ "address", "s" }), NULL,
727 { GDBUS_ASYNC_METHOD("MeshRemovePeer", GDBUS_ARGS({ "address", "s" }), NULL,
733 static const GDBusSignalTable manager_signals[] = {
734 { GDBUS_SIGNAL("PropertyChanged",
735 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
736 { GDBUS_SIGNAL("TechnologyAdded",
737 GDBUS_ARGS({ "path", "o" },
738 { "properties", "a{sv}" })) },
739 { GDBUS_SIGNAL("TechnologyRemoved",
740 GDBUS_ARGS({ "path", "o" })) },
741 { GDBUS_SIGNAL("ServicesChanged",
742 GDBUS_ARGS({ "changed", "a(oa{sv})" },
743 { "removed", "ao" })) },
744 { GDBUS_SIGNAL("PeersChanged",
745 GDBUS_ARGS({ "changed", "a(oa{sv})" },
746 { "removed", "ao" })) },
750 int __connman_manager_init(void)
754 connection = connman_dbus_get_connection();
758 if (connman_notifier_register(&technology_notifier) < 0)
759 connman_error("Failed to register technology notifier");
761 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
762 CONNMAN_MANAGER_INTERFACE,
764 manager_signals, NULL, NULL, NULL);
766 connman_state_idle = true;
771 void __connman_manager_cleanup(void)
778 connman_notifier_unregister(&technology_notifier);
780 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
781 CONNMAN_MANAGER_INTERFACE);
783 dbus_connection_unref(connection);