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 int cmd_enable(char *args[], int num, struct option *options)
102 if (strcmp(args[1], "offlinemode") == 0) {
103 err = set_manager(connection, "OfflineMode", TRUE);
105 printf("OfflineMode enabled\n");
110 message = get_message(connection, "GetTechnologies");
114 set_technology(connection, message, "Powered", args[1], TRUE);
119 static int cmd_disable(char *args[], int num, struct option *options)
121 DBusMessage *message;
130 if (strcmp(args[1], "offlinemode") == 0) {
131 err = set_manager(connection, "OfflineMode", FALSE);
133 printf("OfflineMode enabled\n");
138 message = get_message(connection, "GetTechnologies");
142 set_technology(connection, message, "Powered", args[1], FALSE);
147 static void state_print(DBusMessageIter *iter, const char *error,
150 DBusMessageIter entry;
153 fprintf(stderr, "Error: %s", error);
157 dbus_message_iter_recurse(iter, &entry);
158 __connmanctl_dbus_print(&entry, " ", " = ", "\n");
159 fprintf(stdout, "\n");
162 static int cmd_state(char *args[], int num, struct option *options)
167 return __connmanctl_dbus_method_call(connection, "/",
168 "net.connman.Manager", "GetProperties",
169 state_print, NULL, DBUS_TYPE_INVALID);
172 static int cmd_services(char *args[], int num, struct option *options)
174 char *service_name = NULL;
177 DBusMessage *message;
182 c = parse_args(args[1], options);
189 service_name = args[2];
194 service_name = args[1];
198 message = get_message(connection, "GetServices");
202 err = list_properties(connection, "GetServices", service_name);
203 dbus_message_unref(message);
208 static void technology_print(DBusMessageIter *iter, const char *error,
211 DBusMessageIter array;
214 fprintf(stderr, "Error: %s\n", error);
218 dbus_message_iter_recurse(iter, &array);
219 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
220 DBusMessageIter entry, dict;
223 dbus_message_iter_recurse(&array, &entry);
224 dbus_message_iter_get_basic(&entry, &path);
225 fprintf(stdout, "%s\n", path);
227 dbus_message_iter_next(&entry);
229 dbus_message_iter_recurse(&entry, &dict);
230 __connmanctl_dbus_print(&dict, " ", " = ", "\n");
231 fprintf(stdout, "\n");
233 dbus_message_iter_next(&array);
237 static int cmd_technologies(char *args[], int num, struct option *options)
242 return __connmanctl_dbus_method_call(connection, "/",
243 "net.connman.Manager", "GetTechnologies",
244 technology_print, NULL, DBUS_TYPE_INVALID);
247 static int cmd_scan(char *args[], int num, struct option *options)
249 DBusMessage *message;
258 message = get_message(connection, "GetTechnologies");
262 err = scan_technology(connection, message, args[1]);
264 printf("Scan completed\n");
270 static void connect_return(DBusMessageIter *iter, const char *error,
273 char *path = user_data;
276 char *str = strrchr(path, '/');
278 fprintf(stdout, "Connected %s\n", str);
280 fprintf(stderr, "Error %s: %s\n", path, error);
285 static int cmd_connect(char *args[], int num, struct option *options)
295 path = g_strdup_printf("/net/connman/service/%s", args[1]);
296 return __connmanctl_dbus_method_call(connection, path,
297 "net.connman.Service", "Connect",
298 connect_return, path, DBUS_TYPE_INVALID);
301 static void disconnect_return(DBusMessageIter *iter, const char *error,
304 char *path = user_data;
307 char *str = strrchr(path, '/');
309 fprintf(stdout, "Disconnected %s\n", str);
311 fprintf(stderr, "Error %s: %s\n", path, error);
316 static int cmd_disconnect(char *args[], int num, struct option *options)
326 path = g_strdup_printf("/net/connman/service/%s", args[1]);
327 return __connmanctl_dbus_method_call(connection, path,
328 "net.connman.Service", "Disconnect",
329 disconnect_return, path, DBUS_TYPE_INVALID);
334 static int cmd_config(char *args[], int num, struct option *options)
336 int res = 0, index = 2, oldindex = 0;
339 DBusMessage *message;
343 service_name = args[1];
344 if (service_name == NULL)
347 while (index < num && args[index] != NULL) {
348 c = parse_args(args[index], options);
349 opt_start = &args[index + 1];
352 message = get_message(connection, "GetServices");
360 switch (parse_boolean(*opt_start)) {
372 res = set_service_property(connection, message,
373 service_name, "AutoConnect",
377 res = set_service_property(connection, message,
378 service_name, "IPv4.Configuration",
384 res = set_service_property(connection, message,
385 service_name, "IPv6.Configuration",
391 res = set_service_property(connection, message,
393 "Nameservers.Configuration",
397 res = set_service_property(connection, message,
399 "Timeservers.Configuration",
403 res = set_service_property(connection, message,
405 "Domains.Configuration",
409 if (*opt_start == NULL) {
414 if (strcmp(*opt_start, "direct") == 0) {
415 res = set_service_property(connection, message,
417 "Proxy.Configuration",
418 proxy_simple, opt_start, 1);
422 if (strcmp(*opt_start, "auto") == 0) {
423 res = set_service_property(connection, message,
425 "Proxy.Configuration",
426 proxy_simple, opt_start, 1);
430 if (strcmp(*opt_start, "manual") == 0) {
431 char **url_start = &args[index + 2];
433 if (*url_start != NULL &&
436 url_start = &args[index + 3];
439 res = store_proxy_input(connection,
440 message, service_name,
446 res = remove_service(connection, message, service_name);
453 dbus_message_unref(message);
456 printf("Error '%s': %s\n", args[oldindex],
467 static DBusHandlerResult monitor_changed(DBusConnection *connection,
468 DBusMessage *message, void *user_data)
470 DBusMessageIter iter;
471 const char *interface, *path;
473 interface = dbus_message_get_interface(message);
474 if (strncmp(interface, "net.connman.", 12) != 0)
475 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
477 interface = strrchr(interface, '.');
478 if (interface != NULL && *interface != '\0')
481 path = strrchr(dbus_message_get_path(message), '/');
482 if (path != NULL && *path != '\0')
485 __connmanctl_save_rl();
487 if (dbus_message_is_signal(message, "net.connman.Manager",
488 "ServicesChanged") == TRUE) {
490 fprintf(stdout, "%-12s %-20s = {\n", interface,
492 dbus_message_iter_init(message, &iter);
493 __connmanctl_services_list(&iter);
494 fprintf(stdout, "\n}\n");
496 __connmanctl_redraw_rl();
498 return DBUS_HANDLER_RESULT_HANDLED;
501 if (dbus_message_is_signal(message, "net.connman.Manager",
502 "TechnologyAdded") == TRUE)
503 path = "TechnologyAdded";
505 if (dbus_message_is_signal(message, "net.connman.Manager",
506 "TechnologyRemoved") == TRUE)
507 path = "TechnologyRemoved";
509 fprintf(stdout, "%-12s %-20s ", interface, path);
510 dbus_message_iter_init(message, &iter);
512 __connmanctl_dbus_print(&iter, "", " = ", " = ");
513 fprintf(stdout, "\n");
515 __connmanctl_redraw_rl();
517 return DBUS_HANDLER_RESULT_HANDLED;
520 static bool monitor_s = false;
521 static bool monitor_t = false;
522 static bool monitor_m = false;
524 static void monitor_add(char *interface)
529 if (monitor_s == false && monitor_t == false && monitor_m == false)
530 dbus_connection_add_filter(connection, monitor_changed,
533 if (g_strcmp0(interface, "Service") == 0) {
534 if (monitor_s == true)
537 } else if (g_strcmp0(interface, "Technology") == 0) {
538 if (monitor_t == true)
541 } else if (g_strcmp0(interface, "Manager") == 0) {
542 if (monitor_m == true)
548 dbus_error_init(&err);
549 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
551 dbus_bus_add_match(connection, rule, &err);
554 if (dbus_error_is_set(&err))
555 fprintf(stderr, "Error: %s\n", err.message);
558 static void monitor_del(char *interface)
562 if (g_strcmp0(interface, "Service") == 0) {
563 if (monitor_s == false)
566 } else if (g_strcmp0(interface, "Technology") == 0) {
567 if (monitor_t == false)
570 } else if (g_strcmp0(interface, "Manager") == 0) {
571 if (monitor_m == false)
577 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
579 dbus_bus_remove_match(connection, rule, NULL);
582 if (monitor_s == false && monitor_t == false && monitor_m == false)
583 dbus_connection_remove_filter(connection, monitor_changed,
587 static int cmd_monitor(char *args[], int num, struct option *options)
596 switch (parse_boolean(args[2])) {
606 c = parse_args(args[1], options);
609 monitor_add("Service");
610 monitor_add("Technology");
611 monitor_add("Manager");
616 monitor_add("Service");
618 monitor_del("Service");
623 monitor_add("Technology");
625 monitor_del("Technology");
630 monitor_add("Manager");
632 monitor_del("Manager");
636 switch(parse_boolean(args[1])) {
638 monitor_del("Service");
639 monitor_del("Technology");
640 monitor_del("Manager");
644 monitor_add("Service");
645 monitor_add("Technology");
646 monitor_add("Manager");
660 static int cmd_exit(char *args[], int num, struct option *options)
665 static struct option service_options[] = {
666 {"properties", required_argument, 0, 'p'},
670 static const char *service_desc[] = {
671 "[<service>] (obsolete)",
675 static struct option config_options[] = {
676 {"nameservers", required_argument, 0, 'n'},
677 {"timeservers", required_argument, 0, 't'},
678 {"domains", required_argument, 0, 'd'},
679 {"ipv6", required_argument, 0, 'v'},
680 {"proxy", required_argument, 0, 'x'},
681 {"autoconnect", required_argument, 0, 'a'},
682 {"ipv4", required_argument, 0, 'i'},
683 {"remove", 0, 0, 'r'},
687 static const char *config_desc[] = {
688 "<dns1> [<dns2>] [<dns3>]",
689 "<ntp1> [<ntp2>] [...]",
690 "<domain1> [<domain2>] [...]",
691 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
692 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
693 " [exclude <exclude1> [<exclude2>] [...]]",
695 "off|dhcp|manual <address> <prefixlength> <gateway>",
700 static struct option monitor_options[] = {
701 {"services", no_argument, 0, 's'},
702 {"tech", no_argument, 0, 'c'},
703 {"manager", no_argument, 0, 'm'},
707 static const char *monitor_desc[] = {
708 "[off] Monitor only services",
709 "[off] Monitor only technologies",
710 "[off] Monitor only manager interface",
714 static const struct {
716 const char *argument;
717 struct option *options;
718 const char **options_desc;
719 int (*func) (char *args[], int num, struct option *options);
722 { "enable", "<technology>|offline", NULL, NULL,
723 cmd_enable, "Enables given technology or offline mode" },
724 { "disable", "<technology>|offline", NULL, NULL,
725 cmd_disable, "Disables given technology or offline mode"},
726 { "state", NULL, NULL, NULL,
727 cmd_state, "Shows if the system is online or offline" },
728 { "services", "[<service>]", service_options, &service_desc[0],
729 cmd_services, "Display services" },
730 { "technologies", NULL, NULL, NULL,
731 cmd_technologies, "Display technologies" },
732 { "scan", "<technology>", NULL, NULL,
733 cmd_scan, "Scans for new services for given technology" },
734 { "connect", "<service>", NULL, NULL,
735 cmd_connect, "Connect a given service" },
736 { "disconnect", "<service>", NULL, NULL,
737 cmd_disconnect, "Disconnect a given service" },
738 { "config", "<service>", config_options, &config_desc[0],
739 cmd_config, "Set service configuration options" },
740 { "monitor", "[off]", monitor_options, &monitor_desc[0],
741 cmd_monitor, "Monitor signals from interfaces" },
742 { "help", NULL, NULL, NULL,
743 cmd_help, "Show help" },
744 { "exit", NULL, NULL, NULL,
746 { "quit", NULL, NULL, NULL,
751 static int cmd_help(char *args[], int num, struct option *options)
753 bool interactive = __connmanctl_is_interactive();
756 if (interactive == false)
757 fprintf(stdout, "Usage: connmanctl [[command] [args]]\n");
759 for (i = 0; cmd_table[i].cmd != NULL; i++) {
760 const char *cmd = cmd_table[i].cmd;
761 const char *argument = cmd_table[i].argument;
762 const char *desc = cmd_table[i].desc;
764 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
765 argument != NULL? argument: "",
766 desc != NULL? desc: "");
768 if (cmd_table[i].options != NULL) {
769 for (j = 0; cmd_table[i].options[j].name != NULL;
771 const char *options_desc =
772 cmd_table[i].options_desc != NULL ?
773 cmd_table[i].options_desc[j]: "";
775 printf(" --%-12s%s\n",
776 cmd_table[i].options[j].name,
782 if (interactive == false)
783 fprintf(stdout, "\nNote: arguments and output are considered "
784 "EXPERIMENTAL for now.\n");
789 int commands(DBusConnection *dbus_conn, char *argv[], int argc)
793 connection = dbus_conn;
795 for (i = 0; cmd_table[i].cmd != NULL; i++) {
796 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
797 cmd_table[i].func != NULL) {
798 result = cmd_table[i].func(argv, argc,
799 cmd_table[i].options);
800 if (result < 0 && result != -EINPROGRESS)
801 fprintf(stderr, "Error '%s': %s\n", argv[0],
807 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);