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)
208 static int cmd_connect(char *args[], int num, struct option *options)
218 err = connect_service(connection, args[1]);
220 printf("Connected\n");
225 static int cmd_disconnect(char *args[], int num, struct option *options)
235 err = disconnect_service(connection, args[1]);
237 printf("Disconnected\n");
242 static int cmd_config(char *args[], int num, struct option *options)
244 int res = 0, index = 2, oldindex = 0;
247 DBusMessage *message;
251 service_name = args[1];
252 if (service_name == NULL)
255 while (index < num && args[index] != NULL) {
256 c = parse_args(args[index], options);
257 opt_start = &args[index + 1];
260 message = get_message(connection, "GetServices");
268 switch (parse_boolean(*opt_start)) {
280 res = set_service_property(connection, message,
281 service_name, "AutoConnect",
285 res = set_service_property(connection, message,
286 service_name, "IPv4.Configuration",
292 res = set_service_property(connection, message,
293 service_name, "IPv6.Configuration",
299 res = set_service_property(connection, message,
301 "Nameservers.Configuration",
305 res = set_service_property(connection, message,
307 "Timeservers.Configuration",
311 res = set_service_property(connection, message,
313 "Domains.Configuration",
317 if (*opt_start == NULL) {
322 if (strcmp(*opt_start, "direct") == 0) {
323 res = set_service_property(connection, message,
325 "Proxy.Configuration",
326 proxy_simple, opt_start, 1);
330 if (strcmp(*opt_start, "auto") == 0) {
331 res = set_service_property(connection, message,
333 "Proxy.Configuration",
334 proxy_simple, opt_start, 1);
338 if (strcmp(*opt_start, "manual") == 0) {
339 char **url_start = &args[index + 2];
341 if (*url_start != NULL &&
344 url_start = &args[index + 3];
347 res = store_proxy_input(connection,
348 message, service_name,
354 res = remove_service(connection, message, service_name);
361 dbus_message_unref(message);
364 printf("Error '%s': %s\n", args[oldindex],
375 static int cmd_monitor(char *args[], int num, struct option *options)
380 static int cmd_exit(char *args[], int num, struct option *options)
385 static struct option service_options[] = {
386 {"properties", required_argument, 0, 'p'},
390 static const char *service_desc[] = {
391 "[<service>] (obsolete)",
395 static struct option config_options[] = {
396 {"nameservers", required_argument, 0, 'n'},
397 {"timeservers", required_argument, 0, 't'},
398 {"domains", required_argument, 0, 'd'},
399 {"ipv6", required_argument, 0, 'v'},
400 {"proxy", required_argument, 0, 'x'},
401 {"autoconnect", required_argument, 0, 'a'},
402 {"ipv4", required_argument, 0, 'i'},
403 {"remove", 0, 0, 'r'},
407 static const char *config_desc[] = {
408 "<dns1> [<dns2>] [<dns3>]",
409 "<ntp1> [<ntp2>] [...]",
410 "<domain1> [<domain2>] [...]",
411 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
412 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
413 " [exclude <exclude1> [<exclude2>] [...]]",
415 "off|dhcp|manual <address> <prefixlength> <gateway>",
420 static struct option monitor_options[] = {
421 {"services", no_argument, 0, 's'},
422 {"tech", no_argument, 0, 'c'},
423 {"manager", no_argument, 0, 'm'},
427 static const char *monitor_desc[] = {
428 " Monitor only services",
429 " Monitor only technologies",
430 " Monitor only manager interface",
434 static const struct {
436 const char *argument;
437 struct option *options;
438 const char **options_desc;
439 int (*func) (char *args[], int num, struct option *options);
442 { "enable", "<technology>|offline", NULL, NULL,
443 cmd_enable, "Enables given technology or offline mode" },
444 { "disable", "<technology>|offline", NULL, NULL,
445 cmd_disable, "Disables given technology or offline mode"},
446 { "state", NULL, NULL, NULL,
447 cmd_state, "Shows if the system is online or offline" },
448 { "services", "[<service>]", service_options, &service_desc[0],
449 cmd_services, "Display services" },
450 { "technologies", NULL, NULL, NULL,
451 cmd_technologies, "Display technologies" },
452 { "scan", "<technology>", NULL, NULL,
453 cmd_scan, "Scans for new services for given technology" },
454 { "connect", "<service>", NULL, NULL,
455 cmd_connect, "Connect a given service" },
456 { "disconnect", "<service>", NULL, NULL,
457 cmd_disconnect, "Disconnect a given service" },
458 { "config", "<service>", config_options, &config_desc[0],
459 cmd_config, "Set service configuration options" },
460 { "monitor", NULL, monitor_options, &monitor_desc[0],
461 cmd_monitor, "Monitor signals from interfaces" },
462 { "help", NULL, NULL, NULL,
463 cmd_help, "Show help" },
464 { "exit", NULL, NULL, NULL,
466 { "quit", NULL, NULL, NULL,
471 static int cmd_help(char *args[], int num, struct option *options)
475 for (i = 0; cmd_table[i].cmd != NULL; i++) {
476 const char *cmd = cmd_table[i].cmd;
477 const char *argument = cmd_table[i].argument;
478 const char *desc = cmd_table[i].desc;
480 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
481 argument != NULL? argument: "",
482 desc != NULL? desc: "");
484 if (cmd_table[i].options != NULL) {
485 for (j = 0; cmd_table[i].options[j].name != NULL;
487 const char *options_desc =
488 cmd_table[i].options_desc != NULL ?
489 cmd_table[i].options_desc[j]: "";
491 printf(" --%-12s%s\n",
492 cmd_table[i].options[j].name,
501 int commands(DBusConnection *connection, char *argv[], int argc)
505 for (i = 0; cmd_table[i].cmd != NULL; i++) {
506 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
507 cmd_table[i].func != NULL) {
508 result = cmd_table[i].func(argv, argc,
509 cmd_table[i].options);
511 printf("Error '%s': %s\n", argv[0],
520 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
522 DBusMessage *message = NULL;
525 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
526 strcmp(argv[0], "h") == 0) {
527 printf("Usage: connmanctl [[command] [args]]\n");
528 cmd_help(NULL, 0, NULL);
529 printf("\nNote: arguments and output are considered "
530 "EXPERIMENTAL for now.\n\n");
531 } else if (strcmp(argv[0], "scan") == 0) {
533 fprintf(stderr, "Scan requires a service name or path, "
537 message = get_message(connection, "GetTechnologies");
541 error = scan_technology(connection, message, argv[1]);
542 } else if (strcmp(argv[0], "enable") == 0) {
544 fprintf(stderr, "Enable requires a technology name or "
545 "the argument 'offlinemode', see help\n");
547 } else if (strcmp(argv[1], "offlinemode") == 0) {
548 error = set_manager(connection, "OfflineMode", TRUE);
550 printf("OfflineMode is now enabled\n");
552 message = get_message(connection, "GetTechnologies");
556 error = set_technology(connection, message,
557 "Powered", argv[1], TRUE);
559 printf("Enabled %s technology\n", argv[1]);
561 } else if (strcmp(argv[0], "disable") == 0) {
563 fprintf(stderr, "Disable requires a technology name or "
564 "the argument 'offlinemode' see help\n");
566 } else if (strcmp(argv[1], "offlinemode") == 0) {
567 error = set_manager(connection, "OfflineMode", FALSE);
569 printf("OfflineMode is now disabled\n");
571 message = get_message(connection, "GetTechnologies");
575 error = set_technology(connection, message,
576 "Powered", argv[1], FALSE);
578 printf("Disabled %s technology\n", argv[1]);
584 dbus_message_unref(message);
589 int commands_options(DBusConnection *connection, char *argv[], int argc)
592 int option_index = 0;
594 if (strcmp(argv[0], "monitor") == 0) {
596 fprintf(stderr, "Too many arguments for monitor, "
601 error = monitor_connman(connection, "Service",
605 error = monitor_connman(connection, "Technology",
609 error = monitor_connman(connection, "Manager",
613 error = monitor_connman(connection, "Manager",
617 error = monitor_connman(connection, "Manager",
618 "TechnologyRemoved");
621 error = monitor_connman(connection, "Manager",
625 if (dbus_connection_add_filter(connection,
626 service_property_changed, NULL, NULL)
629 if (dbus_connection_add_filter(connection,
630 tech_property_changed, NULL, NULL)
633 if (dbus_connection_add_filter(connection,
634 tech_added_removed, NULL, NULL)
637 if (dbus_connection_add_filter(connection,
638 manager_property_changed, NULL, NULL)
641 if (dbus_connection_add_filter(connection,
642 manager_services_changed, NULL, NULL)
645 printf("Now monitoring all interfaces.\n");
647 while ((c = getopt_long(argc, argv, "", monitor_options,
650 if (option_index == 0) {
651 printf("Monitor takes an "
652 "option, see help\n");
657 error = monitor_switch(argc, argv, c, connection);