5 * Copyright (C) 2012-2014 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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
39 #include "dbus_helpers.h"
42 #if defined TIZEN_EXT_INS
45 #include "tethering.h"
49 #include "vpnconnections.h"
50 #if defined TIZEN_EXT_WIFI_MESH
54 static DBusConnection *connection;
55 static GHashTable *service_hash;
56 static GHashTable *vpnconnection_hash;
57 static GHashTable *peer_hash;
58 static GHashTable *technology_hash;
59 static char *session_notify_path;
60 static char *session_path;
61 static bool session_connected;
63 struct connman_option {
69 static char *ipv4[] = {
77 static char *ipv6[] = {
85 static int cmd_help(char *args[], int num, struct connman_option *options);
87 static bool check_dbus_name(const char *name)
90 * Valid dbus chars should be [A-Z][a-z][0-9]_
91 * and should not start with number.
95 if (!name || name[0] == '\0')
98 for (i = 0; name[i] != '\0'; i++)
99 if (!((name[i] >= 'A' && name[i] <= 'Z') ||
100 (name[i] >= 'a' && name[i] <= 'z') ||
101 (name[i] >= '0' && name[i] <= '9') ||
108 static int parse_boolean(char *arg)
113 if (strcasecmp(arg, "no") == 0 ||
114 strcasecmp(arg, "false") == 0 ||
115 strcasecmp(arg, "off" ) == 0 ||
116 strcasecmp(arg, "disable" ) == 0 ||
117 strcasecmp(arg, "n") == 0 ||
118 strcasecmp(arg, "f") == 0 ||
119 strcasecmp(arg, "0") == 0)
122 if (strcasecmp(arg, "yes") == 0 ||
123 strcasecmp(arg, "true") == 0 ||
124 strcasecmp(arg, "on") == 0 ||
125 strcasecmp(arg, "enable" ) == 0 ||
126 strcasecmp(arg, "y") == 0 ||
127 strcasecmp(arg, "t") == 0 ||
128 strcasecmp(arg, "1") == 0)
134 static int parse_args(char *arg, struct connman_option *options)
141 for (i = 0; options[i].name; i++) {
142 if (strcmp(options[i].name, arg) == 0 ||
143 (strncmp(arg, "--", 2) == 0 &&
144 strcmp(&arg[2], options[i].name) == 0))
145 return options[i].val;
151 static int enable_return(DBusMessageIter *iter, const char *error,
154 char *tech = user_data;
157 str = strrchr(tech, '/');
164 fprintf(stdout, "Enabled %s\n", str);
166 fprintf(stderr, "Error %s: %s\n", str, error);
173 static int cmd_enable(char *args[], int num, struct connman_option *options)
176 dbus_bool_t b = TRUE;
184 if (check_dbus_name(args[1]) == false)
187 if (strcmp(args[1], "offline") == 0) {
188 tech = g_strdup(args[1]);
189 return __connmanctl_dbus_set_property(connection, "/",
190 "net.connman.Manager", enable_return, tech,
191 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
194 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
195 return __connmanctl_dbus_set_property(connection, tech,
196 "net.connman.Technology", enable_return, tech,
197 "Powered", DBUS_TYPE_BOOLEAN, &b);
200 static int disable_return(DBusMessageIter *iter, const char *error,
203 char *tech = user_data;
206 str = strrchr(tech, '/');
213 fprintf(stdout, "Disabled %s\n", str);
215 fprintf(stderr, "Error %s: %s\n", str, error);
222 static int cmd_disable(char *args[], int num, struct connman_option *options)
225 dbus_bool_t b = FALSE;
233 if (check_dbus_name(args[1]) == false)
236 if (strcmp(args[1], "offline") == 0) {
237 tech = g_strdup(args[1]);
238 return __connmanctl_dbus_set_property(connection, "/",
239 "net.connman.Manager", disable_return, tech,
240 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
243 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
244 return __connmanctl_dbus_set_property(connection, tech,
245 "net.connman.Technology", disable_return, tech,
246 "Powered", DBUS_TYPE_BOOLEAN, &b);
249 static int state_print(DBusMessageIter *iter, const char *error,
252 DBusMessageIter entry;
255 fprintf(stderr, "Error: %s\n", error);
259 dbus_message_iter_recurse(iter, &entry);
260 __connmanctl_dbus_print(&entry, " ", " = ", "\n");
261 fprintf(stdout, "\n");
266 static int cmd_state(char *args[], int num, struct connman_option *options)
271 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
272 CONNMAN_PATH, "net.connman.Manager", "GetProperties",
273 state_print, NULL, NULL, NULL);
276 static int clock_print(DBusMessageIter *iter, const char *error,
279 DBusMessageIter entry;
282 fprintf(stderr, "Error: %s\n", error);
286 dbus_message_iter_recurse(iter, &entry);
287 __connmanctl_dbus_print(&entry, " ", " = ", "\n");
288 fprintf(stdout, "\n");
293 static int cmd_clock(char *args[], int num, struct connman_option *options)
298 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
299 CONNMAN_PATH, "net.connman.Clock", "GetProperties",
300 clock_print, NULL, NULL, NULL);
303 static int services_list(DBusMessageIter *iter, const char *error,
307 __connmanctl_services_list(iter);
308 fprintf(stdout, "\n");
310 fprintf(stderr, "Error: %s\n", error);
316 #if defined TIZEN_EXT_INS
317 static int ins_list(DBusMessageIter *iter, const char *error,
320 char *filter = user_data;
323 __connmanctl_ins_list(iter, filter);
324 fprintf(stdout, "\n");
326 fprintf(stderr, "Error: %s\n", error);
335 static int peers_list(DBusMessageIter *iter,
336 const char *error, void *user_data)
339 __connmanctl_peers_list(iter);
340 fprintf(stdout, "\n");
342 fprintf(stderr, "Error: %s\n", error);
347 static int tethering_clients_list(DBusMessageIter *iter,
348 const char *error, void *user_data)
351 __connmanctl_tethering_clients_list(iter);
352 fprintf(stdout, "\n");
354 fprintf(stderr, "Error: %s\n", error);
359 static int object_properties(DBusMessageIter *iter,
360 const char *error, void *user_data)
362 char *path = user_data;
364 DBusMessageIter dict;
367 fprintf(stdout, "%s\n", path);
369 dbus_message_iter_recurse(iter, &dict);
370 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
372 fprintf(stdout, "\n");
375 str = strrchr(path, '/');
381 fprintf(stderr, "Error %s: %s\n", str, error);
389 static int cmd_services(char *args[], int num, struct connman_option *options)
391 char *service_name = NULL;
398 c = parse_args(args[1], options);
405 service_name = args[2];
410 service_name = args[1];
415 return __connmanctl_dbus_method_call(connection,
416 CONNMAN_SERVICE, CONNMAN_PATH,
417 "net.connman.Manager", "GetServices",
418 services_list, NULL, NULL, NULL);
421 if (check_dbus_name(service_name) == false)
424 path = g_strdup_printf("/net/connman/service/%s", service_name);
425 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE, path,
426 "net.connman.Service", "GetProperties",
427 object_properties, path, NULL, NULL);
430 #if defined TIZEN_EXT_INS
431 static int cmd_ins(char *args[], int num, struct connman_option *options)
439 filter = g_strdup(args[1]);
441 return __connmanctl_dbus_method_call(connection,
442 CONNMAN_SERVICE, CONNMAN_PATH,
443 "net.connman.Manager", "GetINS",
444 ins_list, filter, NULL, NULL);
448 static int cmd_peers(char *args[], int num, struct connman_option *options)
450 char *peer_name = NULL;
460 return __connmanctl_dbus_method_call(connection,
461 CONNMAN_SERVICE, CONNMAN_PATH,
462 "net.connman.Manager", "GetPeers",
463 peers_list, NULL, NULL, NULL);
466 if (check_dbus_name(peer_name) == false)
469 path = g_strdup_printf("/net/connman/peer/%s", peer_name);
470 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
471 path, "net.connman.Peer", "GetProperties",
472 object_properties, path, NULL, NULL);
475 static int technology_print(DBusMessageIter *iter, const char *error,
478 DBusMessageIter array;
481 fprintf(stderr, "Error: %s\n", error);
485 dbus_message_iter_recurse(iter, &array);
486 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
487 DBusMessageIter entry, dict;
490 dbus_message_iter_recurse(&array, &entry);
491 dbus_message_iter_get_basic(&entry, &path);
492 fprintf(stdout, "%s\n", path);
494 dbus_message_iter_next(&entry);
496 dbus_message_iter_recurse(&entry, &dict);
497 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
498 fprintf(stdout, "\n");
500 dbus_message_iter_next(&array);
506 static int cmd_technologies(char *args[], int num,
507 struct connman_option *options)
512 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
513 CONNMAN_PATH, "net.connman.Manager", "GetTechnologies",
514 technology_print, NULL, NULL, NULL);
517 struct tether_enable {
522 static int tether_set_return(DBusMessageIter *iter, const char *error,
525 struct tether_enable *tether = user_data;
528 str = strrchr(tether->path, '/');
535 fprintf(stdout, "%s tethering for %s\n",
536 tether->enable ? "Enabled" : "Disabled",
539 fprintf(stderr, "Error %s %s tethering: %s\n",
541 "enabling" : "disabling", str, error);
543 g_free(tether->path);
549 static int tether_set(char *technology, int set_tethering)
551 struct tether_enable *tether = g_new(struct tether_enable, 1);
553 switch(set_tethering) {
555 tether->enable = TRUE;
558 tether->enable = FALSE;
565 tether->path = g_strdup_printf("/net/connman/technology/%s",
568 return __connmanctl_dbus_set_property(connection, tether->path,
569 "net.connman.Technology", tether_set_return,
570 tether, "Tethering", DBUS_TYPE_BOOLEAN,
574 struct tether_properties {
576 int passphrase_result;
580 static int tether_update(struct tether_properties *tether)
584 if (tether->ssid_result == 0 && tether->passphrase_result == 0) {
585 ret = tether_set("wifi", tether->set_tethering);
590 if (tether->ssid_result != -EINPROGRESS &&
591 tether->passphrase_result != -EINPROGRESS) {
599 static int tether_set_ssid_return(DBusMessageIter *iter, const char *error,
602 struct tether_properties *tether = user_data;
605 fprintf(stdout, "Wifi SSID set\n");
606 tether->ssid_result = 0;
608 fprintf(stderr, "Error setting wifi SSID: %s\n", error);
609 tether->ssid_result = -EINVAL;
612 return tether_update(tether);
615 static int tether_set_passphrase_return(DBusMessageIter *iter,
616 const char *error, void *user_data)
618 struct tether_properties *tether = user_data;
621 fprintf(stdout, "Wifi passphrase set\n");
622 tether->passphrase_result = 0;
624 fprintf(stderr, "Error setting wifi passphrase: %s\n", error);
625 tether->passphrase_result = -EINVAL;
628 return tether_update(tether);
631 static int tether_set_ssid(char *ssid, char *passphrase, int set_tethering)
633 struct tether_properties *tether = g_new(struct tether_properties, 1);
635 tether->set_tethering = set_tethering;
637 tether->ssid_result = __connmanctl_dbus_set_property(connection,
638 "/net/connman/technology/wifi",
639 "net.connman.Technology",
640 tether_set_ssid_return, tether,
641 "TetheringIdentifier", DBUS_TYPE_STRING, &ssid);
643 tether->passphrase_result =__connmanctl_dbus_set_property(connection,
644 "/net/connman/technology/wifi",
645 "net.connman.Technology",
646 tether_set_passphrase_return, tether,
647 "TetheringPassphrase", DBUS_TYPE_STRING, &passphrase);
649 if (tether->ssid_result != -EINPROGRESS &&
650 tether->passphrase_result != -EINPROGRESS) {
658 #if defined TIZEN_EXT_WIFI_MESH
659 struct mesh_if_prop {
665 struct mesh_create_network {
671 struct mesh_specific_scan_params {
676 struct mesh_gate_params {
682 static int mesh_return(DBusMessageIter *iter, const char *error,
685 char *method = user_data;
688 fprintf(stderr, "Error %s: %s\n", method, error);
690 fprintf(stderr, "Success %s\n", method);
697 static void mesh_interface_add_append(DBusMessageIter *iter, void *user_data)
699 struct mesh_if_prop *append = user_data;
701 /* Append Virtual Interface Name */
702 __connmanctl_dbus_append_dict_entry(iter, "Ifname",
703 DBUS_TYPE_STRING, &append->ifname);
705 /* Append Parent WiFi Interface Name */
706 __connmanctl_dbus_append_dict_entry(iter, "ParentIfname",
707 DBUS_TYPE_STRING, &append->parent_ifname);
709 /* Append Bridge Interface Name */
710 if (append->bridge_ifname)
711 __connmanctl_dbus_append_dict_entry(iter, "BridgeIfname",
712 DBUS_TYPE_STRING, &append->bridge_ifname);
715 static void mesh_interface_remove_append(DBusMessageIter *iter, void *user_data)
717 struct mesh_if_prop *append = user_data;
719 /* Append Virtual Interface Name */
720 __connmanctl_dbus_append_dict_entry(iter, "Ifname",
721 DBUS_TYPE_STRING, &append->ifname);
724 static void mesh_create_network_append(DBusMessageIter *iter, void *user_data)
726 struct mesh_create_network *append = user_data;
728 /* Append Mesh Network Name */
729 __connmanctl_dbus_append_dict_entry(iter, "Name",
730 DBUS_TYPE_STRING, &append->name);
732 /* Append Mesh Network Frequency */
733 __connmanctl_dbus_append_dict_entry(iter, "Frequency",
734 DBUS_TYPE_UINT16, &append->freq);
736 /* Append Mesh Network Security Type */
737 __connmanctl_dbus_append_dict_entry(iter, "Security",
738 DBUS_TYPE_STRING, &append->sec_type);
741 static void mesh_specific_scan_append(DBusMessageIter *iter, void *user_data)
743 struct mesh_specific_scan_params *append = user_data;
745 /* Append Mesh Network Name */
746 __connmanctl_dbus_append_dict_entry(iter, "Name",
747 DBUS_TYPE_STRING, &append->name);
749 /* Append Mesh Network Frequency */
750 __connmanctl_dbus_append_dict_entry(iter, "Frequency",
751 DBUS_TYPE_UINT16, &append->freq);
754 static void mesh_set_gate_append(DBusMessageIter *iter, void *user_data)
756 struct mesh_gate_params *append = user_data;
758 /* Append Gate Announce Protocol */
759 __connmanctl_dbus_append_dict_entry(iter, "GateAnnounce",
760 DBUS_TYPE_BOOLEAN, &append->gate_announce);
762 /* Append HWMP Root Mode */
763 __connmanctl_dbus_append_dict_entry(iter, "HWMPRootMode",
764 DBUS_TYPE_UINT16, &append->hwmp_rootmode);
767 __connmanctl_dbus_append_dict_entry(iter, "STP", DBUS_TYPE_UINT16,
771 static void mesh_peer_append(DBusMessageIter *iter, void *user_data)
773 char *peer_addr = user_data;
775 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &peer_addr);
780 static int mesh_peers_list(DBusMessageIter *iter,
781 const char *error, void *user_data)
784 __connmanctl_mesh_peers_list(iter);
785 fprintf(stdout, "\n");
787 fprintf(stderr, "Error: %s\n", error);
792 static int connected_mesh_peers_list(DBusMessageIter *iter,
793 const char *error, void *user_data)
796 __connmanctl_mesh_connected_peers_list(iter);
797 fprintf(stdout, "\n");
799 fprintf(stderr, "Error: %s\n", error);
804 static int disconnected_mesh_peers_list(DBusMessageIter *iter,
805 const char *error, void *user_data)
808 __connmanctl_mesh_disconnected_peers_list(iter);
809 fprintf(stdout, "\n");
811 fprintf(stderr, "Error: %s\n", error);
816 static int mesh_connect_return(DBusMessageIter *iter, const char *error,
819 char *path = user_data;
822 char *str = strrchr(path, '/');
824 fprintf(stdout, "Connected %s\n", str);
826 fprintf(stderr, "Error %s: %s\n", path, error);
833 static int mesh_disconnect_return(DBusMessageIter *iter, const char *error,
836 char *path = user_data;
839 char *str = strrchr(path, '/');
841 fprintf(stdout, "Disconnected %s\n", str);
843 fprintf(stderr, "Error %s: %s\n", path, error);
850 static int mesh_remove_return(DBusMessageIter *iter, const char *error,
853 char *path = user_data;
856 char *str = strrchr(path, '/');
858 fprintf(stdout, "Removed %s\n", str);
860 fprintf(stderr, "Error %s: %s\n", path, error);
867 static int mesh_config_return(DBusMessageIter *iter, const char *error,
870 char *path = user_data;
871 char *str = strrchr(path, '/');
875 fprintf(stderr, "Error %s: %s\n", path, error);
877 fprintf(stdout, "Success SetProperty %s\n", str);
884 static int cmd_mesh(char *args[], int num, struct connman_option *options)
890 char *mesh_peer_name = NULL;
891 char *mesh_peer_path = NULL;
892 char *property = NULL;
894 struct mesh_if_prop *append;
895 struct mesh_create_network *network;
896 struct mesh_specific_scan_params *scan_params;
897 struct mesh_gate_params *gate_params;
898 char *mesh_peer_addr = NULL;
900 c = parse_args(args[1], options);
904 if (num < 4 || num > 5) {
908 path = g_strdup_printf("/net/connman/technology/mesh");
910 append = dbus_malloc0(sizeof(struct mesh_if_prop));
911 append->ifname = g_strdup(args[2]);
912 append->parent_ifname = g_strdup(args[3]);
914 append->bridge_ifname = g_strdup(args[4]);
915 method = g_strdup("MeshInterfaceAdd");
916 result = __connmanctl_dbus_mesh_dict(connection, path,
917 "net.connman.Technology", mesh_return, method,
918 "MeshInterfaceAdd", DBUS_TYPE_STRING,
919 mesh_interface_add_append, append);
920 g_free(append->ifname);
921 g_free(append->parent_ifname);
922 g_free(append->bridge_ifname);
931 path = g_strdup_printf("/net/connman/technology/mesh");
933 append = dbus_malloc0(sizeof(struct mesh_if_prop));
934 append->ifname = g_strdup(args[2]);
935 method = g_strdup("MeshInterfaceRemove");
936 result = __connmanctl_dbus_mesh_dict(connection, path,
937 "net.connman.Technology", mesh_return, method,
938 "MeshInterfaceRemove", DBUS_TYPE_STRING,
939 mesh_interface_remove_append, append);
940 g_free(append->ifname);
951 mesh_peer_name = args[2];
953 if (!mesh_peer_name) {
954 result = __connmanctl_dbus_method_call(connection,
955 CONNMAN_SERVICE, CONNMAN_PATH,
956 "net.connman.Manager", "GetMeshPeers",
957 mesh_peers_list, NULL, NULL, NULL);
961 if (check_dbus_name(mesh_peer_name) == false) {
966 mesh_peer_path = g_strdup_printf("/net/connman/mesh/%s",
968 result = __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
969 mesh_peer_path, "net.connman.Mesh", "GetProperties",
970 object_properties, mesh_peer_path, NULL, NULL);
984 mesh_peer_name = args[2];
986 if (check_dbus_name(mesh_peer_name) == false) {
991 mesh_peer_path = g_strdup_printf("/net/connman/mesh/%s",
993 result = __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
994 mesh_peer_path, "net.connman.Mesh", "Connect",
995 mesh_connect_return, mesh_peer_path, NULL, NULL);
1009 mesh_peer_name = args[2];
1011 if (check_dbus_name(mesh_peer_name) == false) {
1016 mesh_peer_path = g_strdup_printf("/net/connman/mesh/%s",
1018 result = __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
1019 mesh_peer_path, "net.connman.Mesh", "Disconnect",
1020 mesh_disconnect_return, mesh_peer_path, NULL, NULL);
1034 mesh_peer_name = args[2];
1036 if (check_dbus_name(mesh_peer_name) == false) {
1041 mesh_peer_path = g_strdup_printf("/net/connman/mesh/%s",
1043 result = __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
1044 mesh_peer_path, "net.connman.Mesh", "Remove",
1045 mesh_remove_return, mesh_peer_path, NULL, NULL);
1054 result = __connmanctl_dbus_method_call(connection,
1055 CONNMAN_SERVICE, CONNMAN_PATH,
1056 "net.connman.Manager", "GetConnectedMeshPeers",
1057 connected_mesh_peers_list, NULL, NULL, NULL);
1066 result = __connmanctl_dbus_method_call(connection,
1067 CONNMAN_SERVICE, CONNMAN_PATH,
1068 "net.connman.Manager",
1069 "GetDisconnectedMeshPeers",
1070 disconnected_mesh_peers_list, NULL, NULL, NULL);
1078 path = g_strdup_printf("/net/connman/technology/mesh");
1080 network = dbus_malloc0(sizeof(struct mesh_create_network));
1081 network->name = g_strdup(args[2]);
1082 network->freq = atoi(args[3]);
1083 network->sec_type = g_strdup(args[4]);
1084 method = g_strdup("MeshCreateNetwork");
1085 result = __connmanctl_dbus_mesh_dict(connection, path,
1086 "net.connman.Technology", mesh_return, method,
1087 "MeshCreateNetwork", DBUS_TYPE_STRING,
1088 mesh_create_network_append, network);
1089 g_free(network->name);
1090 g_free(network->sec_type);
1099 path = g_strdup_printf("/net/connman/technology/mesh");
1101 method = g_strdup("AbortScan");
1102 result = __connmanctl_dbus_mesh_dict(connection, path,
1103 "net.connman.Technology", mesh_return, method,
1104 "AbortScan", DBUS_TYPE_STRING,
1113 path = g_strdup_printf("/net/connman/technology/mesh");
1115 scan_params = dbus_malloc0(sizeof(struct mesh_specific_scan_params));
1116 scan_params->name = g_strdup(args[2]);
1117 scan_params->freq = atoi(args[3]);
1118 method = g_strdup("MeshSpecificScan");
1119 result = __connmanctl_dbus_mesh_dict(connection, path,
1120 "net.connman.Technology", mesh_return, method,
1121 "MeshSpecificScan", DBUS_TYPE_STRING,
1122 mesh_specific_scan_append, scan_params);
1123 g_free(scan_params->name);
1124 g_free(scan_params);
1133 mesh_peer_name = args[2];
1137 if (check_dbus_name(mesh_peer_name) == false) {
1142 mesh_peer_path = g_strdup_printf("/net/connman/mesh/%s",
1145 if (g_strcmp0(property, "Passphrase") == 0) {
1146 result = __connmanctl_dbus_set_property(connection,
1147 mesh_peer_path, "net.connman.Mesh",
1148 mesh_config_return, mesh_peer_path, property,
1149 DBUS_TYPE_STRING, &value);
1151 printf("Invalid property %s\n", property);
1163 path = g_strdup_printf("/net/connman/technology/mesh");
1165 gate_params = dbus_malloc0(sizeof(struct mesh_gate_params));
1166 gate_params->gate_announce = atoi(args[2]);
1167 gate_params->hwmp_rootmode = atoi(args[3]);
1168 gate_params->stp = atoi(args[4]);
1170 method = g_strdup("SetMeshGate");
1172 result = __connmanctl_dbus_mesh_dict(connection, path,
1173 "net.connman.Technology", mesh_return, method,
1174 "SetMeshGate", DBUS_TYPE_STRING,
1175 mesh_set_gate_append, gate_params);
1185 mesh_peer_addr = g_strdup(args[2]);
1186 method = g_strdup("MeshAddPeer");
1188 result = __connmanctl_dbus_method_call(connection,
1189 CONNMAN_SERVICE, CONNMAN_PATH,
1190 "net.connman.Manager", "MeshAddPeer",
1191 mesh_return, method, mesh_peer_append,
1202 mesh_peer_addr = g_strdup(args[2]);
1203 method = g_strdup("MeshRemovePeer");
1205 result = __connmanctl_dbus_method_call(connection,
1206 CONNMAN_SERVICE, CONNMAN_PATH,
1207 "net.connman.Manager", "MeshRemovePeer",
1208 mesh_return, method, mesh_peer_append,
1221 if (result != -EINPROGRESS)
1222 printf("Error '%s': %s\n", args[1], strerror(-result));
1230 static int cmd_tether(char *args[], int num, struct connman_option *options)
1232 char *ssid, *passphrase;
1238 passphrase = args[num - 1];
1239 ssid = args[num - 2];
1241 set_tethering = parse_boolean(args[2]);
1243 if (strcmp(args[1], "wifi") == 0) {
1248 if (num == 5 && set_tethering == -1)
1255 return tether_set_ssid(ssid, passphrase, set_tethering);
1261 if (set_tethering == -1)
1264 if (check_dbus_name(args[1]) == false)
1267 return tether_set(args[1], set_tethering);
1270 static int cmd_tethering_clients(char *args[], int num, struct connman_option *options)
1275 return __connmanctl_dbus_method_call(connection,
1276 CONNMAN_SERVICE, CONNMAN_PATH,
1277 "net.connman.Manager", "GetTetheringClients",
1278 tethering_clients_list, NULL, NULL, NULL);
1281 static int scan_return(DBusMessageIter *iter, const char *error,
1284 char *path = user_data;
1287 char *str = strrchr(path, '/');
1289 fprintf(stdout, "Scan completed for %s\n", str);
1291 fprintf(stderr, "Error %s: %s\n", path, error);
1298 static int cmd_scan(char *args[], int num, struct connman_option *options)
1308 if (check_dbus_name(args[1]) == false)
1311 path = g_strdup_printf("/net/connman/technology/%s", args[1]);
1312 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE, path,
1313 "net.connman.Technology", "Scan",
1314 scan_return, path, NULL, NULL);
1317 static int connect_return(DBusMessageIter *iter, const char *error,
1320 char *path = user_data;
1323 char *str = strrchr(path, '/');
1325 fprintf(stdout, "Connected %s\n", str);
1327 fprintf(stderr, "Error %s: %s\n", path, error);
1334 static int cmd_connect(char *args[], int num, struct connman_option *options)
1336 const char *iface = "net.connman.Service";
1345 if (check_dbus_name(args[1]) == false)
1348 if (g_strstr_len(args[1], 5, "peer_") == args[1]) {
1349 iface = "net.connman.Peer";
1350 path = g_strdup_printf("/net/connman/peer/%s", args[1]);
1352 path = g_strdup_printf("/net/connman/service/%s", args[1]);
1354 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE, path,
1355 iface, "Connect", connect_return, path, NULL, NULL);
1358 static int disconnect_return(DBusMessageIter *iter, const char *error,
1361 char *path = user_data;
1364 char *str = strrchr(path, '/');
1366 fprintf(stdout, "Disconnected %s\n", str);
1368 fprintf(stderr, "Error %s: %s\n", path, error);
1375 static int cmd_disconnect(char *args[], int num, struct connman_option *options)
1377 const char *iface = "net.connman.Service";
1386 if (check_dbus_name(args[1]) == false)
1389 if (g_strstr_len(args[1], 5, "peer_") == args[1]) {
1390 iface = "net.connman.Peer";
1391 path = g_strdup_printf("/net/connman/peer/%s", args[1]);
1393 path = g_strdup_printf("/net/connman/service/%s", args[1]);
1395 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
1396 path, iface, "Disconnect",
1397 disconnect_return, path, NULL, NULL);
1400 struct move_service {
1405 static int move_before_return(DBusMessageIter *iter, const char *error,
1408 struct move_service *services = user_data;
1413 service = strrchr(services->service, '/');
1415 target = strrchr(services->target, '/');
1417 fprintf(stdout, "Moved %s before %s\n", service, target);
1419 fprintf(stderr, "Error %s: %s\n", services->service, error);
1421 g_free(services->service);
1422 g_free(services->target);
1428 static void move_before_append_args(DBusMessageIter *iter, void *user_data)
1430 char *path = user_data;
1432 dbus_message_iter_append_basic(iter,
1433 DBUS_TYPE_OBJECT_PATH, &path);
1436 static int cmd_service_move_before(char *args[], int num,
1437 struct connman_option *options)
1439 const char *iface = "net.connman.Service";
1440 struct move_service *services;
1448 if (check_dbus_name(args[1]) == false)
1451 services = g_new(struct move_service, 1);
1453 services->service = g_strdup_printf("/net/connman/service/%s", args[1]);
1454 services->target = g_strdup_printf("/net/connman/service/%s", args[2]);
1456 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
1457 services->service, iface, "MoveBefore",
1458 move_before_return, services,
1459 move_before_append_args,
1463 static int move_after_return(DBusMessageIter *iter, const char *error,
1466 struct move_service *services = user_data;
1471 service = strrchr(services->service, '/');
1473 target = strrchr(services->target, '/');
1475 fprintf(stdout, "Moved %s after %s\n", service, target);
1477 fprintf(stderr, "Error %s: %s\n", services->service, error);
1479 g_free(services->service);
1480 g_free(services->target);
1486 static void move_after_append_args(DBusMessageIter *iter, void *user_data)
1488 char *path = user_data;
1490 dbus_message_iter_append_basic(iter,
1491 DBUS_TYPE_OBJECT_PATH, &path);
1494 static int cmd_service_move_after(char *args[], int num,
1495 struct connman_option *options)
1497 const char *iface = "net.connman.Service";
1498 struct move_service *services;
1506 if (check_dbus_name(args[1]) == false)
1509 services = g_new(struct move_service, 1);
1511 services->service = g_strdup_printf("/net/connman/service/%s", args[1]);
1512 services->target = g_strdup_printf("/net/connman/service/%s", args[2]);
1514 return __connmanctl_dbus_method_call(connection, CONNMAN_SERVICE,
1515 services->service, iface, "MoveAfter",
1516 move_after_return, services,
1517 move_after_append_args,
1521 static int config_return(DBusMessageIter *iter, const char *error,
1524 char *service_name = user_data;
1527 fprintf(stderr, "Error %s: %s\n", service_name, error);
1534 struct config_append {
1539 struct session_options {
1543 struct connman_option *options;
1546 static void config_append_ipv4(DBusMessageIter *iter,
1549 struct config_append *append = user_data;
1550 char **opts = append->opts;
1556 while (opts[i] && ipv4[i]) {
1557 __connmanctl_dbus_append_dict_entry(iter, ipv4[i],
1558 DBUS_TYPE_STRING, &opts[i]);
1565 static void config_append_ipv6(DBusMessageIter *iter, void *user_data)
1567 struct config_append *append = user_data;
1568 char **opts = append->opts;
1575 if (g_strcmp0(opts[0], "auto") == 0) {
1578 switch (parse_boolean(opts[1])) {
1583 __connmanctl_dbus_append_dict_entry(iter, "Privacy",
1584 DBUS_TYPE_STRING, &str);
1591 __connmanctl_dbus_append_dict_entry(iter, "Privacy",
1592 DBUS_TYPE_STRING, &str);
1599 if (g_strcmp0(opts[1], "prefered") != 0 &&
1601 "preferred") != 0) {
1602 fprintf(stderr, "Error %s: %s\n",
1609 __connmanctl_dbus_append_dict_entry(iter,
1610 "Privacy", DBUS_TYPE_STRING,
1615 } else if (g_strcmp0(opts[0], "manual") == 0) {
1618 while (opts[i] && ipv6[i]) {
1620 int value = atoi(opts[i]);
1621 __connmanctl_dbus_append_dict_entry(iter,
1622 ipv6[i], DBUS_TYPE_BYTE,
1625 __connmanctl_dbus_append_dict_entry(iter,
1626 ipv6[i], DBUS_TYPE_STRING,
1634 } else if (g_strcmp0(opts[0], "off") != 0) {
1635 fprintf(stderr, "Error %s: %s\n", opts[0], strerror(EINVAL));
1640 __connmanctl_dbus_append_dict_entry(iter, "Method", DBUS_TYPE_STRING,
1644 static void config_append_str(DBusMessageIter *iter, void *user_data)
1646 struct config_append *append = user_data;
1647 char **opts = append->opts;
1654 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1662 static void append_servers(DBusMessageIter *iter, void *user_data)
1664 struct config_append *append = user_data;
1665 char **opts = append->opts;
1671 while (opts[i] && g_strcmp0(opts[i], "--excludes") != 0) {
1672 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1680 static void append_excludes(DBusMessageIter *iter, void *user_data)
1682 struct config_append *append = user_data;
1683 char **opts = append->opts;
1684 int i = append->values;
1686 if (!opts || !opts[i] ||
1687 g_strcmp0(opts[i], "--excludes") != 0)
1692 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1700 static void config_append_proxy(DBusMessageIter *iter, void *user_data)
1702 struct config_append *append = user_data;
1703 char **opts = append->opts;
1708 if (g_strcmp0(opts[0], "manual") == 0) {
1709 __connmanctl_dbus_append_dict_string_array(iter, "Servers",
1710 append_servers, append);
1712 __connmanctl_dbus_append_dict_string_array(iter, "Excludes",
1713 append_excludes, append);
1715 } else if (g_strcmp0(opts[0], "auto") == 0) {
1717 __connmanctl_dbus_append_dict_entry(iter, "URL",
1718 DBUS_TYPE_STRING, &opts[1]);
1722 } else if (g_strcmp0(opts[0], "direct") != 0)
1725 __connmanctl_dbus_append_dict_entry(iter, "Method",DBUS_TYPE_STRING,
1731 static int cmd_config(char *args[], int num, struct connman_option *options)
1733 int result = 0, res = 0, index = 2, oldindex = 0;
1735 char *service_name, *path;
1738 struct config_append append;
1740 service_name = args[1];
1744 if (check_dbus_name(service_name) == false)
1747 while (index < num && args[index]) {
1748 c = parse_args(args[index], options);
1749 opt_start = &args[index + 1];
1750 append.opts = opt_start;
1756 path = g_strdup_printf("/net/connman/service/%s", service_name);
1760 switch (parse_boolean(*opt_start)) {
1775 res = __connmanctl_dbus_set_property(connection,
1776 path, "net.connman.Service",
1778 g_strdup(service_name),
1780 DBUS_TYPE_BOOLEAN, &val);
1784 res = __connmanctl_dbus_set_property_dict(connection,
1785 path, "net.connman.Service",
1786 config_return, g_strdup(service_name),
1787 "IPv4.Configuration", DBUS_TYPE_STRING,
1788 config_append_ipv4, &append);
1789 index += append.values;
1793 res = __connmanctl_dbus_set_property_dict(connection,
1794 path, "net.connman.Service",
1795 config_return, g_strdup(service_name),
1796 "IPv6.Configuration", DBUS_TYPE_STRING,
1797 config_append_ipv6, &append);
1798 index += append.values;
1802 res = __connmanctl_dbus_set_property_array(connection,
1803 path, "net.connman.Service",
1804 config_return, g_strdup(service_name),
1805 "Nameservers.Configuration",
1806 DBUS_TYPE_STRING, config_append_str,
1808 index += append.values;
1812 res = __connmanctl_dbus_set_property_array(connection,
1813 path, "net.connman.Service",
1814 config_return, g_strdup(service_name),
1815 "Timeservers.Configuration",
1816 DBUS_TYPE_STRING, config_append_str,
1818 index += append.values;
1822 res = __connmanctl_dbus_set_property_array(connection,
1823 path, "net.connman.Service",
1824 config_return, g_strdup(service_name),
1825 "Domains.Configuration",
1826 DBUS_TYPE_STRING, config_append_str,
1828 index += append.values;
1832 res = __connmanctl_dbus_set_property_dict(connection,
1833 path, "net.connman.Service",
1834 config_return, g_strdup(service_name),
1835 "Proxy.Configuration",
1836 DBUS_TYPE_STRING, config_append_proxy,
1838 index += append.values;
1841 res = __connmanctl_dbus_method_call(connection,
1842 CONNMAN_SERVICE, path,
1843 "net.connman.Service", "Remove",
1844 config_return, g_strdup(service_name),
1849 switch (parse_boolean(*opt_start)) {
1861 res = __connmanctl_dbus_set_property(connection,
1862 path, "net.connman.Service",
1864 g_strdup(service_name),
1865 "mDNS.Configuration",
1866 DBUS_TYPE_BOOLEAN, &val);
1879 if (res == -EINPROGRESS)
1880 result = -EINPROGRESS;
1882 printf("Error '%s': %s\n", args[oldindex],
1893 static DBusHandlerResult monitor_changed(DBusConnection *connection,
1894 DBusMessage *message, void *user_data)
1896 DBusMessageIter iter;
1897 const char *interface, *path;
1899 interface = dbus_message_get_interface(message);
1901 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1903 if (strncmp(interface, "net.connman.", 12) != 0)
1904 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1906 if (!strcmp(interface, "net.connman.Agent") ||
1907 !strcmp(interface, "net.connman.vpn.Agent") ||
1908 !strcmp(interface, "net.connman.Session") ||
1909 !strcmp(interface, "net.connman.Notification"))
1910 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1912 interface = strrchr(interface, '.');
1913 if (interface && *interface != '\0')
1916 path = strrchr(dbus_message_get_path(message), '/');
1917 if (path && *path != '\0')
1920 __connmanctl_save_rl();
1922 if (dbus_message_is_signal(message, "net.connman.Manager",
1923 "ServicesChanged")) {
1925 fprintf(stdout, "%-12s %-20s = {\n", interface,
1927 dbus_message_iter_init(message, &iter);
1928 __connmanctl_services_list(&iter);
1929 fprintf(stdout, "\n}\n");
1931 __connmanctl_redraw_rl();
1933 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1934 } else if (dbus_message_is_signal(message, "net.connman.Manager",
1936 fprintf(stdout, "%-12s %-20s = {\n", interface,
1938 dbus_message_iter_init(message, &iter);
1939 __connmanctl_peers_list(&iter);
1940 fprintf(stdout, "\n}\n");
1942 __connmanctl_redraw_rl();
1944 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1945 } else if (dbus_message_is_signal(message, "net.connman.vpn.Manager",
1946 "ConnectionAdded") ||
1947 dbus_message_is_signal(message,
1948 "net.connman.vpn.Manager",
1949 "ConnectionRemoved")) {
1950 interface = "vpn.Manager";
1951 path = dbus_message_get_member(message);
1953 } else if (dbus_message_is_signal(message, "net.connman.Manager",
1954 "TechnologyAdded") ||
1955 dbus_message_is_signal(message, "net.connman.Manager",
1956 "TechnologyRemoved"))
1957 path = dbus_message_get_member(message);
1959 fprintf(stdout, "%-12s %-20s ", interface, path);
1960 dbus_message_iter_init(message, &iter);
1962 __connmanctl_dbus_print(&iter, "", " = ", " = ");
1963 fprintf(stdout, "\n");
1965 __connmanctl_redraw_rl();
1967 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1974 { "Service", false },
1975 { "Technology", false },
1976 { "Manager", false },
1977 { "vpn.Manager", false },
1978 { "vpn.Connection", false },
1982 static void monitor_add(char *interface)
1984 bool add_filter = true, found = false;
1989 for (i = 0; monitor[i].interface; i++) {
1990 if (monitor[i].enabled == true)
1993 if (g_strcmp0(interface, monitor[i].interface) == 0) {
1994 if (monitor[i].enabled == true)
1997 monitor[i].enabled = true;
2005 if (add_filter == true)
2006 dbus_connection_add_filter(connection, monitor_changed,
2009 dbus_error_init(&err);
2010 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
2012 dbus_bus_add_match(connection, rule, &err);
2015 if (dbus_error_is_set(&err))
2016 fprintf(stderr, "Error: %s\n", err.message);
2019 static void monitor_del(char *interface)
2021 bool del_filter = true, found = false;
2026 for (i = 0; monitor[i].interface; i++) {
2027 if (g_strcmp0(interface, monitor[i].interface) == 0) {
2028 if (monitor[i].enabled == false)
2031 monitor[i].enabled = false;
2035 if (monitor[i].enabled == true)
2042 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
2044 dbus_bus_remove_match(connection, rule, NULL);
2047 if (del_filter == true)
2048 dbus_connection_remove_filter(connection, monitor_changed,
2052 static int cmd_monitor(char *args[], int num, struct connman_option *options)
2061 switch (parse_boolean(args[2])) {
2071 c = parse_args(args[1], options);
2074 monitor_add("Service");
2075 monitor_add("Technology");
2076 monitor_add("Manager");
2077 monitor_add("vpn.Manager");
2078 monitor_add("vpn.Connection");
2083 monitor_add("Service");
2085 monitor_del("Service");
2090 monitor_add("Technology");
2092 monitor_del("Technology");
2097 monitor_add("Manager");
2099 monitor_del("Manager");
2104 monitor_add("vpn.Manager");
2106 monitor_del("vpn.Manager");
2111 monitor_add("vpn.Connection");
2113 monitor_del("vpn.Connection");
2117 switch(parse_boolean(args[1])) {
2119 monitor_del("Service");
2120 monitor_del("Technology");
2121 monitor_del("Manager");
2122 monitor_del("vpn.Manager");
2123 monitor_del("vpn.Connection");
2127 monitor_add("Service");
2128 monitor_add("Technology");
2129 monitor_add("Manager");
2130 monitor_add("vpn.Manager");
2131 monitor_add("vpn.Connection");
2140 return -EINPROGRESS;
2145 static int cmd_agent(char *args[], int num, struct connman_option *options)
2147 if (!__connmanctl_is_interactive()) {
2148 fprintf(stderr, "Error: Not supported in non-interactive "
2159 switch(parse_boolean(args[1])) {
2161 __connmanctl_agent_unregister(connection);
2165 if (__connmanctl_agent_register(connection) == -EINPROGRESS)
2166 return -EINPROGRESS;
2178 static int vpnconnections_properties(DBusMessageIter *iter, const char *error,
2181 char *path = user_data;
2183 DBusMessageIter dict;
2186 fprintf(stdout, "%s\n", path);
2188 dbus_message_iter_recurse(iter, &dict);
2189 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
2191 fprintf(stdout, "\n");
2194 str = strrchr(path, '/');
2200 fprintf(stderr, "Error %s: %s\n", str, error);
2208 static int vpnconnections_list(DBusMessageIter *iter, const char *error,
2212 __connmanctl_vpnconnections_list(iter);
2214 fprintf(stderr, "Error: %s\n", error);
2219 static int cmd_vpnconnections(char *args[], int num,
2220 struct connman_option *options)
2222 char *vpnconnection_name, *path;
2227 vpnconnection_name = args[1];
2229 if (!vpnconnection_name)
2230 return __connmanctl_dbus_method_call(connection,
2231 VPN_SERVICE, VPN_PATH,
2232 "net.connman.vpn.Manager", "GetConnections",
2233 vpnconnections_list, NULL,
2236 if (check_dbus_name(vpnconnection_name) == false)
2239 path = g_strdup_printf("/net/connman/vpn/connection/%s",
2240 vpnconnection_name);
2241 return __connmanctl_dbus_method_call(connection, VPN_SERVICE, path,
2242 "net.connman.vpn.Connection", "GetProperties",
2243 vpnconnections_properties, path, NULL, NULL);
2247 static int cmd_vpnagent(char *args[], int num, struct connman_option *options)
2249 if (!__connmanctl_is_interactive()) {
2250 fprintf(stderr, "Error: Not supported in non-interactive "
2261 switch(parse_boolean(args[1])) {
2263 __connmanctl_vpn_agent_unregister(connection);
2267 if (__connmanctl_vpn_agent_register(connection) ==
2269 return -EINPROGRESS;
2281 static DBusMessage *session_release(DBusConnection *connection,
2282 DBusMessage *message, void *user_data)
2284 __connmanctl_save_rl();
2286 fprintf(stdout, "Session %s released\n", session_path);
2288 __connmanctl_redraw_rl();
2290 g_free(session_path);
2291 session_path = NULL;
2292 session_connected = false;
2294 return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
2297 static DBusMessage *session_update(DBusConnection *connection,
2298 DBusMessage *message, void *user_data)
2300 DBusMessageIter iter, dict;
2302 __connmanctl_save_rl();
2304 fprintf(stdout, "Session Update = {\n");
2306 dbus_message_iter_init(message, &iter);
2307 dbus_message_iter_recurse(&iter, &dict);
2309 __connmanctl_dbus_print(&dict, "", " = ", "\n");
2310 fprintf(stdout, "\n}\n");
2312 dbus_message_iter_recurse(&iter, &dict);
2314 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2315 DBusMessageIter entry, variant;
2316 char *field, *state;
2318 dbus_message_iter_recurse(&dict, &entry);
2320 dbus_message_iter_get_basic(&entry, &field);
2322 if (dbus_message_iter_get_arg_type(&entry)
2324 && !strcmp(field, "State")) {
2326 dbus_message_iter_next(&entry);
2327 dbus_message_iter_recurse(&entry, &variant);
2328 if (dbus_message_iter_get_arg_type(&variant)
2329 != DBUS_TYPE_STRING)
2332 dbus_message_iter_get_basic(&variant, &state);
2334 if (!session_connected && (!strcmp(state, "connected")
2335 || !strcmp(state, "online"))) {
2337 fprintf(stdout, "Session %s connected\n",
2339 session_connected = true;
2344 if (!strcmp(state, "disconnected") &&
2345 session_connected) {
2347 fprintf(stdout, "Session %s disconnected\n",
2349 session_connected = false;
2354 dbus_message_iter_next(&dict);
2357 __connmanctl_redraw_rl();
2359 return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
2362 static const GDBusMethodTable notification_methods[] = {
2363 { GDBUS_METHOD("Release", NULL, NULL, session_release) },
2364 { GDBUS_METHOD("Update", GDBUS_ARGS({"settings", "a{sv}"}),
2365 NULL, session_update) },
2369 static int session_notify_add(const char *path)
2371 if (session_notify_path)
2374 if (!g_dbus_register_interface(connection, path,
2375 "net.connman.Notification",
2376 notification_methods, NULL, NULL,
2378 fprintf(stderr, "Error: Failed to register VPN Agent "
2383 session_notify_path = g_strdup(path);
2388 static void session_notify_remove(void)
2390 if (!session_notify_path)
2393 g_dbus_unregister_interface(connection, session_notify_path,
2394 "net.connman.Notification");
2396 g_free(session_notify_path);
2397 session_notify_path = NULL;
2400 static int session_connect_cb(DBusMessageIter *iter, const char *error,
2404 fprintf(stderr, "Error: %s\n", error);
2408 return -EINPROGRESS;
2412 static int session_connect(void)
2414 return __connmanctl_dbus_method_call(connection, "net.connman",
2415 session_path, "net.connman.Session", "Connect",
2416 session_connect_cb, NULL, NULL, NULL);
2419 static int session_disconnect_cb(DBusMessageIter *iter, const char *error,
2423 fprintf(stderr, "Error: %s\n", error);
2428 static int session_disconnect(void)
2430 return __connmanctl_dbus_method_call(connection, "net.connman",
2431 session_path, "net.connman.Session", "Disconnect",
2432 session_disconnect_cb, NULL, NULL, NULL);
2435 static int session_create_cb(DBusMessageIter *iter, const char *error,
2438 gboolean connect = GPOINTER_TO_INT(user_data);
2442 fprintf(stderr, "Error creating session: %s", error);
2443 session_notify_remove();
2447 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_OBJECT_PATH) {
2448 fprintf(stderr, "Error creating session: No session path\n");
2452 g_free(session_path);
2454 dbus_message_iter_get_basic(iter, &str);
2455 session_path = g_strdup(str);
2457 fprintf(stdout, "Session %s created\n", session_path);
2460 return session_connect();
2462 return -EINPROGRESS;
2465 static void session_config_append_array(DBusMessageIter *iter,
2468 struct config_append *append = user_data;
2469 char **opts = append->opts;
2475 while (opts[i] && strncmp(opts[i], "--", 2) != 0) {
2476 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2484 static void session_create_append_dict(DBusMessageIter *iter, void *user_data)
2486 struct session_options *args_struct = user_data;
2487 int index = 0, res = 0;
2488 struct config_append append;
2491 dbus_bool_t source_ip_rule;
2493 while (index < args_struct->num && args_struct->args[index]) {
2494 append.opts = &args_struct->args[index];
2497 c = parse_args(args_struct->args[index], args_struct->options);
2501 __connmanctl_dbus_append_dict_string_array(iter, "AllowedBearers",
2502 session_config_append_array,
2506 if (! args_struct->args[index + 1]) {
2510 __connmanctl_dbus_append_dict_entry(iter, "ConnectionType",
2512 &args_struct->args[index + 1]);
2516 if (index + 1 < args_struct->num)
2517 ifname = args_struct->args[index + 1];
2520 __connmanctl_dbus_append_dict_entry(iter, "AllowedInterface",
2526 if (! args_struct->args[index + 1]) {
2530 switch (parse_boolean( args_struct->args[index + 1])) {
2532 source_ip_rule = TRUE;
2535 source_ip_rule = FALSE;
2541 __connmanctl_dbus_append_dict_entry(iter, "SourceIPRule",
2547 if (!args_struct->args[index + 1]) {
2551 __connmanctl_dbus_append_dict_entry(iter, "ContextIdentifier",
2553 &args_struct->args[index + 1]);
2560 if (res < 0 && res != -EINPROGRESS) {
2561 printf("Error '%s': %s\n", args_struct->args[index],
2566 index += append.values;
2570 static void session_create_append(DBusMessageIter *iter, void *user_data)
2572 struct session_options *args_struct = user_data;
2574 __connmanctl_dbus_append_dict(iter, session_create_append_dict,
2577 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2578 &args_struct->notify_path);
2581 static int session_create(gboolean connect, char *args[], int num,
2582 struct connman_option *options)
2586 struct session_options args_struct;
2587 args_struct.args = args;
2588 args_struct.num = num;
2589 args_struct.options = options;
2591 notify_path = g_strdup_printf("/net/connman/connmanctl%d", getpid());
2592 session_notify_add(notify_path);
2593 args_struct.notify_path = notify_path;
2595 res = __connmanctl_dbus_method_call(connection, "net.connman", "/",
2596 "net.connman.Manager", "CreateSession",
2597 session_create_cb, GINT_TO_POINTER(connect),
2598 session_create_append, &args_struct);
2600 g_free(notify_path);
2602 if (res < 0 && res != -EINPROGRESS)
2603 session_notify_remove();
2608 static int session_destroy_cb(DBusMessageIter *iter, const char *error,
2612 fprintf(stderr, "Error destroying session: %s", error);
2616 fprintf(stdout, "Session %s ended\n", session_path);
2618 g_free(session_path);
2619 session_path = NULL;
2620 session_connected = false;
2625 static void session_destroy_append(DBusMessageIter *iter, void *user_data)
2627 const char *path = user_data;
2629 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
2632 static int session_destroy(void)
2634 return __connmanctl_dbus_method_call(connection, "net.connman", "/",
2635 "net.connman.Manager", "DestroySession",
2636 session_destroy_cb, NULL,
2637 session_destroy_append, session_path);
2640 static int session_config_return(DBusMessageIter *iter, const char *error,
2643 char *property_name = user_data;
2646 fprintf(stderr, "Error setting session %s: %s\n",
2647 property_name, error);
2652 static int session_config(char *args[], int num,
2653 struct connman_option *options)
2655 int index = 0, res = 0;
2656 struct config_append append;
2659 dbus_bool_t source_ip_rule;
2661 while (index < num && args[index]) {
2662 append.opts = &args[index];
2665 c = parse_args(args[index], options);
2669 res = __connmanctl_dbus_session_change_array(connection,
2670 session_path, session_config_return,
2671 "AllowedBearers", "AllowedBearers",
2672 session_config_append_array, &append);
2675 if (!args[index + 1]) {
2680 res = __connmanctl_dbus_session_change(connection,
2681 session_path, session_config_return,
2682 "ConnectionType", "ConnectionType",
2683 DBUS_TYPE_STRING, &args[index + 1]);
2687 if (index + 1 < num)
2688 ifname = args[index + 1];
2692 res = __connmanctl_dbus_session_change(connection,
2693 session_path, session_config_return,
2694 "AllowedInterface", "AllowedInterface",
2695 DBUS_TYPE_STRING, &ifname);
2699 if (!args[index + 1]) {
2703 switch (parse_boolean(args[index + 1])) {
2705 source_ip_rule = TRUE;
2708 source_ip_rule = FALSE;
2715 res = __connmanctl_dbus_session_change(connection,
2716 session_path, session_config_return,
2717 "SourceIPRule", "SourceIPRule",
2718 DBUS_TYPE_BOOLEAN, &source_ip_rule);
2722 if (!args[index + 1]) {
2727 res = __connmanctl_dbus_session_change(connection,
2728 session_path, session_config_return,
2729 "ctxid", "ctxid", DBUS_TYPE_STRING,
2738 if (res < 0 && res != -EINPROGRESS) {
2739 printf("Error '%s': %s\n", args[index],
2744 index += append.values;
2750 static int cmd_session(char *args[], int num, struct connman_option *options)
2759 switch(parse_boolean(command)) {
2763 return session_destroy();
2768 return session_create(FALSE, &args[2], num - 2, options);
2771 if (!strcmp(command, "connect")) {
2773 return session_create(TRUE, &args[2], num - 2,
2776 return session_connect();
2778 } else if (!strcmp(command, "disconnect")) {
2780 if (!session_path) {
2781 fprintf(stdout, "Session does not exist\n");
2785 return session_disconnect();
2786 } else if (!strcmp(command, "config")) {
2787 if (!session_path) {
2788 fprintf(stdout, "Session does not exist\n");
2795 return session_config(&args[2], num - 2, options);
2803 static int cmd_exit(char *args[], int num, struct connman_option *options)
2808 static char *lookup_key_from_table(GHashTable *hash, const char *text,
2812 static GHashTableIter iter;
2813 gpointer key, value;
2816 g_hash_table_iter_init(&iter, hash);
2820 while (g_hash_table_iter_next(&iter, &key, &value))
2821 if (strncmp(text, key, len) == 0)
2827 static char *lookup_service_arg(const char *text, int state)
2829 if (__connmanctl_input_calc_level() > 1) {
2830 __connmanctl_input_lookup_end();
2834 return lookup_key_from_table(service_hash, text, state);
2837 static char *lookup_peer(const char *text, int state)
2839 static GHashTableIter iter;
2840 gpointer key, value;
2844 g_hash_table_iter_init(&iter, peer_hash);
2848 while (g_hash_table_iter_next(&iter, &key, &value)) {
2849 const char *peer = key;
2850 if (strncmp(text, peer, len) == 0)
2851 return strdup(peer);
2857 static char *lookup_peer_arg(const char *text, int state)
2859 if (__connmanctl_input_calc_level() > 1) {
2860 __connmanctl_input_lookup_end();
2864 return lookup_peer(text, state);
2867 static char *lookup_technology(const char *text, int state)
2870 static GHashTableIter iter;
2871 gpointer key, value;
2874 g_hash_table_iter_init(&iter, technology_hash);
2878 while (g_hash_table_iter_next(&iter, &key, &value)) {
2879 const char *technology = key;
2880 if (strncmp(text, technology, len) == 0)
2881 return strdup(technology);
2887 static char *lookup_technology_arg(const char *text, int state)
2889 if (__connmanctl_input_calc_level() > 1) {
2890 __connmanctl_input_lookup_end();
2894 return lookup_technology(text, state);
2897 static char *lookup_technology_offline(const char *text, int state)
2900 static bool end = false;
2903 if (__connmanctl_input_calc_level() > 1) {
2904 __connmanctl_input_lookup_end();
2916 str = lookup_technology(text, state);
2922 if (strncmp(text, "offline", len) == 0)
2923 return strdup("offline");
2928 static char *lookup_on_off(const char *text, int state)
2930 char *onoff[] = { "on", "off", NULL };
2941 while (onoff[idx]) {
2945 if (!strncmp(text, str, len))
2952 static char *lookup_tether(const char *text, int state)
2956 level = __connmanctl_input_calc_level();
2958 return lookup_technology(text, state);
2961 return lookup_on_off(text, state);
2963 __connmanctl_input_lookup_end();
2968 static char *lookup_agent(const char *text, int state)
2970 if (__connmanctl_input_calc_level() > 1) {
2971 __connmanctl_input_lookup_end();
2975 return lookup_on_off(text, state);
2978 static char *lookup_vpnconnection_arg(const char *text, int state)
2980 if (__connmanctl_input_calc_level() > 1) {
2981 __connmanctl_input_lookup_end();
2985 return lookup_key_from_table(vpnconnection_hash, text, state);
2988 static struct connman_option service_options[] = {
2989 {"properties", 'p', "[<service>] (obsolete)"},
2993 #if defined TIZEN_EXT_INS
2994 static struct connman_option ins_options[] = {
2996 {"filter-ssid", 's', "ssid"},
2997 {"filter-name", 'n', "[<service_name>]"},
3002 static struct connman_option config_options[] = {
3003 {"nameservers", 'n', "<dns1> [<dns2>] [<dns3>]"},
3004 {"timeservers", 't', "<ntp1> [<ntp2>] [...]"},
3005 {"domains", 'd', "<domain1> [<domain2>] [...]"},
3006 {"mdns", 'm', "yes|no"},
3007 {"ipv6", 'v', "off|auto [enable|disable|preferred]|\n"
3008 "\t\t\tmanual <address> <prefixlength> <gateway>"},
3009 {"proxy", 'x', "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
3010 "\t\t\t[exclude <exclude1> [<exclude2>] [...]]"},
3011 {"autoconnect", 'a', "yes|no"},
3012 {"ipv4", 'i', "off|dhcp|manual <address> <netmask> <gateway>"},
3013 {"remove", 'r', " Remove service"},
3017 static struct connman_option monitor_options[] = {
3018 {"services", 's', "[off] Monitor only services"},
3019 {"tech", 'c', "[off] Monitor only technologies"},
3020 {"manager", 'm', "[off] Monitor only manager interface"},
3021 {"vpnmanager", 'M', "[off] Monitor only VPN manager "
3023 {"vpnconnection", 'C', "[off] Monitor only VPN "
3028 static struct connman_option session_options[] = {
3029 {"bearers", 'b', "<technology1> [<technology2> [...]]"},
3030 {"type", 't', "local|internet|any"},
3031 {"ifname", 'i', "[<interface_name>]"},
3032 {"srciprule", 's', "yes|no"},
3033 {"ctxid", 'c', "<context_identifier>"},
3037 #if defined TIZEN_EXT_WIFI_MESH
3038 static struct connman_option mesh_options[] = {
3039 {"ifadd", 'a', "<ifname> <wifi_ifname>\n"
3040 " [bridge_ifname] Add Virtual Mesh "
3042 {"ifrmv", 'r', "<ifname> Remove Virtual Mesh "
3044 {"peers", 'p', "[peer] Display Mesh peer "
3046 {"connect", 'c', "<peer> Connect Mesh Peer"},
3047 {"disconnect", 'd', "<peer> Disconnect Mesh Peer"},
3048 {"remove", 'f', "<peer> Forget Mesh Peer"},
3049 {"connected_peers", 'C', "[] Displays connected"
3050 " Peer informations"},
3051 {"disconnected_peers", 'D', "[] Displays "
3052 "Disconnected Peer informations"},
3053 {"create_network", 'n', "<name> <frequency> <sec_type> Create New Mesh "
3055 {"abort_scan", 'A', " Abort ongoing mesh "
3057 {"specific_scan", 'S', "<name> <frequency> Create New Mesh "
3059 {"config", 'P', "<peer> Set Mesh Network "
3060 "Configurations\n Passphrase <passphrase>"},
3061 {"set_gate", 'G', "<gate_ann> <rootmode> <stp> Set Mesh Gate "
3063 {"add_peer", 'z', "<addr> Add Mesh Peer"},
3064 {"remove_peer", 'y', "<addr> Remove Mesh Peer"},
3069 static char *lookup_options(struct connman_option *options, const char *text,
3081 while (options[idx].name) {
3082 str = options[idx].name;
3085 if (str && strncmp(text, str, len) == 0)
3092 static char *lookup_monitor(const char *text, int state)
3096 level = __connmanctl_input_calc_level();
3099 return lookup_options(monitor_options, text, state);
3102 return lookup_on_off(text, state);
3104 __connmanctl_input_lookup_end();
3108 static char *lookup_config(const char *text, int state)
3110 if (__connmanctl_input_calc_level() < 2)
3111 return lookup_key_from_table(service_hash, text, state);
3113 return lookup_options(config_options, text, state);
3116 static char *lookup_session(const char *text, int state)
3118 return lookup_options(session_options, text, state);
3121 #if defined TIZEN_EXT_WIFI_MESH
3122 static char *lookup_mesh(const char *text, int state)
3124 return lookup_options(mesh_options, text, state);
3128 static int peer_service_cb(DBusMessageIter *iter, const char *error,
3131 bool registration = GPOINTER_TO_INT(user_data);
3134 fprintf(stderr, "Error %s peer service: %s\n",
3135 registration ? "registering" : "unregistering", error);
3137 fprintf(stdout, "Peer service %s\n",
3138 registration ? "registered" : "unregistered");
3143 struct _peer_service {
3144 unsigned char *bjr_query;
3146 unsigned char *bjr_response;
3147 int bjr_response_len;
3148 unsigned char *wfd_ies;
3155 static void append_dict_entry_fixed_array(DBusMessageIter *iter,
3156 const char *property, void *value, int length)
3158 DBusMessageIter dict_entry, variant, array;
3160 dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY,
3162 dbus_message_iter_append_basic(&dict_entry, DBUS_TYPE_STRING,
3164 dbus_message_iter_open_container(&dict_entry, DBUS_TYPE_VARIANT,
3165 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
3167 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
3168 DBUS_TYPE_BYTE_AS_STRING, &array);
3169 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
3171 dbus_message_iter_close_container(&variant, &array);
3172 dbus_message_iter_close_container(&dict_entry, &variant);
3173 dbus_message_iter_close_container(iter, &dict_entry);
3176 static void append_peer_service_dict(DBusMessageIter *iter, void *user_data)
3178 struct _peer_service *service = user_data;
3180 if (service->bjr_query && service->bjr_response) {
3181 append_dict_entry_fixed_array(iter, "BonjourQuery",
3182 &service->bjr_query, service->bjr_query_len);
3183 append_dict_entry_fixed_array(iter, "BonjourResponse",
3184 &service->bjr_response, service->bjr_response_len);
3185 } else if (service->upnp_service && service->version) {
3186 __connmanctl_dbus_append_dict_entry(iter, "UpnpVersion",
3187 DBUS_TYPE_INT32, &service->version);
3188 __connmanctl_dbus_append_dict_entry(iter, "UpnpService",
3189 DBUS_TYPE_STRING, &service->upnp_service);
3190 } else if (service->wfd_ies) {
3191 append_dict_entry_fixed_array(iter, "WiFiDisplayIEs",
3192 &service->wfd_ies, service->wfd_ies_len);
3196 static void peer_service_append(DBusMessageIter *iter, void *user_data)
3198 struct _peer_service *service = user_data;
3201 __connmanctl_dbus_append_dict(iter, append_peer_service_dict, service);
3203 if (service->master < 0)
3206 master = service->master == 1 ? TRUE : FALSE;
3207 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &master);
3210 static struct _peer_service *fill_in_peer_service(unsigned char *bjr_query,
3211 int bjr_query_len, unsigned char *bjr_response,
3212 int bjr_response_len, char *upnp_service,
3213 int version, unsigned char *wfd_ies,
3216 struct _peer_service *service;
3218 service = dbus_malloc0(sizeof(*service));
3220 #if defined TIZEN_EXT
3225 if (bjr_query_len && bjr_response_len) {
3226 service->bjr_query = dbus_malloc0(bjr_query_len);
3227 #if defined TIZEN_EXT
3228 if(!service->bjr_query) {
3233 memcpy(service->bjr_query, bjr_query, bjr_query_len);
3234 service->bjr_query_len = bjr_query_len;
3236 service->bjr_response = dbus_malloc0(bjr_response_len);
3237 #if defined TIZEN_EXT
3238 if(!service->bjr_response) {
3239 dbus_free(service->bjr_query);
3244 memcpy(service->bjr_response, bjr_response, bjr_response_len);
3245 service->bjr_response_len = bjr_response_len;
3246 } else if (upnp_service && version) {
3247 service->upnp_service = strdup(upnp_service);
3248 service->version = version;
3249 } else if (wfd_ies && wfd_ies_len) {
3250 service->wfd_ies = dbus_malloc0(wfd_ies_len);
3251 #if defined TIZEN_EXT
3252 if (!service->wfd_ies) {
3257 memcpy(service->wfd_ies, wfd_ies, wfd_ies_len);
3258 service->wfd_ies_len = wfd_ies_len;
3267 static void free_peer_service(struct _peer_service *service)
3269 dbus_free(service->bjr_query);
3270 dbus_free(service->bjr_response);
3271 dbus_free(service->wfd_ies);
3272 free(service->upnp_service);
3276 static int peer_service_register(unsigned char *bjr_query, int bjr_query_len,
3277 unsigned char *bjr_response, int bjr_response_len,
3278 char *upnp_service, int version,
3279 unsigned char *wfd_ies, int wfd_ies_len, int master)
3281 struct _peer_service *service;
3282 bool registration = true;
3285 service = fill_in_peer_service(bjr_query, bjr_query_len, bjr_response,
3286 bjr_response_len, upnp_service, version,
3287 wfd_ies, wfd_ies_len);
3291 service->master = master;
3293 ret = __connmanctl_dbus_method_call(connection, "net.connman", "/",
3294 "net.connman.Manager", "RegisterPeerService",
3295 peer_service_cb, GINT_TO_POINTER(registration),
3296 peer_service_append, service);
3298 free_peer_service(service);
3303 static int peer_service_unregister(unsigned char *bjr_query, int bjr_query_len,
3304 unsigned char *bjr_response, int bjr_response_len,
3305 char *upnp_service, int version,
3306 unsigned char *wfd_ies, int wfd_ies_len)
3308 struct _peer_service *service;
3309 bool registration = false;
3312 service = fill_in_peer_service(bjr_query, bjr_query_len, bjr_response,
3313 bjr_response_len, upnp_service, version,
3314 wfd_ies, wfd_ies_len);
3318 service->master = -1;
3320 ret = __connmanctl_dbus_method_call(connection, "net.connman", "/",
3321 "net.connman.Manager", "UnregisterPeerService",
3322 peer_service_cb, GINT_TO_POINTER(registration),
3323 peer_service_append, service);
3325 free_peer_service(service);
3330 static int parse_spec_array(char *command, unsigned char spec[1024])
3332 int length, pos, end;
3336 end = strlen(command);
3337 for (e = NULL, length = pos = 0; command[pos] != '\0'; length++) {
3341 b[0] = command[pos];
3342 b[1] = command[pos+1];
3344 spec[length] = strtol(b, &e, 16);
3345 if (e && *e != '\0')
3354 static int cmd_peer_service(char *args[], int num,
3355 struct connman_option *options)
3357 unsigned char bjr_query[1024] = {};
3358 unsigned char bjr_response[1024] = {};
3359 unsigned char wfd_ies[1024] = {};
3360 char *upnp_service = NULL;
3361 int bjr_query_len = 0, bjr_response_len = 0;
3362 int version = 0, master = 0, wfd_ies_len = 0;
3368 if (!strcmp(args[2], "wfd_ies")) {
3369 wfd_ies_len = parse_spec_array(args[3], wfd_ies);
3370 if (wfd_ies_len == -EINVAL)
3380 if (!strcmp(args[2], "bjr_query")) {
3381 if (strcmp(args[4], "bjr_response"))
3383 bjr_query_len = parse_spec_array(args[3], bjr_query);
3384 bjr_response_len = parse_spec_array(args[5], bjr_response);
3386 if (bjr_query_len == -EINVAL || bjr_response_len == -EINVAL)
3388 } else if (!strcmp(args[2], "upnp_service")) {
3391 if (strcmp(args[4], "upnp_version"))
3393 upnp_service = args[3];
3394 version = strtol(args[5], &e, 10);
3401 master = parse_boolean(args[6]);
3406 if (!strcmp(args[1], "register")) {
3407 return peer_service_register(bjr_query, bjr_query_len,
3408 bjr_response, bjr_response_len, upnp_service,
3409 version, wfd_ies, wfd_ies_len, master);
3410 } else if (!strcmp(args[1], "unregister")) {
3411 return peer_service_unregister(bjr_query, bjr_query_len,
3412 bjr_response, bjr_response_len, upnp_service,
3413 version, wfd_ies, wfd_ies_len);
3419 static const struct {
3421 const char *argument;
3422 struct connman_option *options;
3423 int (*func) (char *args[], int num, struct connman_option *options);
3425 __connmanctl_lookup_cb cb;
3427 { "state", NULL, NULL, cmd_state,
3428 "Shows if the system is online or offline", NULL },
3429 { "technologies", NULL, NULL, cmd_technologies,
3430 "Display technologies", NULL },
3431 { "clock", NULL, NULL, cmd_clock,
3432 "Get System Clock Properties", NULL },
3433 { "enable", "<technology>|offline", NULL, cmd_enable,
3434 "Enables given technology or offline mode",
3435 lookup_technology_offline },
3436 { "disable", "<technology>|offline", NULL, cmd_disable,
3437 "Disables given technology or offline mode",
3438 lookup_technology_offline },
3439 #if defined TIZEN_EXT_WIFI_MESH
3440 { "mesh", "", mesh_options, cmd_mesh, "Mesh specific commands",
3443 { "tether", "<technology> on|off\n"
3444 " wifi [on|off] <ssid> <passphrase> ",
3446 "Enable, disable tethering, set SSID and passphrase for wifi",
3448 { "tethering_clients", NULL, NULL, cmd_tethering_clients,
3449 "Display tethering clients", NULL },
3450 { "services", "[<service>]", service_options, cmd_services,
3451 "Display services", lookup_service_arg },
3452 #if defined TIZEN_EXT_INS
3453 { "ins", NULL, ins_options, cmd_ins,
3454 "Display intelligent network selection", NULL },
3456 { "peers", "[peer]", NULL, cmd_peers,
3457 "Display peers", lookup_peer_arg },
3458 { "scan", "<technology>", NULL, cmd_scan,
3459 "Scans for new services for given technology",
3460 lookup_technology_arg },
3461 { "connect", "<service/peer>", NULL, cmd_connect,
3462 "Connect a given service or peer", lookup_service_arg },
3463 { "disconnect", "<service/peer>", NULL, cmd_disconnect,
3464 "Disconnect a given service or peer", lookup_service_arg },
3465 { "move-before", "<service> <target service> ", NULL,
3466 cmd_service_move_before, "Move <service> before <target service>",
3467 lookup_service_arg },
3468 { "move-after", "<service> <target service> ", NULL,
3469 cmd_service_move_after, "Move <service> after <target service>",
3470 lookup_service_arg },
3471 { "config", "<service>", config_options, cmd_config,
3472 "Set service configuration options", lookup_config },
3473 { "monitor", "[off]", monitor_options, cmd_monitor,
3474 "Monitor signals from interfaces", lookup_monitor },
3475 { "agent", "on|off", NULL, cmd_agent,
3476 "Agent mode", lookup_agent },
3477 { "vpnconnections", "[<connection>]", NULL, cmd_vpnconnections,
3478 "Display VPN connections", lookup_vpnconnection_arg },
3479 { "vpnagent", "on|off", NULL, cmd_vpnagent,
3480 "VPN Agent mode", lookup_agent },
3481 { "session", "on|off|connect|disconnect|config", session_options,
3482 cmd_session, "Enable or disable a session", lookup_session },
3483 { "peer_service", "register|unregister <specs> <master>\n"
3484 "Where specs are:\n"
3485 "\tbjr_query <query> bjr_response <response>\n"
3486 "\tupnp_service <service> upnp_version <version>\n"
3487 "\twfd_ies <ies>\n", NULL,
3488 cmd_peer_service, "(Un)Register a Peer Service", NULL },
3489 { "help", NULL, NULL, cmd_help,
3490 "Show help", NULL },
3491 { "exit", NULL, NULL, cmd_exit,
3493 { "quit", NULL, NULL, cmd_exit,
3498 static int cmd_help(char *args[], int num, struct connman_option *options)
3500 bool interactive = __connmanctl_is_interactive();
3503 if (interactive == false)
3504 fprintf(stdout, "Usage: connmanctl [[command] [args]]\n");
3506 for (i = 0; cmd_table[i].cmd; i++) {
3507 const char *cmd = cmd_table[i].cmd;
3508 const char *argument = cmd_table[i].argument;
3509 const char *desc = cmd_table[i].desc;
3511 printf("%-16s%-22s%s\n", cmd? cmd: "",
3512 argument? argument: "",
3515 if (cmd_table[i].options) {
3516 for (j = 0; cmd_table[i].options[j].name;
3518 const char *options_desc =
3519 cmd_table[i].options[j].desc ?
3520 cmd_table[i].options[j].desc: "";
3522 printf(" --%-16s%s\n",
3523 cmd_table[i].options[j].name,
3529 if (interactive == false)
3530 fprintf(stdout, "\nNote: arguments and output are considered "
3531 "EXPERIMENTAL for now.\n");
3536 __connmanctl_lookup_cb __connmanctl_get_lookup_func(const char *text)
3538 int i, cmdlen, textlen;
3543 textlen = strlen(text);
3545 for (i = 0; cmd_table[i].cmd; i++) {
3546 cmdlen = strlen(cmd_table[i].cmd);
3548 if (textlen > cmdlen && text[cmdlen] != ' ')
3551 if (strncmp(cmd_table[i].cmd, text, cmdlen) == 0)
3552 return cmd_table[i].cb;
3558 int __connmanctl_commands(DBusConnection *dbus_conn, char *argv[], int argc)
3562 connection = dbus_conn;
3564 for (i = 0; cmd_table[i].cmd; i++) {
3565 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
3566 cmd_table[i].func) {
3567 result = cmd_table[i].func(argv, argc,
3568 cmd_table[i].options);
3569 if (result < 0 && result != -EINPROGRESS)
3570 fprintf(stderr, "Error '%s': %s\n", argv[0],
3576 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);
3580 char *__connmanctl_lookup_command(const char *text, int state)
3590 while (cmd_table[i].cmd) {
3591 const char *command = cmd_table[i].cmd;
3595 if (strncmp(text, command, len) == 0)
3596 return strdup(command);
3602 static char *get_path(char *full_path)
3606 path = strrchr(full_path, '/');
3607 if (path && *path != '\0')
3615 static void add_service_id(const char *path)
3617 g_hash_table_replace(service_hash, g_strdup(path),
3618 GINT_TO_POINTER(TRUE));
3621 static void remove_service_id(const char *path)
3623 g_hash_table_remove(service_hash, path);
3626 static void services_added(DBusMessageIter *iter)
3628 DBusMessageIter array;
3631 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
3633 dbus_message_iter_recurse(iter, &array);
3634 if (dbus_message_iter_get_arg_type(&array) !=
3635 DBUS_TYPE_OBJECT_PATH)
3638 dbus_message_iter_get_basic(&array, &path);
3639 add_service_id(get_path(path));
3641 dbus_message_iter_next(iter);
3645 static void update_services(DBusMessageIter *iter)
3647 DBusMessageIter array;
3650 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
3653 dbus_message_iter_recurse(iter, &array);
3654 services_added(&array);
3656 dbus_message_iter_next(iter);
3657 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
3660 dbus_message_iter_recurse(iter, &array);
3661 while (dbus_message_iter_get_arg_type(&array) ==
3662 DBUS_TYPE_OBJECT_PATH) {
3663 dbus_message_iter_get_basic(&array, &path);
3664 remove_service_id(get_path(path));
3666 dbus_message_iter_next(&array);
3670 static int populate_service_hash(DBusMessageIter *iter, const char *error,
3674 fprintf(stderr, "Error getting services: %s", error);
3678 update_services(iter);
3682 static void add_vpnconnection_id(const char *path)
3684 g_hash_table_replace(vpnconnection_hash, g_strdup(path),
3685 GINT_TO_POINTER(TRUE));
3688 static void remove_vpnconnection_id(const char *path)
3690 g_hash_table_remove(vpnconnection_hash, path);
3693 static void vpnconnection_added(DBusMessageIter *iter)
3697 dbus_message_iter_get_basic(iter, &path);
3698 add_vpnconnection_id(get_path(path));
3701 static void vpnconnection_removed(DBusMessageIter *iter)
3705 dbus_message_iter_get_basic(iter, &path);
3706 remove_vpnconnection_id(get_path(path));
3709 static void add_vpnconnections(DBusMessageIter *iter)
3711 DBusMessageIter array;
3714 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
3716 dbus_message_iter_recurse(iter, &array);
3717 if (dbus_message_iter_get_arg_type(&array) !=
3718 DBUS_TYPE_OBJECT_PATH)
3721 dbus_message_iter_get_basic(&array, &path);
3722 add_vpnconnection_id(get_path(path));
3724 dbus_message_iter_next(iter);
3728 static int populate_vpnconnection_hash(DBusMessageIter *iter, const char *error,
3731 DBusMessageIter array;
3734 fprintf(stderr, "Error getting VPN connections: %s", error);
3738 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
3741 dbus_message_iter_recurse(iter, &array);
3743 add_vpnconnections(&array);
3748 static void add_peer_id(const char *path)
3750 g_hash_table_replace(peer_hash, g_strdup(path), GINT_TO_POINTER(TRUE));
3753 static void remove_peer_id(const char *path)
3755 g_hash_table_remove(peer_hash, path);
3758 static void peers_added(DBusMessageIter *iter)
3760 DBusMessageIter array;
3763 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
3765 dbus_message_iter_recurse(iter, &array);
3766 if (dbus_message_iter_get_arg_type(&array) !=
3767 DBUS_TYPE_OBJECT_PATH)
3770 dbus_message_iter_get_basic(&array, &path);
3771 add_peer_id(get_path(path));
3773 dbus_message_iter_next(iter);
3777 static void update_peers(DBusMessageIter *iter)
3779 DBusMessageIter array;
3782 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
3785 dbus_message_iter_recurse(iter, &array);
3786 peers_added(&array);
3788 dbus_message_iter_next(iter);
3789 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
3792 dbus_message_iter_recurse(iter, &array);
3793 while (dbus_message_iter_get_arg_type(&array) ==
3794 DBUS_TYPE_OBJECT_PATH) {
3795 dbus_message_iter_get_basic(&array, &path);
3796 remove_peer_id(get_path(path));
3798 dbus_message_iter_next(&array);
3802 static int populate_peer_hash(DBusMessageIter *iter,
3803 const char *error, void *user_data)
3806 fprintf(stderr, "Error getting peers: %s", error);
3814 static void add_technology_id(const char *path)
3816 g_hash_table_replace(technology_hash, g_strdup(path),
3817 GINT_TO_POINTER(TRUE));
3820 static void remove_technology_id(const char *path)
3822 g_hash_table_remove(technology_hash, path);
3825 static void remove_technology(DBusMessageIter *iter)
3829 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_OBJECT_PATH)
3832 dbus_message_iter_get_basic(iter, &path);
3833 remove_technology_id(get_path(path));
3836 static void add_technology(DBusMessageIter *iter)
3840 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_OBJECT_PATH)
3843 dbus_message_iter_get_basic(iter, &path);
3844 add_technology_id(get_path(path));
3847 static void update_technologies(DBusMessageIter *iter)
3849 DBusMessageIter array;
3851 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
3854 dbus_message_iter_recurse(iter, &array);
3856 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
3857 DBusMessageIter object_path;
3859 dbus_message_iter_recurse(&array, &object_path);
3861 add_technology(&object_path);
3863 dbus_message_iter_next(&array);
3867 static int populate_technology_hash(DBusMessageIter *iter, const char *error,
3871 fprintf(stderr, "Error getting technologies: %s\n", error);
3875 update_technologies(iter);
3880 static DBusHandlerResult monitor_completions_changed(
3881 DBusConnection *connection,
3882 DBusMessage *message, void *user_data)
3884 bool *enabled = user_data;
3885 DBusMessageIter iter;
3886 DBusHandlerResult handled;
3889 handled = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3891 handled = DBUS_HANDLER_RESULT_HANDLED;
3893 if (dbus_message_is_signal(message, "net.connman.Manager",
3894 "ServicesChanged")) {
3895 dbus_message_iter_init(message, &iter);
3896 update_services(&iter);
3900 if (dbus_message_is_signal(message, "net.connman.vpn.Manager",
3901 "ConnectionAdded")) {
3902 dbus_message_iter_init(message, &iter);
3903 vpnconnection_added(&iter);
3907 if (dbus_message_is_signal(message, "net.connman.vpn.Manager",
3908 "ConnectionRemoved")) {
3909 dbus_message_iter_init(message, &iter);
3910 vpnconnection_removed(&iter);
3914 if (dbus_message_is_signal(message, "net.connman.Manager",
3916 dbus_message_iter_init(message, &iter);
3917 update_peers(&iter);
3921 if (dbus_message_is_signal(message, "net.connman.Manager",
3922 "TechnologyAdded")) {
3923 dbus_message_iter_init(message, &iter);
3924 add_technology(&iter);
3928 if (dbus_message_is_signal(message, "net.connman.Manager",
3929 "TechnologyRemoved")) {
3930 dbus_message_iter_init(message, &iter);
3931 remove_technology(&iter);
3935 if (!g_strcmp0(dbus_message_get_interface(message),
3936 "net.connman.Manager"))
3939 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3942 void __connmanctl_monitor_completions(DBusConnection *dbus_conn)
3944 bool *manager_enabled = NULL;
3948 for (i = 0; monitor[i].interface; i++) {
3949 if (!strcmp(monitor[i].interface, "Manager")) {
3950 manager_enabled = &monitor[i].enabled;
3956 g_hash_table_destroy(service_hash);
3957 g_hash_table_destroy(vpnconnection_hash);
3958 g_hash_table_destroy(technology_hash);
3960 dbus_bus_remove_match(connection,
3961 "type='signal',interface='net.connman.Manager'", NULL);
3962 dbus_bus_remove_match(connection,
3963 "type='signal',interface='net.connman.vpn.Manager'",
3965 dbus_connection_remove_filter(connection,
3966 monitor_completions_changed,
3971 connection = dbus_conn;
3973 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3976 vpnconnection_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3979 peer_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3982 technology_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3985 __connmanctl_dbus_method_call(connection,
3986 CONNMAN_SERVICE, CONNMAN_PATH,
3987 "net.connman.Manager", "GetServices",
3988 populate_service_hash, NULL, NULL, NULL);
3990 __connmanctl_dbus_method_call(connection,
3991 VPN_SERVICE, CONNMAN_PATH,
3992 "net.connman.vpn.Manager", "GetConnections",
3993 populate_vpnconnection_hash, NULL, NULL, NULL);
3995 __connmanctl_dbus_method_call(connection,
3996 CONNMAN_SERVICE, CONNMAN_PATH,
3997 "net.connman.Manager", "GetPeers",
3998 populate_peer_hash, NULL, NULL, NULL);
4000 __connmanctl_dbus_method_call(connection,
4001 CONNMAN_SERVICE, CONNMAN_PATH,
4002 "net.connman.Manager", "GetTechnologies",
4003 populate_technology_hash, NULL, NULL, NULL);
4005 dbus_connection_add_filter(connection,
4006 monitor_completions_changed, manager_enabled,
4009 dbus_error_init(&err);
4010 dbus_bus_add_match(connection,
4011 "type='signal',interface='net.connman.Manager'", &err);
4013 if (dbus_error_is_set(&err)) {
4014 fprintf(stderr, "Error: %s\n", err.message);
4018 dbus_bus_add_match(connection,
4019 "type='signal',interface='net.connman.vpn.Manager'",
4022 if (dbus_error_is_set(&err))
4023 fprintf(stderr, "Error: %s\n", err.message);