5 * Copyright (C) 2012 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
37 #include "technology.h"
38 #include "data_manager.h"
40 #include "interactive.h"
42 #define MANDATORY_ARGS 3
44 static char *ipv4[] = {
52 static char *ipv6[] = {
61 static char *proxy_simple[] = {
67 static int cmd_help(char *args[], int num, struct option *options);
69 static int parse_args(char *arg, struct option *options)
76 for (i = 0; options[i].name != NULL; i++) {
77 if (strcmp(options[i].name, arg) == 0 ||
78 (strncmp(arg, "--", 2) == 0 &&
79 strcmp(&arg[2], options[i].name) == 0))
80 return options[i].val;
86 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
92 error = monitor_connman(conn, "Service", "PropertyChanged");
95 if (dbus_connection_add_filter(conn, service_property_changed,
98 printf("Now monitoring the service interface.\n");
101 error = monitor_connman(conn, "Technology", "PropertyChanged");
104 if (dbus_connection_add_filter(conn, tech_property_changed,
105 NULL, NULL) == FALSE)
107 printf("Now monitoring the technology interface.\n");
110 error = monitor_connman(conn, "Manager", "PropertyChanged");
113 error = monitor_connman(conn, "Manager", "TechnologyAdded");
116 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
119 error = monitor_connman(conn, "Manager", "ServicesChanged");
122 if (dbus_connection_add_filter(conn, manager_property_changed,
123 NULL, NULL) == FALSE)
125 if (dbus_connection_add_filter(conn, tech_added_removed,
126 NULL, NULL) == FALSE)
128 if (dbus_connection_add_filter(conn, manager_services_changed,
129 NULL, NULL) == FALSE)
131 printf("Now monitoring the manager interface.\n");
134 fprintf(stderr, "Command not recognized, please check help\n");
141 static int cmd_enable(char *args[], int num, struct option *options)
146 static int cmd_disable(char *args[], int num, struct option *options)
151 static int cmd_state(char *args[], int num, struct option *options)
156 return list_properties(connection, "GetProperties", NULL);
159 static int cmd_services(char *args[], int num, struct option *options)
161 char *service_name = NULL;
164 DBusMessage *message;
169 c = parse_args(args[1], options);
176 service_name = args[2];
181 service_name = args[1];
185 message = get_message(connection, "GetServices");
189 err = list_properties(connection, "GetServices", service_name);
190 dbus_message_unref(message);
195 static int cmd_technologies(char *args[], int num, struct option *options)
200 return list_properties(connection, "GetTechnologies", NULL);
203 static int cmd_scan(char *args[], int num, struct option *options)
205 DBusMessage *message;
214 message = get_message(connection, "GetTechnologies");
218 err = scan_technology(connection, message, args[1]);
220 printf("Scan completed\n");
226 static int cmd_connect(char *args[], int num, struct option *options)
236 err = connect_service(connection, args[1]);
238 printf("Connected\n");
243 static int cmd_disconnect(char *args[], int num, struct option *options)
253 err = disconnect_service(connection, args[1]);
255 printf("Disconnected\n");
260 static int cmd_config(char *args[], int num, struct option *options)
262 int res = 0, index = 2, oldindex = 0;
265 DBusMessage *message;
269 service_name = args[1];
270 if (service_name == NULL)
273 while (index < num && args[index] != NULL) {
274 c = parse_args(args[index], options);
275 opt_start = &args[index + 1];
278 message = get_message(connection, "GetServices");
286 switch (parse_boolean(*opt_start)) {
298 res = set_service_property(connection, message,
299 service_name, "AutoConnect",
303 res = set_service_property(connection, message,
304 service_name, "IPv4.Configuration",
310 res = set_service_property(connection, message,
311 service_name, "IPv6.Configuration",
317 res = set_service_property(connection, message,
319 "Nameservers.Configuration",
323 res = set_service_property(connection, message,
325 "Timeservers.Configuration",
329 res = set_service_property(connection, message,
331 "Domains.Configuration",
335 if (*opt_start == NULL) {
340 if (strcmp(*opt_start, "direct") == 0) {
341 res = set_service_property(connection, message,
343 "Proxy.Configuration",
344 proxy_simple, opt_start, 1);
348 if (strcmp(*opt_start, "auto") == 0) {
349 res = set_service_property(connection, message,
351 "Proxy.Configuration",
352 proxy_simple, opt_start, 1);
356 if (strcmp(*opt_start, "manual") == 0) {
357 char **url_start = &args[index + 2];
359 if (*url_start != NULL &&
362 url_start = &args[index + 3];
365 res = store_proxy_input(connection,
366 message, service_name,
372 res = remove_service(connection, message, service_name);
379 dbus_message_unref(message);
382 printf("Error '%s': %s\n", args[oldindex],
393 static int cmd_monitor(char *args[], int num, struct option *options)
398 static int cmd_exit(char *args[], int num, struct option *options)
403 static struct option service_options[] = {
404 {"properties", required_argument, 0, 'p'},
408 static const char *service_desc[] = {
409 "[<service>] (obsolete)",
413 static struct option config_options[] = {
414 {"nameservers", required_argument, 0, 'n'},
415 {"timeservers", required_argument, 0, 't'},
416 {"domains", required_argument, 0, 'd'},
417 {"ipv6", required_argument, 0, 'v'},
418 {"proxy", required_argument, 0, 'x'},
419 {"autoconnect", required_argument, 0, 'a'},
420 {"ipv4", required_argument, 0, 'i'},
421 {"remove", 0, 0, 'r'},
425 static const char *config_desc[] = {
426 "<dns1> [<dns2>] [<dns3>]",
427 "<ntp1> [<ntp2>] [...]",
428 "<domain1> [<domain2>] [...]",
429 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
430 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
431 " [exclude <exclude1> [<exclude2>] [...]]",
433 "off|dhcp|manual <address> <prefixlength> <gateway>",
438 static struct option monitor_options[] = {
439 {"services", no_argument, 0, 's'},
440 {"tech", no_argument, 0, 'c'},
441 {"manager", no_argument, 0, 'm'},
445 static const char *monitor_desc[] = {
446 " Monitor only services",
447 " Monitor only technologies",
448 " Monitor only manager interface",
452 static const struct {
454 const char *argument;
455 struct option *options;
456 const char **options_desc;
457 int (*func) (char *args[], int num, struct option *options);
460 { "enable", "<technology>|offline", NULL, NULL,
461 cmd_enable, "Enables given technology or offline mode" },
462 { "disable", "<technology>|offline", NULL, NULL,
463 cmd_disable, "Disables given technology or offline mode"},
464 { "state", NULL, NULL, NULL,
465 cmd_state, "Shows if the system is online or offline" },
466 { "services", "[<service>]", service_options, &service_desc[0],
467 cmd_services, "Display services" },
468 { "technologies", NULL, NULL, NULL,
469 cmd_technologies, "Display technologies" },
470 { "scan", "<technology>", NULL, NULL,
471 cmd_scan, "Scans for new services for given technology" },
472 { "connect", "<service>", NULL, NULL,
473 cmd_connect, "Connect a given service" },
474 { "disconnect", "<service>", NULL, NULL,
475 cmd_disconnect, "Disconnect a given service" },
476 { "config", "<service>", config_options, &config_desc[0],
477 cmd_config, "Set service configuration options" },
478 { "monitor", NULL, monitor_options, &monitor_desc[0],
479 cmd_monitor, "Monitor signals from interfaces" },
480 { "help", NULL, NULL, NULL,
481 cmd_help, "Show help" },
482 { "exit", NULL, NULL, NULL,
484 { "quit", NULL, NULL, NULL,
489 static int cmd_help(char *args[], int num, struct option *options)
493 for (i = 0; cmd_table[i].cmd != NULL; i++) {
494 const char *cmd = cmd_table[i].cmd;
495 const char *argument = cmd_table[i].argument;
496 const char *desc = cmd_table[i].desc;
498 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
499 argument != NULL? argument: "",
500 desc != NULL? desc: "");
502 if (cmd_table[i].options != NULL) {
503 for (j = 0; cmd_table[i].options[j].name != NULL;
505 const char *options_desc =
506 cmd_table[i].options_desc != NULL ?
507 cmd_table[i].options_desc[j]: "";
509 printf(" --%-12s%s\n",
510 cmd_table[i].options[j].name,
519 int commands(DBusConnection *connection, char *argv[], int argc)
523 for (i = 0; cmd_table[i].cmd != NULL; i++) {
524 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
525 cmd_table[i].func != NULL) {
526 result = cmd_table[i].func(argv, argc,
527 cmd_table[i].options);
529 printf("Error '%s': %s\n", argv[0],
538 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
540 DBusMessage *message = NULL;
543 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
544 strcmp(argv[0], "h") == 0) {
545 printf("Usage: connmanctl [[command] [args]]\n");
546 cmd_help(NULL, 0, NULL);
547 printf("\nNote: arguments and output are considered "
548 "EXPERIMENTAL for now.\n\n");
549 } else if (strcmp(argv[0], "enable") == 0) {
551 fprintf(stderr, "Enable requires a technology name or "
552 "the argument 'offlinemode', see help\n");
554 } else if (strcmp(argv[1], "offlinemode") == 0) {
555 error = set_manager(connection, "OfflineMode", TRUE);
557 printf("OfflineMode is now enabled\n");
559 message = get_message(connection, "GetTechnologies");
563 error = set_technology(connection, message,
564 "Powered", argv[1], TRUE);
566 printf("Enabled %s technology\n", argv[1]);
568 } else if (strcmp(argv[0], "disable") == 0) {
570 fprintf(stderr, "Disable requires a technology name or "
571 "the argument 'offlinemode' see help\n");
573 } else if (strcmp(argv[1], "offlinemode") == 0) {
574 error = set_manager(connection, "OfflineMode", FALSE);
576 printf("OfflineMode is now disabled\n");
578 message = get_message(connection, "GetTechnologies");
582 error = set_technology(connection, message,
583 "Powered", argv[1], FALSE);
585 printf("Disabled %s technology\n", argv[1]);
591 dbus_message_unref(message);
596 int commands_options(DBusConnection *connection, char *argv[], int argc)
599 int option_index = 0;
601 if (strcmp(argv[0], "monitor") == 0) {
603 fprintf(stderr, "Too many arguments for monitor, "
608 error = monitor_connman(connection, "Service",
612 error = monitor_connman(connection, "Technology",
616 error = monitor_connman(connection, "Manager",
620 error = monitor_connman(connection, "Manager",
624 error = monitor_connman(connection, "Manager",
625 "TechnologyRemoved");
628 error = monitor_connman(connection, "Manager",
632 if (dbus_connection_add_filter(connection,
633 service_property_changed, NULL, NULL)
636 if (dbus_connection_add_filter(connection,
637 tech_property_changed, NULL, NULL)
640 if (dbus_connection_add_filter(connection,
641 tech_added_removed, NULL, NULL)
644 if (dbus_connection_add_filter(connection,
645 manager_property_changed, NULL, NULL)
648 if (dbus_connection_add_filter(connection,
649 manager_services_changed, NULL, NULL)
652 printf("Now monitoring all interfaces.\n");
654 while ((c = getopt_long(argc, argv, "", monitor_options,
657 if (option_index == 0) {
658 printf("Monitor takes an "
659 "option, see help\n");
664 error = monitor_switch(argc, argv, c, connection);