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 int cmd_connect(char *args[], int num, struct option *options)
249 err = connect_service(connection, args[1]);
251 printf("Connected\n");
256 static int cmd_disconnect(char *args[], int num, struct option *options)
266 err = disconnect_service(connection, args[1]);
268 printf("Disconnected\n");
273 static int cmd_config(char *args[], int num, struct option *options)
275 int res = 0, index = 2, oldindex = 0;
278 DBusMessage *message;
282 service_name = args[1];
283 if (service_name == NULL)
286 while (index < num && args[index] != NULL) {
287 c = parse_args(args[index], options);
288 opt_start = &args[index + 1];
291 message = get_message(connection, "GetServices");
299 switch (parse_boolean(*opt_start)) {
311 res = set_service_property(connection, message,
312 service_name, "AutoConnect",
316 res = set_service_property(connection, message,
317 service_name, "IPv4.Configuration",
323 res = set_service_property(connection, message,
324 service_name, "IPv6.Configuration",
330 res = set_service_property(connection, message,
332 "Nameservers.Configuration",
336 res = set_service_property(connection, message,
338 "Timeservers.Configuration",
342 res = set_service_property(connection, message,
344 "Domains.Configuration",
348 if (*opt_start == NULL) {
353 if (strcmp(*opt_start, "direct") == 0) {
354 res = set_service_property(connection, message,
356 "Proxy.Configuration",
357 proxy_simple, opt_start, 1);
361 if (strcmp(*opt_start, "auto") == 0) {
362 res = set_service_property(connection, message,
364 "Proxy.Configuration",
365 proxy_simple, opt_start, 1);
369 if (strcmp(*opt_start, "manual") == 0) {
370 char **url_start = &args[index + 2];
372 if (*url_start != NULL &&
375 url_start = &args[index + 3];
378 res = store_proxy_input(connection,
379 message, service_name,
385 res = remove_service(connection, message, service_name);
392 dbus_message_unref(message);
395 printf("Error '%s': %s\n", args[oldindex],
406 static DBusHandlerResult monitor_changed(DBusConnection *connection,
407 DBusMessage *message, void *user_data)
409 DBusMessageIter iter;
410 const char *interface, *path;
412 interface = dbus_message_get_interface(message);
413 if (strncmp(interface, "net.connman.", 12) != 0)
414 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
416 interface = strrchr(interface, '.');
417 if (interface != NULL && *interface != '\0')
420 path = strrchr(dbus_message_get_path(message), '/');
421 if (path != NULL && *path != '\0')
424 __connmanctl_save_rl();
426 if (dbus_message_is_signal(message, "net.connman.Manager",
427 "ServicesChanged") == TRUE) {
429 fprintf(stdout, "%-12s %-20s = {\n", interface,
431 dbus_message_iter_init(message, &iter);
432 __connmanctl_services_list(&iter);
433 fprintf(stdout, "\n}\n");
435 __connmanctl_redraw_rl();
437 return DBUS_HANDLER_RESULT_HANDLED;
440 if (dbus_message_is_signal(message, "net.connman.Manager",
441 "TechnologyAdded") == TRUE)
442 path = "TechnologyAdded";
444 if (dbus_message_is_signal(message, "net.connman.Manager",
445 "TechnologyRemoved") == TRUE)
446 path = "TechnologyRemoved";
448 fprintf(stdout, "%-12s %-20s ", interface, path);
449 dbus_message_iter_init(message, &iter);
451 __connmanctl_dbus_print(&iter, "", " = ", " = ");
452 fprintf(stdout, "\n");
454 __connmanctl_redraw_rl();
456 return DBUS_HANDLER_RESULT_HANDLED;
459 static bool monitor_s = false;
460 static bool monitor_t = false;
461 static bool monitor_m = false;
463 static void monitor_add(char *interface)
468 if (monitor_s == false && monitor_t == false && monitor_m == false)
469 dbus_connection_add_filter(connection, monitor_changed,
472 if (g_strcmp0(interface, "Service") == 0) {
473 if (monitor_s == true)
476 } else if (g_strcmp0(interface, "Technology") == 0) {
477 if (monitor_t == true)
480 } else if (g_strcmp0(interface, "Manager") == 0) {
481 if (monitor_m == true)
487 dbus_error_init(&err);
488 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
490 dbus_bus_add_match(connection, rule, &err);
493 if (dbus_error_is_set(&err))
494 fprintf(stderr, "Error: %s\n", err.message);
497 static void monitor_del(char *interface)
501 if (g_strcmp0(interface, "Service") == 0) {
502 if (monitor_s == false)
505 } else if (g_strcmp0(interface, "Technology") == 0) {
506 if (monitor_t == false)
509 } else if (g_strcmp0(interface, "Manager") == 0) {
510 if (monitor_m == false)
516 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
518 dbus_bus_remove_match(connection, rule, NULL);
521 if (monitor_s == false && monitor_t == false && monitor_m == false)
522 dbus_connection_remove_filter(connection, monitor_changed,
526 static int cmd_monitor(char *args[], int num, struct option *options)
535 switch (parse_boolean(args[2])) {
545 c = parse_args(args[1], options);
548 monitor_add("Service");
549 monitor_add("Technology");
550 monitor_add("Manager");
555 monitor_add("Service");
557 monitor_del("Service");
562 monitor_add("Technology");
564 monitor_del("Technology");
569 monitor_add("Manager");
571 monitor_del("Manager");
575 switch(parse_boolean(args[1])) {
577 monitor_del("Service");
578 monitor_del("Technology");
579 monitor_del("Manager");
583 monitor_add("Service");
584 monitor_add("Technology");
585 monitor_add("Manager");
599 static int cmd_exit(char *args[], int num, struct option *options)
604 static struct option service_options[] = {
605 {"properties", required_argument, 0, 'p'},
609 static const char *service_desc[] = {
610 "[<service>] (obsolete)",
614 static struct option config_options[] = {
615 {"nameservers", required_argument, 0, 'n'},
616 {"timeservers", required_argument, 0, 't'},
617 {"domains", required_argument, 0, 'd'},
618 {"ipv6", required_argument, 0, 'v'},
619 {"proxy", required_argument, 0, 'x'},
620 {"autoconnect", required_argument, 0, 'a'},
621 {"ipv4", required_argument, 0, 'i'},
622 {"remove", 0, 0, 'r'},
626 static const char *config_desc[] = {
627 "<dns1> [<dns2>] [<dns3>]",
628 "<ntp1> [<ntp2>] [...]",
629 "<domain1> [<domain2>] [...]",
630 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
631 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
632 " [exclude <exclude1> [<exclude2>] [...]]",
634 "off|dhcp|manual <address> <prefixlength> <gateway>",
639 static struct option monitor_options[] = {
640 {"services", no_argument, 0, 's'},
641 {"tech", no_argument, 0, 'c'},
642 {"manager", no_argument, 0, 'm'},
646 static const char *monitor_desc[] = {
647 "[off] Monitor only services",
648 "[off] Monitor only technologies",
649 "[off] Monitor only manager interface",
653 static const struct {
655 const char *argument;
656 struct option *options;
657 const char **options_desc;
658 int (*func) (char *args[], int num, struct option *options);
661 { "enable", "<technology>|offline", NULL, NULL,
662 cmd_enable, "Enables given technology or offline mode" },
663 { "disable", "<technology>|offline", NULL, NULL,
664 cmd_disable, "Disables given technology or offline mode"},
665 { "state", NULL, NULL, NULL,
666 cmd_state, "Shows if the system is online or offline" },
667 { "services", "[<service>]", service_options, &service_desc[0],
668 cmd_services, "Display services" },
669 { "technologies", NULL, NULL, NULL,
670 cmd_technologies, "Display technologies" },
671 { "scan", "<technology>", NULL, NULL,
672 cmd_scan, "Scans for new services for given technology" },
673 { "connect", "<service>", NULL, NULL,
674 cmd_connect, "Connect a given service" },
675 { "disconnect", "<service>", NULL, NULL,
676 cmd_disconnect, "Disconnect a given service" },
677 { "config", "<service>", config_options, &config_desc[0],
678 cmd_config, "Set service configuration options" },
679 { "monitor", "[off]", monitor_options, &monitor_desc[0],
680 cmd_monitor, "Monitor signals from interfaces" },
681 { "help", NULL, NULL, NULL,
682 cmd_help, "Show help" },
683 { "exit", NULL, NULL, NULL,
685 { "quit", NULL, NULL, NULL,
690 static int cmd_help(char *args[], int num, struct option *options)
692 bool interactive = __connmanctl_is_interactive();
695 if (interactive == false)
696 fprintf(stdout, "Usage: connmanctl [[command] [args]]\n");
698 for (i = 0; cmd_table[i].cmd != NULL; i++) {
699 const char *cmd = cmd_table[i].cmd;
700 const char *argument = cmd_table[i].argument;
701 const char *desc = cmd_table[i].desc;
703 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
704 argument != NULL? argument: "",
705 desc != NULL? desc: "");
707 if (cmd_table[i].options != NULL) {
708 for (j = 0; cmd_table[i].options[j].name != NULL;
710 const char *options_desc =
711 cmd_table[i].options_desc != NULL ?
712 cmd_table[i].options_desc[j]: "";
714 printf(" --%-12s%s\n",
715 cmd_table[i].options[j].name,
721 if (interactive == false)
722 fprintf(stdout, "\nNote: arguments and output are considered "
723 "EXPERIMENTAL for now.\n");
728 int commands(DBusConnection *dbus_conn, char *argv[], int argc)
732 connection = dbus_conn;
734 for (i = 0; cmd_table[i].cmd != NULL; i++) {
735 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
736 cmd_table[i].func != NULL) {
737 result = cmd_table[i].func(argv, argc,
738 cmd_table[i].options);
739 if (result < 0 && result != -EINPROGRESS)
740 fprintf(stderr, "Error '%s': %s\n", argv[0],
746 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);