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"
40 #include "interactive.h"
42 #include "dbus_helpers.h"
45 #define MANDATORY_ARGS 3
47 static char *ipv4[] = {
55 static char *ipv6[] = {
64 static char *proxy_simple[] = {
70 static int cmd_help(char *args[], int num, struct option *options);
72 static int parse_args(char *arg, struct option *options)
79 for (i = 0; options[i].name != NULL; i++) {
80 if (strcmp(options[i].name, arg) == 0 ||
81 (strncmp(arg, "--", 2) == 0 &&
82 strcmp(&arg[2], options[i].name) == 0))
83 return options[i].val;
89 static int cmd_enable(char *args[], int num, struct option *options)
100 if (strcmp(args[1], "offlinemode") == 0) {
101 err = set_manager(connection, "OfflineMode", TRUE);
103 printf("OfflineMode enabled\n");
108 message = get_message(connection, "GetTechnologies");
112 set_technology(connection, message, "Powered", args[1], TRUE);
117 static int cmd_disable(char *args[], int num, struct option *options)
119 DBusMessage *message;
128 if (strcmp(args[1], "offlinemode") == 0) {
129 err = set_manager(connection, "OfflineMode", FALSE);
131 printf("OfflineMode enabled\n");
136 message = get_message(connection, "GetTechnologies");
140 set_technology(connection, message, "Powered", args[1], FALSE);
145 static int cmd_state(char *args[], int num, struct option *options)
150 return list_properties(connection, "GetProperties", NULL);
153 static int cmd_services(char *args[], int num, struct option *options)
155 char *service_name = NULL;
158 DBusMessage *message;
163 c = parse_args(args[1], options);
170 service_name = args[2];
175 service_name = args[1];
179 message = get_message(connection, "GetServices");
183 err = list_properties(connection, "GetServices", service_name);
184 dbus_message_unref(message);
189 static int cmd_technologies(char *args[], int num, struct option *options)
194 return list_properties(connection, "GetTechnologies", NULL);
197 static int cmd_scan(char *args[], int num, struct option *options)
199 DBusMessage *message;
208 message = get_message(connection, "GetTechnologies");
212 err = scan_technology(connection, message, args[1]);
214 printf("Scan completed\n");
220 static int cmd_connect(char *args[], int num, struct option *options)
230 err = connect_service(connection, args[1]);
232 printf("Connected\n");
237 static int cmd_disconnect(char *args[], int num, struct option *options)
247 err = disconnect_service(connection, args[1]);
249 printf("Disconnected\n");
254 static int cmd_config(char *args[], int num, struct option *options)
256 int res = 0, index = 2, oldindex = 0;
259 DBusMessage *message;
263 service_name = args[1];
264 if (service_name == NULL)
267 while (index < num && args[index] != NULL) {
268 c = parse_args(args[index], options);
269 opt_start = &args[index + 1];
272 message = get_message(connection, "GetServices");
280 switch (parse_boolean(*opt_start)) {
292 res = set_service_property(connection, message,
293 service_name, "AutoConnect",
297 res = set_service_property(connection, message,
298 service_name, "IPv4.Configuration",
304 res = set_service_property(connection, message,
305 service_name, "IPv6.Configuration",
311 res = set_service_property(connection, message,
313 "Nameservers.Configuration",
317 res = set_service_property(connection, message,
319 "Timeservers.Configuration",
323 res = set_service_property(connection, message,
325 "Domains.Configuration",
329 if (*opt_start == NULL) {
334 if (strcmp(*opt_start, "direct") == 0) {
335 res = set_service_property(connection, message,
337 "Proxy.Configuration",
338 proxy_simple, opt_start, 1);
342 if (strcmp(*opt_start, "auto") == 0) {
343 res = set_service_property(connection, message,
345 "Proxy.Configuration",
346 proxy_simple, opt_start, 1);
350 if (strcmp(*opt_start, "manual") == 0) {
351 char **url_start = &args[index + 2];
353 if (*url_start != NULL &&
356 url_start = &args[index + 3];
359 res = store_proxy_input(connection,
360 message, service_name,
366 res = remove_service(connection, message, service_name);
373 dbus_message_unref(message);
376 printf("Error '%s': %s\n", args[oldindex],
387 static DBusHandlerResult monitor_changed(DBusConnection *connection,
388 DBusMessage *message, void *user_data)
390 DBusMessageIter iter;
391 const char *interface, *path;
393 interface = dbus_message_get_interface(message);
394 if (strncmp(interface, "net.connman.", 12) != 0)
395 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
397 interface = strrchr(interface, '.');
398 if (interface != NULL && *interface != '\0')
401 path = strrchr(dbus_message_get_path(message), '/');
402 if (path != NULL && *path != '\0')
405 if (dbus_message_is_signal(message, "net.connman.Manager",
406 "ServicesChanged") == TRUE) {
408 fprintf(stdout, "%-12s %-20s = {\n", interface,
410 dbus_message_iter_init(message, &iter);
411 __connmanctl_services_list(&iter);
412 fprintf(stdout, "\n}\n");
414 __connmanctl_redraw_rl();
416 return DBUS_HANDLER_RESULT_HANDLED;
419 if (dbus_message_is_signal(message, "net.connman.Manager",
420 "TechnologyAdded") == TRUE)
421 path = "TechnologyAdded";
423 if (dbus_message_is_signal(message, "net.connman.Manager",
424 "TechnologyRemoved") == TRUE)
425 path = "TechnologyRemoved";
427 fprintf(stdout, "%-12s %-20s ", interface, path);
428 dbus_message_iter_init(message, &iter);
430 __connmanctl_dbus_print(&iter, "", " = ", " = ");
431 fprintf(stdout, "\n");
433 return DBUS_HANDLER_RESULT_HANDLED;
436 static bool monitor_s = false;
437 static bool monitor_t = false;
438 static bool monitor_m = false;
440 static void monitor_add(char *interface)
445 if (monitor_s == false && monitor_t == false && monitor_m == false)
446 dbus_connection_add_filter(connection, monitor_changed,
449 if (g_strcmp0(interface, "Service") == 0) {
450 if (monitor_s == true)
453 } else if (g_strcmp0(interface, "Technology") == 0) {
454 if (monitor_t == true)
457 } else if (g_strcmp0(interface, "Manager") == 0) {
458 if (monitor_m == true)
464 dbus_error_init(&err);
465 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
467 dbus_bus_add_match(connection, rule, &err);
470 if (dbus_error_is_set(&err))
471 fprintf(stderr, "Error: %s\n", err.message);
474 static void monitor_del(char *interface)
478 if (g_strcmp0(interface, "Service") == 0) {
479 if (monitor_s == false)
482 } else if (g_strcmp0(interface, "Technology") == 0) {
483 if (monitor_t == false)
486 } else if (g_strcmp0(interface, "Manager") == 0) {
487 if (monitor_m == false)
493 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
495 dbus_bus_remove_match(connection, rule, NULL);
498 if (monitor_s == false && monitor_t == false && monitor_m == false)
499 dbus_connection_remove_filter(connection, monitor_changed,
503 static int cmd_monitor(char *args[], int num, struct option *options)
512 switch (parse_boolean(args[2])) {
522 c = parse_args(args[1], options);
525 monitor_add("Service");
526 monitor_add("Technology");
527 monitor_add("Manager");
532 monitor_add("Service");
534 monitor_del("Service");
539 monitor_add("Technology");
541 monitor_del("Technology");
546 monitor_add("Manager");
548 monitor_del("Manager");
552 switch(parse_boolean(args[1])) {
554 monitor_del("Service");
555 monitor_del("Technology");
556 monitor_del("Manager");
560 monitor_add("Service");
561 monitor_add("Technology");
562 monitor_add("Manager");
576 static int cmd_exit(char *args[], int num, struct option *options)
581 static struct option service_options[] = {
582 {"properties", required_argument, 0, 'p'},
586 static const char *service_desc[] = {
587 "[<service>] (obsolete)",
591 static struct option config_options[] = {
592 {"nameservers", required_argument, 0, 'n'},
593 {"timeservers", required_argument, 0, 't'},
594 {"domains", required_argument, 0, 'd'},
595 {"ipv6", required_argument, 0, 'v'},
596 {"proxy", required_argument, 0, 'x'},
597 {"autoconnect", required_argument, 0, 'a'},
598 {"ipv4", required_argument, 0, 'i'},
599 {"remove", 0, 0, 'r'},
603 static const char *config_desc[] = {
604 "<dns1> [<dns2>] [<dns3>]",
605 "<ntp1> [<ntp2>] [...]",
606 "<domain1> [<domain2>] [...]",
607 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
608 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
609 " [exclude <exclude1> [<exclude2>] [...]]",
611 "off|dhcp|manual <address> <prefixlength> <gateway>",
616 static struct option monitor_options[] = {
617 {"services", no_argument, 0, 's'},
618 {"tech", no_argument, 0, 'c'},
619 {"manager", no_argument, 0, 'm'},
623 static const char *monitor_desc[] = {
624 "[off] Monitor only services",
625 "[off] Monitor only technologies",
626 "[off] Monitor only manager interface",
630 static const struct {
632 const char *argument;
633 struct option *options;
634 const char **options_desc;
635 int (*func) (char *args[], int num, struct option *options);
638 { "enable", "<technology>|offline", NULL, NULL,
639 cmd_enable, "Enables given technology or offline mode" },
640 { "disable", "<technology>|offline", NULL, NULL,
641 cmd_disable, "Disables given technology or offline mode"},
642 { "state", NULL, NULL, NULL,
643 cmd_state, "Shows if the system is online or offline" },
644 { "services", "[<service>]", service_options, &service_desc[0],
645 cmd_services, "Display services" },
646 { "technologies", NULL, NULL, NULL,
647 cmd_technologies, "Display technologies" },
648 { "scan", "<technology>", NULL, NULL,
649 cmd_scan, "Scans for new services for given technology" },
650 { "connect", "<service>", NULL, NULL,
651 cmd_connect, "Connect a given service" },
652 { "disconnect", "<service>", NULL, NULL,
653 cmd_disconnect, "Disconnect a given service" },
654 { "config", "<service>", config_options, &config_desc[0],
655 cmd_config, "Set service configuration options" },
656 { "monitor", "[off]", monitor_options, &monitor_desc[0],
657 cmd_monitor, "Monitor signals from interfaces" },
658 { "help", NULL, NULL, NULL,
659 cmd_help, "Show help" },
660 { "exit", NULL, NULL, NULL,
662 { "quit", NULL, NULL, NULL,
667 static int cmd_help(char *args[], int num, struct option *options)
671 for (i = 0; cmd_table[i].cmd != NULL; i++) {
672 const char *cmd = cmd_table[i].cmd;
673 const char *argument = cmd_table[i].argument;
674 const char *desc = cmd_table[i].desc;
676 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
677 argument != NULL? argument: "",
678 desc != NULL? desc: "");
680 if (cmd_table[i].options != NULL) {
681 for (j = 0; cmd_table[i].options[j].name != NULL;
683 const char *options_desc =
684 cmd_table[i].options_desc != NULL ?
685 cmd_table[i].options_desc[j]: "";
687 printf(" --%-12s%s\n",
688 cmd_table[i].options[j].name,
697 int commands(DBusConnection *connection, char *argv[], int argc)
701 for (i = 0; cmd_table[i].cmd != NULL; i++) {
702 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
703 cmd_table[i].func != NULL) {
704 result = cmd_table[i].func(argv, argc,
705 cmd_table[i].options);
707 printf("Error '%s': %s\n", argv[0],