5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "client/services.h"
30 #include "client/technology.h"
31 #include "client/data_manager.h"
32 #include "client/monitor.h"
33 #include "client/interactive.h"
34 #include "gdbus/gdbus.h"
36 #define MANDATORY_ARGS 3
38 static char *ipv4[] = {
46 static char *ipv6[] = {
55 static char *proxy_simple[] = {
63 printf("Usage: connmanctl <command> [args]\n"
64 " enable Enables given technology\n"
66 " offlinemode Enables OfflineMode\n"
67 " disable Disables given technology\n"
69 " offlinemode Disables OfflineMode\n"
70 " state Shows if the system is online or offline\n"
71 " services Display list of all services\n"
72 " --properties <service name> Show properties of service\n"
73 " technologies Current technology on the system\n"
74 " scan <technology> Scans for new services on the given technology\n"
75 " connect <service> Connect to a given service\n"
76 " disconnect <service> Disconnect from service\n"
77 " config <service> [arg] Set certain config options\n"
78 " --autoconnect=y/n Set autoconnect to service\n"
79 " --nameservers <names> Set manual name servers\n"
80 " --timeservers <names> Set manual time servers\n"
81 " --domains <domains> Set manual domains\n"
82 " --ipv4 Set ipv4 configuration\n"
83 " [METHOD|DHCP|AUTO|MANUAL] [IP] [NETMASK] [GATEWAY]\n"
84 " --ipv6 Set ipv6 configuration\n"
85 " [METHOD|AUTO|MANUAL|OFF] [IP] [PREFIXLENGTH] [GATEWAY]\n"
86 " [PRIVACY|DISABLED|ENABLED|PREFERED]\n"
87 " --proxy Set proxy configuration\n"
88 " [METHOD|URL|SERVERS|EXCLUDES]\n"
89 " if METHOD = manual, enter 'servers' then the list of servers\n"
90 " then enter 'excludes' then the list of excludes\n"
91 " monitor Monitor signals from all Connman interfaces\n"
92 " --services Monitor signals from the Service interface\n"
93 " --tech Monitor signals from the Technology interface\n"
94 " --manager Monitor signals from the Manager interface\n"
95 " help, --help, (no arguments) Show this dialogue\n"
96 " interactive Drop into the interactive shell\n"
97 " exit, quit, q Quit interactive mode\n"
98 "\nNote: arguments and output are considered EXPERIMENTAL for now.\n\n");
101 int service_switch(int argc, char *argv[], int c, DBusConnection *conn,
102 struct service_data *service)
105 DBusMessage *message;
108 message = get_message(conn, "GetServices");
112 name = find_service(conn, message, argv[2], service);
115 error = list_properties(conn, "GetServices", (char *) name);
120 fprintf(stderr, "Command not recognized, please check help\n");
127 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
129 DBusMessage *message;
130 int num_args = argc - MANDATORY_ARGS;
134 message = get_message(conn, "GetServices");
138 if (*optarg != 'y' || *optarg != 'n' || *optarg != '1' ||
139 *optarg != '0' || *optarg != 't' ||
142 if (*optarg == 'y' || *optarg == '1' ||
145 else if (*optarg == 'n' || *optarg == '0' ||
148 error = set_service_property(conn, message, argv[1],
155 error = set_service_property(conn, message, argv[1],
156 "IPv4.Configuration", ipv4,
157 argv + MANDATORY_ARGS, num_args);
162 error = set_service_property(conn, message, argv[1],
163 "IPv6.Configuration", ipv6,
164 argv + MANDATORY_ARGS, num_args);
169 error = set_service_property(conn, message, argv[1],
170 "Nameservers.Configuration", NULL,
171 argv + MANDATORY_ARGS, num_args);
176 error = set_service_property(conn, message, argv[1],
177 "Timeservers.Configuration", NULL,
178 argv + MANDATORY_ARGS, num_args);
183 error = set_service_property(conn, message, argv[1],
184 "Domains.Configuration", NULL,
185 argv + MANDATORY_ARGS, num_args);
190 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
191 (strcmp(argv[3], "auto") == 0 && argc < 6)) {
192 error = set_service_property(conn, message, argv[1],
193 "Proxy.Configuration", proxy_simple,
194 argv + MANDATORY_ARGS, num_args);
197 } else if (strcmp(argv[3], "manual") == 0
198 && strcmp(argv[4], "servers") == 0
201 error = store_proxy_input(conn, message, argv[1],
206 fprintf(stderr, "Incorrect arguments\n");
211 fprintf(stderr, "Command not recognized, please check help\n");
218 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
224 error = monitor_connman(conn, "Service", "PropertyChanged");
227 if (dbus_connection_add_filter(conn, service_property_changed,
228 NULL, NULL) == FALSE)
230 printf("Now monitoring the service interface.\n");
233 error = monitor_connman(conn, "Technology", "PropertyChanged");
236 if (dbus_connection_add_filter(conn, tech_property_changed,
237 NULL, NULL) == FALSE)
239 printf("Now monitoring the technology interface.\n");
242 error = monitor_connman(conn, "Manager", "PropertyChanged");
245 error = monitor_connman(conn, "Manager", "TechnologyAdded");
248 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
251 error = monitor_connman(conn, "Manager", "ServicesChanged");
254 if (dbus_connection_add_filter(conn, manager_property_changed,
255 NULL, NULL) == FALSE)
257 if (dbus_connection_add_filter(conn, tech_added_removed,
258 NULL, NULL) == FALSE)
260 if (dbus_connection_add_filter(conn, manager_services_changed,
261 NULL, NULL) == FALSE)
263 printf("Now monitoring the manager interface.\n");
266 fprintf(stderr, "Command not recognized, please check help\n");
273 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
276 DBusMessage *message;
279 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
280 strcmp(argv[0], "h") == 0) {
282 } else if (strcmp(argv[0], "state") == 0) {
284 fprintf(stderr, "State cannot accept an argument, "
288 error = list_properties(connection, "GetProperties", NULL);
291 } else if (strcmp(argv[0], "technologies") == 0) {
293 fprintf(stderr, "Tech cannot accept an argument, "
297 error = list_properties(connection, "GetTechnologies", NULL);
300 } else if (strcmp(argv[0], "connect") == 0) {
302 fprintf(stderr, "Connect requires a service name or "
306 error = connect_service(connection, strip_service_path(argv[1]));
309 printf("Connected to: %s\n", strip_service_path(argv[1]));
310 } else if (strcmp(argv[0], "disconnect") == 0) {
312 fprintf(stderr, "Disconnect requires a service name or "
316 error = disconnect_service(connection, strip_service_path(argv[1]));
319 printf("Disconnected from: %s\n", strip_service_path(argv[1]));
320 } else if (strcmp(argv[0], "scan") == 0) {
322 fprintf(stderr, "Scan requires a service name or path, "
326 message = get_message(connection, "GetTechnologies");
327 error = scan_technology(connection, message, argv[1]);
330 dbus_message_unref(message);
331 } else if (strcmp(argv[0], "enable") == 0) {
333 fprintf(stderr, "Enable requires a technology name or "
334 "the argument 'offlinemode', see help\n");
337 if (strcmp(argv[1], "offlinemode") == 0) {
338 error = set_manager(connection, "OfflineMode", TRUE);
341 printf("OfflineMode is now enabled\n");
343 message = get_message(connection, "GetTechnologies");
344 error = set_technology(connection, message, "Powered",
348 printf("Enabled %s technology\n", argv[1]);
349 dbus_message_unref(message);
351 } else if (strcmp(argv[0], "disable") == 0) {
353 fprintf(stderr, "Disable requires a technology name or "
354 "the argument 'offlinemode' see help\n");
357 if (strcmp(argv[1], "offlinemode") == 0) {
358 error = set_manager(connection, "OfflineMode", FALSE);
361 printf("OfflineMode is now disabled\n");
363 message = get_message(connection, "GetTechnologies");
364 error = set_technology(connection, message, "Powered",
368 printf("Disabled %s technology\n", argv[1]);
369 dbus_message_unref(message);
376 int commands_options(DBusConnection *connection, char *argv[], int argc)
379 int option_index = 0;
380 struct service_data service;
382 static struct option service_options[] = {
383 {"properties", required_argument, 0, 'p'},
387 static struct option config_options[] = {
388 {"nameservers", required_argument, 0, 'n'},
389 {"timeservers", required_argument, 0, 't'},
390 {"domains", required_argument, 0, 'd'},
391 {"ipv6", required_argument, 0, 'v'},
392 {"proxy", required_argument, 0, 'x'},
393 {"autoconnect", required_argument, 0, 'a'},
394 {"ipv4", required_argument, 0, 'i'},
398 static struct option monitor_options[] = {
399 {"services", no_argument, 0, 's'},
400 {"tech", no_argument, 0, 'c'},
401 {"manager", no_argument, 0, 'm'},
405 if (strcmp(argv[0], "services") == 0) {
407 fprintf(stderr, "Too many arguments for services, "
412 printf("List of all services:\n");
413 error = list_properties(connection, "GetServices", NULL);
417 while ((c = getopt_long(argc, argv, "", service_options,
420 if (option_index == 0) {
421 printf("Services takes an "
422 "option, see help.\n");
427 error = service_switch(argc, argv, c,
435 } else if (strcmp(argv[0], "config") == 0) {
437 fprintf(stderr, "Config requires an option, "
441 while ((c = getopt_long(argc, argv, "", config_options,
444 if (option_index == 0) {
445 printf("Config requires an option, "
451 error = config_switch(argc, argv, c, connection);
456 } else if (strcmp(argv[0], "monitor") == 0) {
458 fprintf(stderr, "Too many arguments for monitor, "
463 error = monitor_connman(connection, "Service",
467 error = monitor_connman(connection, "Technology",
471 error = monitor_connman(connection, "Manager",
475 error = monitor_connman(connection, "Manager",
479 error = monitor_connman(connection, "Manager",
480 "TechnologyRemoved");
483 error = monitor_connman(connection, "Manager",
487 if (dbus_connection_add_filter(connection,
488 service_property_changed, NULL, NULL)
491 if (dbus_connection_add_filter(connection,
492 tech_property_changed, NULL, NULL)
495 if (dbus_connection_add_filter(connection,
496 tech_added_removed, NULL, NULL)
499 if (dbus_connection_add_filter(connection,
500 manager_property_changed, NULL, NULL)
503 if (dbus_connection_add_filter(connection,
504 manager_services_changed, NULL, NULL)
507 printf("Now monitoring all interfaces.\n");
509 while ((c = getopt_long(argc, argv, "", monitor_options,
512 if (option_index == 0) {
513 printf("Monitor takes an "
514 "option, see help\n");
519 error = monitor_switch(argc, argv, c, connection);