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 static int cmd_enable(char *args[], int num, struct option *options)
97 if (strcmp(args[1], "offlinemode") == 0) {
98 err = set_manager(connection, "OfflineMode", TRUE);
100 printf("OfflineMode enabled\n");
105 message = get_message(connection, "GetTechnologies");
109 set_technology(connection, message, "Powered", args[1], TRUE);
114 static int cmd_disable(char *args[], int num, struct option *options)
116 DBusMessage *message;
125 if (strcmp(args[1], "offlinemode") == 0) {
126 err = set_manager(connection, "OfflineMode", FALSE);
128 printf("OfflineMode enabled\n");
133 message = get_message(connection, "GetTechnologies");
137 set_technology(connection, message, "Powered", args[1], FALSE);
142 static int cmd_state(char *args[], int num, struct option *options)
147 return list_properties(connection, "GetProperties", NULL);
150 static int cmd_services(char *args[], int num, struct option *options)
152 char *service_name = NULL;
155 DBusMessage *message;
160 c = parse_args(args[1], options);
167 service_name = args[2];
172 service_name = args[1];
176 message = get_message(connection, "GetServices");
180 err = list_properties(connection, "GetServices", service_name);
181 dbus_message_unref(message);
186 static int cmd_technologies(char *args[], int num, struct option *options)
191 return list_properties(connection, "GetTechnologies", NULL);
194 static int cmd_scan(char *args[], int num, struct option *options)
196 DBusMessage *message;
205 message = get_message(connection, "GetTechnologies");
209 err = scan_technology(connection, message, args[1]);
211 printf("Scan completed\n");
217 static int cmd_connect(char *args[], int num, struct option *options)
227 err = connect_service(connection, args[1]);
229 printf("Connected\n");
234 static int cmd_disconnect(char *args[], int num, struct option *options)
244 err = disconnect_service(connection, args[1]);
246 printf("Disconnected\n");
251 static int cmd_config(char *args[], int num, struct option *options)
253 int res = 0, index = 2, oldindex = 0;
256 DBusMessage *message;
260 service_name = args[1];
261 if (service_name == NULL)
264 while (index < num && args[index] != NULL) {
265 c = parse_args(args[index], options);
266 opt_start = &args[index + 1];
269 message = get_message(connection, "GetServices");
277 switch (parse_boolean(*opt_start)) {
289 res = set_service_property(connection, message,
290 service_name, "AutoConnect",
294 res = set_service_property(connection, message,
295 service_name, "IPv4.Configuration",
301 res = set_service_property(connection, message,
302 service_name, "IPv6.Configuration",
308 res = set_service_property(connection, message,
310 "Nameservers.Configuration",
314 res = set_service_property(connection, message,
316 "Timeservers.Configuration",
320 res = set_service_property(connection, message,
322 "Domains.Configuration",
326 if (*opt_start == NULL) {
331 if (strcmp(*opt_start, "direct") == 0) {
332 res = set_service_property(connection, message,
334 "Proxy.Configuration",
335 proxy_simple, opt_start, 1);
339 if (strcmp(*opt_start, "auto") == 0) {
340 res = set_service_property(connection, message,
342 "Proxy.Configuration",
343 proxy_simple, opt_start, 1);
347 if (strcmp(*opt_start, "manual") == 0) {
348 char **url_start = &args[index + 2];
350 if (*url_start != NULL &&
353 url_start = &args[index + 3];
356 res = store_proxy_input(connection,
357 message, service_name,
363 res = remove_service(connection, message, service_name);
370 dbus_message_unref(message);
373 printf("Error '%s': %s\n", args[oldindex],
384 static int cmd_monitor(char *args[], int num, struct option *options)
391 c = parse_args(args[1], options);
394 monitor_connman_service(connection);
395 monitor_connman_technology(connection);
396 monitor_connman_manager(connection);
400 monitor_connman_service(connection);
404 monitor_connman_technology(connection);
408 monitor_connman_manager(connection);
418 static int cmd_exit(char *args[], int num, struct option *options)
423 static struct option service_options[] = {
424 {"properties", required_argument, 0, 'p'},
428 static const char *service_desc[] = {
429 "[<service>] (obsolete)",
433 static struct option config_options[] = {
434 {"nameservers", required_argument, 0, 'n'},
435 {"timeservers", required_argument, 0, 't'},
436 {"domains", required_argument, 0, 'd'},
437 {"ipv6", required_argument, 0, 'v'},
438 {"proxy", required_argument, 0, 'x'},
439 {"autoconnect", required_argument, 0, 'a'},
440 {"ipv4", required_argument, 0, 'i'},
441 {"remove", 0, 0, 'r'},
445 static const char *config_desc[] = {
446 "<dns1> [<dns2>] [<dns3>]",
447 "<ntp1> [<ntp2>] [...]",
448 "<domain1> [<domain2>] [...]",
449 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
450 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
451 " [exclude <exclude1> [<exclude2>] [...]]",
453 "off|dhcp|manual <address> <prefixlength> <gateway>",
458 static struct option monitor_options[] = {
459 {"services", no_argument, 0, 's'},
460 {"tech", no_argument, 0, 'c'},
461 {"manager", no_argument, 0, 'm'},
465 static const char *monitor_desc[] = {
466 " Monitor only services",
467 " Monitor only technologies",
468 " Monitor only manager interface",
472 static const struct {
474 const char *argument;
475 struct option *options;
476 const char **options_desc;
477 int (*func) (char *args[], int num, struct option *options);
480 { "enable", "<technology>|offline", NULL, NULL,
481 cmd_enable, "Enables given technology or offline mode" },
482 { "disable", "<technology>|offline", NULL, NULL,
483 cmd_disable, "Disables given technology or offline mode"},
484 { "state", NULL, NULL, NULL,
485 cmd_state, "Shows if the system is online or offline" },
486 { "services", "[<service>]", service_options, &service_desc[0],
487 cmd_services, "Display services" },
488 { "technologies", NULL, NULL, NULL,
489 cmd_technologies, "Display technologies" },
490 { "scan", "<technology>", NULL, NULL,
491 cmd_scan, "Scans for new services for given technology" },
492 { "connect", "<service>", NULL, NULL,
493 cmd_connect, "Connect a given service" },
494 { "disconnect", "<service>", NULL, NULL,
495 cmd_disconnect, "Disconnect a given service" },
496 { "config", "<service>", config_options, &config_desc[0],
497 cmd_config, "Set service configuration options" },
498 { "monitor", NULL, monitor_options, &monitor_desc[0],
499 cmd_monitor, "Monitor signals from interfaces" },
500 { "help", NULL, NULL, NULL,
501 cmd_help, "Show help" },
502 { "exit", NULL, NULL, NULL,
504 { "quit", NULL, NULL, NULL,
509 static int cmd_help(char *args[], int num, struct option *options)
513 for (i = 0; cmd_table[i].cmd != NULL; i++) {
514 const char *cmd = cmd_table[i].cmd;
515 const char *argument = cmd_table[i].argument;
516 const char *desc = cmd_table[i].desc;
518 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
519 argument != NULL? argument: "",
520 desc != NULL? desc: "");
522 if (cmd_table[i].options != NULL) {
523 for (j = 0; cmd_table[i].options[j].name != NULL;
525 const char *options_desc =
526 cmd_table[i].options_desc != NULL ?
527 cmd_table[i].options_desc[j]: "";
529 printf(" --%-12s%s\n",
530 cmd_table[i].options[j].name,
539 int commands(DBusConnection *connection, char *argv[], int argc)
543 for (i = 0; cmd_table[i].cmd != NULL; i++) {
544 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
545 cmd_table[i].func != NULL) {
546 result = cmd_table[i].func(argv, argc,
547 cmd_table[i].options);
549 printf("Error '%s': %s\n", argv[0],
558 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
560 DBusMessage *message = NULL;
563 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
564 strcmp(argv[0], "h") == 0) {
565 printf("Usage: connmanctl [[command] [args]]\n");
566 cmd_help(NULL, 0, NULL);
567 printf("\nNote: arguments and output are considered "
568 "EXPERIMENTAL for now.\n\n");
573 dbus_message_unref(message);
578 int commands_options(DBusConnection *connection, char *argv[], int argc)