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)
213 static int cmd_disconnect(char *args[], int num, struct option *options)
218 static int cmd_config(char *args[], int num, struct option *options)
220 int res = 0, index = 2, oldindex = 0;
223 DBusMessage *message;
227 service_name = args[1];
228 if (service_name == NULL)
231 while (index < num && args[index] != NULL) {
232 c = parse_args(args[index], options);
233 opt_start = &args[index + 1];
236 message = get_message(connection, "GetServices");
244 switch (parse_boolean(*opt_start)) {
256 res = set_service_property(connection, message,
257 service_name, "AutoConnect",
261 res = set_service_property(connection, message,
262 service_name, "IPv4.Configuration",
268 res = set_service_property(connection, message,
269 service_name, "IPv6.Configuration",
275 res = set_service_property(connection, message,
277 "Nameservers.Configuration",
281 res = set_service_property(connection, message,
283 "Timeservers.Configuration",
287 res = set_service_property(connection, message,
289 "Domains.Configuration",
293 if (*opt_start == NULL) {
298 if (strcmp(*opt_start, "direct") == 0) {
299 res = set_service_property(connection, message,
301 "Proxy.Configuration",
302 proxy_simple, opt_start, 1);
306 if (strcmp(*opt_start, "auto") == 0) {
307 res = set_service_property(connection, message,
309 "Proxy.Configuration",
310 proxy_simple, opt_start, 1);
314 if (strcmp(*opt_start, "manual") == 0) {
315 char **url_start = &args[index + 2];
317 if (*url_start != NULL &&
320 url_start = &args[index + 3];
323 res = store_proxy_input(connection,
324 message, service_name,
330 res = remove_service(connection, message, service_name);
337 dbus_message_unref(message);
340 printf("Error '%s': %s\n", args[oldindex],
351 static int cmd_monitor(char *args[], int num, struct option *options)
356 static int cmd_exit(char *args[], int num, struct option *options)
361 static struct option service_options[] = {
362 {"properties", required_argument, 0, 'p'},
366 static const char *service_desc[] = {
367 "[<service>] (obsolete)",
371 static struct option config_options[] = {
372 {"nameservers", required_argument, 0, 'n'},
373 {"timeservers", required_argument, 0, 't'},
374 {"domains", required_argument, 0, 'd'},
375 {"ipv6", required_argument, 0, 'v'},
376 {"proxy", required_argument, 0, 'x'},
377 {"autoconnect", required_argument, 0, 'a'},
378 {"ipv4", required_argument, 0, 'i'},
379 {"remove", 0, 0, 'r'},
383 static const char *config_desc[] = {
384 "<dns1> [<dns2>] [<dns3>]",
385 "<ntp1> [<ntp2>] [...]",
386 "<domain1> [<domain2>] [...]",
387 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
388 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
389 " [exclude <exclude1> [<exclude2>] [...]]",
391 "off|dhcp|manual <address> <prefixlength> <gateway>",
396 static struct option monitor_options[] = {
397 {"services", no_argument, 0, 's'},
398 {"tech", no_argument, 0, 'c'},
399 {"manager", no_argument, 0, 'm'},
403 static const char *monitor_desc[] = {
404 " Monitor only services",
405 " Monitor only technologies",
406 " Monitor only manager interface",
410 static const struct {
412 const char *argument;
413 struct option *options;
414 const char **options_desc;
415 int (*func) (char *args[], int num, struct option *options);
418 { "enable", "<technology>|offline", NULL, NULL,
419 cmd_enable, "Enables given technology or offline mode" },
420 { "disable", "<technology>|offline", NULL, NULL,
421 cmd_disable, "Disables given technology or offline mode"},
422 { "state", NULL, NULL, NULL,
423 cmd_state, "Shows if the system is online or offline" },
424 { "services", "[<service>]", service_options, &service_desc[0],
425 cmd_services, "Display services" },
426 { "technologies", NULL, NULL, NULL,
427 cmd_technologies, "Display technologies" },
428 { "scan", "<technology>", NULL, NULL,
429 cmd_scan, "Scans for new services for given technology" },
430 { "connect", "<service>", NULL, NULL,
431 cmd_connect, "Connect a given service" },
432 { "disconnect", "<service>", NULL, NULL,
433 cmd_disconnect, "Disconnect a given service" },
434 { "config", "<service>", config_options, &config_desc[0],
435 cmd_config, "Set service configuration options" },
436 { "monitor", NULL, monitor_options, &monitor_desc[0],
437 cmd_monitor, "Monitor signals from interfaces" },
438 { "help", NULL, NULL, NULL,
439 cmd_help, "Show help" },
440 { "exit", NULL, NULL, NULL,
442 { "quit", NULL, NULL, NULL,
447 static int cmd_help(char *args[], int num, struct option *options)
451 for (i = 0; cmd_table[i].cmd != NULL; i++) {
452 const char *cmd = cmd_table[i].cmd;
453 const char *argument = cmd_table[i].argument;
454 const char *desc = cmd_table[i].desc;
456 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
457 argument != NULL? argument: "",
458 desc != NULL? desc: "");
460 if (cmd_table[i].options != NULL) {
461 for (j = 0; cmd_table[i].options[j].name != NULL;
463 const char *options_desc =
464 cmd_table[i].options_desc != NULL ?
465 cmd_table[i].options_desc[j]: "";
467 printf(" --%-12s%s\n",
468 cmd_table[i].options[j].name,
477 int commands(DBusConnection *connection, char *argv[], int argc)
481 for (i = 0; cmd_table[i].cmd != NULL; i++) {
482 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
483 cmd_table[i].func != NULL) {
484 result = cmd_table[i].func(argv, argc,
485 cmd_table[i].options);
487 printf("Error '%s': %s\n", argv[0],
496 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
498 DBusMessage *message = NULL;
501 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
502 strcmp(argv[0], "h") == 0) {
503 printf("Usage: connmanctl [[command] [args]]\n");
504 cmd_help(NULL, 0, NULL);
505 printf("\nNote: arguments and output are considered "
506 "EXPERIMENTAL for now.\n\n");
507 } else if (strcmp(argv[0], "connect") == 0) {
509 fprintf(stderr, "Connect requires a service name or "
513 error = connect_service(connection,
514 strip_service_path(argv[1]));
516 printf("Connected to: %s\n",
517 strip_service_path(argv[1]));
518 } else if (strcmp(argv[0], "disconnect") == 0) {
520 fprintf(stderr, "Disconnect requires a service name or "
524 error = disconnect_service(connection,
525 strip_service_path(argv[1]));
527 printf("Disconnected from: %s\n",
528 strip_service_path(argv[1]));
529 } else if (strcmp(argv[0], "scan") == 0) {
531 fprintf(stderr, "Scan requires a service name or path, "
535 message = get_message(connection, "GetTechnologies");
539 error = scan_technology(connection, message, argv[1]);
540 } else if (strcmp(argv[0], "enable") == 0) {
542 fprintf(stderr, "Enable requires a technology name or "
543 "the argument 'offlinemode', see help\n");
545 } else if (strcmp(argv[1], "offlinemode") == 0) {
546 error = set_manager(connection, "OfflineMode", TRUE);
548 printf("OfflineMode is now enabled\n");
550 message = get_message(connection, "GetTechnologies");
554 error = set_technology(connection, message,
555 "Powered", argv[1], TRUE);
557 printf("Enabled %s technology\n", argv[1]);
559 } else if (strcmp(argv[0], "disable") == 0) {
561 fprintf(stderr, "Disable requires a technology name or "
562 "the argument 'offlinemode' see help\n");
564 } else if (strcmp(argv[1], "offlinemode") == 0) {
565 error = set_manager(connection, "OfflineMode", FALSE);
567 printf("OfflineMode is now disabled\n");
569 message = get_message(connection, "GetTechnologies");
573 error = set_technology(connection, message,
574 "Powered", argv[1], FALSE);
576 printf("Disabled %s technology\n", argv[1]);
582 dbus_message_unref(message);
587 int commands_options(DBusConnection *connection, char *argv[], int argc)
590 int option_index = 0;
592 if (strcmp(argv[0], "monitor") == 0) {
594 fprintf(stderr, "Too many arguments for monitor, "
599 error = monitor_connman(connection, "Service",
603 error = monitor_connman(connection, "Technology",
607 error = monitor_connman(connection, "Manager",
611 error = monitor_connman(connection, "Manager",
615 error = monitor_connman(connection, "Manager",
616 "TechnologyRemoved");
619 error = monitor_connman(connection, "Manager",
623 if (dbus_connection_add_filter(connection,
624 service_property_changed, NULL, NULL)
627 if (dbus_connection_add_filter(connection,
628 tech_property_changed, NULL, NULL)
631 if (dbus_connection_add_filter(connection,
632 tech_added_removed, NULL, NULL)
635 if (dbus_connection_add_filter(connection,
636 manager_property_changed, NULL, NULL)
639 if (dbus_connection_add_filter(connection,
640 manager_services_changed, NULL, NULL)
643 printf("Now monitoring all interfaces.\n");
645 while ((c = getopt_long(argc, argv, "", monitor_options,
648 if (option_index == 0) {
649 printf("Monitor takes an "
650 "option, see help\n");
655 error = monitor_switch(argc, argv, c, connection);