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");
113 name = find_service(conn, message, argv[2], service);
119 error = list_properties(conn, "GetServices", (char *) name);
122 fprintf(stderr, "Command not recognized, please check help\n");
127 dbus_message_unref(message);
132 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
134 DBusMessage *message;
135 int num_args = argc - MANDATORY_ARGS;
139 message = get_message(conn, "GetServices");
159 error = set_service_property(conn, message, argv[1],
164 error = set_service_property(conn, message, argv[1],
165 "IPv4.Configuration", ipv4,
166 argv + MANDATORY_ARGS, num_args);
169 error = set_service_property(conn, message, argv[1],
170 "IPv6.Configuration", ipv6,
171 argv + MANDATORY_ARGS, num_args);
174 error = set_service_property(conn, message, argv[1],
175 "Nameservers.Configuration", NULL,
176 argv + MANDATORY_ARGS, num_args);
179 error = set_service_property(conn, message, argv[1],
180 "Timeservers.Configuration", NULL,
181 argv + MANDATORY_ARGS, num_args);
184 error = set_service_property(conn, message, argv[1],
185 "Domains.Configuration", NULL,
186 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);
194 } else if (strcmp(argv[3], "manual") == 0
195 && strcmp(argv[4], "servers") == 0
198 error = store_proxy_input(conn, message, argv[1],
201 fprintf(stderr, "Incorrect arguments\n");
206 fprintf(stderr, "Command not recognized, please check help\n");
211 dbus_message_unref(message);
216 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
222 error = monitor_connman(conn, "Service", "PropertyChanged");
225 if (dbus_connection_add_filter(conn, service_property_changed,
226 NULL, NULL) == FALSE)
228 printf("Now monitoring the service interface.\n");
231 error = monitor_connman(conn, "Technology", "PropertyChanged");
234 if (dbus_connection_add_filter(conn, tech_property_changed,
235 NULL, NULL) == FALSE)
237 printf("Now monitoring the technology interface.\n");
240 error = monitor_connman(conn, "Manager", "PropertyChanged");
243 error = monitor_connman(conn, "Manager", "TechnologyAdded");
246 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
249 error = monitor_connman(conn, "Manager", "ServicesChanged");
252 if (dbus_connection_add_filter(conn, manager_property_changed,
253 NULL, NULL) == FALSE)
255 if (dbus_connection_add_filter(conn, tech_added_removed,
256 NULL, NULL) == FALSE)
258 if (dbus_connection_add_filter(conn, manager_services_changed,
259 NULL, NULL) == FALSE)
261 printf("Now monitoring the manager interface.\n");
264 fprintf(stderr, "Command not recognized, please check help\n");
271 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
273 DBusMessage *message = NULL;
277 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
278 strcmp(argv[0], "h") == 0) {
280 } else if (strcmp(argv[0], "state") == 0) {
282 fprintf(stderr, "State cannot accept an argument, "
286 error = list_properties(connection,
287 "GetProperties", NULL);
288 } else if (strcmp(argv[0], "technologies") == 0) {
290 fprintf(stderr, "Tech cannot accept an argument, "
294 error = list_properties(connection,
295 "GetTechnologies", NULL);
296 } else if (strcmp(argv[0], "connect") == 0) {
298 fprintf(stderr, "Connect requires a service name or "
302 error = connect_service(connection,
303 strip_service_path(argv[1]));
305 printf("Connected to: %s\n",
306 strip_service_path(argv[1]));
307 } else if (strcmp(argv[0], "disconnect") == 0) {
309 fprintf(stderr, "Disconnect requires a service name or "
313 error = disconnect_service(connection,
314 strip_service_path(argv[1]));
316 printf("Disconnected from: %s\n",
317 strip_service_path(argv[1]));
318 } else if (strcmp(argv[0], "scan") == 0) {
320 fprintf(stderr, "Scan requires a service name or path, "
324 message = get_message(connection, "GetTechnologies");
328 error = scan_technology(connection, message, argv[1]);
329 } else if (strcmp(argv[0], "enable") == 0) {
331 fprintf(stderr, "Enable requires a technology name or "
332 "the argument 'offlinemode', see help\n");
334 } else if (strcmp(argv[1], "offlinemode") == 0) {
335 error = set_manager(connection, "OfflineMode", TRUE);
337 printf("OfflineMode is now enabled\n");
339 message = get_message(connection, "GetTechnologies");
343 error = set_technology(connection, message,
344 "Powered", argv[1], TRUE);
346 printf("Enabled %s technology\n", argv[1]);
348 } else if (strcmp(argv[0], "disable") == 0) {
350 fprintf(stderr, "Disable requires a technology name or "
351 "the argument 'offlinemode' see help\n");
353 } else if (strcmp(argv[1], "offlinemode") == 0) {
354 error = set_manager(connection, "OfflineMode", FALSE);
356 printf("OfflineMode is now disabled\n");
358 message = get_message(connection, "GetTechnologies");
362 error = set_technology(connection, message,
363 "Powered", argv[1], FALSE);
365 printf("Disabled %s technology\n", argv[1]);
371 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);