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 "interactive.h"
43 #include "dbus_helpers.h"
46 #define MANDATORY_ARGS 3
48 static char *ipv4[] = {
56 static char *ipv6[] = {
65 static char *proxy_simple[] = {
71 static int cmd_help(char *args[], int num, struct option *options);
73 static int parse_args(char *arg, struct option *options)
80 for (i = 0; options[i].name != NULL; i++) {
81 if (strcmp(options[i].name, arg) == 0 ||
82 (strncmp(arg, "--", 2) == 0 &&
83 strcmp(&arg[2], options[i].name) == 0))
84 return options[i].val;
90 static int cmd_enable(char *args[], int num, struct option *options)
101 if (strcmp(args[1], "offlinemode") == 0) {
102 err = set_manager(connection, "OfflineMode", TRUE);
104 printf("OfflineMode enabled\n");
109 message = get_message(connection, "GetTechnologies");
113 set_technology(connection, message, "Powered", args[1], TRUE);
118 static int cmd_disable(char *args[], int num, struct option *options)
120 DBusMessage *message;
129 if (strcmp(args[1], "offlinemode") == 0) {
130 err = set_manager(connection, "OfflineMode", FALSE);
132 printf("OfflineMode enabled\n");
137 message = get_message(connection, "GetTechnologies");
141 set_technology(connection, message, "Powered", args[1], FALSE);
146 static int cmd_state(char *args[], int num, struct option *options)
151 return list_properties(connection, "GetProperties", NULL);
154 static int cmd_services(char *args[], int num, struct option *options)
156 char *service_name = NULL;
159 DBusMessage *message;
164 c = parse_args(args[1], options);
171 service_name = args[2];
176 service_name = args[1];
180 message = get_message(connection, "GetServices");
184 err = list_properties(connection, "GetServices", service_name);
185 dbus_message_unref(message);
190 static int cmd_technologies(char *args[], int num, struct option *options)
195 return list_properties(connection, "GetTechnologies", NULL);
198 static int cmd_scan(char *args[], int num, struct option *options)
200 DBusMessage *message;
209 message = get_message(connection, "GetTechnologies");
213 err = scan_technology(connection, message, args[1]);
215 printf("Scan completed\n");
221 static int cmd_connect(char *args[], int num, struct option *options)
231 err = connect_service(connection, args[1]);
233 printf("Connected\n");
238 static int cmd_disconnect(char *args[], int num, struct option *options)
248 err = disconnect_service(connection, args[1]);
250 printf("Disconnected\n");
255 static int cmd_config(char *args[], int num, struct option *options)
257 int res = 0, index = 2, oldindex = 0;
260 DBusMessage *message;
264 service_name = args[1];
265 if (service_name == NULL)
268 while (index < num && args[index] != NULL) {
269 c = parse_args(args[index], options);
270 opt_start = &args[index + 1];
273 message = get_message(connection, "GetServices");
281 switch (parse_boolean(*opt_start)) {
293 res = set_service_property(connection, message,
294 service_name, "AutoConnect",
298 res = set_service_property(connection, message,
299 service_name, "IPv4.Configuration",
305 res = set_service_property(connection, message,
306 service_name, "IPv6.Configuration",
312 res = set_service_property(connection, message,
314 "Nameservers.Configuration",
318 res = set_service_property(connection, message,
320 "Timeservers.Configuration",
324 res = set_service_property(connection, message,
326 "Domains.Configuration",
330 if (*opt_start == NULL) {
335 if (strcmp(*opt_start, "direct") == 0) {
336 res = set_service_property(connection, message,
338 "Proxy.Configuration",
339 proxy_simple, opt_start, 1);
343 if (strcmp(*opt_start, "auto") == 0) {
344 res = set_service_property(connection, message,
346 "Proxy.Configuration",
347 proxy_simple, opt_start, 1);
351 if (strcmp(*opt_start, "manual") == 0) {
352 char **url_start = &args[index + 2];
354 if (*url_start != NULL &&
357 url_start = &args[index + 3];
360 res = store_proxy_input(connection,
361 message, service_name,
367 res = remove_service(connection, message, service_name);
374 dbus_message_unref(message);
377 printf("Error '%s': %s\n", args[oldindex],
388 static DBusHandlerResult monitor_changed(DBusConnection *connection,
389 DBusMessage *message, void *user_data)
391 DBusMessageIter iter;
392 const char *interface, *path;
394 interface = dbus_message_get_interface(message);
395 if (strncmp(interface, "net.connman.", 12) != 0)
396 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
398 interface = strrchr(interface, '.');
399 if (interface != NULL && *interface != '\0')
402 path = strrchr(dbus_message_get_path(message), '/');
403 if (path != NULL && *path != '\0')
406 if (dbus_message_is_signal(message, "net.connman.Manager",
407 "ServicesChanged") == TRUE) {
409 fprintf(stdout, "%-12s %-20s = {\n", interface,
411 dbus_message_iter_init(message, &iter);
412 __connmanctl_services_list(&iter);
413 fprintf(stdout, "\n}\n");
415 __connmanctl_redraw_rl();
417 return DBUS_HANDLER_RESULT_HANDLED;
420 if (dbus_message_is_signal(message, "net.connman.Manager",
421 "TechnologyAdded") == TRUE)
422 path = "TechnologyAdded";
424 if (dbus_message_is_signal(message, "net.connman.Manager",
425 "TechnologyRemoved") == TRUE)
426 path = "TechnologyRemoved";
428 fprintf(stdout, "%-12s %-20s ", interface, path);
429 dbus_message_iter_init(message, &iter);
431 __connmanctl_dbus_print(&iter, "", " = ", " = ");
432 fprintf(stdout, "\n");
434 return DBUS_HANDLER_RESULT_HANDLED;
437 static bool monitor_s = false;
438 static bool monitor_t = false;
439 static bool monitor_m = false;
441 static void monitor_add(char *interface)
446 if (monitor_s == false && monitor_t == false && monitor_m == false)
447 dbus_connection_add_filter(connection, monitor_changed,
450 if (g_strcmp0(interface, "Service") == 0) {
451 if (monitor_s == true)
454 } else if (g_strcmp0(interface, "Technology") == 0) {
455 if (monitor_t == true)
458 } else if (g_strcmp0(interface, "Manager") == 0) {
459 if (monitor_m == true)
465 dbus_error_init(&err);
466 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
468 dbus_bus_add_match(connection, rule, &err);
471 if (dbus_error_is_set(&err))
472 fprintf(stderr, "Error: %s\n", err.message);
475 static void monitor_del(char *interface)
479 if (g_strcmp0(interface, "Service") == 0) {
480 if (monitor_s == false)
483 } else if (g_strcmp0(interface, "Technology") == 0) {
484 if (monitor_t == false)
487 } else if (g_strcmp0(interface, "Manager") == 0) {
488 if (monitor_m == false)
494 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
496 dbus_bus_remove_match(connection, rule, NULL);
499 if (monitor_s == false && monitor_t == false && monitor_m == false)
500 dbus_connection_remove_filter(connection, monitor_changed,
504 static int cmd_monitor(char *args[], int num, struct option *options)
513 switch (parse_boolean(args[2])) {
523 c = parse_args(args[1], options);
526 monitor_add("Service");
527 monitor_add("Technology");
528 monitor_add("Manager");
533 monitor_add("Service");
535 monitor_del("Service");
540 monitor_add("Technology");
542 monitor_del("Technology");
547 monitor_add("Manager");
549 monitor_del("Manager");
553 switch(parse_boolean(args[1])) {
555 monitor_del("Service");
556 monitor_del("Technology");
557 monitor_del("Manager");
561 monitor_add("Service");
562 monitor_add("Technology");
563 monitor_add("Manager");
577 static int cmd_exit(char *args[], int num, struct option *options)
582 static struct option service_options[] = {
583 {"properties", required_argument, 0, 'p'},
587 static const char *service_desc[] = {
588 "[<service>] (obsolete)",
592 static struct option config_options[] = {
593 {"nameservers", required_argument, 0, 'n'},
594 {"timeservers", required_argument, 0, 't'},
595 {"domains", required_argument, 0, 'd'},
596 {"ipv6", required_argument, 0, 'v'},
597 {"proxy", required_argument, 0, 'x'},
598 {"autoconnect", required_argument, 0, 'a'},
599 {"ipv4", required_argument, 0, 'i'},
600 {"remove", 0, 0, 'r'},
604 static const char *config_desc[] = {
605 "<dns1> [<dns2>] [<dns3>]",
606 "<ntp1> [<ntp2>] [...]",
607 "<domain1> [<domain2>] [...]",
608 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
609 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
610 " [exclude <exclude1> [<exclude2>] [...]]",
612 "off|dhcp|manual <address> <prefixlength> <gateway>",
617 static struct option monitor_options[] = {
618 {"services", no_argument, 0, 's'},
619 {"tech", no_argument, 0, 'c'},
620 {"manager", no_argument, 0, 'm'},
624 static const char *monitor_desc[] = {
625 "[off] Monitor only services",
626 "[off] Monitor only technologies",
627 "[off] Monitor only manager interface",
631 static const struct {
633 const char *argument;
634 struct option *options;
635 const char **options_desc;
636 int (*func) (char *args[], int num, struct option *options);
639 { "enable", "<technology>|offline", NULL, NULL,
640 cmd_enable, "Enables given technology or offline mode" },
641 { "disable", "<technology>|offline", NULL, NULL,
642 cmd_disable, "Disables given technology or offline mode"},
643 { "state", NULL, NULL, NULL,
644 cmd_state, "Shows if the system is online or offline" },
645 { "services", "[<service>]", service_options, &service_desc[0],
646 cmd_services, "Display services" },
647 { "technologies", NULL, NULL, NULL,
648 cmd_technologies, "Display technologies" },
649 { "scan", "<technology>", NULL, NULL,
650 cmd_scan, "Scans for new services for given technology" },
651 { "connect", "<service>", NULL, NULL,
652 cmd_connect, "Connect a given service" },
653 { "disconnect", "<service>", NULL, NULL,
654 cmd_disconnect, "Disconnect a given service" },
655 { "config", "<service>", config_options, &config_desc[0],
656 cmd_config, "Set service configuration options" },
657 { "monitor", "[off]", monitor_options, &monitor_desc[0],
658 cmd_monitor, "Monitor signals from interfaces" },
659 { "help", NULL, NULL, NULL,
660 cmd_help, "Show help" },
661 { "exit", NULL, NULL, NULL,
663 { "quit", NULL, NULL, NULL,
668 static int cmd_help(char *args[], int num, struct option *options)
672 for (i = 0; cmd_table[i].cmd != NULL; i++) {
673 const char *cmd = cmd_table[i].cmd;
674 const char *argument = cmd_table[i].argument;
675 const char *desc = cmd_table[i].desc;
677 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
678 argument != NULL? argument: "",
679 desc != NULL? desc: "");
681 if (cmd_table[i].options != NULL) {
682 for (j = 0; cmd_table[i].options[j].name != NULL;
684 const char *options_desc =
685 cmd_table[i].options_desc != NULL ?
686 cmd_table[i].options_desc[j]: "";
688 printf(" --%-12s%s\n",
689 cmd_table[i].options[j].name,
698 int commands(DBusConnection *connection, char *argv[], int argc)
702 for (i = 0; cmd_table[i].cmd != NULL; i++) {
703 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
704 cmd_table[i].func != NULL) {
705 result = cmd_table[i].func(argv, argc,
706 cmd_table[i].options);
708 printf("Error '%s': %s\n", argv[0],