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
38 #include "technology.h"
39 #include "data_manager.h"
41 #include "dbus_helpers.h"
45 #define MANDATORY_ARGS 3
47 static DBusConnection *connection;
49 static char *ipv4[] = {
57 static char *ipv6[] = {
65 static char *proxy_simple[] = {
71 static int cmd_help(char *args[], int num, struct option *options);
73 static int parse_args(char *arg, struct option *options)
80 for (i = 0; options[i].name != NULL; i++) {
81 if (strcmp(options[i].name, arg) == 0 ||
82 (strncmp(arg, "--", 2) == 0 &&
83 strcmp(&arg[2], options[i].name) == 0))
84 return options[i].val;
90 static void enable_return(DBusMessageIter *iter, const char *error,
93 char *tech = user_data;
96 str = strrchr(tech, '/');
103 fprintf(stdout, "Enabled %s\n", str);
105 fprintf(stderr, "Error %s: %s\n", str, error);
110 static int cmd_enable(char *args[], int num, struct option *options)
113 dbus_bool_t b = TRUE;
121 if (strcmp(args[1], "offlinemode") == 0) {
122 tech = g_strdup(args[1]);
123 return __connmanctl_dbus_set_property(connection, "/",
124 "net.connman.Manager", enable_return, tech,
125 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
128 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
129 return __connmanctl_dbus_set_property(connection, tech,
130 "net.connman.Technology", enable_return, tech,
131 "Powered", DBUS_TYPE_BOOLEAN, &b);
134 static void disable_return(DBusMessageIter *iter, const char *error,
137 char *tech = user_data;
140 str = strrchr(tech, '/');
147 fprintf(stdout, "Disabled %s\n", str);
149 fprintf(stderr, "Error %s: %s\n", str, error);
154 static int cmd_disable(char *args[], int num, struct option *options)
157 dbus_bool_t b = FALSE;
165 if (strcmp(args[1], "offlinemode") == 0) {
166 tech = g_strdup(args[1]);
167 return __connmanctl_dbus_set_property(connection, "/",
168 "net.connman.Manager", disable_return, tech,
169 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
172 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
173 return __connmanctl_dbus_set_property(connection, tech,
174 "net.connman.Technology", disable_return, tech,
175 "Powered", DBUS_TYPE_BOOLEAN, &b);
178 static void state_print(DBusMessageIter *iter, const char *error,
181 DBusMessageIter entry;
184 fprintf(stderr, "Error: %s", error);
188 dbus_message_iter_recurse(iter, &entry);
189 __connmanctl_dbus_print(&entry, " ", " = ", "\n");
190 fprintf(stdout, "\n");
193 static int cmd_state(char *args[], int num, struct option *options)
198 return __connmanctl_dbus_method_call(connection, "/",
199 "net.connman.Manager", "GetProperties",
200 state_print, NULL, DBUS_TYPE_INVALID);
203 static void services_list(DBusMessageIter *iter, const char *error,
207 __connmanctl_services_list(iter);
208 fprintf(stdout, "\n");
210 fprintf(stderr, "Error: %s\n", error);
214 static void services_properties(DBusMessageIter *iter, const char *error,
217 char *path = user_data;
219 DBusMessageIter dict;
222 fprintf(stdout, "%s\n", path);
224 dbus_message_iter_recurse(iter, &dict);
225 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
227 fprintf(stdout, "\n");
230 str = strrchr(path, '/');
236 fprintf(stderr, "Error %s: %s\n", str, error);
242 static int cmd_services(char *args[], int num, struct option *options)
244 char *service_name = NULL;
251 c = parse_args(args[1], options);
258 service_name = args[2];
263 service_name = args[1];
267 if (service_name == NULL) {
268 return __connmanctl_dbus_method_call(connection, "/",
269 "net.connman.Manager", "GetServices",
270 services_list, NULL, DBUS_TYPE_INVALID);
273 path = g_strdup_printf("/net/connman/service/%s", service_name);
274 return __connmanctl_dbus_method_call(connection, path,
275 "net.connman.Service", "GetProperties",
276 services_properties, path, DBUS_TYPE_INVALID);
279 static void technology_print(DBusMessageIter *iter, const char *error,
282 DBusMessageIter array;
285 fprintf(stderr, "Error: %s\n", error);
289 dbus_message_iter_recurse(iter, &array);
290 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
291 DBusMessageIter entry, dict;
294 dbus_message_iter_recurse(&array, &entry);
295 dbus_message_iter_get_basic(&entry, &path);
296 fprintf(stdout, "%s\n", path);
298 dbus_message_iter_next(&entry);
300 dbus_message_iter_recurse(&entry, &dict);
301 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
302 fprintf(stdout, "\n");
304 dbus_message_iter_next(&array);
308 static int cmd_technologies(char *args[], int num, struct option *options)
313 return __connmanctl_dbus_method_call(connection, "/",
314 "net.connman.Manager", "GetTechnologies",
315 technology_print, NULL, DBUS_TYPE_INVALID);
318 static void scan_return(DBusMessageIter *iter, const char *error,
321 char *path = user_data;
324 char *str = strrchr(path, '/');
326 fprintf(stdout, "Scan completed for %s\n", str);
328 fprintf(stderr, "Error %s: %s", path, error);
333 static int cmd_scan(char *args[], int num, struct option *options)
343 path = g_strdup_printf("/net/connman/technology/%s", args[1]);
344 return __connmanctl_dbus_method_call(connection, path,
345 "net.connman.Technology", "Scan",
346 scan_return, path, DBUS_TYPE_INVALID);
349 static void connect_return(DBusMessageIter *iter, const char *error,
352 char *path = user_data;
355 char *str = strrchr(path, '/');
357 fprintf(stdout, "Connected %s\n", str);
359 fprintf(stderr, "Error %s: %s\n", path, error);
364 static int cmd_connect(char *args[], int num, struct option *options)
374 path = g_strdup_printf("/net/connman/service/%s", args[1]);
375 return __connmanctl_dbus_method_call(connection, path,
376 "net.connman.Service", "Connect",
377 connect_return, path, DBUS_TYPE_INVALID);
380 static void disconnect_return(DBusMessageIter *iter, const char *error,
383 char *path = user_data;
386 char *str = strrchr(path, '/');
388 fprintf(stdout, "Disconnected %s\n", str);
390 fprintf(stderr, "Error %s: %s\n", path, error);
395 static int cmd_disconnect(char *args[], int num, struct option *options)
405 path = g_strdup_printf("/net/connman/service/%s", args[1]);
406 return __connmanctl_dbus_method_call(connection, path,
407 "net.connman.Service", "Disconnect",
408 disconnect_return, path, DBUS_TYPE_INVALID);
413 static void config_return(DBusMessageIter *iter, const char *error,
416 char *service_name = user_data;
419 fprintf(stderr, "Error %s: %s\n", service_name, error);
424 struct config_append {
429 static void config_append_ipv4(DBusMessageIter *iter,
432 struct config_append *append = user_data;
433 char **opts = append->opts;
439 while (opts[i] != NULL && ipv4[i] != NULL) {
440 __connmanctl_dbus_append_dict_entry(iter, ipv4[i],
441 DBUS_TYPE_STRING, &opts[i]);
448 static void config_append_ipv6(DBusMessageIter *iter, void *user_data)
450 struct config_append *append = user_data;
451 char **opts = append->opts;
458 if (g_strcmp0(opts[0], "auto") == 0) {
461 switch (parse_boolean(opts[1])) {
466 __connmanctl_dbus_append_dict_entry(iter, "Privacy",
467 DBUS_TYPE_STRING, &str);
474 __connmanctl_dbus_append_dict_entry(iter, "Privacy",
475 DBUS_TYPE_STRING, &str);
479 if (opts[1] != NULL) {
482 if (g_strcmp0(opts[0], "prefered") != 0) {
483 fprintf(stderr, "Error %s: %s\n",
490 __connmanctl_dbus_append_dict_entry(iter,
491 "Privacy", DBUS_TYPE_STRING,
496 } else if (g_strcmp0(opts[0], "manual") == 0) {
499 while (opts[i] != NULL && ipv6[i] != NULL) {
501 int value = atoi(opts[i]);
502 __connmanctl_dbus_append_dict_entry(iter,
503 ipv6[i], DBUS_TYPE_BYTE,
506 __connmanctl_dbus_append_dict_entry(iter,
507 ipv6[i], DBUS_TYPE_STRING,
515 } else if (g_strcmp0(opts[0], "off") != 0) {
516 fprintf(stderr, "Error %s: %s\n", opts[0], strerror(-EINVAL));
521 __connmanctl_dbus_append_dict_entry(iter, "Method", DBUS_TYPE_STRING,
525 static int cmd_config(char *args[], int num, struct option *options)
527 int result = 0, res = 0, index = 2, oldindex = 0;
529 char *service_name, *path;
530 DBusMessage *message;
533 struct config_append append;
535 service_name = args[1];
536 if (service_name == NULL)
539 while (index < num && args[index] != NULL) {
540 c = parse_args(args[index], options);
541 opt_start = &args[index + 1];
542 append.opts = opt_start;
547 message = get_message(connection, "GetServices");
552 path = g_strdup_printf("/net/connman/service/%s", service_name);
556 switch (parse_boolean(*opt_start)) {
571 res = __connmanctl_dbus_set_property(connection,
572 path, "net.connman.Service",
574 g_strdup(service_name),
576 DBUS_TYPE_BOOLEAN, &val);
580 res = __connmanctl_dbus_set_property_dict(connection,
581 path, "net.connman.Service",
582 config_return, g_strdup(service_name),
583 "IPv4.Configuration", DBUS_TYPE_STRING,
584 config_append_ipv4, &append);
585 index += append.values;
589 res = __connmanctl_dbus_set_property_dict(connection,
590 path, "net.connman.Service",
591 config_return, g_strdup(service_name),
592 "IPv6.Configuration", DBUS_TYPE_STRING,
593 config_append_ipv6, &append);
594 index += append.values;
598 res = set_service_property(connection, message,
600 "Nameservers.Configuration",
604 res = set_service_property(connection, message,
606 "Timeservers.Configuration",
610 res = set_service_property(connection, message,
612 "Domains.Configuration",
616 if (*opt_start == NULL) {
621 if (strcmp(*opt_start, "direct") == 0) {
622 res = set_service_property(connection, message,
624 "Proxy.Configuration",
625 proxy_simple, opt_start, 1);
629 if (strcmp(*opt_start, "auto") == 0) {
630 res = set_service_property(connection, message,
632 "Proxy.Configuration",
633 proxy_simple, opt_start, 1);
637 if (strcmp(*opt_start, "manual") == 0) {
638 char **url_start = &args[index + 2];
640 if (*url_start != NULL &&
643 url_start = &args[index + 3];
646 res = store_proxy_input(connection,
647 message, service_name,
653 res = __connmanctl_dbus_method_call(connection,
654 path, "net.connman.Service", "Remove",
655 config_return, g_strdup(service_name),
664 dbus_message_unref(message);
667 if (res == -EINPROGRESS)
668 result = -EINPROGRESS;
670 printf("Error '%s': %s\n", args[oldindex],
681 static DBusHandlerResult monitor_changed(DBusConnection *connection,
682 DBusMessage *message, void *user_data)
684 DBusMessageIter iter;
685 const char *interface, *path;
687 interface = dbus_message_get_interface(message);
688 if (strncmp(interface, "net.connman.", 12) != 0)
689 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
691 interface = strrchr(interface, '.');
692 if (interface != NULL && *interface != '\0')
695 path = strrchr(dbus_message_get_path(message), '/');
696 if (path != NULL && *path != '\0')
699 __connmanctl_save_rl();
701 if (dbus_message_is_signal(message, "net.connman.Manager",
702 "ServicesChanged") == TRUE) {
704 fprintf(stdout, "%-12s %-20s = {\n", interface,
706 dbus_message_iter_init(message, &iter);
707 __connmanctl_services_list(&iter);
708 fprintf(stdout, "\n}\n");
710 __connmanctl_redraw_rl();
712 return DBUS_HANDLER_RESULT_HANDLED;
715 if (dbus_message_is_signal(message, "net.connman.Manager",
716 "TechnologyAdded") == TRUE)
717 path = "TechnologyAdded";
719 if (dbus_message_is_signal(message, "net.connman.Manager",
720 "TechnologyRemoved") == TRUE)
721 path = "TechnologyRemoved";
723 fprintf(stdout, "%-12s %-20s ", interface, path);
724 dbus_message_iter_init(message, &iter);
726 __connmanctl_dbus_print(&iter, "", " = ", " = ");
727 fprintf(stdout, "\n");
729 __connmanctl_redraw_rl();
731 return DBUS_HANDLER_RESULT_HANDLED;
734 static bool monitor_s = false;
735 static bool monitor_t = false;
736 static bool monitor_m = false;
738 static void monitor_add(char *interface)
743 if (monitor_s == false && monitor_t == false && monitor_m == false)
744 dbus_connection_add_filter(connection, monitor_changed,
747 if (g_strcmp0(interface, "Service") == 0) {
748 if (monitor_s == true)
751 } else if (g_strcmp0(interface, "Technology") == 0) {
752 if (monitor_t == true)
755 } else if (g_strcmp0(interface, "Manager") == 0) {
756 if (monitor_m == true)
762 dbus_error_init(&err);
763 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
765 dbus_bus_add_match(connection, rule, &err);
768 if (dbus_error_is_set(&err))
769 fprintf(stderr, "Error: %s\n", err.message);
772 static void monitor_del(char *interface)
776 if (g_strcmp0(interface, "Service") == 0) {
777 if (monitor_s == false)
780 } else if (g_strcmp0(interface, "Technology") == 0) {
781 if (monitor_t == false)
784 } else if (g_strcmp0(interface, "Manager") == 0) {
785 if (monitor_m == false)
791 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
793 dbus_bus_remove_match(connection, rule, NULL);
796 if (monitor_s == false && monitor_t == false && monitor_m == false)
797 dbus_connection_remove_filter(connection, monitor_changed,
801 static int cmd_monitor(char *args[], int num, struct option *options)
810 switch (parse_boolean(args[2])) {
820 c = parse_args(args[1], options);
823 monitor_add("Service");
824 monitor_add("Technology");
825 monitor_add("Manager");
830 monitor_add("Service");
832 monitor_del("Service");
837 monitor_add("Technology");
839 monitor_del("Technology");
844 monitor_add("Manager");
846 monitor_del("Manager");
850 switch(parse_boolean(args[1])) {
852 monitor_del("Service");
853 monitor_del("Technology");
854 monitor_del("Manager");
858 monitor_add("Service");
859 monitor_add("Technology");
860 monitor_add("Manager");
874 static int cmd_exit(char *args[], int num, struct option *options)
879 static struct option service_options[] = {
880 {"properties", required_argument, 0, 'p'},
884 static const char *service_desc[] = {
885 "[<service>] (obsolete)",
889 static struct option config_options[] = {
890 {"nameservers", required_argument, 0, 'n'},
891 {"timeservers", required_argument, 0, 't'},
892 {"domains", required_argument, 0, 'd'},
893 {"ipv6", required_argument, 0, 'v'},
894 {"proxy", required_argument, 0, 'x'},
895 {"autoconnect", required_argument, 0, 'a'},
896 {"ipv4", required_argument, 0, 'i'},
897 {"remove", 0, 0, 'r'},
901 static const char *config_desc[] = {
902 "<dns1> [<dns2>] [<dns3>]",
903 "<ntp1> [<ntp2>] [...]",
904 "<domain1> [<domain2>] [...]",
905 "off|auto [enable|disable|prefered]|\n"
906 "\t\t\tmanual <address> <prefixlength> <gateway>",
907 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
908 "\t\t\t[exclude <exclude1> [<exclude2>] [...]]",
910 "off|dhcp|manual <address> <prefixlength> <gateway>",
915 static struct option monitor_options[] = {
916 {"services", no_argument, 0, 's'},
917 {"tech", no_argument, 0, 'c'},
918 {"manager", no_argument, 0, 'm'},
922 static const char *monitor_desc[] = {
923 "[off] Monitor only services",
924 "[off] Monitor only technologies",
925 "[off] Monitor only manager interface",
929 static const struct {
931 const char *argument;
932 struct option *options;
933 const char **options_desc;
934 int (*func) (char *args[], int num, struct option *options);
937 { "enable", "<technology>|offline", NULL, NULL,
938 cmd_enable, "Enables given technology or offline mode" },
939 { "disable", "<technology>|offline", NULL, NULL,
940 cmd_disable, "Disables given technology or offline mode"},
941 { "state", NULL, NULL, NULL,
942 cmd_state, "Shows if the system is online or offline" },
943 { "services", "[<service>]", service_options, &service_desc[0],
944 cmd_services, "Display services" },
945 { "technologies", NULL, NULL, NULL,
946 cmd_technologies, "Display technologies" },
947 { "scan", "<technology>", NULL, NULL,
948 cmd_scan, "Scans for new services for given technology" },
949 { "connect", "<service>", NULL, NULL,
950 cmd_connect, "Connect a given service" },
951 { "disconnect", "<service>", NULL, NULL,
952 cmd_disconnect, "Disconnect a given service" },
953 { "config", "<service>", config_options, &config_desc[0],
954 cmd_config, "Set service configuration options" },
955 { "monitor", "[off]", monitor_options, &monitor_desc[0],
956 cmd_monitor, "Monitor signals from interfaces" },
957 { "help", NULL, NULL, NULL,
958 cmd_help, "Show help" },
959 { "exit", NULL, NULL, NULL,
961 { "quit", NULL, NULL, NULL,
966 static int cmd_help(char *args[], int num, struct option *options)
968 bool interactive = __connmanctl_is_interactive();
971 if (interactive == false)
972 fprintf(stdout, "Usage: connmanctl [[command] [args]]\n");
974 for (i = 0; cmd_table[i].cmd != NULL; i++) {
975 const char *cmd = cmd_table[i].cmd;
976 const char *argument = cmd_table[i].argument;
977 const char *desc = cmd_table[i].desc;
979 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
980 argument != NULL? argument: "",
981 desc != NULL? desc: "");
983 if (cmd_table[i].options != NULL) {
984 for (j = 0; cmd_table[i].options[j].name != NULL;
986 const char *options_desc =
987 cmd_table[i].options_desc != NULL ?
988 cmd_table[i].options_desc[j]: "";
990 printf(" --%-12s%s\n",
991 cmd_table[i].options[j].name,
997 if (interactive == false)
998 fprintf(stdout, "\nNote: arguments and output are considered "
999 "EXPERIMENTAL for now.\n");
1004 int commands(DBusConnection *dbus_conn, char *argv[], int argc)
1008 connection = dbus_conn;
1010 for (i = 0; cmd_table[i].cmd != NULL; i++) {
1011 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
1012 cmd_table[i].func != NULL) {
1013 result = cmd_table[i].func(argv, argc,
1014 cmd_table[i].options);
1015 if (result < 0 && result != -EINPROGRESS)
1016 fprintf(stderr, "Error '%s': %s\n", argv[0],
1022 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);