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 int service_switch(int argc, char *argv[], int c, DBusConnection *conn,
70 struct service_data *service)
76 message = get_message(conn, "GetServices");
82 name = find_service(conn, message, argv[2], service);
88 error = list_properties(conn, "GetServices", (char *) name);
91 fprintf(stderr, "Command not recognized, please check help\n");
96 dbus_message_unref(message);
101 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
103 DBusMessage *message;
104 int num_args = argc - MANDATORY_ARGS;
108 message = get_message(conn, "GetServices");
128 error = set_service_property(conn, message, argv[1],
133 error = set_service_property(conn, message, argv[1],
134 "IPv4.Configuration", ipv4,
135 argv + MANDATORY_ARGS, num_args);
138 error = set_service_property(conn, message, argv[1],
139 "IPv6.Configuration", ipv6,
140 argv + MANDATORY_ARGS, num_args);
143 error = set_service_property(conn, message, argv[1],
144 "Nameservers.Configuration", NULL,
145 argv + MANDATORY_ARGS, num_args);
148 error = set_service_property(conn, message, argv[1],
149 "Timeservers.Configuration", NULL,
150 argv + MANDATORY_ARGS, num_args);
153 error = set_service_property(conn, message, argv[1],
154 "Domains.Configuration", NULL,
155 argv + MANDATORY_ARGS, num_args);
158 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
159 (strcmp(argv[3], "auto") == 0 && argc < 6)) {
160 error = set_service_property(conn, message, argv[1],
161 "Proxy.Configuration", proxy_simple,
162 argv + MANDATORY_ARGS, num_args);
163 } else if (strcmp(argv[3], "manual") == 0
164 && strcmp(argv[4], "servers") == 0
167 error = store_proxy_input(conn, message, argv[1],
170 fprintf(stderr, "Incorrect arguments\n");
175 error = remove_service(conn, message, argv[1]);
178 fprintf(stderr, "Command not recognized, please check help\n");
183 dbus_message_unref(message);
188 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
194 error = monitor_connman(conn, "Service", "PropertyChanged");
197 if (dbus_connection_add_filter(conn, service_property_changed,
198 NULL, NULL) == FALSE)
200 printf("Now monitoring the service interface.\n");
203 error = monitor_connman(conn, "Technology", "PropertyChanged");
206 if (dbus_connection_add_filter(conn, tech_property_changed,
207 NULL, NULL) == FALSE)
209 printf("Now monitoring the technology interface.\n");
212 error = monitor_connman(conn, "Manager", "PropertyChanged");
215 error = monitor_connman(conn, "Manager", "TechnologyAdded");
218 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
221 error = monitor_connman(conn, "Manager", "ServicesChanged");
224 if (dbus_connection_add_filter(conn, manager_property_changed,
225 NULL, NULL) == FALSE)
227 if (dbus_connection_add_filter(conn, tech_added_removed,
228 NULL, NULL) == FALSE)
230 if (dbus_connection_add_filter(conn, manager_services_changed,
231 NULL, NULL) == FALSE)
233 printf("Now monitoring the manager interface.\n");
236 fprintf(stderr, "Command not recognized, please check help\n");
243 static int cmd_enable(char *args[], int num, struct option *options)
248 static int cmd_disable(char *args[], int num, struct option *options)
253 static int cmd_state(char *args[], int num, struct option *options)
258 static int cmd_services(char *args[], int num, struct option *options)
263 static int cmd_technologies(char *args[], int num, struct option *options)
268 static int cmd_scan(char *args[], int num, struct option *options)
273 static int cmd_connect(char *args[], int num, struct option *options)
278 static int cmd_disconnect(char *args[], int num, struct option *options)
283 static int cmd_config(char *args[], int num, struct option *options)
288 static int cmd_monitor(char *args[], int num, struct option *options)
293 static int cmd_exit(char *args[], int num, struct option *options)
298 static struct option service_options[] = {
299 {"properties", required_argument, 0, 'p'},
303 static const char *service_desc[] = {
304 "<service> Show properties for service",
308 static struct option config_options[] = {
309 {"nameservers", required_argument, 0, 'n'},
310 {"timeservers", required_argument, 0, 't'},
311 {"domains", required_argument, 0, 'd'},
312 {"ipv6", required_argument, 0, 'v'},
313 {"proxy", required_argument, 0, 'x'},
314 {"autoconnect", required_argument, 0, 'a'},
315 {"ipv4", required_argument, 0, 'i'},
316 {"remove", 0, 0, 'r'},
320 static const char *config_desc[] = {
321 "<dns1> [<dns2>] [<dns3>]",
322 "<ntp1> [<ntp2>] [...]",
323 "<domain1> [<domain2>] [...]",
324 "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
325 "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
326 " [exclude <exclude1> [<exclude2>] [...]]",
328 "off|dhcp|manual <address> <prefixlength> <gateway>",
333 static struct option monitor_options[] = {
334 {"services", no_argument, 0, 's'},
335 {"tech", no_argument, 0, 'c'},
336 {"manager", no_argument, 0, 'm'},
340 static const char *monitor_desc[] = {
341 " Monitor only services",
342 " Monitor only technologies",
343 " Monitor only manager interface",
347 static const struct {
349 const char *argument;
350 struct option *options;
351 const char **options_desc;
352 int (*func) (char *args[], int num, struct option *options);
355 { "enable", "<technology>|offline", NULL, NULL,
356 cmd_enable, "Enables given technology or offline mode" },
357 { "disable", "<technology>|offline", NULL, NULL,
358 cmd_disable, "Disables given technology or offline mode"},
359 { "state", NULL, NULL, NULL,
360 cmd_state, "Shows if the system is online or offline" },
361 { "services", NULL, service_options, &service_desc[0],
362 cmd_services, "Display services" },
363 { "technologies", NULL, NULL, NULL,
364 cmd_technologies, "Display technologies" },
365 { "scan", "<technology>", NULL, NULL,
366 cmd_scan, "Scans for new services for given technology" },
367 { "connect", "<service>", NULL, NULL,
368 cmd_connect, "Connect a given service" },
369 { "disconnect", "<service>", NULL, NULL,
370 cmd_disconnect, "Disconnect a given service" },
371 { "config", "<service>", config_options, &config_desc[0],
372 cmd_config, "Set service configuration options" },
373 { "monitor", NULL, monitor_options, &monitor_desc[0],
374 cmd_monitor, "Monitor signals from interfaces" },
375 { "help", NULL, NULL, NULL,
376 cmd_help, "Show help" },
377 { "exit", NULL, NULL, NULL,
379 { "quit", NULL, NULL, NULL,
384 static int cmd_help(char *args[], int num, struct option *options)
388 for (i = 0; cmd_table[i].cmd != NULL; i++) {
389 const char *cmd = cmd_table[i].cmd;
390 const char *argument = cmd_table[i].argument;
391 const char *desc = cmd_table[i].desc;
393 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
394 argument != NULL? argument: "",
395 desc != NULL? desc: "");
397 if (cmd_table[i].options != NULL) {
398 for (j = 0; cmd_table[i].options[j].name != NULL;
400 const char *options_desc =
401 cmd_table[i].options_desc != NULL ?
402 cmd_table[i].options_desc[j]: "";
404 printf(" --%-12s%s\n",
405 cmd_table[i].options[j].name,
414 int commands(DBusConnection *connection, char *argv[], int argc)
418 for (i = 0; cmd_table[i].cmd != NULL; i++) {
419 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
420 cmd_table[i].func != NULL) {
421 return cmd_table[i].func(argv, argc,
422 cmd_table[i].options);
429 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
431 DBusMessage *message = NULL;
434 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
435 strcmp(argv[0], "h") == 0) {
436 printf("Usage: connmanctl [[command] [args]]\n");
437 cmd_help(NULL, 0, NULL);
438 printf("\nNote: arguments and output are considered "
439 "EXPERIMENTAL for now.\n\n");
440 } else if (strcmp(argv[0], "state") == 0) {
442 fprintf(stderr, "State cannot accept an argument, "
446 error = list_properties(connection,
447 "GetProperties", NULL);
448 } else if (strcmp(argv[0], "technologies") == 0) {
450 fprintf(stderr, "Tech cannot accept an argument, "
454 error = list_properties(connection,
455 "GetTechnologies", NULL);
456 } else if (strcmp(argv[0], "connect") == 0) {
458 fprintf(stderr, "Connect requires a service name or "
462 error = connect_service(connection,
463 strip_service_path(argv[1]));
465 printf("Connected to: %s\n",
466 strip_service_path(argv[1]));
467 } else if (strcmp(argv[0], "disconnect") == 0) {
469 fprintf(stderr, "Disconnect requires a service name or "
473 error = disconnect_service(connection,
474 strip_service_path(argv[1]));
476 printf("Disconnected from: %s\n",
477 strip_service_path(argv[1]));
478 } else if (strcmp(argv[0], "scan") == 0) {
480 fprintf(stderr, "Scan requires a service name or path, "
484 message = get_message(connection, "GetTechnologies");
488 error = scan_technology(connection, message, argv[1]);
489 } else if (strcmp(argv[0], "enable") == 0) {
491 fprintf(stderr, "Enable requires a technology name or "
492 "the argument 'offlinemode', see help\n");
494 } else if (strcmp(argv[1], "offlinemode") == 0) {
495 error = set_manager(connection, "OfflineMode", TRUE);
497 printf("OfflineMode is now enabled\n");
499 message = get_message(connection, "GetTechnologies");
503 error = set_technology(connection, message,
504 "Powered", argv[1], TRUE);
506 printf("Enabled %s technology\n", argv[1]);
508 } else if (strcmp(argv[0], "disable") == 0) {
510 fprintf(stderr, "Disable requires a technology name or "
511 "the argument 'offlinemode' see help\n");
513 } else if (strcmp(argv[1], "offlinemode") == 0) {
514 error = set_manager(connection, "OfflineMode", FALSE);
516 printf("OfflineMode is now disabled\n");
518 message = get_message(connection, "GetTechnologies");
522 error = set_technology(connection, message,
523 "Powered", argv[1], FALSE);
525 printf("Disabled %s technology\n", argv[1]);
531 dbus_message_unref(message);
536 int commands_options(DBusConnection *connection, char *argv[], int argc)
539 int option_index = 0;
540 struct service_data service;
542 if (strcmp(argv[0], "services") == 0) {
544 fprintf(stderr, "Too many arguments for services, "
549 printf("List of all services:\n");
550 error = list_properties(connection, "GetServices", NULL);
554 while ((c = getopt_long(argc, argv, "", service_options,
557 if (option_index == 0) {
558 printf("Services takes an "
559 "option, see help.\n");
564 error = service_switch(argc, argv, c,
572 } else if (strcmp(argv[0], "config") == 0) {
574 fprintf(stderr, "Config requires an option, "
578 while ((c = getopt_long(argc, argv, "", config_options,
581 if (option_index == 0) {
582 printf("Config requires an option, "
588 error = config_switch(argc, argv, c, connection);
593 } else 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);