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[] = {
66 static char *proxy_simple[] = {
72 static int cmd_help(char *args[], int num, struct option *options);
74 static int parse_args(char *arg, struct option *options)
81 for (i = 0; options[i].name != NULL; i++) {
82 if (strcmp(options[i].name, arg) == 0 ||
83 (strncmp(arg, "--", 2) == 0 &&
84 strcmp(&arg[2], options[i].name) == 0))
85 return options[i].val;
91 static void enable_return(DBusMessageIter *iter, const char *error,
94 char *tech = user_data;
97 str = strrchr(tech, '/');
104 fprintf(stdout, "Enabled %s\n", str);
106 fprintf(stderr, "Error %s: %s\n", str, error);
111 static int cmd_enable(char *args[], int num, struct option *options)
114 dbus_bool_t b = TRUE;
122 if (strcmp(args[1], "offlinemode") == 0) {
123 tech = g_strdup(args[1]);
124 return __connmanctl_dbus_set_property(connection, "/",
125 "net.connman.Manager", enable_return, tech,
126 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
129 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
130 return __connmanctl_dbus_set_property(connection, tech,
131 "net.connman.Technology", enable_return, tech,
132 "Powered", DBUS_TYPE_BOOLEAN, &b);
135 static void disable_return(DBusMessageIter *iter, const char *error,
138 char *tech = user_data;
141 str = strrchr(tech, '/');
148 fprintf(stdout, "Disabled %s\n", str);
150 fprintf(stderr, "Error %s: %s\n", str, error);
155 static int cmd_disable(char *args[], int num, struct option *options)
158 dbus_bool_t b = FALSE;
166 if (strcmp(args[1], "offlinemode") == 0) {
167 tech = g_strdup(args[1]);
168 return __connmanctl_dbus_set_property(connection, "/",
169 "net.connman.Manager", disable_return, tech,
170 "OfflineMode", DBUS_TYPE_BOOLEAN, &b);
173 tech = g_strdup_printf("/net/connman/technology/%s", args[1]);
174 return __connmanctl_dbus_set_property(connection, tech,
175 "net.connman.Technology", disable_return, tech,
176 "Powered", DBUS_TYPE_BOOLEAN, &b);
179 static void state_print(DBusMessageIter *iter, const char *error,
182 DBusMessageIter entry;
185 fprintf(stderr, "Error: %s", error);
189 dbus_message_iter_recurse(iter, &entry);
190 __connmanctl_dbus_print(&entry, " ", " = ", "\n");
191 fprintf(stdout, "\n");
194 static int cmd_state(char *args[], int num, struct option *options)
199 return __connmanctl_dbus_method_call(connection, "/",
200 "net.connman.Manager", "GetProperties",
201 state_print, NULL, DBUS_TYPE_INVALID);
204 static int cmd_services(char *args[], int num, struct option *options)
206 char *service_name = NULL;
209 DBusMessage *message;
214 c = parse_args(args[1], options);
221 service_name = args[2];
226 service_name = args[1];
230 message = get_message(connection, "GetServices");
234 err = list_properties(connection, "GetServices", service_name);
235 dbus_message_unref(message);
240 static void technology_print(DBusMessageIter *iter, const char *error,
243 DBusMessageIter array;
246 fprintf(stderr, "Error: %s\n", error);
250 dbus_message_iter_recurse(iter, &array);
251 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
252 DBusMessageIter entry, dict;
255 dbus_message_iter_recurse(&array, &entry);
256 dbus_message_iter_get_basic(&entry, &path);
257 fprintf(stdout, "%s\n", path);
259 dbus_message_iter_next(&entry);
261 dbus_message_iter_recurse(&entry, &dict);
262 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
263 fprintf(stdout, "\n");
265 dbus_message_iter_next(&array);
269 static int cmd_technologies(char *args[], int num, struct option *options)
274 return __connmanctl_dbus_method_call(connection, "/",
275 "net.connman.Manager", "GetTechnologies",
276 technology_print, NULL, DBUS_TYPE_INVALID);
279 static void scan_return(DBusMessageIter *iter, const char *error,
282 char *path = user_data;
285 char *str = strrchr(path, '/');
287 fprintf(stdout, "Scan completed for %s\n", str);
289 fprintf(stderr, "Error %s: %s", path, error);
294 static int cmd_scan(char *args[], int num, struct option *options)
304 path = g_strdup_printf("/net/connman/technology/%s", args[1]);
305 return __connmanctl_dbus_method_call(connection, path,
306 "net.connman.Technology", "Scan",
307 scan_return, path, DBUS_TYPE_INVALID);
310 static void connect_return(DBusMessageIter *iter, const char *error,
313 char *path = user_data;
316 char *str = strrchr(path, '/');
318 fprintf(stdout, "Connected %s\n", str);
320 fprintf(stderr, "Error %s: %s\n", path, error);
325 static int cmd_connect(char *args[], int num, struct option *options)
335 path = g_strdup_printf("/net/connman/service/%s", args[1]);
336 return __connmanctl_dbus_method_call(connection, path,
337 "net.connman.Service", "Connect",
338 connect_return, path, DBUS_TYPE_INVALID);
341 static void disconnect_return(DBusMessageIter *iter, const char *error,
344 char *path = user_data;
347 char *str = strrchr(path, '/');
349 fprintf(stdout, "Disconnected %s\n", str);
351 fprintf(stderr, "Error %s: %s\n", path, error);
356 static int cmd_disconnect(char *args[], int num, struct option *options)
366 path = g_strdup_printf("/net/connman/service/%s", args[1]);
367 return __connmanctl_dbus_method_call(connection, path,
368 "net.connman.Service", "Disconnect",
369 disconnect_return, path, DBUS_TYPE_INVALID);
374 static int cmd_config(char *args[], int num, struct option *options)
376 int res = 0, index = 2, oldindex = 0;
379 DBusMessage *message;
383 service_name = args[1];
384 if (service_name == NULL)
387 while (index < num && args[index] != NULL) {
388 c = parse_args(args[index], options);
389 opt_start = &args[index + 1];
392 message = get_message(connection, "GetServices");
400 switch (parse_boolean(*opt_start)) {
412 res = set_service_property(connection, message,
413 service_name, "AutoConnect",
417 res = set_service_property(connection, message,
418 service_name, "IPv4.Configuration",
424 res = set_service_property(connection, message,
425 service_name, "IPv6.Configuration",
431 res = set_service_property(connection, message,
433 "Nameservers.Configuration",
437 res = set_service_property(connection, message,
439 "Timeservers.Configuration",
443 res = set_service_property(connection, message,
445 "Domains.Configuration",
449 if (*opt_start == NULL) {
454 if (strcmp(*opt_start, "direct") == 0) {
455 res = set_service_property(connection, message,
457 "Proxy.Configuration",
458 proxy_simple, opt_start, 1);
462 if (strcmp(*opt_start, "auto") == 0) {
463 res = set_service_property(connection, message,
465 "Proxy.Configuration",
466 proxy_simple, opt_start, 1);
470 if (strcmp(*opt_start, "manual") == 0) {
471 char **url_start = &args[index + 2];
473 if (*url_start != NULL &&
476 url_start = &args[index + 3];
479 res = store_proxy_input(connection,
480 message, service_name,
486 res = remove_service(connection, message, service_name);
493 dbus_message_unref(message);
496 printf("Error '%s': %s\n", args[oldindex],
507 static DBusHandlerResult monitor_changed(DBusConnection *connection,
508 DBusMessage *message, void *user_data)
510 DBusMessageIter iter;
511 const char *interface, *path;
513 interface = dbus_message_get_interface(message);
514 if (strncmp(interface, "net.connman.", 12) != 0)
515 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
517 interface = strrchr(interface, '.');
518 if (interface != NULL && *interface != '\0')
521 path = strrchr(dbus_message_get_path(message), '/');
522 if (path != NULL && *path != '\0')
525 __connmanctl_save_rl();
527 if (dbus_message_is_signal(message, "net.connman.Manager",
528 "ServicesChanged") == TRUE) {
530 fprintf(stdout, "%-12s %-20s = {\n", interface,
532 dbus_message_iter_init(message, &iter);
533 __connmanctl_services_list(&iter);
534 fprintf(stdout, "\n}\n");
536 __connmanctl_redraw_rl();
538 return DBUS_HANDLER_RESULT_HANDLED;
541 if (dbus_message_is_signal(message, "net.connman.Manager",
542 "TechnologyAdded") == TRUE)
543 path = "TechnologyAdded";
545 if (dbus_message_is_signal(message, "net.connman.Manager",
546 "TechnologyRemoved") == TRUE)
547 path = "TechnologyRemoved";
549 fprintf(stdout, "%-12s %-20s ", interface, path);
550 dbus_message_iter_init(message, &iter);
552 __connmanctl_dbus_print(&iter, "", " = ", " = ");
553 fprintf(stdout, "\n");
555 __connmanctl_redraw_rl();
557 return DBUS_HANDLER_RESULT_HANDLED;
560 static bool monitor_s = false;
561 static bool monitor_t = false;
562 static bool monitor_m = false;
564 static void monitor_add(char *interface)
569 if (monitor_s == false && monitor_t == false && monitor_m == false)
570 dbus_connection_add_filter(connection, monitor_changed,
573 if (g_strcmp0(interface, "Service") == 0) {
574 if (monitor_s == true)
577 } else if (g_strcmp0(interface, "Technology") == 0) {
578 if (monitor_t == true)
581 } else if (g_strcmp0(interface, "Manager") == 0) {
582 if (monitor_m == true)
588 dbus_error_init(&err);
589 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
591 dbus_bus_add_match(connection, rule, &err);
594 if (dbus_error_is_set(&err))
595 fprintf(stderr, "Error: %s\n", err.message);
598 static void monitor_del(char *interface)
602 if (g_strcmp0(interface, "Service") == 0) {
603 if (monitor_s == false)
606 } else if (g_strcmp0(interface, "Technology") == 0) {
607 if (monitor_t == false)
610 } else if (g_strcmp0(interface, "Manager") == 0) {
611 if (monitor_m == false)
617 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
619 dbus_bus_remove_match(connection, rule, NULL);
622 if (monitor_s == false && monitor_t == false && monitor_m == false)
623 dbus_connection_remove_filter(connection, monitor_changed,
627 static int cmd_monitor(char *args[], int num, struct option *options)
636 switch (parse_boolean(args[2])) {
646 c = parse_args(args[1], options);
649 monitor_add("Service");
650 monitor_add("Technology");
651 monitor_add("Manager");
656 monitor_add("Service");
658 monitor_del("Service");
663 monitor_add("Technology");
665 monitor_del("Technology");
670 monitor_add("Manager");
672 monitor_del("Manager");
676 switch(parse_boolean(args[1])) {
678 monitor_del("Service");
679 monitor_del("Technology");
680 monitor_del("Manager");
684 monitor_add("Service");
685 monitor_add("Technology");
686 monitor_add("Manager");
700 static int cmd_exit(char *args[], int num, struct option *options)
705 static struct option service_options[] = {
706 {"properties", required_argument, 0, 'p'},
710 static const char *service_desc[] = {
711 "[<service>] (obsolete)",
715 static struct option config_options[] = {
716 {"nameservers", required_argument, 0, 'n'},
717 {"timeservers", required_argument, 0, 't'},
718 {"domains", required_argument, 0, 'd'},
719 {"ipv6", required_argument, 0, 'v'},
720 {"proxy", required_argument, 0, 'x'},
721 {"autoconnect", required_argument, 0, 'a'},
722 {"ipv4", required_argument, 0, 'i'},
723 {"remove", 0, 0, 'r'},
727 static const char *config_desc[] = {
728 "<dns1> [<dns2>] [<dns3>]",
729 "<ntp1> [<ntp2>] [...]",
730 "<domain1> [<domain2>] [...]",
731 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
732 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
733 " [exclude <exclude1> [<exclude2>] [...]]",
735 "off|dhcp|manual <address> <prefixlength> <gateway>",
740 static struct option monitor_options[] = {
741 {"services", no_argument, 0, 's'},
742 {"tech", no_argument, 0, 'c'},
743 {"manager", no_argument, 0, 'm'},
747 static const char *monitor_desc[] = {
748 "[off] Monitor only services",
749 "[off] Monitor only technologies",
750 "[off] Monitor only manager interface",
754 static const struct {
756 const char *argument;
757 struct option *options;
758 const char **options_desc;
759 int (*func) (char *args[], int num, struct option *options);
762 { "enable", "<technology>|offline", NULL, NULL,
763 cmd_enable, "Enables given technology or offline mode" },
764 { "disable", "<technology>|offline", NULL, NULL,
765 cmd_disable, "Disables given technology or offline mode"},
766 { "state", NULL, NULL, NULL,
767 cmd_state, "Shows if the system is online or offline" },
768 { "services", "[<service>]", service_options, &service_desc[0],
769 cmd_services, "Display services" },
770 { "technologies", NULL, NULL, NULL,
771 cmd_technologies, "Display technologies" },
772 { "scan", "<technology>", NULL, NULL,
773 cmd_scan, "Scans for new services for given technology" },
774 { "connect", "<service>", NULL, NULL,
775 cmd_connect, "Connect a given service" },
776 { "disconnect", "<service>", NULL, NULL,
777 cmd_disconnect, "Disconnect a given service" },
778 { "config", "<service>", config_options, &config_desc[0],
779 cmd_config, "Set service configuration options" },
780 { "monitor", "[off]", monitor_options, &monitor_desc[0],
781 cmd_monitor, "Monitor signals from interfaces" },
782 { "help", NULL, NULL, NULL,
783 cmd_help, "Show help" },
784 { "exit", NULL, NULL, NULL,
786 { "quit", NULL, NULL, NULL,
791 static int cmd_help(char *args[], int num, struct option *options)
793 bool interactive = __connmanctl_is_interactive();
796 if (interactive == false)
797 fprintf(stdout, "Usage: connmanctl [[command] [args]]\n");
799 for (i = 0; cmd_table[i].cmd != NULL; i++) {
800 const char *cmd = cmd_table[i].cmd;
801 const char *argument = cmd_table[i].argument;
802 const char *desc = cmd_table[i].desc;
804 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
805 argument != NULL? argument: "",
806 desc != NULL? desc: "");
808 if (cmd_table[i].options != NULL) {
809 for (j = 0; cmd_table[i].options[j].name != NULL;
811 const char *options_desc =
812 cmd_table[i].options_desc != NULL ?
813 cmd_table[i].options_desc[j]: "";
815 printf(" --%-12s%s\n",
816 cmd_table[i].options[j].name,
822 if (interactive == false)
823 fprintf(stdout, "\nNote: arguments and output are considered "
824 "EXPERIMENTAL for now.\n");
829 int commands(DBusConnection *dbus_conn, char *argv[], int argc)
833 connection = dbus_conn;
835 for (i = 0; cmd_table[i].cmd != NULL; i++) {
836 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
837 cmd_table[i].func != NULL) {
838 result = cmd_table[i].func(argv, argc,
839 cmd_table[i].options);
840 if (result < 0 && result != -EINPROGRESS)
841 fprintf(stderr, "Error '%s': %s\n", argv[0],
847 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);