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 int cmd_technologies(char *args[], int num, struct option *options)
213 return list_properties(connection, "GetTechnologies", NULL);
216 static int cmd_scan(char *args[], int num, struct option *options)
218 DBusMessage *message;
227 message = get_message(connection, "GetTechnologies");
231 err = scan_technology(connection, message, args[1]);
233 printf("Scan completed\n");
239 static void connect_return(DBusMessageIter *iter, const char *error,
242 char *path = user_data;
245 char *str = strrchr(path, '/');
247 fprintf(stdout, "Connected %s\n", str);
249 fprintf(stderr, "Error %s: %s\n", path, error);
254 static int cmd_connect(char *args[], int num, struct option *options)
264 path = g_strdup_printf("/net/connman/service/%s", args[1]);
265 return __connmanctl_dbus_method_call(connection, path,
266 "net.connman.Service", "Connect",
267 connect_return, path, DBUS_TYPE_INVALID);
270 static void disconnect_return(DBusMessageIter *iter, const char *error,
273 char *path = user_data;
276 char *str = strrchr(path, '/');
278 fprintf(stdout, "Disconnected %s\n", str);
280 fprintf(stderr, "Error %s: %s\n", path, error);
285 static int cmd_disconnect(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", "Disconnect",
298 disconnect_return, path, DBUS_TYPE_INVALID);
303 static int cmd_config(char *args[], int num, struct option *options)
305 int res = 0, index = 2, oldindex = 0;
308 DBusMessage *message;
312 service_name = args[1];
313 if (service_name == NULL)
316 while (index < num && args[index] != NULL) {
317 c = parse_args(args[index], options);
318 opt_start = &args[index + 1];
321 message = get_message(connection, "GetServices");
329 switch (parse_boolean(*opt_start)) {
341 res = set_service_property(connection, message,
342 service_name, "AutoConnect",
346 res = set_service_property(connection, message,
347 service_name, "IPv4.Configuration",
353 res = set_service_property(connection, message,
354 service_name, "IPv6.Configuration",
360 res = set_service_property(connection, message,
362 "Nameservers.Configuration",
366 res = set_service_property(connection, message,
368 "Timeservers.Configuration",
372 res = set_service_property(connection, message,
374 "Domains.Configuration",
378 if (*opt_start == NULL) {
383 if (strcmp(*opt_start, "direct") == 0) {
384 res = set_service_property(connection, message,
386 "Proxy.Configuration",
387 proxy_simple, opt_start, 1);
391 if (strcmp(*opt_start, "auto") == 0) {
392 res = set_service_property(connection, message,
394 "Proxy.Configuration",
395 proxy_simple, opt_start, 1);
399 if (strcmp(*opt_start, "manual") == 0) {
400 char **url_start = &args[index + 2];
402 if (*url_start != NULL &&
405 url_start = &args[index + 3];
408 res = store_proxy_input(connection,
409 message, service_name,
415 res = remove_service(connection, message, service_name);
422 dbus_message_unref(message);
425 printf("Error '%s': %s\n", args[oldindex],
436 static DBusHandlerResult monitor_changed(DBusConnection *connection,
437 DBusMessage *message, void *user_data)
439 DBusMessageIter iter;
440 const char *interface, *path;
442 interface = dbus_message_get_interface(message);
443 if (strncmp(interface, "net.connman.", 12) != 0)
444 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
446 interface = strrchr(interface, '.');
447 if (interface != NULL && *interface != '\0')
450 path = strrchr(dbus_message_get_path(message), '/');
451 if (path != NULL && *path != '\0')
454 __connmanctl_save_rl();
456 if (dbus_message_is_signal(message, "net.connman.Manager",
457 "ServicesChanged") == TRUE) {
459 fprintf(stdout, "%-12s %-20s = {\n", interface,
461 dbus_message_iter_init(message, &iter);
462 __connmanctl_services_list(&iter);
463 fprintf(stdout, "\n}\n");
465 __connmanctl_redraw_rl();
467 return DBUS_HANDLER_RESULT_HANDLED;
470 if (dbus_message_is_signal(message, "net.connman.Manager",
471 "TechnologyAdded") == TRUE)
472 path = "TechnologyAdded";
474 if (dbus_message_is_signal(message, "net.connman.Manager",
475 "TechnologyRemoved") == TRUE)
476 path = "TechnologyRemoved";
478 fprintf(stdout, "%-12s %-20s ", interface, path);
479 dbus_message_iter_init(message, &iter);
481 __connmanctl_dbus_print(&iter, "", " = ", " = ");
482 fprintf(stdout, "\n");
484 __connmanctl_redraw_rl();
486 return DBUS_HANDLER_RESULT_HANDLED;
489 static bool monitor_s = false;
490 static bool monitor_t = false;
491 static bool monitor_m = false;
493 static void monitor_add(char *interface)
498 if (monitor_s == false && monitor_t == false && monitor_m == false)
499 dbus_connection_add_filter(connection, monitor_changed,
502 if (g_strcmp0(interface, "Service") == 0) {
503 if (monitor_s == true)
506 } else if (g_strcmp0(interface, "Technology") == 0) {
507 if (monitor_t == true)
510 } else if (g_strcmp0(interface, "Manager") == 0) {
511 if (monitor_m == true)
517 dbus_error_init(&err);
518 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
520 dbus_bus_add_match(connection, rule, &err);
523 if (dbus_error_is_set(&err))
524 fprintf(stderr, "Error: %s\n", err.message);
527 static void monitor_del(char *interface)
531 if (g_strcmp0(interface, "Service") == 0) {
532 if (monitor_s == false)
535 } else if (g_strcmp0(interface, "Technology") == 0) {
536 if (monitor_t == false)
539 } else if (g_strcmp0(interface, "Manager") == 0) {
540 if (monitor_m == false)
546 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
548 dbus_bus_remove_match(connection, rule, NULL);
551 if (monitor_s == false && monitor_t == false && monitor_m == false)
552 dbus_connection_remove_filter(connection, monitor_changed,
556 static int cmd_monitor(char *args[], int num, struct option *options)
565 switch (parse_boolean(args[2])) {
575 c = parse_args(args[1], options);
578 monitor_add("Service");
579 monitor_add("Technology");
580 monitor_add("Manager");
585 monitor_add("Service");
587 monitor_del("Service");
592 monitor_add("Technology");
594 monitor_del("Technology");
599 monitor_add("Manager");
601 monitor_del("Manager");
605 switch(parse_boolean(args[1])) {
607 monitor_del("Service");
608 monitor_del("Technology");
609 monitor_del("Manager");
613 monitor_add("Service");
614 monitor_add("Technology");
615 monitor_add("Manager");
629 static int cmd_exit(char *args[], int num, struct option *options)
634 static struct option service_options[] = {
635 {"properties", required_argument, 0, 'p'},
639 static const char *service_desc[] = {
640 "[<service>] (obsolete)",
644 static struct option config_options[] = {
645 {"nameservers", required_argument, 0, 'n'},
646 {"timeservers", required_argument, 0, 't'},
647 {"domains", required_argument, 0, 'd'},
648 {"ipv6", required_argument, 0, 'v'},
649 {"proxy", required_argument, 0, 'x'},
650 {"autoconnect", required_argument, 0, 'a'},
651 {"ipv4", required_argument, 0, 'i'},
652 {"remove", 0, 0, 'r'},
656 static const char *config_desc[] = {
657 "<dns1> [<dns2>] [<dns3>]",
658 "<ntp1> [<ntp2>] [...]",
659 "<domain1> [<domain2>] [...]",
660 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
661 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
662 " [exclude <exclude1> [<exclude2>] [...]]",
664 "off|dhcp|manual <address> <prefixlength> <gateway>",
669 static struct option monitor_options[] = {
670 {"services", no_argument, 0, 's'},
671 {"tech", no_argument, 0, 'c'},
672 {"manager", no_argument, 0, 'm'},
676 static const char *monitor_desc[] = {
677 "[off] Monitor only services",
678 "[off] Monitor only technologies",
679 "[off] Monitor only manager interface",
683 static const struct {
685 const char *argument;
686 struct option *options;
687 const char **options_desc;
688 int (*func) (char *args[], int num, struct option *options);
691 { "enable", "<technology>|offline", NULL, NULL,
692 cmd_enable, "Enables given technology or offline mode" },
693 { "disable", "<technology>|offline", NULL, NULL,
694 cmd_disable, "Disables given technology or offline mode"},
695 { "state", NULL, NULL, NULL,
696 cmd_state, "Shows if the system is online or offline" },
697 { "services", "[<service>]", service_options, &service_desc[0],
698 cmd_services, "Display services" },
699 { "technologies", NULL, NULL, NULL,
700 cmd_technologies, "Display technologies" },
701 { "scan", "<technology>", NULL, NULL,
702 cmd_scan, "Scans for new services for given technology" },
703 { "connect", "<service>", NULL, NULL,
704 cmd_connect, "Connect a given service" },
705 { "disconnect", "<service>", NULL, NULL,
706 cmd_disconnect, "Disconnect a given service" },
707 { "config", "<service>", config_options, &config_desc[0],
708 cmd_config, "Set service configuration options" },
709 { "monitor", "[off]", monitor_options, &monitor_desc[0],
710 cmd_monitor, "Monitor signals from interfaces" },
711 { "help", NULL, NULL, NULL,
712 cmd_help, "Show help" },
713 { "exit", NULL, NULL, NULL,
715 { "quit", NULL, NULL, NULL,
720 static int cmd_help(char *args[], int num, struct option *options)
722 bool interactive = __connmanctl_is_interactive();
725 if (interactive == false)
726 fprintf(stdout, "Usage: connmanctl [[command] [args]]\n");
728 for (i = 0; cmd_table[i].cmd != NULL; i++) {
729 const char *cmd = cmd_table[i].cmd;
730 const char *argument = cmd_table[i].argument;
731 const char *desc = cmd_table[i].desc;
733 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
734 argument != NULL? argument: "",
735 desc != NULL? desc: "");
737 if (cmd_table[i].options != NULL) {
738 for (j = 0; cmd_table[i].options[j].name != NULL;
740 const char *options_desc =
741 cmd_table[i].options_desc != NULL ?
742 cmd_table[i].options_desc[j]: "";
744 printf(" --%-12s%s\n",
745 cmd_table[i].options[j].name,
751 if (interactive == false)
752 fprintf(stdout, "\nNote: arguments and output are considered "
753 "EXPERIMENTAL for now.\n");
758 int commands(DBusConnection *dbus_conn, char *argv[], int argc)
762 connection = dbus_conn;
764 for (i = 0; cmd_table[i].cmd != NULL; i++) {
765 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
766 cmd_table[i].func != NULL) {
767 result = cmd_table[i].func(argv, argc,
768 cmd_table[i].options);
769 if (result < 0 && result != -EINPROGRESS)
770 fprintf(stderr, "Error '%s': %s\n", argv[0],
776 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);