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 " --remove Remove the service from favorite\n"
92 " monitor Monitor signals from all Connman interfaces\n"
93 " --services Monitor signals from the Service interface\n"
94 " --tech Monitor signals from the Technology interface\n"
95 " --manager Monitor signals from the Manager interface\n"
96 " help, --help, (no arguments) Show this dialogue\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");
114 name = find_service(conn, message, argv[2], service);
120 error = list_properties(conn, "GetServices", (char *) name);
123 fprintf(stderr, "Command not recognized, please check help\n");
128 dbus_message_unref(message);
133 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
135 DBusMessage *message;
136 int num_args = argc - MANDATORY_ARGS;
140 message = get_message(conn, "GetServices");
160 error = set_service_property(conn, message, argv[1],
165 error = set_service_property(conn, message, argv[1],
166 "IPv4.Configuration", ipv4,
167 argv + MANDATORY_ARGS, num_args);
170 error = set_service_property(conn, message, argv[1],
171 "IPv6.Configuration", ipv6,
172 argv + MANDATORY_ARGS, num_args);
175 error = set_service_property(conn, message, argv[1],
176 "Nameservers.Configuration", NULL,
177 argv + MANDATORY_ARGS, num_args);
180 error = set_service_property(conn, message, argv[1],
181 "Timeservers.Configuration", NULL,
182 argv + MANDATORY_ARGS, num_args);
185 error = set_service_property(conn, message, argv[1],
186 "Domains.Configuration", NULL,
187 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);
195 } else if (strcmp(argv[3], "manual") == 0
196 && strcmp(argv[4], "servers") == 0
199 error = store_proxy_input(conn, message, argv[1],
202 fprintf(stderr, "Incorrect arguments\n");
207 error = remove_service(conn, message, argv[1]);
210 fprintf(stderr, "Command not recognized, please check help\n");
215 dbus_message_unref(message);
220 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
226 error = monitor_connman(conn, "Service", "PropertyChanged");
229 if (dbus_connection_add_filter(conn, service_property_changed,
230 NULL, NULL) == FALSE)
232 printf("Now monitoring the service interface.\n");
235 error = monitor_connman(conn, "Technology", "PropertyChanged");
238 if (dbus_connection_add_filter(conn, tech_property_changed,
239 NULL, NULL) == FALSE)
241 printf("Now monitoring the technology interface.\n");
244 error = monitor_connman(conn, "Manager", "PropertyChanged");
247 error = monitor_connman(conn, "Manager", "TechnologyAdded");
250 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
253 error = monitor_connman(conn, "Manager", "ServicesChanged");
256 if (dbus_connection_add_filter(conn, manager_property_changed,
257 NULL, NULL) == FALSE)
259 if (dbus_connection_add_filter(conn, tech_added_removed,
260 NULL, NULL) == FALSE)
262 if (dbus_connection_add_filter(conn, manager_services_changed,
263 NULL, NULL) == FALSE)
265 printf("Now monitoring the manager interface.\n");
268 fprintf(stderr, "Command not recognized, please check help\n");
275 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
277 DBusMessage *message = NULL;
281 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
282 strcmp(argv[0], "h") == 0) {
284 } else if (strcmp(argv[0], "state") == 0) {
286 fprintf(stderr, "State cannot accept an argument, "
290 error = list_properties(connection,
291 "GetProperties", NULL);
292 } else if (strcmp(argv[0], "technologies") == 0) {
294 fprintf(stderr, "Tech cannot accept an argument, "
298 error = list_properties(connection,
299 "GetTechnologies", NULL);
300 } else if (strcmp(argv[0], "connect") == 0) {
302 fprintf(stderr, "Connect requires a service name or "
306 error = connect_service(connection,
307 strip_service_path(argv[1]));
309 printf("Connected to: %s\n",
310 strip_service_path(argv[1]));
311 } else if (strcmp(argv[0], "disconnect") == 0) {
313 fprintf(stderr, "Disconnect requires a service name or "
317 error = disconnect_service(connection,
318 strip_service_path(argv[1]));
320 printf("Disconnected from: %s\n",
321 strip_service_path(argv[1]));
322 } else if (strcmp(argv[0], "scan") == 0) {
324 fprintf(stderr, "Scan requires a service name or path, "
328 message = get_message(connection, "GetTechnologies");
332 error = scan_technology(connection, message, argv[1]);
333 } else if (strcmp(argv[0], "enable") == 0) {
335 fprintf(stderr, "Enable requires a technology name or "
336 "the argument 'offlinemode', see help\n");
338 } else if (strcmp(argv[1], "offlinemode") == 0) {
339 error = set_manager(connection, "OfflineMode", TRUE);
341 printf("OfflineMode is now enabled\n");
343 message = get_message(connection, "GetTechnologies");
347 error = set_technology(connection, message,
348 "Powered", argv[1], TRUE);
350 printf("Enabled %s technology\n", argv[1]);
352 } else if (strcmp(argv[0], "disable") == 0) {
354 fprintf(stderr, "Disable requires a technology name or "
355 "the argument 'offlinemode' see help\n");
357 } else if (strcmp(argv[1], "offlinemode") == 0) {
358 error = set_manager(connection, "OfflineMode", FALSE);
360 printf("OfflineMode is now disabled\n");
362 message = get_message(connection, "GetTechnologies");
366 error = set_technology(connection, message,
367 "Powered", argv[1], FALSE);
369 printf("Disabled %s technology\n", argv[1]);
375 dbus_message_unref(message);
380 int commands_options(DBusConnection *connection, char *argv[], int argc)
383 int option_index = 0;
384 struct service_data service;
386 static struct option service_options[] = {
387 {"properties", required_argument, 0, 'p'},
391 static struct option config_options[] = {
392 {"nameservers", required_argument, 0, 'n'},
393 {"timeservers", required_argument, 0, 't'},
394 {"domains", required_argument, 0, 'd'},
395 {"ipv6", required_argument, 0, 'v'},
396 {"proxy", required_argument, 0, 'x'},
397 {"autoconnect", required_argument, 0, 'a'},
398 {"ipv4", required_argument, 0, 'i'},
399 {"remove", 0, 0, 'r'},
403 static struct option monitor_options[] = {
404 {"services", no_argument, 0, 's'},
405 {"tech", no_argument, 0, 'c'},
406 {"manager", no_argument, 0, 'm'},
410 if (strcmp(argv[0], "services") == 0) {
412 fprintf(stderr, "Too many arguments for services, "
417 printf("List of all services:\n");
418 error = list_properties(connection, "GetServices", NULL);
422 while ((c = getopt_long(argc, argv, "", service_options,
425 if (option_index == 0) {
426 printf("Services takes an "
427 "option, see help.\n");
432 error = service_switch(argc, argv, c,
440 } else if (strcmp(argv[0], "config") == 0) {
442 fprintf(stderr, "Config requires an option, "
446 while ((c = getopt_long(argc, argv, "", config_options,
449 if (option_index == 0) {
450 printf("Config requires an option, "
456 error = config_switch(argc, argv, c, connection);
461 } else if (strcmp(argv[0], "monitor") == 0) {
463 fprintf(stderr, "Too many arguments for monitor, "
468 error = monitor_connman(connection, "Service",
472 error = monitor_connman(connection, "Technology",
476 error = monitor_connman(connection, "Manager",
480 error = monitor_connman(connection, "Manager",
484 error = monitor_connman(connection, "Manager",
485 "TechnologyRemoved");
488 error = monitor_connman(connection, "Manager",
492 if (dbus_connection_add_filter(connection,
493 service_property_changed, NULL, NULL)
496 if (dbus_connection_add_filter(connection,
497 tech_property_changed, NULL, NULL)
500 if (dbus_connection_add_filter(connection,
501 tech_added_removed, NULL, NULL)
504 if (dbus_connection_add_filter(connection,
505 manager_property_changed, NULL, NULL)
508 if (dbus_connection_add_filter(connection,
509 manager_services_changed, NULL, NULL)
512 printf("Now monitoring all interfaces.\n");
514 while ((c = getopt_long(argc, argv, "", monitor_options,
517 if (option_index == 0) {
518 printf("Monitor takes an "
519 "option, see help\n");
524 error = monitor_switch(argc, argv, c, connection);