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)
143 DBusMessage *message;
152 if (strcmp(args[1], "offlinemode") == 0) {
153 err = set_manager(connection, "OfflineMode", TRUE);
155 printf("OfflineMode enabled\n");
160 message = get_message(connection, "GetTechnologies");
164 set_technology(connection, message, "Powered", args[1], TRUE);
169 static int cmd_disable(char *args[], int num, struct option *options)
171 DBusMessage *message;
180 if (strcmp(args[1], "offlinemode") == 0) {
181 err = set_manager(connection, "OfflineMode", FALSE);
183 printf("OfflineMode enabled\n");
188 message = get_message(connection, "GetTechnologies");
192 set_technology(connection, message, "Powered", args[1], FALSE);
197 static int cmd_state(char *args[], int num, struct option *options)
202 return list_properties(connection, "GetProperties", NULL);
205 static int cmd_services(char *args[], int num, struct option *options)
207 char *service_name = NULL;
210 DBusMessage *message;
215 c = parse_args(args[1], options);
222 service_name = args[2];
227 service_name = args[1];
231 message = get_message(connection, "GetServices");
235 err = list_properties(connection, "GetServices", service_name);
236 dbus_message_unref(message);
241 static int cmd_technologies(char *args[], int num, struct option *options)
246 return list_properties(connection, "GetTechnologies", NULL);
249 static int cmd_scan(char *args[], int num, struct option *options)
251 DBusMessage *message;
260 message = get_message(connection, "GetTechnologies");
264 err = scan_technology(connection, message, args[1]);
266 printf("Scan completed\n");
272 static int cmd_connect(char *args[], int num, struct option *options)
282 err = connect_service(connection, args[1]);
284 printf("Connected\n");
289 static int cmd_disconnect(char *args[], int num, struct option *options)
299 err = disconnect_service(connection, args[1]);
301 printf("Disconnected\n");
306 static int cmd_config(char *args[], int num, struct option *options)
308 int res = 0, index = 2, oldindex = 0;
311 DBusMessage *message;
315 service_name = args[1];
316 if (service_name == NULL)
319 while (index < num && args[index] != NULL) {
320 c = parse_args(args[index], options);
321 opt_start = &args[index + 1];
324 message = get_message(connection, "GetServices");
332 switch (parse_boolean(*opt_start)) {
344 res = set_service_property(connection, message,
345 service_name, "AutoConnect",
349 res = set_service_property(connection, message,
350 service_name, "IPv4.Configuration",
356 res = set_service_property(connection, message,
357 service_name, "IPv6.Configuration",
363 res = set_service_property(connection, message,
365 "Nameservers.Configuration",
369 res = set_service_property(connection, message,
371 "Timeservers.Configuration",
375 res = set_service_property(connection, message,
377 "Domains.Configuration",
381 if (*opt_start == NULL) {
386 if (strcmp(*opt_start, "direct") == 0) {
387 res = set_service_property(connection, message,
389 "Proxy.Configuration",
390 proxy_simple, opt_start, 1);
394 if (strcmp(*opt_start, "auto") == 0) {
395 res = set_service_property(connection, message,
397 "Proxy.Configuration",
398 proxy_simple, opt_start, 1);
402 if (strcmp(*opt_start, "manual") == 0) {
403 char **url_start = &args[index + 2];
405 if (*url_start != NULL &&
408 url_start = &args[index + 3];
411 res = store_proxy_input(connection,
412 message, service_name,
418 res = remove_service(connection, message, service_name);
425 dbus_message_unref(message);
428 printf("Error '%s': %s\n", args[oldindex],
439 static int cmd_monitor(char *args[], int num, struct option *options)
444 static int cmd_exit(char *args[], int num, struct option *options)
449 static struct option service_options[] = {
450 {"properties", required_argument, 0, 'p'},
454 static const char *service_desc[] = {
455 "[<service>] (obsolete)",
459 static struct option config_options[] = {
460 {"nameservers", required_argument, 0, 'n'},
461 {"timeservers", required_argument, 0, 't'},
462 {"domains", required_argument, 0, 'd'},
463 {"ipv6", required_argument, 0, 'v'},
464 {"proxy", required_argument, 0, 'x'},
465 {"autoconnect", required_argument, 0, 'a'},
466 {"ipv4", required_argument, 0, 'i'},
467 {"remove", 0, 0, 'r'},
471 static const char *config_desc[] = {
472 "<dns1> [<dns2>] [<dns3>]",
473 "<ntp1> [<ntp2>] [...]",
474 "<domain1> [<domain2>] [...]",
475 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
476 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
477 " [exclude <exclude1> [<exclude2>] [...]]",
479 "off|dhcp|manual <address> <prefixlength> <gateway>",
484 static struct option monitor_options[] = {
485 {"services", no_argument, 0, 's'},
486 {"tech", no_argument, 0, 'c'},
487 {"manager", no_argument, 0, 'm'},
491 static const char *monitor_desc[] = {
492 " Monitor only services",
493 " Monitor only technologies",
494 " Monitor only manager interface",
498 static const struct {
500 const char *argument;
501 struct option *options;
502 const char **options_desc;
503 int (*func) (char *args[], int num, struct option *options);
506 { "enable", "<technology>|offline", NULL, NULL,
507 cmd_enable, "Enables given technology or offline mode" },
508 { "disable", "<technology>|offline", NULL, NULL,
509 cmd_disable, "Disables given technology or offline mode"},
510 { "state", NULL, NULL, NULL,
511 cmd_state, "Shows if the system is online or offline" },
512 { "services", "[<service>]", service_options, &service_desc[0],
513 cmd_services, "Display services" },
514 { "technologies", NULL, NULL, NULL,
515 cmd_technologies, "Display technologies" },
516 { "scan", "<technology>", NULL, NULL,
517 cmd_scan, "Scans for new services for given technology" },
518 { "connect", "<service>", NULL, NULL,
519 cmd_connect, "Connect a given service" },
520 { "disconnect", "<service>", NULL, NULL,
521 cmd_disconnect, "Disconnect a given service" },
522 { "config", "<service>", config_options, &config_desc[0],
523 cmd_config, "Set service configuration options" },
524 { "monitor", NULL, monitor_options, &monitor_desc[0],
525 cmd_monitor, "Monitor signals from interfaces" },
526 { "help", NULL, NULL, NULL,
527 cmd_help, "Show help" },
528 { "exit", NULL, NULL, NULL,
530 { "quit", NULL, NULL, NULL,
535 static int cmd_help(char *args[], int num, struct option *options)
539 for (i = 0; cmd_table[i].cmd != NULL; i++) {
540 const char *cmd = cmd_table[i].cmd;
541 const char *argument = cmd_table[i].argument;
542 const char *desc = cmd_table[i].desc;
544 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
545 argument != NULL? argument: "",
546 desc != NULL? desc: "");
548 if (cmd_table[i].options != NULL) {
549 for (j = 0; cmd_table[i].options[j].name != NULL;
551 const char *options_desc =
552 cmd_table[i].options_desc != NULL ?
553 cmd_table[i].options_desc[j]: "";
555 printf(" --%-12s%s\n",
556 cmd_table[i].options[j].name,
565 int commands(DBusConnection *connection, char *argv[], int argc)
569 for (i = 0; cmd_table[i].cmd != NULL; i++) {
570 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
571 cmd_table[i].func != NULL) {
572 result = cmd_table[i].func(argv, argc,
573 cmd_table[i].options);
575 printf("Error '%s': %s\n", argv[0],
584 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
586 DBusMessage *message = NULL;
589 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
590 strcmp(argv[0], "h") == 0) {
591 printf("Usage: connmanctl [[command] [args]]\n");
592 cmd_help(NULL, 0, NULL);
593 printf("\nNote: arguments and output are considered "
594 "EXPERIMENTAL for now.\n\n");
599 dbus_message_unref(message);
604 int commands_options(DBusConnection *connection, char *argv[], int argc)
607 int option_index = 0;
609 if (strcmp(argv[0], "monitor") == 0) {
611 fprintf(stderr, "Too many arguments for monitor, "
616 error = monitor_connman(connection, "Service",
620 error = monitor_connman(connection, "Technology",
624 error = monitor_connman(connection, "Manager",
628 error = monitor_connman(connection, "Manager",
632 error = monitor_connman(connection, "Manager",
633 "TechnologyRemoved");
636 error = monitor_connman(connection, "Manager",
640 if (dbus_connection_add_filter(connection,
641 service_property_changed, NULL, NULL)
644 if (dbus_connection_add_filter(connection,
645 tech_property_changed, NULL, NULL)
648 if (dbus_connection_add_filter(connection,
649 tech_added_removed, NULL, NULL)
652 if (dbus_connection_add_filter(connection,
653 manager_property_changed, NULL, NULL)
656 if (dbus_connection_add_filter(connection,
657 manager_services_changed, NULL, NULL)
660 printf("Now monitoring all interfaces.\n");
662 while ((c = getopt_long(argc, argv, "", monitor_options,
665 if (option_index == 0) {
666 printf("Monitor takes an "
667 "option, see help\n");
672 error = monitor_switch(argc, argv, c, connection);