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)
230 static int cmd_config(char *args[], int num, struct option *options)
232 int res = 0, index = 2, oldindex = 0;
235 DBusMessage *message;
239 service_name = args[1];
240 if (service_name == NULL)
243 while (index < num && args[index] != NULL) {
244 c = parse_args(args[index], options);
245 opt_start = &args[index + 1];
248 message = get_message(connection, "GetServices");
256 switch (parse_boolean(*opt_start)) {
268 res = set_service_property(connection, message,
269 service_name, "AutoConnect",
273 res = set_service_property(connection, message,
274 service_name, "IPv4.Configuration",
280 res = set_service_property(connection, message,
281 service_name, "IPv6.Configuration",
287 res = set_service_property(connection, message,
289 "Nameservers.Configuration",
293 res = set_service_property(connection, message,
295 "Timeservers.Configuration",
299 res = set_service_property(connection, message,
301 "Domains.Configuration",
305 if (*opt_start == NULL) {
310 if (strcmp(*opt_start, "direct") == 0) {
311 res = set_service_property(connection, message,
313 "Proxy.Configuration",
314 proxy_simple, opt_start, 1);
318 if (strcmp(*opt_start, "auto") == 0) {
319 res = set_service_property(connection, message,
321 "Proxy.Configuration",
322 proxy_simple, opt_start, 1);
326 if (strcmp(*opt_start, "manual") == 0) {
327 char **url_start = &args[index + 2];
329 if (*url_start != NULL &&
332 url_start = &args[index + 3];
335 res = store_proxy_input(connection,
336 message, service_name,
342 res = remove_service(connection, message, service_name);
349 dbus_message_unref(message);
352 printf("Error '%s': %s\n", args[oldindex],
363 static int cmd_monitor(char *args[], int num, struct option *options)
368 static int cmd_exit(char *args[], int num, struct option *options)
373 static struct option service_options[] = {
374 {"properties", required_argument, 0, 'p'},
378 static const char *service_desc[] = {
379 "[<service>] (obsolete)",
383 static struct option config_options[] = {
384 {"nameservers", required_argument, 0, 'n'},
385 {"timeservers", required_argument, 0, 't'},
386 {"domains", required_argument, 0, 'd'},
387 {"ipv6", required_argument, 0, 'v'},
388 {"proxy", required_argument, 0, 'x'},
389 {"autoconnect", required_argument, 0, 'a'},
390 {"ipv4", required_argument, 0, 'i'},
391 {"remove", 0, 0, 'r'},
395 static const char *config_desc[] = {
396 "<dns1> [<dns2>] [<dns3>]",
397 "<ntp1> [<ntp2>] [...]",
398 "<domain1> [<domain2>] [...]",
399 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
400 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
401 " [exclude <exclude1> [<exclude2>] [...]]",
403 "off|dhcp|manual <address> <prefixlength> <gateway>",
408 static struct option monitor_options[] = {
409 {"services", no_argument, 0, 's'},
410 {"tech", no_argument, 0, 'c'},
411 {"manager", no_argument, 0, 'm'},
415 static const char *monitor_desc[] = {
416 " Monitor only services",
417 " Monitor only technologies",
418 " Monitor only manager interface",
422 static const struct {
424 const char *argument;
425 struct option *options;
426 const char **options_desc;
427 int (*func) (char *args[], int num, struct option *options);
430 { "enable", "<technology>|offline", NULL, NULL,
431 cmd_enable, "Enables given technology or offline mode" },
432 { "disable", "<technology>|offline", NULL, NULL,
433 cmd_disable, "Disables given technology or offline mode"},
434 { "state", NULL, NULL, NULL,
435 cmd_state, "Shows if the system is online or offline" },
436 { "services", "[<service>]", service_options, &service_desc[0],
437 cmd_services, "Display services" },
438 { "technologies", NULL, NULL, NULL,
439 cmd_technologies, "Display technologies" },
440 { "scan", "<technology>", NULL, NULL,
441 cmd_scan, "Scans for new services for given technology" },
442 { "connect", "<service>", NULL, NULL,
443 cmd_connect, "Connect a given service" },
444 { "disconnect", "<service>", NULL, NULL,
445 cmd_disconnect, "Disconnect a given service" },
446 { "config", "<service>", config_options, &config_desc[0],
447 cmd_config, "Set service configuration options" },
448 { "monitor", NULL, monitor_options, &monitor_desc[0],
449 cmd_monitor, "Monitor signals from interfaces" },
450 { "help", NULL, NULL, NULL,
451 cmd_help, "Show help" },
452 { "exit", NULL, NULL, NULL,
454 { "quit", NULL, NULL, NULL,
459 static int cmd_help(char *args[], int num, struct option *options)
463 for (i = 0; cmd_table[i].cmd != NULL; i++) {
464 const char *cmd = cmd_table[i].cmd;
465 const char *argument = cmd_table[i].argument;
466 const char *desc = cmd_table[i].desc;
468 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
469 argument != NULL? argument: "",
470 desc != NULL? desc: "");
472 if (cmd_table[i].options != NULL) {
473 for (j = 0; cmd_table[i].options[j].name != NULL;
475 const char *options_desc =
476 cmd_table[i].options_desc != NULL ?
477 cmd_table[i].options_desc[j]: "";
479 printf(" --%-12s%s\n",
480 cmd_table[i].options[j].name,
489 int commands(DBusConnection *connection, char *argv[], int argc)
493 for (i = 0; cmd_table[i].cmd != NULL; i++) {
494 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
495 cmd_table[i].func != NULL) {
496 result = cmd_table[i].func(argv, argc,
497 cmd_table[i].options);
499 printf("Error '%s': %s\n", argv[0],
508 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
510 DBusMessage *message = NULL;
513 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
514 strcmp(argv[0], "h") == 0) {
515 printf("Usage: connmanctl [[command] [args]]\n");
516 cmd_help(NULL, 0, NULL);
517 printf("\nNote: arguments and output are considered "
518 "EXPERIMENTAL for now.\n\n");
519 } else if (strcmp(argv[0], "disconnect") == 0) {
521 fprintf(stderr, "Disconnect requires a service name or "
525 error = disconnect_service(connection,
526 strip_service_path(argv[1]));
528 printf("Disconnected from: %s\n",
529 strip_service_path(argv[1]));
530 } else if (strcmp(argv[0], "scan") == 0) {
532 fprintf(stderr, "Scan requires a service name or path, "
536 message = get_message(connection, "GetTechnologies");
540 error = scan_technology(connection, message, argv[1]);
541 } else if (strcmp(argv[0], "enable") == 0) {
543 fprintf(stderr, "Enable requires a technology name or "
544 "the argument 'offlinemode', see help\n");
546 } else if (strcmp(argv[1], "offlinemode") == 0) {
547 error = set_manager(connection, "OfflineMode", TRUE);
549 printf("OfflineMode is now enabled\n");
551 message = get_message(connection, "GetTechnologies");
555 error = set_technology(connection, message,
556 "Powered", argv[1], TRUE);
558 printf("Enabled %s technology\n", argv[1]);
560 } else if (strcmp(argv[0], "disable") == 0) {
562 fprintf(stderr, "Disable requires a technology name or "
563 "the argument 'offlinemode' see help\n");
565 } else if (strcmp(argv[1], "offlinemode") == 0) {
566 error = set_manager(connection, "OfflineMode", FALSE);
568 printf("OfflineMode is now disabled\n");
570 message = get_message(connection, "GetTechnologies");
574 error = set_technology(connection, message,
575 "Powered", argv[1], FALSE);
577 printf("Disabled %s technology\n", argv[1]);
583 dbus_message_unref(message);
588 int commands_options(DBusConnection *connection, char *argv[], int argc)
591 int option_index = 0;
593 if (strcmp(argv[0], "monitor") == 0) {
595 fprintf(stderr, "Too many arguments for monitor, "
600 error = monitor_connman(connection, "Service",
604 error = monitor_connman(connection, "Technology",
608 error = monitor_connman(connection, "Manager",
612 error = monitor_connman(connection, "Manager",
616 error = monitor_connman(connection, "Manager",
617 "TechnologyRemoved");
620 error = monitor_connman(connection, "Manager",
624 if (dbus_connection_add_filter(connection,
625 service_property_changed, NULL, NULL)
628 if (dbus_connection_add_filter(connection,
629 tech_property_changed, NULL, NULL)
632 if (dbus_connection_add_filter(connection,
633 tech_added_removed, NULL, NULL)
636 if (dbus_connection_add_filter(connection,
637 manager_property_changed, NULL, NULL)
640 if (dbus_connection_add_filter(connection,
641 manager_services_changed, NULL, NULL)
644 printf("Now monitoring all interfaces.\n");
646 while ((c = getopt_long(argc, argv, "", monitor_options,
649 if (option_index == 0) {
650 printf("Monitor takes an "
651 "option, see help\n");
656 error = monitor_switch(argc, argv, c, connection);