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 static int cmd_services(char *args[], int num, struct option *options)
158 char *service_name = NULL;
161 DBusMessage *message;
166 c = parse_args(args[1], options);
173 service_name = args[2];
178 service_name = args[1];
182 message = get_message(connection, "GetServices");
186 err = list_properties(connection, "GetServices", service_name);
187 dbus_message_unref(message);
192 static int cmd_technologies(char *args[], int num, struct option *options)
197 static int cmd_scan(char *args[], int num, struct option *options)
202 static int cmd_connect(char *args[], int num, struct option *options)
207 static int cmd_disconnect(char *args[], int num, struct option *options)
212 static int cmd_config(char *args[], int num, struct option *options)
214 int res = 0, index = 2, oldindex = 0;
217 DBusMessage *message;
221 service_name = args[1];
222 if (service_name == NULL)
225 while (index < num && args[index] != NULL) {
226 c = parse_args(args[index], options);
227 opt_start = &args[index + 1];
230 message = get_message(connection, "GetServices");
238 switch (parse_boolean(*opt_start)) {
250 res = set_service_property(connection, message,
251 service_name, "AutoConnect",
255 res = set_service_property(connection, message,
256 service_name, "IPv4.Configuration",
262 res = set_service_property(connection, message,
263 service_name, "IPv6.Configuration",
269 res = set_service_property(connection, message,
271 "Nameservers.Configuration",
275 res = set_service_property(connection, message,
277 "Timeservers.Configuration",
281 res = set_service_property(connection, message,
283 "Domains.Configuration",
287 if (*opt_start == NULL) {
292 if (strcmp(*opt_start, "direct") == 0) {
293 res = set_service_property(connection, message,
295 "Proxy.Configuration",
296 proxy_simple, opt_start, 1);
300 if (strcmp(*opt_start, "auto") == 0) {
301 res = set_service_property(connection, message,
303 "Proxy.Configuration",
304 proxy_simple, opt_start, 1);
308 if (strcmp(*opt_start, "manual") == 0) {
309 char **url_start = &args[index + 2];
311 if (*url_start != NULL &&
314 url_start = &args[index + 3];
317 res = store_proxy_input(connection,
318 message, service_name,
324 res = remove_service(connection, message, service_name);
331 dbus_message_unref(message);
334 printf("Error '%s': %s\n", args[oldindex],
345 static int cmd_monitor(char *args[], int num, struct option *options)
350 static int cmd_exit(char *args[], int num, struct option *options)
355 static struct option service_options[] = {
356 {"properties", required_argument, 0, 'p'},
360 static const char *service_desc[] = {
361 "[<service>] (obsolete)",
365 static struct option config_options[] = {
366 {"nameservers", required_argument, 0, 'n'},
367 {"timeservers", required_argument, 0, 't'},
368 {"domains", required_argument, 0, 'd'},
369 {"ipv6", required_argument, 0, 'v'},
370 {"proxy", required_argument, 0, 'x'},
371 {"autoconnect", required_argument, 0, 'a'},
372 {"ipv4", required_argument, 0, 'i'},
373 {"remove", 0, 0, 'r'},
377 static const char *config_desc[] = {
378 "<dns1> [<dns2>] [<dns3>]",
379 "<ntp1> [<ntp2>] [...]",
380 "<domain1> [<domain2>] [...]",
381 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
382 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
383 " [exclude <exclude1> [<exclude2>] [...]]",
385 "off|dhcp|manual <address> <prefixlength> <gateway>",
390 static struct option monitor_options[] = {
391 {"services", no_argument, 0, 's'},
392 {"tech", no_argument, 0, 'c'},
393 {"manager", no_argument, 0, 'm'},
397 static const char *monitor_desc[] = {
398 " Monitor only services",
399 " Monitor only technologies",
400 " Monitor only manager interface",
404 static const struct {
406 const char *argument;
407 struct option *options;
408 const char **options_desc;
409 int (*func) (char *args[], int num, struct option *options);
412 { "enable", "<technology>|offline", NULL, NULL,
413 cmd_enable, "Enables given technology or offline mode" },
414 { "disable", "<technology>|offline", NULL, NULL,
415 cmd_disable, "Disables given technology or offline mode"},
416 { "state", NULL, NULL, NULL,
417 cmd_state, "Shows if the system is online or offline" },
418 { "services", "[<service>]", service_options, &service_desc[0],
419 cmd_services, "Display services" },
420 { "technologies", NULL, NULL, NULL,
421 cmd_technologies, "Display technologies" },
422 { "scan", "<technology>", NULL, NULL,
423 cmd_scan, "Scans for new services for given technology" },
424 { "connect", "<service>", NULL, NULL,
425 cmd_connect, "Connect a given service" },
426 { "disconnect", "<service>", NULL, NULL,
427 cmd_disconnect, "Disconnect a given service" },
428 { "config", "<service>", config_options, &config_desc[0],
429 cmd_config, "Set service configuration options" },
430 { "monitor", NULL, monitor_options, &monitor_desc[0],
431 cmd_monitor, "Monitor signals from interfaces" },
432 { "help", NULL, NULL, NULL,
433 cmd_help, "Show help" },
434 { "exit", NULL, NULL, NULL,
436 { "quit", NULL, NULL, NULL,
441 static int cmd_help(char *args[], int num, struct option *options)
445 for (i = 0; cmd_table[i].cmd != NULL; i++) {
446 const char *cmd = cmd_table[i].cmd;
447 const char *argument = cmd_table[i].argument;
448 const char *desc = cmd_table[i].desc;
450 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
451 argument != NULL? argument: "",
452 desc != NULL? desc: "");
454 if (cmd_table[i].options != NULL) {
455 for (j = 0; cmd_table[i].options[j].name != NULL;
457 const char *options_desc =
458 cmd_table[i].options_desc != NULL ?
459 cmd_table[i].options_desc[j]: "";
461 printf(" --%-12s%s\n",
462 cmd_table[i].options[j].name,
471 int commands(DBusConnection *connection, char *argv[], int argc)
475 for (i = 0; cmd_table[i].cmd != NULL; i++) {
476 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
477 cmd_table[i].func != NULL) {
478 return cmd_table[i].func(argv, argc,
479 cmd_table[i].options);
486 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
488 DBusMessage *message = NULL;
491 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
492 strcmp(argv[0], "h") == 0) {
493 printf("Usage: connmanctl [[command] [args]]\n");
494 cmd_help(NULL, 0, NULL);
495 printf("\nNote: arguments and output are considered "
496 "EXPERIMENTAL for now.\n\n");
497 } else if (strcmp(argv[0], "state") == 0) {
499 fprintf(stderr, "State cannot accept an argument, "
503 error = list_properties(connection,
504 "GetProperties", NULL);
505 } else if (strcmp(argv[0], "technologies") == 0) {
507 fprintf(stderr, "Tech cannot accept an argument, "
511 error = list_properties(connection,
512 "GetTechnologies", NULL);
513 } else if (strcmp(argv[0], "connect") == 0) {
515 fprintf(stderr, "Connect requires a service name or "
519 error = connect_service(connection,
520 strip_service_path(argv[1]));
522 printf("Connected to: %s\n",
523 strip_service_path(argv[1]));
524 } else if (strcmp(argv[0], "disconnect") == 0) {
526 fprintf(stderr, "Disconnect requires a service name or "
530 error = disconnect_service(connection,
531 strip_service_path(argv[1]));
533 printf("Disconnected from: %s\n",
534 strip_service_path(argv[1]));
535 } else if (strcmp(argv[0], "scan") == 0) {
537 fprintf(stderr, "Scan requires a service name or path, "
541 message = get_message(connection, "GetTechnologies");
545 error = scan_technology(connection, message, argv[1]);
546 } else if (strcmp(argv[0], "enable") == 0) {
548 fprintf(stderr, "Enable requires a technology name or "
549 "the argument 'offlinemode', see help\n");
551 } else if (strcmp(argv[1], "offlinemode") == 0) {
552 error = set_manager(connection, "OfflineMode", TRUE);
554 printf("OfflineMode is now enabled\n");
556 message = get_message(connection, "GetTechnologies");
560 error = set_technology(connection, message,
561 "Powered", argv[1], TRUE);
563 printf("Enabled %s technology\n", argv[1]);
565 } else if (strcmp(argv[0], "disable") == 0) {
567 fprintf(stderr, "Disable requires a technology name or "
568 "the argument 'offlinemode' see help\n");
570 } else if (strcmp(argv[1], "offlinemode") == 0) {
571 error = set_manager(connection, "OfflineMode", FALSE);
573 printf("OfflineMode is now disabled\n");
575 message = get_message(connection, "GetTechnologies");
579 error = set_technology(connection, message,
580 "Powered", argv[1], FALSE);
582 printf("Disabled %s technology\n", argv[1]);
588 dbus_message_unref(message);
593 int commands_options(DBusConnection *connection, char *argv[], int argc)
596 int option_index = 0;
598 if (strcmp(argv[0], "monitor") == 0) {
600 fprintf(stderr, "Too many arguments for monitor, "
605 error = monitor_connman(connection, "Service",
609 error = monitor_connman(connection, "Technology",
613 error = monitor_connman(connection, "Manager",
617 error = monitor_connman(connection, "Manager",
621 error = monitor_connman(connection, "Manager",
622 "TechnologyRemoved");
625 error = monitor_connman(connection, "Manager",
629 if (dbus_connection_add_filter(connection,
630 service_property_changed, NULL, NULL)
633 if (dbus_connection_add_filter(connection,
634 tech_property_changed, NULL, NULL)
637 if (dbus_connection_add_filter(connection,
638 tech_added_removed, NULL, NULL)
641 if (dbus_connection_add_filter(connection,
642 manager_property_changed, NULL, NULL)
645 if (dbus_connection_add_filter(connection,
646 manager_services_changed, NULL, NULL)
649 printf("Now monitoring all interfaces.\n");
651 while ((c = getopt_long(argc, argv, "", monitor_options,
654 if (option_index == 0) {
655 printf("Monitor takes an "
656 "option, see help\n");
661 error = monitor_switch(argc, argv, c, connection);