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");
151 error = set_service_property(conn, message, argv[1],
158 error = set_service_property(conn, message, argv[1],
159 "IPv4.Configuration", ipv4,
160 argv + MANDATORY_ARGS, num_args);
165 error = set_service_property(conn, message, argv[1],
166 "IPv6.Configuration", ipv6,
167 argv + MANDATORY_ARGS, num_args);
172 error = set_service_property(conn, message, argv[1],
173 "Nameservers.Configuration", NULL,
174 argv + MANDATORY_ARGS, num_args);
179 error = set_service_property(conn, message, argv[1],
180 "Timeservers.Configuration", NULL,
181 argv + MANDATORY_ARGS, num_args);
186 error = set_service_property(conn, message, argv[1],
187 "Domains.Configuration", NULL,
188 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);
200 } else if (strcmp(argv[3], "manual") == 0
201 && strcmp(argv[4], "servers") == 0
204 error = store_proxy_input(conn, message, argv[1],
209 fprintf(stderr, "Incorrect arguments\n");
214 fprintf(stderr, "Command not recognized, please check help\n");
221 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
227 error = monitor_connman(conn, "Service", "PropertyChanged");
230 if (dbus_connection_add_filter(conn, service_property_changed,
231 NULL, NULL) == FALSE)
233 printf("Now monitoring the service interface.\n");
236 error = monitor_connman(conn, "Technology", "PropertyChanged");
239 if (dbus_connection_add_filter(conn, tech_property_changed,
240 NULL, NULL) == FALSE)
242 printf("Now monitoring the technology interface.\n");
245 error = monitor_connman(conn, "Manager", "PropertyChanged");
248 error = monitor_connman(conn, "Manager", "TechnologyAdded");
251 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
254 error = monitor_connman(conn, "Manager", "ServicesChanged");
257 if (dbus_connection_add_filter(conn, manager_property_changed,
258 NULL, NULL) == FALSE)
260 if (dbus_connection_add_filter(conn, tech_added_removed,
261 NULL, NULL) == FALSE)
263 if (dbus_connection_add_filter(conn, manager_services_changed,
264 NULL, NULL) == FALSE)
266 printf("Now monitoring the manager interface.\n");
269 fprintf(stderr, "Command not recognized, please check help\n");
276 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
279 DBusMessage *message;
282 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
283 strcmp(argv[0], "h") == 0) {
285 } else if (strcmp(argv[0], "state") == 0) {
287 fprintf(stderr, "State cannot accept an argument, "
291 error = list_properties(connection, "GetProperties", NULL);
294 } else if (strcmp(argv[0], "technologies") == 0) {
296 fprintf(stderr, "Tech cannot accept an argument, "
300 error = list_properties(connection, "GetTechnologies", NULL);
303 } else if (strcmp(argv[0], "connect") == 0) {
305 fprintf(stderr, "Connect requires a service name or "
309 error = connect_service(connection, strip_service_path(argv[1]));
312 printf("Connected to: %s\n", strip_service_path(argv[1]));
313 } else if (strcmp(argv[0], "disconnect") == 0) {
315 fprintf(stderr, "Disconnect requires a service name or "
319 error = disconnect_service(connection, strip_service_path(argv[1]));
322 printf("Disconnected from: %s\n", strip_service_path(argv[1]));
323 } else if (strcmp(argv[0], "scan") == 0) {
325 fprintf(stderr, "Scan requires a service name or path, "
329 message = get_message(connection, "GetTechnologies");
330 error = scan_technology(connection, message, argv[1]);
333 dbus_message_unref(message);
334 } else if (strcmp(argv[0], "enable") == 0) {
336 fprintf(stderr, "Enable requires a technology name or "
337 "the argument 'offlinemode', see help\n");
340 if (strcmp(argv[1], "offlinemode") == 0) {
341 error = set_manager(connection, "OfflineMode", TRUE);
344 printf("OfflineMode is now enabled\n");
346 message = get_message(connection, "GetTechnologies");
347 error = set_technology(connection, message, "Powered",
351 printf("Enabled %s technology\n", argv[1]);
352 dbus_message_unref(message);
354 } else if (strcmp(argv[0], "disable") == 0) {
356 fprintf(stderr, "Disable requires a technology name or "
357 "the argument 'offlinemode' see help\n");
360 if (strcmp(argv[1], "offlinemode") == 0) {
361 error = set_manager(connection, "OfflineMode", FALSE);
364 printf("OfflineMode is now disabled\n");
366 message = get_message(connection, "GetTechnologies");
367 error = set_technology(connection, message, "Powered",
371 printf("Disabled %s technology\n", argv[1]);
372 dbus_message_unref(message);
379 int commands_options(DBusConnection *connection, char *argv[], int argc)
382 int option_index = 0;
383 struct service_data service;
385 static struct option service_options[] = {
386 {"properties", required_argument, 0, 'p'},
390 static struct option config_options[] = {
391 {"nameservers", required_argument, 0, 'n'},
392 {"timeservers", required_argument, 0, 't'},
393 {"domains", required_argument, 0, 'd'},
394 {"ipv6", required_argument, 0, 'v'},
395 {"proxy", required_argument, 0, 'x'},
396 {"autoconnect", required_argument, 0, 'a'},
397 {"ipv4", required_argument, 0, 'i'},
401 static struct option monitor_options[] = {
402 {"services", no_argument, 0, 's'},
403 {"tech", no_argument, 0, 'c'},
404 {"manager", no_argument, 0, 'm'},
408 if (strcmp(argv[0], "services") == 0) {
410 fprintf(stderr, "Too many arguments for services, "
415 printf("List of all services:\n");
416 error = list_properties(connection, "GetServices", NULL);
420 while ((c = getopt_long(argc, argv, "", service_options,
423 if (option_index == 0) {
424 printf("Services takes an "
425 "option, see help.\n");
430 error = service_switch(argc, argv, c,
438 } else if (strcmp(argv[0], "config") == 0) {
440 fprintf(stderr, "Config requires an option, "
444 while ((c = getopt_long(argc, argv, "", config_options,
447 if (option_index == 0) {
448 printf("Config requires an option, "
454 error = config_switch(argc, argv, c, connection);
459 } else if (strcmp(argv[0], "monitor") == 0) {
461 fprintf(stderr, "Too many arguments for monitor, "
466 error = monitor_connman(connection, "Service",
470 error = monitor_connman(connection, "Technology",
474 error = monitor_connman(connection, "Manager",
478 error = monitor_connman(connection, "Manager",
482 error = monitor_connman(connection, "Manager",
483 "TechnologyRemoved");
486 error = monitor_connman(connection, "Manager",
490 if (dbus_connection_add_filter(connection,
491 service_property_changed, NULL, NULL)
494 if (dbus_connection_add_filter(connection,
495 tech_property_changed, NULL, NULL)
498 if (dbus_connection_add_filter(connection,
499 tech_added_removed, NULL, NULL)
502 if (dbus_connection_add_filter(connection,
503 manager_property_changed, NULL, NULL)
506 if (dbus_connection_add_filter(connection,
507 manager_services_changed, NULL, NULL)
510 printf("Now monitoring all interfaces.\n");
512 while ((c = getopt_long(argc, argv, "", monitor_options,
515 if (option_index == 0) {
516 printf("Monitor takes an "
517 "option, see help\n");
522 error = monitor_switch(argc, argv, c, connection);