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 int cmd_state(char *args[], int num, struct option *options)
152 return list_properties(connection, "GetProperties", NULL);
155 static int cmd_services(char *args[], int num, struct option *options)
157 char *service_name = NULL;
160 DBusMessage *message;
165 c = parse_args(args[1], options);
172 service_name = args[2];
177 service_name = args[1];
181 message = get_message(connection, "GetServices");
185 err = list_properties(connection, "GetServices", service_name);
186 dbus_message_unref(message);
191 static int cmd_technologies(char *args[], int num, struct option *options)
196 return list_properties(connection, "GetTechnologies", NULL);
199 static int cmd_scan(char *args[], int num, struct option *options)
201 DBusMessage *message;
210 message = get_message(connection, "GetTechnologies");
214 err = scan_technology(connection, message, args[1]);
216 printf("Scan completed\n");
222 static int cmd_connect(char *args[], int num, struct option *options)
232 err = connect_service(connection, args[1]);
234 printf("Connected\n");
239 static int cmd_disconnect(char *args[], int num, struct option *options)
249 err = disconnect_service(connection, args[1]);
251 printf("Disconnected\n");
256 static int cmd_config(char *args[], int num, struct option *options)
258 int res = 0, index = 2, oldindex = 0;
261 DBusMessage *message;
265 service_name = args[1];
266 if (service_name == NULL)
269 while (index < num && args[index] != NULL) {
270 c = parse_args(args[index], options);
271 opt_start = &args[index + 1];
274 message = get_message(connection, "GetServices");
282 switch (parse_boolean(*opt_start)) {
294 res = set_service_property(connection, message,
295 service_name, "AutoConnect",
299 res = set_service_property(connection, message,
300 service_name, "IPv4.Configuration",
306 res = set_service_property(connection, message,
307 service_name, "IPv6.Configuration",
313 res = set_service_property(connection, message,
315 "Nameservers.Configuration",
319 res = set_service_property(connection, message,
321 "Timeservers.Configuration",
325 res = set_service_property(connection, message,
327 "Domains.Configuration",
331 if (*opt_start == NULL) {
336 if (strcmp(*opt_start, "direct") == 0) {
337 res = set_service_property(connection, message,
339 "Proxy.Configuration",
340 proxy_simple, opt_start, 1);
344 if (strcmp(*opt_start, "auto") == 0) {
345 res = set_service_property(connection, message,
347 "Proxy.Configuration",
348 proxy_simple, opt_start, 1);
352 if (strcmp(*opt_start, "manual") == 0) {
353 char **url_start = &args[index + 2];
355 if (*url_start != NULL &&
358 url_start = &args[index + 3];
361 res = store_proxy_input(connection,
362 message, service_name,
368 res = remove_service(connection, message, service_name);
375 dbus_message_unref(message);
378 printf("Error '%s': %s\n", args[oldindex],
389 static DBusHandlerResult monitor_changed(DBusConnection *connection,
390 DBusMessage *message, void *user_data)
392 DBusMessageIter iter;
393 const char *interface, *path;
395 interface = dbus_message_get_interface(message);
396 if (strncmp(interface, "net.connman.", 12) != 0)
397 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
399 interface = strrchr(interface, '.');
400 if (interface != NULL && *interface != '\0')
403 path = strrchr(dbus_message_get_path(message), '/');
404 if (path != NULL && *path != '\0')
407 __connmanctl_save_rl();
409 if (dbus_message_is_signal(message, "net.connman.Manager",
410 "ServicesChanged") == TRUE) {
412 fprintf(stdout, "%-12s %-20s = {\n", interface,
414 dbus_message_iter_init(message, &iter);
415 __connmanctl_services_list(&iter);
416 fprintf(stdout, "\n}\n");
418 __connmanctl_redraw_rl();
420 return DBUS_HANDLER_RESULT_HANDLED;
423 if (dbus_message_is_signal(message, "net.connman.Manager",
424 "TechnologyAdded") == TRUE)
425 path = "TechnologyAdded";
427 if (dbus_message_is_signal(message, "net.connman.Manager",
428 "TechnologyRemoved") == TRUE)
429 path = "TechnologyRemoved";
431 fprintf(stdout, "%-12s %-20s ", interface, path);
432 dbus_message_iter_init(message, &iter);
434 __connmanctl_dbus_print(&iter, "", " = ", " = ");
435 fprintf(stdout, "\n");
437 __connmanctl_redraw_rl();
439 return DBUS_HANDLER_RESULT_HANDLED;
442 static bool monitor_s = false;
443 static bool monitor_t = false;
444 static bool monitor_m = false;
446 static void monitor_add(char *interface)
451 if (monitor_s == false && monitor_t == false && monitor_m == false)
452 dbus_connection_add_filter(connection, monitor_changed,
455 if (g_strcmp0(interface, "Service") == 0) {
456 if (monitor_s == true)
459 } else if (g_strcmp0(interface, "Technology") == 0) {
460 if (monitor_t == true)
463 } else if (g_strcmp0(interface, "Manager") == 0) {
464 if (monitor_m == true)
470 dbus_error_init(&err);
471 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
473 dbus_bus_add_match(connection, rule, &err);
476 if (dbus_error_is_set(&err))
477 fprintf(stderr, "Error: %s\n", err.message);
480 static void monitor_del(char *interface)
484 if (g_strcmp0(interface, "Service") == 0) {
485 if (monitor_s == false)
488 } else if (g_strcmp0(interface, "Technology") == 0) {
489 if (monitor_t == false)
492 } else if (g_strcmp0(interface, "Manager") == 0) {
493 if (monitor_m == false)
499 rule = g_strdup_printf("type='signal',interface='net.connman.%s'",
501 dbus_bus_remove_match(connection, rule, NULL);
504 if (monitor_s == false && monitor_t == false && monitor_m == false)
505 dbus_connection_remove_filter(connection, monitor_changed,
509 static int cmd_monitor(char *args[], int num, struct option *options)
518 switch (parse_boolean(args[2])) {
528 c = parse_args(args[1], options);
531 monitor_add("Service");
532 monitor_add("Technology");
533 monitor_add("Manager");
538 monitor_add("Service");
540 monitor_del("Service");
545 monitor_add("Technology");
547 monitor_del("Technology");
552 monitor_add("Manager");
554 monitor_del("Manager");
558 switch(parse_boolean(args[1])) {
560 monitor_del("Service");
561 monitor_del("Technology");
562 monitor_del("Manager");
566 monitor_add("Service");
567 monitor_add("Technology");
568 monitor_add("Manager");
582 static int cmd_exit(char *args[], int num, struct option *options)
587 static struct option service_options[] = {
588 {"properties", required_argument, 0, 'p'},
592 static const char *service_desc[] = {
593 "[<service>] (obsolete)",
597 static struct option config_options[] = {
598 {"nameservers", required_argument, 0, 'n'},
599 {"timeservers", required_argument, 0, 't'},
600 {"domains", required_argument, 0, 'd'},
601 {"ipv6", required_argument, 0, 'v'},
602 {"proxy", required_argument, 0, 'x'},
603 {"autoconnect", required_argument, 0, 'a'},
604 {"ipv4", required_argument, 0, 'i'},
605 {"remove", 0, 0, 'r'},
609 static const char *config_desc[] = {
610 "<dns1> [<dns2>] [<dns3>]",
611 "<ntp1> [<ntp2>] [...]",
612 "<domain1> [<domain2>] [...]",
613 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
614 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
615 " [exclude <exclude1> [<exclude2>] [...]]",
617 "off|dhcp|manual <address> <prefixlength> <gateway>",
622 static struct option monitor_options[] = {
623 {"services", no_argument, 0, 's'},
624 {"tech", no_argument, 0, 'c'},
625 {"manager", no_argument, 0, 'm'},
629 static const char *monitor_desc[] = {
630 "[off] Monitor only services",
631 "[off] Monitor only technologies",
632 "[off] Monitor only manager interface",
636 static const struct {
638 const char *argument;
639 struct option *options;
640 const char **options_desc;
641 int (*func) (char *args[], int num, struct option *options);
644 { "enable", "<technology>|offline", NULL, NULL,
645 cmd_enable, "Enables given technology or offline mode" },
646 { "disable", "<technology>|offline", NULL, NULL,
647 cmd_disable, "Disables given technology or offline mode"},
648 { "state", NULL, NULL, NULL,
649 cmd_state, "Shows if the system is online or offline" },
650 { "services", "[<service>]", service_options, &service_desc[0],
651 cmd_services, "Display services" },
652 { "technologies", NULL, NULL, NULL,
653 cmd_technologies, "Display technologies" },
654 { "scan", "<technology>", NULL, NULL,
655 cmd_scan, "Scans for new services for given technology" },
656 { "connect", "<service>", NULL, NULL,
657 cmd_connect, "Connect a given service" },
658 { "disconnect", "<service>", NULL, NULL,
659 cmd_disconnect, "Disconnect a given service" },
660 { "config", "<service>", config_options, &config_desc[0],
661 cmd_config, "Set service configuration options" },
662 { "monitor", "[off]", monitor_options, &monitor_desc[0],
663 cmd_monitor, "Monitor signals from interfaces" },
664 { "help", NULL, NULL, NULL,
665 cmd_help, "Show help" },
666 { "exit", NULL, NULL, NULL,
668 { "quit", NULL, NULL, NULL,
673 static int cmd_help(char *args[], int num, struct option *options)
677 for (i = 0; cmd_table[i].cmd != NULL; i++) {
678 const char *cmd = cmd_table[i].cmd;
679 const char *argument = cmd_table[i].argument;
680 const char *desc = cmd_table[i].desc;
682 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
683 argument != NULL? argument: "",
684 desc != NULL? desc: "");
686 if (cmd_table[i].options != NULL) {
687 for (j = 0; cmd_table[i].options[j].name != NULL;
689 const char *options_desc =
690 cmd_table[i].options_desc != NULL ?
691 cmd_table[i].options_desc[j]: "";
693 printf(" --%-12s%s\n",
694 cmd_table[i].options[j].name,
703 int commands(DBusConnection *dbus_conn, char *argv[], int argc)
707 connection = dbus_conn;
709 for (i = 0; cmd_table[i].cmd != NULL; i++) {
710 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
711 cmd_table[i].func != NULL) {
712 result = cmd_table[i].func(argv, argc,
713 cmd_table[i].options);
714 if (result < 0 && result != -EINPROGRESS)
715 fprintf(stderr, "Error '%s': %s\n", argv[0],
721 fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);