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
32 #include "client/services.h"
33 #include "client/technology.h"
34 #include "client/data_manager.h"
35 #include "client/monitor.h"
36 #include "client/interactive.h"
37 #include "gdbus/gdbus.h"
39 #define MANDATORY_ARGS 3
41 static char *ipv4[] = {
49 static char *ipv6[] = {
58 static char *proxy_simple[] = {
66 printf("Usage: connmanctl <command> [args]\n"
67 " enable Enables given technology\n"
69 " offlinemode Enables OfflineMode\n"
70 " disable Disables given technology\n"
72 " offlinemode Disables OfflineMode\n"
73 " state Shows if the system is online or offline\n"
74 " services Display list of all services\n"
75 " --properties <service name> Show properties of service\n"
76 " technologies Current technology on the system\n"
77 " scan <technology> Scans for new services on the given technology\n"
78 " connect <service> Connect to a given service\n"
79 " disconnect <service> Disconnect from service\n"
80 " config <service> [arg] Set certain config options\n"
81 " --autoconnect=y/n Set autoconnect to service\n"
82 " --nameservers <names> Set manual name servers\n"
83 " --timeservers <names> Set manual time servers\n"
84 " --domains <domains> Set manual domains\n"
85 " --ipv4 Set ipv4 configuration\n"
86 " [METHOD|DHCP|AUTO|MANUAL] [IP] [NETMASK] [GATEWAY]\n"
87 " --ipv6 Set ipv6 configuration\n"
88 " [METHOD|AUTO|MANUAL|OFF] [IP] [PREFIXLENGTH] [GATEWAY]\n"
89 " [PRIVACY|DISABLED|ENABLED|PREFERED]\n"
90 " --proxy Set proxy configuration\n"
91 " [METHOD|URL|SERVERS|EXCLUDES]\n"
92 " if METHOD = manual, enter 'servers' then the list of servers\n"
93 " then enter 'excludes' then the list of excludes\n"
94 " --remove Remove the service from favorite\n"
95 " monitor Monitor signals from all Connman interfaces\n"
96 " --services Monitor signals from the Service interface\n"
97 " --tech Monitor signals from the Technology interface\n"
98 " --manager Monitor signals from the Manager interface\n"
99 " help, --help, (no arguments) Show this dialogue\n"
100 " exit, quit, q Quit interactive mode\n"
101 "\nNote: arguments and output are considered EXPERIMENTAL for now.\n\n");
104 int service_switch(int argc, char *argv[], int c, DBusConnection *conn,
105 struct service_data *service)
108 DBusMessage *message;
111 message = get_message(conn, "GetServices");
117 name = find_service(conn, message, argv[2], service);
123 error = list_properties(conn, "GetServices", (char *) name);
126 fprintf(stderr, "Command not recognized, please check help\n");
131 dbus_message_unref(message);
136 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
138 DBusMessage *message;
139 int num_args = argc - MANDATORY_ARGS;
143 message = get_message(conn, "GetServices");
163 error = set_service_property(conn, message, argv[1],
168 error = set_service_property(conn, message, argv[1],
169 "IPv4.Configuration", ipv4,
170 argv + MANDATORY_ARGS, num_args);
173 error = set_service_property(conn, message, argv[1],
174 "IPv6.Configuration", ipv6,
175 argv + MANDATORY_ARGS, num_args);
178 error = set_service_property(conn, message, argv[1],
179 "Nameservers.Configuration", NULL,
180 argv + MANDATORY_ARGS, num_args);
183 error = set_service_property(conn, message, argv[1],
184 "Timeservers.Configuration", NULL,
185 argv + MANDATORY_ARGS, num_args);
188 error = set_service_property(conn, message, argv[1],
189 "Domains.Configuration", NULL,
190 argv + MANDATORY_ARGS, num_args);
193 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
194 (strcmp(argv[3], "auto") == 0 && argc < 6)) {
195 error = set_service_property(conn, message, argv[1],
196 "Proxy.Configuration", proxy_simple,
197 argv + MANDATORY_ARGS, num_args);
198 } else if (strcmp(argv[3], "manual") == 0
199 && strcmp(argv[4], "servers") == 0
202 error = store_proxy_input(conn, message, argv[1],
205 fprintf(stderr, "Incorrect arguments\n");
210 error = remove_service(conn, message, argv[1]);
213 fprintf(stderr, "Command not recognized, please check help\n");
218 dbus_message_unref(message);
223 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
229 error = monitor_connman(conn, "Service", "PropertyChanged");
232 if (dbus_connection_add_filter(conn, service_property_changed,
233 NULL, NULL) == FALSE)
235 printf("Now monitoring the service interface.\n");
238 error = monitor_connman(conn, "Technology", "PropertyChanged");
241 if (dbus_connection_add_filter(conn, tech_property_changed,
242 NULL, NULL) == FALSE)
244 printf("Now monitoring the technology interface.\n");
247 error = monitor_connman(conn, "Manager", "PropertyChanged");
250 error = monitor_connman(conn, "Manager", "TechnologyAdded");
253 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
256 error = monitor_connman(conn, "Manager", "ServicesChanged");
259 if (dbus_connection_add_filter(conn, manager_property_changed,
260 NULL, NULL) == FALSE)
262 if (dbus_connection_add_filter(conn, tech_added_removed,
263 NULL, NULL) == FALSE)
265 if (dbus_connection_add_filter(conn, manager_services_changed,
266 NULL, NULL) == FALSE)
268 printf("Now monitoring the manager interface.\n");
271 fprintf(stderr, "Command not recognized, please check help\n");
278 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
280 DBusMessage *message = NULL;
284 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
285 strcmp(argv[0], "h") == 0) {
287 } else if (strcmp(argv[0], "state") == 0) {
289 fprintf(stderr, "State cannot accept an argument, "
293 error = list_properties(connection,
294 "GetProperties", NULL);
295 } else if (strcmp(argv[0], "technologies") == 0) {
297 fprintf(stderr, "Tech cannot accept an argument, "
301 error = list_properties(connection,
302 "GetTechnologies", NULL);
303 } else if (strcmp(argv[0], "connect") == 0) {
305 fprintf(stderr, "Connect requires a service name or "
309 error = connect_service(connection,
310 strip_service_path(argv[1]));
312 printf("Connected to: %s\n",
313 strip_service_path(argv[1]));
314 } else if (strcmp(argv[0], "disconnect") == 0) {
316 fprintf(stderr, "Disconnect requires a service name or "
320 error = disconnect_service(connection,
321 strip_service_path(argv[1]));
323 printf("Disconnected from: %s\n",
324 strip_service_path(argv[1]));
325 } else if (strcmp(argv[0], "scan") == 0) {
327 fprintf(stderr, "Scan requires a service name or path, "
331 message = get_message(connection, "GetTechnologies");
335 error = scan_technology(connection, message, argv[1]);
336 } else if (strcmp(argv[0], "enable") == 0) {
338 fprintf(stderr, "Enable requires a technology name or "
339 "the argument 'offlinemode', see help\n");
341 } else if (strcmp(argv[1], "offlinemode") == 0) {
342 error = set_manager(connection, "OfflineMode", TRUE);
344 printf("OfflineMode is now enabled\n");
346 message = get_message(connection, "GetTechnologies");
350 error = set_technology(connection, message,
351 "Powered", argv[1], TRUE);
353 printf("Enabled %s technology\n", argv[1]);
355 } else if (strcmp(argv[0], "disable") == 0) {
357 fprintf(stderr, "Disable requires a technology name or "
358 "the argument 'offlinemode' see help\n");
360 } else if (strcmp(argv[1], "offlinemode") == 0) {
361 error = set_manager(connection, "OfflineMode", FALSE);
363 printf("OfflineMode is now disabled\n");
365 message = get_message(connection, "GetTechnologies");
369 error = set_technology(connection, message,
370 "Powered", argv[1], FALSE);
372 printf("Disabled %s technology\n", argv[1]);
378 dbus_message_unref(message);
383 int commands_options(DBusConnection *connection, char *argv[], int argc)
386 int option_index = 0;
387 struct service_data service;
389 static struct option service_options[] = {
390 {"properties", required_argument, 0, 'p'},
394 static struct option config_options[] = {
395 {"nameservers", required_argument, 0, 'n'},
396 {"timeservers", required_argument, 0, 't'},
397 {"domains", required_argument, 0, 'd'},
398 {"ipv6", required_argument, 0, 'v'},
399 {"proxy", required_argument, 0, 'x'},
400 {"autoconnect", required_argument, 0, 'a'},
401 {"ipv4", required_argument, 0, 'i'},
402 {"remove", 0, 0, 'r'},
406 static struct option monitor_options[] = {
407 {"services", no_argument, 0, 's'},
408 {"tech", no_argument, 0, 'c'},
409 {"manager", no_argument, 0, 'm'},
413 if (strcmp(argv[0], "services") == 0) {
415 fprintf(stderr, "Too many arguments for services, "
420 printf("List of all services:\n");
421 error = list_properties(connection, "GetServices", NULL);
425 while ((c = getopt_long(argc, argv, "", service_options,
428 if (option_index == 0) {
429 printf("Services takes an "
430 "option, see help.\n");
435 error = service_switch(argc, argv, c,
443 } else if (strcmp(argv[0], "config") == 0) {
445 fprintf(stderr, "Config requires an option, "
449 while ((c = getopt_long(argc, argv, "", config_options,
452 if (option_index == 0) {
453 printf("Config requires an option, "
459 error = config_switch(argc, argv, c, connection);
464 } else if (strcmp(argv[0], "monitor") == 0) {
466 fprintf(stderr, "Too many arguments for monitor, "
471 error = monitor_connman(connection, "Service",
475 error = monitor_connman(connection, "Technology",
479 error = monitor_connman(connection, "Manager",
483 error = monitor_connman(connection, "Manager",
487 error = monitor_connman(connection, "Manager",
488 "TechnologyRemoved");
491 error = monitor_connman(connection, "Manager",
495 if (dbus_connection_add_filter(connection,
496 service_property_changed, NULL, NULL)
499 if (dbus_connection_add_filter(connection,
500 tech_property_changed, NULL, NULL)
503 if (dbus_connection_add_filter(connection,
504 tech_added_removed, NULL, NULL)
507 if (dbus_connection_add_filter(connection,
508 manager_property_changed, NULL, NULL)
511 if (dbus_connection_add_filter(connection,
512 manager_services_changed, NULL, NULL)
515 printf("Now monitoring all interfaces.\n");
517 while ((c = getopt_long(argc, argv, "", monitor_options,
520 if (option_index == 0) {
521 printf("Monitor takes an "
522 "option, see help\n");
527 error = monitor_switch(argc, argv, c, connection);