5 * Copyright (C) 2012-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 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
36 #include "dbus_helpers.h"
42 static DBusConnection *connection;
44 struct connman_option {
50 static char *ipv4[] = {
58 static char *ipv6[] = {
66 static int cmd_help(char *args[], int num, struct connman_option *options);
68 static int parse_boolean(char *arg)
73 if (strcasecmp(arg, "no") == 0 ||
74 strcasecmp(arg, "false") == 0 ||
75 strcasecmp(arg, "off" ) == 0 ||
76 strcasecmp(arg, "disable" ) == 0 ||
77 strcasecmp(arg, "n") == 0 ||
78 strcasecmp(arg, "f") == 0 ||
79 strcasecmp(arg, "0") == 0)
82 if (strcasecmp(arg, "yes") == 0 ||
83 strcasecmp(arg, "true") == 0 ||
84 strcasecmp(arg, "on") == 0 ||
85 strcasecmp(arg, "enable" ) == 0 ||
86 strcasecmp(arg, "y") == 0 ||
87 strcasecmp(arg, "t") == 0 ||
88 strcasecmp(arg, "1") == 0)
94 static int parse_args(char *arg, struct connman_option *options)
101 for (i = 0; options[i].name != NULL; i++) {
102 if (strcmp(options[i].name, arg) == 0 ||
103 (strncmp(arg, "--", 2) == 0 &&
104 strcmp(&arg[2], options[i].name) == 0))
105 return options[i].val;
111 static int enable_return(DBusMessageIter *iter, const char *error,
114 char *tech = user_data;
117 str = strrchr(tech, '/');
124 fprintf(stdout, "Enabled %s\n", str);
126 fprintf(stderr, "Error %s: %s\n", str, error);
133 static int cmd_enable(char *args[], int num, struct connman_option *options)
136 dbus_bool_t b = TRUE;
144 if (strcmp(args[1], "offlinemode") == 0) {
145 tech = g_strdup(args[1]);
146 return __connmanctl_dbus_set_property(connection, "/",
147 "net.connman.Manager", enable_return, tech,
148 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
151 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
152 return __connmanctl_dbus_set_property(connection, tech,
153 "net.connman.Technology", enable_return, tech,
154 "Powered", DBUS_TYPE_BOOLEAN, &b);
157 static int disable_return(DBusMessageIter *iter, const char *error,
160 char *tech = user_data;
163 str = strrchr(tech, '/');
170 fprintf(stdout, "Disabled %s\n", str);
172 fprintf(stderr, "Error %s: %s\n", str, error);
179 static int cmd_disable(char *args[], int num, struct connman_option *options)
182 dbus_bool_t b = FALSE;
190 if (strcmp(args[1], "offlinemode") == 0) {
191 tech = g_strdup(args[1]);
192 return __connmanctl_dbus_set_property(connection, "/",
193 "net.connman.Manager", disable_return, tech,
194 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
197 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
198 return __connmanctl_dbus_set_property(connection, tech,
199 "net.connman.Technology", disable_return, tech,
200 "Powered", DBUS_TYPE_BOOLEAN, &b);
203 static int state_print(DBusMessageIter *iter, const char *error,
206 DBusMessageIter entry;
209 fprintf(stderr, "Error: %s", error);
213 dbus_message_iter_recurse(iter, &entry);
214 __connmanctl_dbus_print(&entry, " ", " = ", "\n");
215 fprintf(stdout, "\n");
220 static int cmd_state(char *args[], int num, struct connman_option *options)
225 return __connmanctl_dbus_method_call(connection, "/",
226 "net.connman.Manager", "GetProperties",
227 state_print, NULL, DBUS_TYPE_INVALID);
230 static int services_list(DBusMessageIter *iter, const char *error,
234 __connmanctl_services_list(iter);
235 fprintf(stdout, "\n");
237 fprintf(stderr, "Error: %s\n", error);
243 static int services_properties(DBusMessageIter *iter, const char *error,
246 char *path = user_data;
248 DBusMessageIter dict;
251 fprintf(stdout, "%s\n", path);
253 dbus_message_iter_recurse(iter, &dict);
254 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
256 fprintf(stdout, "\n");
259 str = strrchr(path, '/');
265 fprintf(stderr, "Error %s: %s\n", str, error);
273 static int cmd_services(char *args[], int num, struct connman_option *options)
275 char *service_name = NULL;
282 c = parse_args(args[1], options);
289 service_name = args[2];
294 service_name = args[1];
298 if (service_name == NULL) {
299 return __connmanctl_dbus_method_call(connection, "/",
300 "net.connman.Manager", "GetServices",
301 services_list, NULL, DBUS_TYPE_INVALID);
304 path = g_strdup_printf("/net/connman/service/%s", service_name);
305 return __connmanctl_dbus_method_call(connection, path,
306 "net.connman.Service", "GetProperties",
307 services_properties, path, DBUS_TYPE_INVALID);
310 static int technology_print(DBusMessageIter *iter, const char *error,
313 DBusMessageIter array;
316 fprintf(stderr, "Error: %s\n", error);
320 dbus_message_iter_recurse(iter, &array);
321 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
322 DBusMessageIter entry, dict;
325 dbus_message_iter_recurse(&array, &entry);
326 dbus_message_iter_get_basic(&entry, &path);
327 fprintf(stdout, "%s\n", path);
329 dbus_message_iter_next(&entry);
331 dbus_message_iter_recurse(&entry, &dict);
332 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
333 fprintf(stdout, "\n");
335 dbus_message_iter_next(&array);
341 static int cmd_technologies(char *args[], int num,
342 struct connman_option *options)
347 return __connmanctl_dbus_method_call(connection, "/",
348 "net.connman.Manager", "GetTechnologies",
349 technology_print, NULL, DBUS_TYPE_INVALID);
352 struct tether_enable {
357 static int tether_set_return(DBusMessageIter *iter, const char *error,
360 struct tether_enable *tether = user_data;
363 str = strrchr(tether->path, '/');
370 fprintf(stdout, "%s tethering for %s\n",
371 tether->enable == TRUE ? "Enabled": "Disabled",
374 fprintf(stderr, "Error %s %s tethering: %s\n",
375 tether->enable == TRUE ?
376 "enabling": "disabling", str, error);
378 g_free(tether->path);
384 static int tether_set(char *technology, int set_tethering)
386 struct tether_enable *tether = g_new(struct tether_enable, 1);
388 switch(set_tethering) {
390 tether->enable = TRUE;
393 tether->enable = FALSE;
400 tether->path = g_strdup_printf("/net/connman/technology/%s",
403 return __connmanctl_dbus_set_property(connection, tether->path,
404 "net.connman.Technology", tether_set_return,
405 tether, "Tethering", DBUS_TYPE_BOOLEAN,
409 struct tether_properties {
411 int passphrase_result;
415 static int tether_update(struct tether_properties *tether)
417 printf("%d %d %d\n", tether->ssid_result, tether->passphrase_result,
418 tether->set_tethering);
420 if (tether->ssid_result == 0 && tether->passphrase_result == 0)
421 return tether_set("wifi", tether->set_tethering);
423 if (tether->ssid_result != -EINPROGRESS &&
424 tether->passphrase_result != -EINPROGRESS) {
432 static int tether_set_ssid_return(DBusMessageIter *iter, const char *error,
435 struct tether_properties *tether = user_data;
438 fprintf(stdout, "Wifi SSID set\n");
439 tether->ssid_result = 0;
441 fprintf(stderr, "Error setting wifi SSID: %s\n", error);
442 tether->ssid_result = -EINVAL;
445 return tether_update(tether);
448 static int tether_set_passphrase_return(DBusMessageIter *iter,
449 const char *error, void *user_data)
451 struct tether_properties *tether = user_data;
454 fprintf(stdout, "Wifi passphrase set\n");
455 tether->passphrase_result = 0;
457 fprintf(stderr, "Error setting wifi passphrase: %s\n", error);
458 tether->passphrase_result = -EINVAL;
461 return tether_update(tether);
464 static int tether_set_ssid(char *ssid, char *passphrase, int set_tethering)
466 struct tether_properties *tether = g_new(struct tether_properties, 1);
468 tether->set_tethering = set_tethering;
470 tether->ssid_result = __connmanctl_dbus_set_property(connection,
471 "/net/connman/technology/wifi",
472 "net.connman.Technology",
473 tether_set_ssid_return, tether,
474 "TetheringIdentifier", DBUS_TYPE_STRING, &ssid);
476 tether->passphrase_result =__connmanctl_dbus_set_property(connection,
477 "/net/connman/technology/wifi",
478 "net.connman.Technology",
479 tether_set_passphrase_return, tether,
480 "TetheringPassphrase", DBUS_TYPE_STRING, &passphrase);
482 if (tether->ssid_result != -EINPROGRESS &&
483 tether->passphrase_result != -EINPROGRESS) {
491 static int cmd_tether(char *args[], int num, struct connman_option *options)
493 char *ssid, *passphrase;
499 passphrase = args[num - 1];
500 ssid = args[num - 2];
502 set_tethering = parse_boolean(args[2]);
504 if (strcmp(args[1], "wifi") == 0) {
509 if (num == 5 && set_tethering == -1)
516 return tether_set_ssid(ssid, passphrase, set_tethering);
522 if (set_tethering == -1)
525 return tether_set(args[1], set_tethering);
528 static int scan_return(DBusMessageIter *iter, const char *error,
531 char *path = user_data;
534 char *str = strrchr(path, '/');
536 fprintf(stdout, "Scan completed for %s\n", str);
538 fprintf(stderr, "Error %s: %s", path, error);
545 static int cmd_scan(char *args[], int num, struct connman_option *options)
555 path = g_strdup_printf("/net/connman/technology/%s", args[1]);
556 return __connmanctl_dbus_method_call(connection, path,
557 "net.connman.Technology", "Scan",
558 scan_return, path, DBUS_TYPE_INVALID);
561 static int connect_return(DBusMessageIter *iter, const char *error,
564 char *path = user_data;
567 char *str = strrchr(path, '/');
569 fprintf(stdout, "Connected %s\n", str);
571 fprintf(stderr, "Error %s: %s\n", path, error);
578 static int cmd_connect(char *args[], int num, struct connman_option *options)
588 path = g_strdup_printf("/net/connman/service/%s", args[1]);
589 return __connmanctl_dbus_method_call(connection, path,
590 "net.connman.Service", "Connect",
591 connect_return, path, DBUS_TYPE_INVALID);
594 static int disconnect_return(DBusMessageIter *iter, const char *error,
597 char *path = user_data;
600 char *str = strrchr(path, '/');
602 fprintf(stdout, "Disconnected %s\n", str);
604 fprintf(stderr, "Error %s: %s\n", path, error);
611 static int cmd_disconnect(char *args[], int num, struct connman_option *options)
621 path = g_strdup_printf("/net/connman/service/%s", args[1]);
622 return __connmanctl_dbus_method_call(connection, path,
623 "net.connman.Service", "Disconnect",
624 disconnect_return, path, DBUS_TYPE_INVALID);
627 static int config_return(DBusMessageIter *iter, const char *error,
630 char *service_name = user_data;
633 fprintf(stderr, "Error %s: %s\n", service_name, error);
640 struct config_append {
645 static void config_append_ipv4(DBusMessageIter *iter,
648 struct config_append *append = user_data;
649 char **opts = append->opts;
655 while (opts[i] != NULL && ipv4[i] != NULL) {
656 __connmanctl_dbus_append_dict_entry(iter, ipv4[i],
657 DBUS_TYPE_STRING, &opts[i]);
664 static void config_append_ipv6(DBusMessageIter *iter, void *user_data)
666 struct config_append *append = user_data;
667 char **opts = append->opts;
674 if (g_strcmp0(opts[0], "auto") == 0) {
677 switch (parse_boolean(opts[1])) {
682 __connmanctl_dbus_append_dict_entry(iter, "Privacy",
683 DBUS_TYPE_STRING, &str);
690 __connmanctl_dbus_append_dict_entry(iter, "Privacy",
691 DBUS_TYPE_STRING, &str);
695 if (opts[1] != NULL) {
698 if (g_strcmp0(opts[0], "prefered") != 0) {
699 fprintf(stderr, "Error %s: %s\n",
706 __connmanctl_dbus_append_dict_entry(iter,
707 "Privacy", DBUS_TYPE_STRING,
712 } else if (g_strcmp0(opts[0], "manual") == 0) {
715 while (opts[i] != NULL && ipv6[i] != NULL) {
717 int value = atoi(opts[i]);
718 __connmanctl_dbus_append_dict_entry(iter,
719 ipv6[i], DBUS_TYPE_BYTE,
722 __connmanctl_dbus_append_dict_entry(iter,
723 ipv6[i], DBUS_TYPE_STRING,
731 } else if (g_strcmp0(opts[0], "off") != 0) {
732 fprintf(stderr, "Error %s: %s\n", opts[0], strerror(-EINVAL));
737 __connmanctl_dbus_append_dict_entry(iter, "Method", DBUS_TYPE_STRING,
741 static void config_append_str(DBusMessageIter *iter, void *user_data)
743 struct config_append *append = user_data;
744 char **opts = append->opts;
750 while (opts[i] != NULL) {
751 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
759 static void append_servers(DBusMessageIter *iter, void *user_data)
761 struct config_append *append = user_data;
762 char **opts = append->opts;
768 while (opts[i] != NULL && g_strcmp0(opts[i], "--excludes") != 0) {
769 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
777 static void append_excludes(DBusMessageIter *iter, void *user_data)
779 struct config_append *append = user_data;
780 char **opts = append->opts;
781 int i = append->values;
783 if (opts == NULL || opts[i] == NULL ||
784 g_strcmp0(opts[i], "--excludes") != 0)
788 while (opts[i] != NULL) {
789 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
797 static void config_append_proxy(DBusMessageIter *iter, void *user_data)
799 struct config_append *append = user_data;
800 char **opts = append->opts;
805 if (g_strcmp0(opts[0], "manual") == 0) {
806 __connmanctl_dbus_append_dict_string_array(iter, "Servers",
807 append_servers, append);
809 __connmanctl_dbus_append_dict_string_array(iter, "Excludes",
810 append_excludes, append);
812 } else if (g_strcmp0(opts[0], "auto") == 0) {
813 if (opts[1] != NULL) {
814 __connmanctl_dbus_append_dict_entry(iter, "URL",
815 DBUS_TYPE_STRING, &opts[1]);
819 } else if (g_strcmp0(opts[0], "direct") != 0)
822 __connmanctl_dbus_append_dict_entry(iter, "Method",DBUS_TYPE_STRING,
828 static int cmd_config(char *args[], int num, struct connman_option *options)
830 int result = 0, res = 0, index = 2, oldindex = 0;
832 char *service_name, *path;
835 struct config_append append;
837 service_name = args[1];
838 if (service_name == NULL)
841 while (index < num && args[index] != NULL) {
842 c = parse_args(args[index], options);
843 opt_start = &args[index + 1];
844 append.opts = opt_start;
850 path = g_strdup_printf("/net/connman/service/%s", service_name);
854 switch (parse_boolean(*opt_start)) {
869 res = __connmanctl_dbus_set_property(connection,
870 path, "net.connman.Service",
872 g_strdup(service_name),
874 DBUS_TYPE_BOOLEAN, &val);
878 res = __connmanctl_dbus_set_property_dict(connection,
879 path, "net.connman.Service",
880 config_return, g_strdup(service_name),
881 "IPv4.Configuration", DBUS_TYPE_STRING,
882 config_append_ipv4, &append);
883 index += append.values;
887 res = __connmanctl_dbus_set_property_dict(connection,
888 path, "net.connman.Service",
889 config_return, g_strdup(service_name),
890 "IPv6.Configuration", DBUS_TYPE_STRING,
891 config_append_ipv6, &append);
892 index += append.values;
896 res = __connmanctl_dbus_set_property_array(connection,
897 path, "net.connman.Service",
898 config_return, g_strdup(service_name),
899 "Nameservers.Configuration",
900 DBUS_TYPE_STRING, config_append_str,
902 index += append.values;
906 res = __connmanctl_dbus_set_property_array(connection,
907 path, "net.connman.Service",
908 config_return, g_strdup(service_name),
909 "Timeservers.Configuration",
910 DBUS_TYPE_STRING, config_append_str,
912 index += append.values;
916 res = __connmanctl_dbus_set_property_array(connection,
917 path, "net.connman.Service",
918 config_return, g_strdup(service_name),
919 "Domains.Configuration",
920 DBUS_TYPE_STRING, config_append_str,
922 index += append.values;
926 res = __connmanctl_dbus_set_property_dict(connection,
927 path, "net.connman.Service",
928 config_return, g_strdup(service_name),
929 "Proxy.Configuration",
930 DBUS_TYPE_STRING, config_append_proxy,
932 index += append.values;
935 res = __connmanctl_dbus_method_call(connection,
936 path, "net.connman.Service", "Remove",
937 config_return, g_strdup(service_name),
948 if (res == -EINPROGRESS)
949 result = -EINPROGRESS;
951 printf("Error '%s': %s\n", args[oldindex],
962 static DBusHandlerResult monitor_changed(DBusConnection *connection,
963 DBusMessage *message, void *user_data)
965 DBusMessageIter iter;
966 const char *interface, *path;
968 interface = dbus_message_get_interface(message);
969 if (strncmp(interface, "net.connman.", 12) != 0)
970 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
972 interface = strrchr(interface, '.');
973 if (interface != NULL && *interface != '\0')
976 path = strrchr(dbus_message_get_path(message), '/');
977 if (path != NULL && *path != '\0')
980 __connmanctl_save_rl();
982 if (dbus_message_is_signal(message, "net.connman.Manager",
983 "ServicesChanged") == TRUE) {
985 fprintf(stdout, "%-12s %-20s = {\n", interface,
987 dbus_message_iter_init(message, &iter);
988 __connmanctl_services_list(&iter);
989 fprintf(stdout, "\n}\n");
991 __connmanctl_redraw_rl();
993 return DBUS_HANDLER_RESULT_HANDLED;
996 if (dbus_message_is_signal(message, "net.connman.Manager",
997 "TechnologyAdded") == TRUE)
998 path = "TechnologyAdded";
1000 if (dbus_message_is_signal(message, "net.connman.Manager",
1001 "TechnologyRemoved") == TRUE)
1002 path = "TechnologyRemoved";
1004 fprintf(stdout, "%-12s %-20s ", interface, path);
1005 dbus_message_iter_init(message, &iter);
1007 __connmanctl_dbus_print(&iter, "", " = ", " = ");
1008 fprintf(stdout, "\n");
1010 __connmanctl_redraw_rl();
1012 return DBUS_HANDLER_RESULT_HANDLED;
1015 static bool monitor_s = false;
1016 static bool monitor_t = false;
1017 static bool monitor_m = false;
1019 static void monitor_add(char *interface)
1024 if (monitor_s == false && monitor_t == false && monitor_m == false)
1025 dbus_connection_add_filter(connection, monitor_changed,
1028 if (g_strcmp0(interface, "Service") == 0) {
1029 if (monitor_s == true)
1032 } else if (g_strcmp0(interface, "Technology") == 0) {
1033 if (monitor_t == true)
1036 } else if (g_strcmp0(interface, "Manager") == 0) {
1037 if (monitor_m == true)
1043 dbus_error_init(&err);
1044 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
1046 dbus_bus_add_match(connection, rule, &err);
1049 if (dbus_error_is_set(&err))
1050 fprintf(stderr, "Error: %s\n", err.message);
1053 static void monitor_del(char *interface)
1057 if (g_strcmp0(interface, "Service") == 0) {
1058 if (monitor_s == false)
1061 } else if (g_strcmp0(interface, "Technology") == 0) {
1062 if (monitor_t == false)
1065 } else if (g_strcmp0(interface, "Manager") == 0) {
1066 if (monitor_m == false)
1072 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
1074 dbus_bus_remove_match(connection, rule, NULL);
1077 if (monitor_s == false && monitor_t == false && monitor_m == false)
1078 dbus_connection_remove_filter(connection, monitor_changed,
1082 static int cmd_monitor(char *args[], int num, struct connman_option *options)
1091 switch (parse_boolean(args[2])) {
1101 c = parse_args(args[1], options);
1104 monitor_add("Service");
1105 monitor_add("Technology");
1106 monitor_add("Manager");
1111 monitor_add("Service");
1113 monitor_del("Service");
1118 monitor_add("Technology");
1120 monitor_del("Technology");
1125 monitor_add("Manager");
1127 monitor_del("Manager");
1131 switch(parse_boolean(args[1])) {
1133 monitor_del("Service");
1134 monitor_del("Technology");
1135 monitor_del("Manager");
1139 monitor_add("Service");
1140 monitor_add("Technology");
1141 monitor_add("Manager");
1150 return -EINPROGRESS;
1155 static int cmd_agent(char *args[], int num, struct connman_option *options)
1163 switch(parse_boolean(args[1])) {
1165 __connmanctl_agent_unregister(connection);
1169 if (__connmanctl_agent_register(connection) == -EINPROGRESS)
1170 return -EINPROGRESS;
1182 static int cmd_exit(char *args[], int num, struct connman_option *options)
1187 static struct connman_option service_options[] = {
1188 {"properties", 'p', "[<service>] (obsolete)"},
1192 static struct connman_option config_options[] = {
1193 {"nameservers", 'n', "<dns1> [<dns2>] [<dns3>]"},
1194 {"timeservers", 't', "<ntp1> [<ntp2>] [...]"},
1195 {"domains", 'd', "<domain1> [<domain2>] [...]"},
1196 {"ipv6", 'v', "off|auto [enable|disable|prefered]|\n"
1197 "\t\t\tmanual <address> <prefixlength> <gateway>"},
1198 {"proxy", 'x', "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
1199 "\t\t\t[exclude <exclude1> [<exclude2>] [...]]"},
1200 {"autoconnect", 'a', "yes|no"},
1201 {"ipv4", 'i', "off|dhcp|manual <address> <netmask> <gateway>"},
1202 {"remove", 'r', " Remove service"},
1206 static struct connman_option monitor_options[] = {
1207 {"services", 's', "[off] Monitor only services"},
1208 {"tech", 'c', "[off] Monitor only technologies"},
1209 {"manager", 'm', "[off] Monitor only manager interface"},
1213 static const struct {
1215 const char *argument;
1216 struct connman_option *options;
1217 int (*func) (char *args[], int num, struct connman_option *options);
1220 { "state", NULL, NULL, cmd_state,
1221 "Shows if the system is online or offline" },
1222 { "technologies", NULL, NULL, cmd_technologies,
1223 "Display technologies" },
1224 { "enable", "<technology>|offline", NULL, cmd_enable,
1225 "Enables given technology or offline mode" },
1226 { "disable", "<technology>|offline", NULL, cmd_disable,
1227 "Disables given technology or offline mode"},
1228 { "tether", "<technology> on|off\n"
1229 " wifi [on|off] <ssid> <passphrase> ",
1231 "Enable, disable tethering, set SSID and passphrase for wifi" },
1232 { "services", "[<service>]", service_options, cmd_services,
1233 "Display services" },
1234 { "scan", "<technology>", NULL, cmd_scan,
1235 "Scans for new services for given technology" },
1236 { "connect", "<service>", NULL, cmd_connect,
1237 "Connect a given service" },
1238 { "disconnect", "<service>", NULL, cmd_disconnect,
1239 "Disconnect a given service" },
1240 { "config", "<service>", config_options, cmd_config,
1241 "Set service configuration options" },
1242 { "monitor", "[off]", monitor_options, cmd_monitor,
1243 "Monitor signals from interfaces" },
1244 { "agent", "on|off", NULL, cmd_agent,
1246 { "help", NULL, NULL, cmd_help,
1248 { "exit", NULL, NULL, cmd_exit,
1250 { "quit", NULL, NULL, cmd_exit,
1255 static int cmd_help(char *args[], int num, struct connman_option *options)
1257 bool interactive = __connmanctl_is_interactive();
1260 if (interactive == false)
1261 fprintf(stdout, "Usage: connmanctl [[command] [args]]\n");
1263 for (i = 0; cmd_table[i].cmd != NULL; i++) {
1264 const char *cmd = cmd_table[i].cmd;
1265 const char *argument = cmd_table[i].argument;
1266 const char *desc = cmd_table[i].desc;
1268 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
1269 argument != NULL? argument: "",
1270 desc != NULL? desc: "");
1272 if (cmd_table[i].options != NULL) {
1273 for (j = 0; cmd_table[i].options[j].name != NULL;
1275 const char *options_desc =
1276 cmd_table[i].options[j].desc != NULL ?
1277 cmd_table[i].options[j].desc: "";
1279 printf(" --%-12s%s\n",
1280 cmd_table[i].options[j].name,
1286 if (interactive == false)
1287 fprintf(stdout, "\nNote: arguments and output are considered "
1288 "EXPERIMENTAL for now.\n");
1293 int __connmanctl_commands(DBusConnection *dbus_conn, char *argv[], int argc)
1297 connection = dbus_conn;
1299 for (i = 0; cmd_table[i].cmd != NULL; i++) {
1300 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
1301 cmd_table[i].func != NULL) {
1302 result = cmd_table[i].func(argv, argc,
1303 cmd_table[i].options);
1304 if (result < 0 && result != -EINPROGRESS)
1305 fprintf(stderr, "Error '%s': %s\n", argv[0],
1311 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);
1315 char *__connmanctl_lookup_command(const char *text, int state)
1325 while (cmd_table[i].cmd != NULL) {
1326 const char *command = cmd_table[i].cmd;
1330 if (strncmp(text, command, len) == 0)
1331 return strdup(command);