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 config_switch(int argc, char *argv[], int c, DBusConnection *conn)
89 int num_args = argc - MANDATORY_ARGS;
93 message = get_message(conn, "GetServices");
113 error = set_service_property(conn, message, argv[1],
118 error = set_service_property(conn, message, argv[1],
119 "IPv4.Configuration", ipv4,
120 argv + MANDATORY_ARGS, num_args);
123 error = set_service_property(conn, message, argv[1],
124 "IPv6.Configuration", ipv6,
125 argv + MANDATORY_ARGS, num_args);
128 error = set_service_property(conn, message, argv[1],
129 "Nameservers.Configuration", NULL,
130 argv + MANDATORY_ARGS, num_args);
133 error = set_service_property(conn, message, argv[1],
134 "Timeservers.Configuration", NULL,
135 argv + MANDATORY_ARGS, num_args);
138 error = set_service_property(conn, message, argv[1],
139 "Domains.Configuration", NULL,
140 argv + MANDATORY_ARGS, num_args);
143 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
144 (strcmp(argv[3], "auto") == 0 && argc < 6)) {
145 error = set_service_property(conn, message, argv[1],
146 "Proxy.Configuration", proxy_simple,
147 argv + MANDATORY_ARGS, num_args);
148 } else if (strcmp(argv[3], "manual") == 0
149 && strcmp(argv[4], "servers") == 0
152 error = store_proxy_input(conn, message, argv[1],
155 fprintf(stderr, "Incorrect arguments\n");
160 error = remove_service(conn, message, argv[1]);
163 fprintf(stderr, "Command not recognized, please check help\n");
168 dbus_message_unref(message);
173 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
179 error = monitor_connman(conn, "Service", "PropertyChanged");
182 if (dbus_connection_add_filter(conn, service_property_changed,
183 NULL, NULL) == FALSE)
185 printf("Now monitoring the service interface.\n");
188 error = monitor_connman(conn, "Technology", "PropertyChanged");
191 if (dbus_connection_add_filter(conn, tech_property_changed,
192 NULL, NULL) == FALSE)
194 printf("Now monitoring the technology interface.\n");
197 error = monitor_connman(conn, "Manager", "PropertyChanged");
200 error = monitor_connman(conn, "Manager", "TechnologyAdded");
203 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
206 error = monitor_connman(conn, "Manager", "ServicesChanged");
209 if (dbus_connection_add_filter(conn, manager_property_changed,
210 NULL, NULL) == FALSE)
212 if (dbus_connection_add_filter(conn, tech_added_removed,
213 NULL, NULL) == FALSE)
215 if (dbus_connection_add_filter(conn, manager_services_changed,
216 NULL, NULL) == FALSE)
218 printf("Now monitoring the manager interface.\n");
221 fprintf(stderr, "Command not recognized, please check help\n");
228 static int cmd_enable(char *args[], int num, struct option *options)
233 static int cmd_disable(char *args[], int num, struct option *options)
238 static int cmd_state(char *args[], int num, struct option *options)
243 static int cmd_services(char *args[], int num, struct option *options)
245 char *service_name = NULL;
248 DBusMessage *message;
253 c = parse_args(args[1], options);
260 service_name = args[2];
265 service_name = args[1];
269 message = get_message(connection, "GetServices");
273 err = list_properties(connection, "GetServices", service_name);
274 dbus_message_unref(message);
279 static int cmd_technologies(char *args[], int num, struct option *options)
284 static int cmd_scan(char *args[], int num, struct option *options)
289 static int cmd_connect(char *args[], int num, struct option *options)
294 static int cmd_disconnect(char *args[], int num, struct option *options)
299 static int cmd_config(char *args[], int num, struct option *options)
304 static int cmd_monitor(char *args[], int num, struct option *options)
309 static int cmd_exit(char *args[], int num, struct option *options)
314 static struct option service_options[] = {
315 {"properties", required_argument, 0, 'p'},
319 static const char *service_desc[] = {
320 "[<service>] (obsolete)",
324 static struct option config_options[] = {
325 {"nameservers", required_argument, 0, 'n'},
326 {"timeservers", required_argument, 0, 't'},
327 {"domains", required_argument, 0, 'd'},
328 {"ipv6", required_argument, 0, 'v'},
329 {"proxy", required_argument, 0, 'x'},
330 {"autoconnect", required_argument, 0, 'a'},
331 {"ipv4", required_argument, 0, 'i'},
332 {"remove", 0, 0, 'r'},
336 static const char *config_desc[] = {
337 "<dns1> [<dns2>] [<dns3>]",
338 "<ntp1> [<ntp2>] [...]",
339 "<domain1> [<domain2>] [...]",
340 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
341 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
342 " [exclude <exclude1> [<exclude2>] [...]]",
344 "off|dhcp|manual <address> <prefixlength> <gateway>",
349 static struct option monitor_options[] = {
350 {"services", no_argument, 0, 's'},
351 {"tech", no_argument, 0, 'c'},
352 {"manager", no_argument, 0, 'm'},
356 static const char *monitor_desc[] = {
357 " Monitor only services",
358 " Monitor only technologies",
359 " Monitor only manager interface",
363 static const struct {
365 const char *argument;
366 struct option *options;
367 const char **options_desc;
368 int (*func) (char *args[], int num, struct option *options);
371 { "enable", "<technology>|offline", NULL, NULL,
372 cmd_enable, "Enables given technology or offline mode" },
373 { "disable", "<technology>|offline", NULL, NULL,
374 cmd_disable, "Disables given technology or offline mode"},
375 { "state", NULL, NULL, NULL,
376 cmd_state, "Shows if the system is online or offline" },
377 { "services", "[<service>]", service_options, &service_desc[0],
378 cmd_services, "Display services" },
379 { "technologies", NULL, NULL, NULL,
380 cmd_technologies, "Display technologies" },
381 { "scan", "<technology>", NULL, NULL,
382 cmd_scan, "Scans for new services for given technology" },
383 { "connect", "<service>", NULL, NULL,
384 cmd_connect, "Connect a given service" },
385 { "disconnect", "<service>", NULL, NULL,
386 cmd_disconnect, "Disconnect a given service" },
387 { "config", "<service>", config_options, &config_desc[0],
388 cmd_config, "Set service configuration options" },
389 { "monitor", NULL, monitor_options, &monitor_desc[0],
390 cmd_monitor, "Monitor signals from interfaces" },
391 { "help", NULL, NULL, NULL,
392 cmd_help, "Show help" },
393 { "exit", NULL, NULL, NULL,
395 { "quit", NULL, NULL, NULL,
400 static int cmd_help(char *args[], int num, struct option *options)
404 for (i = 0; cmd_table[i].cmd != NULL; i++) {
405 const char *cmd = cmd_table[i].cmd;
406 const char *argument = cmd_table[i].argument;
407 const char *desc = cmd_table[i].desc;
409 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
410 argument != NULL? argument: "",
411 desc != NULL? desc: "");
413 if (cmd_table[i].options != NULL) {
414 for (j = 0; cmd_table[i].options[j].name != NULL;
416 const char *options_desc =
417 cmd_table[i].options_desc != NULL ?
418 cmd_table[i].options_desc[j]: "";
420 printf(" --%-12s%s\n",
421 cmd_table[i].options[j].name,
430 int commands(DBusConnection *connection, char *argv[], int argc)
434 for (i = 0; cmd_table[i].cmd != NULL; i++) {
435 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
436 cmd_table[i].func != NULL) {
437 return cmd_table[i].func(argv, argc,
438 cmd_table[i].options);
445 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
447 DBusMessage *message = NULL;
450 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
451 strcmp(argv[0], "h") == 0) {
452 printf("Usage: connmanctl [[command] [args]]\n");
453 cmd_help(NULL, 0, NULL);
454 printf("\nNote: arguments and output are considered "
455 "EXPERIMENTAL for now.\n\n");
456 } else if (strcmp(argv[0], "state") == 0) {
458 fprintf(stderr, "State cannot accept an argument, "
462 error = list_properties(connection,
463 "GetProperties", NULL);
464 } else if (strcmp(argv[0], "technologies") == 0) {
466 fprintf(stderr, "Tech cannot accept an argument, "
470 error = list_properties(connection,
471 "GetTechnologies", NULL);
472 } else if (strcmp(argv[0], "connect") == 0) {
474 fprintf(stderr, "Connect requires a service name or "
478 error = connect_service(connection,
479 strip_service_path(argv[1]));
481 printf("Connected to: %s\n",
482 strip_service_path(argv[1]));
483 } else if (strcmp(argv[0], "disconnect") == 0) {
485 fprintf(stderr, "Disconnect requires a service name or "
489 error = disconnect_service(connection,
490 strip_service_path(argv[1]));
492 printf("Disconnected from: %s\n",
493 strip_service_path(argv[1]));
494 } else if (strcmp(argv[0], "scan") == 0) {
496 fprintf(stderr, "Scan requires a service name or path, "
500 message = get_message(connection, "GetTechnologies");
504 error = scan_technology(connection, message, argv[1]);
505 } else if (strcmp(argv[0], "enable") == 0) {
507 fprintf(stderr, "Enable requires a technology name or "
508 "the argument 'offlinemode', see help\n");
510 } else if (strcmp(argv[1], "offlinemode") == 0) {
511 error = set_manager(connection, "OfflineMode", TRUE);
513 printf("OfflineMode is now enabled\n");
515 message = get_message(connection, "GetTechnologies");
519 error = set_technology(connection, message,
520 "Powered", argv[1], TRUE);
522 printf("Enabled %s technology\n", argv[1]);
524 } else if (strcmp(argv[0], "disable") == 0) {
526 fprintf(stderr, "Disable requires a technology name or "
527 "the argument 'offlinemode' see help\n");
529 } else if (strcmp(argv[1], "offlinemode") == 0) {
530 error = set_manager(connection, "OfflineMode", FALSE);
532 printf("OfflineMode is now disabled\n");
534 message = get_message(connection, "GetTechnologies");
538 error = set_technology(connection, message,
539 "Powered", argv[1], FALSE);
541 printf("Disabled %s technology\n", argv[1]);
547 dbus_message_unref(message);
552 int commands_options(DBusConnection *connection, char *argv[], int argc)
555 int option_index = 0;
557 if (strcmp(argv[0], "config") == 0) {
559 fprintf(stderr, "Config requires an option, "
563 while ((c = getopt_long(argc, argv, "", config_options,
566 if (option_index == 0) {
567 printf("Config requires an option, "
573 error = config_switch(argc, argv, c, connection);
578 } else if (strcmp(argv[0], "monitor") == 0) {
580 fprintf(stderr, "Too many arguments for monitor, "
585 error = monitor_connman(connection, "Service",
589 error = monitor_connman(connection, "Technology",
593 error = monitor_connman(connection, "Manager",
597 error = monitor_connman(connection, "Manager",
601 error = monitor_connman(connection, "Manager",
602 "TechnologyRemoved");
605 error = monitor_connman(connection, "Manager",
609 if (dbus_connection_add_filter(connection,
610 service_property_changed, NULL, NULL)
613 if (dbus_connection_add_filter(connection,
614 tech_property_changed, NULL, NULL)
617 if (dbus_connection_add_filter(connection,
618 tech_added_removed, NULL, NULL)
621 if (dbus_connection_add_filter(connection,
622 manager_property_changed, NULL, NULL)
625 if (dbus_connection_add_filter(connection,
626 manager_services_changed, NULL, NULL)
629 printf("Now monitoring all interfaces.\n");
631 while ((c = getopt_long(argc, argv, "", monitor_options,
634 if (option_index == 0) {
635 printf("Monitor takes an "
636 "option, see help\n");
641 error = monitor_switch(argc, argv, c, connection);