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 " exit, quit, q Quit interactive mode\n"
97 "\nNote: arguments and output are considered EXPERIMENTAL for now.\n\n");
100 int service_switch(int argc, char *argv[], int c, DBusConnection *conn,
101 struct service_data *service)
104 DBusMessage *message;
107 message = get_message(conn, "GetServices");
111 name = find_service(conn, message, argv[2], service);
114 error = list_properties(conn, "GetServices", (char *) name);
119 fprintf(stderr, "Command not recognized, please check help\n");
126 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
128 DBusMessage *message;
129 int num_args = argc - MANDATORY_ARGS;
133 message = get_message(conn, "GetServices");
137 if (*optarg != 'y' || *optarg != 'n' || *optarg != '1' ||
138 *optarg != '0' || *optarg != 't' ||
141 if (*optarg == 'y' || *optarg == '1' ||
144 else if (*optarg == 'n' || *optarg == '0' ||
147 error = set_service_property(conn, message, argv[1],
154 error = set_service_property(conn, message, argv[1],
155 "IPv4.Configuration", ipv4,
156 argv + MANDATORY_ARGS, num_args);
161 error = set_service_property(conn, message, argv[1],
162 "IPv6.Configuration", ipv6,
163 argv + MANDATORY_ARGS, num_args);
168 error = set_service_property(conn, message, argv[1],
169 "Nameservers.Configuration", NULL,
170 argv + MANDATORY_ARGS, num_args);
175 error = set_service_property(conn, message, argv[1],
176 "Timeservers.Configuration", NULL,
177 argv + MANDATORY_ARGS, num_args);
182 error = set_service_property(conn, message, argv[1],
183 "Domains.Configuration", NULL,
184 argv + MANDATORY_ARGS, num_args);
189 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
190 (strcmp(argv[3], "auto") == 0 && argc < 6)) {
191 error = set_service_property(conn, message, argv[1],
192 "Proxy.Configuration", proxy_simple,
193 argv + MANDATORY_ARGS, num_args);
196 } else if (strcmp(argv[3], "manual") == 0
197 && strcmp(argv[4], "servers") == 0
200 error = store_proxy_input(conn, message, argv[1],
205 fprintf(stderr, "Incorrect arguments\n");
210 fprintf(stderr, "Command not recognized, please check help\n");
217 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
223 error = monitor_connman(conn, "Service", "PropertyChanged");
226 if (dbus_connection_add_filter(conn, service_property_changed,
227 NULL, NULL) == FALSE)
229 printf("Now monitoring the service interface.\n");
232 error = monitor_connman(conn, "Technology", "PropertyChanged");
235 if (dbus_connection_add_filter(conn, tech_property_changed,
236 NULL, NULL) == FALSE)
238 printf("Now monitoring the technology interface.\n");
241 error = monitor_connman(conn, "Manager", "PropertyChanged");
244 error = monitor_connman(conn, "Manager", "TechnologyAdded");
247 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
250 error = monitor_connman(conn, "Manager", "ServicesChanged");
253 if (dbus_connection_add_filter(conn, manager_property_changed,
254 NULL, NULL) == FALSE)
256 if (dbus_connection_add_filter(conn, tech_added_removed,
257 NULL, NULL) == FALSE)
259 if (dbus_connection_add_filter(conn, manager_services_changed,
260 NULL, NULL) == FALSE)
262 printf("Now monitoring the manager interface.\n");
265 fprintf(stderr, "Command not recognized, please check help\n");
272 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
275 DBusMessage *message;
278 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
279 strcmp(argv[0], "h") == 0) {
281 } else if (strcmp(argv[0], "state") == 0) {
283 fprintf(stderr, "State cannot accept an argument, "
287 error = list_properties(connection, "GetProperties", NULL);
290 } else if (strcmp(argv[0], "technologies") == 0) {
292 fprintf(stderr, "Tech cannot accept an argument, "
296 error = list_properties(connection, "GetTechnologies", NULL);
299 } else if (strcmp(argv[0], "connect") == 0) {
301 fprintf(stderr, "Connect requires a service name or "
305 error = connect_service(connection, strip_service_path(argv[1]));
308 printf("Connected to: %s\n", strip_service_path(argv[1]));
309 } else if (strcmp(argv[0], "disconnect") == 0) {
311 fprintf(stderr, "Disconnect requires a service name or "
315 error = disconnect_service(connection, strip_service_path(argv[1]));
318 printf("Disconnected from: %s\n", strip_service_path(argv[1]));
319 } else if (strcmp(argv[0], "scan") == 0) {
321 fprintf(stderr, "Scan requires a service name or path, "
325 message = get_message(connection, "GetTechnologies");
326 error = scan_technology(connection, message, argv[1]);
329 dbus_message_unref(message);
330 } else if (strcmp(argv[0], "enable") == 0) {
332 fprintf(stderr, "Enable requires a technology name or "
333 "the argument 'offlinemode', see help\n");
336 if (strcmp(argv[1], "offlinemode") == 0) {
337 error = set_manager(connection, "OfflineMode", TRUE);
340 printf("OfflineMode is now enabled\n");
342 message = get_message(connection, "GetTechnologies");
343 error = set_technology(connection, message, "Powered",
347 printf("Enabled %s technology\n", argv[1]);
348 dbus_message_unref(message);
350 } else if (strcmp(argv[0], "disable") == 0) {
352 fprintf(stderr, "Disable requires a technology name or "
353 "the argument 'offlinemode' see help\n");
356 if (strcmp(argv[1], "offlinemode") == 0) {
357 error = set_manager(connection, "OfflineMode", FALSE);
360 printf("OfflineMode is now disabled\n");
362 message = get_message(connection, "GetTechnologies");
363 error = set_technology(connection, message, "Powered",
367 printf("Disabled %s technology\n", argv[1]);
368 dbus_message_unref(message);
375 int commands_options(DBusConnection *connection, char *argv[], int argc)
378 int option_index = 0;
379 struct service_data service;
381 static struct option service_options[] = {
382 {"properties", required_argument, 0, 'p'},
386 static struct option config_options[] = {
387 {"nameservers", required_argument, 0, 'n'},
388 {"timeservers", required_argument, 0, 't'},
389 {"domains", required_argument, 0, 'd'},
390 {"ipv6", required_argument, 0, 'v'},
391 {"proxy", required_argument, 0, 'x'},
392 {"autoconnect", required_argument, 0, 'a'},
393 {"ipv4", required_argument, 0, 'i'},
397 static struct option monitor_options[] = {
398 {"services", no_argument, 0, 's'},
399 {"tech", no_argument, 0, 'c'},
400 {"manager", no_argument, 0, 'm'},
404 if (strcmp(argv[0], "services") == 0) {
406 fprintf(stderr, "Too many arguments for services, "
411 printf("List of all services:\n");
412 error = list_properties(connection, "GetServices", NULL);
416 while ((c = getopt_long(argc, argv, "", service_options,
419 if (option_index == 0) {
420 printf("Services takes an "
421 "option, see help.\n");
426 error = service_switch(argc, argv, c,
434 } else if (strcmp(argv[0], "config") == 0) {
436 fprintf(stderr, "Config requires an option, "
440 while ((c = getopt_long(argc, argv, "", config_options,
443 if (option_index == 0) {
444 printf("Config requires an option, "
450 error = config_switch(argc, argv, c, connection);
455 } else if (strcmp(argv[0], "monitor") == 0) {
457 fprintf(stderr, "Too many arguments for monitor, "
462 error = monitor_connman(connection, "Service",
466 error = monitor_connman(connection, "Technology",
470 error = monitor_connman(connection, "Manager",
474 error = monitor_connman(connection, "Manager",
478 error = monitor_connman(connection, "Manager",
479 "TechnologyRemoved");
482 error = monitor_connman(connection, "Manager",
486 if (dbus_connection_add_filter(connection,
487 service_property_changed, NULL, NULL)
490 if (dbus_connection_add_filter(connection,
491 tech_property_changed, NULL, NULL)
494 if (dbus_connection_add_filter(connection,
495 tech_added_removed, NULL, NULL)
498 if (dbus_connection_add_filter(connection,
499 manager_property_changed, NULL, NULL)
502 if (dbus_connection_add_filter(connection,
503 manager_services_changed, NULL, NULL)
506 printf("Now monitoring all interfaces.\n");
508 while ((c = getopt_long(argc, argv, "", monitor_options,
511 if (option_index == 0) {
512 printf("Monitor takes an "
513 "option, see help\n");
518 error = monitor_switch(argc, argv, c, connection);