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
37 #include "technology.h"
38 #include "data_manager.h"
40 #include "interactive.h"
42 #define MANDATORY_ARGS 3
44 static char *ipv4[] = {
52 static char *ipv6[] = {
61 static char *proxy_simple[] = {
69 printf("Usage: connmanctl <command> [args]\n"
70 " enable Enables given technology\n"
72 " offlinemode Enables OfflineMode\n"
73 " disable Disables given technology\n"
75 " offlinemode Disables OfflineMode\n"
76 " state Shows if the system is online or offline\n"
77 " services Display list of all services\n"
78 " --properties <service name> Show properties of service\n"
79 " technologies Current technology on the system\n"
80 " scan <technology> Scans for new services on the given technology\n"
81 " connect <service> Connect to a given service\n"
82 " disconnect <service> Disconnect from service\n"
83 " config <service> [arg] Set certain config options\n"
84 " --autoconnect=y/n Set autoconnect to service\n"
85 " --nameservers <names> Set manual name servers\n"
86 " --timeservers <names> Set manual time servers\n"
87 " --domains <domains> Set manual domains\n"
88 " --ipv4 Set ipv4 configuration\n"
89 " [METHOD|DHCP|AUTO|MANUAL] [IP] [NETMASK] [GATEWAY]\n"
90 " --ipv6 Set ipv6 configuration\n"
91 " [METHOD|AUTO|MANUAL|OFF] [IP] [PREFIXLENGTH] [GATEWAY]\n"
92 " [PRIVACY|DISABLED|ENABLED|PREFERED]\n"
93 " --proxy Set proxy configuration\n"
94 " [METHOD|URL|SERVERS|EXCLUDES]\n"
95 " if METHOD = manual, enter 'servers' then the list of servers\n"
96 " then enter 'excludes' then the list of excludes\n"
97 " --remove Remove the service from favorite\n"
98 " monitor Monitor signals from all Connman interfaces\n"
99 " --services Monitor signals from the Service interface\n"
100 " --tech Monitor signals from the Technology interface\n"
101 " --manager Monitor signals from the Manager interface\n"
102 " help, --help, (no arguments) Show this dialogue\n"
103 " exit, quit, q Quit interactive mode\n"
104 "\nNote: arguments and output are considered EXPERIMENTAL for now.\n\n");
107 int service_switch(int argc, char *argv[], int c, DBusConnection *conn,
108 struct service_data *service)
111 DBusMessage *message;
114 message = get_message(conn, "GetServices");
120 name = find_service(conn, message, argv[2], service);
126 error = list_properties(conn, "GetServices", (char *) name);
129 fprintf(stderr, "Command not recognized, please check help\n");
134 dbus_message_unref(message);
139 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
141 DBusMessage *message;
142 int num_args = argc - MANDATORY_ARGS;
146 message = get_message(conn, "GetServices");
166 error = set_service_property(conn, message, argv[1],
171 error = set_service_property(conn, message, argv[1],
172 "IPv4.Configuration", ipv4,
173 argv + MANDATORY_ARGS, num_args);
176 error = set_service_property(conn, message, argv[1],
177 "IPv6.Configuration", ipv6,
178 argv + MANDATORY_ARGS, num_args);
181 error = set_service_property(conn, message, argv[1],
182 "Nameservers.Configuration", NULL,
183 argv + MANDATORY_ARGS, num_args);
186 error = set_service_property(conn, message, argv[1],
187 "Timeservers.Configuration", NULL,
188 argv + MANDATORY_ARGS, num_args);
191 error = set_service_property(conn, message, argv[1],
192 "Domains.Configuration", NULL,
193 argv + MANDATORY_ARGS, num_args);
196 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
197 (strcmp(argv[3], "auto") == 0 && argc < 6)) {
198 error = set_service_property(conn, message, argv[1],
199 "Proxy.Configuration", proxy_simple,
200 argv + MANDATORY_ARGS, num_args);
201 } else if (strcmp(argv[3], "manual") == 0
202 && strcmp(argv[4], "servers") == 0
205 error = store_proxy_input(conn, message, argv[1],
208 fprintf(stderr, "Incorrect arguments\n");
213 error = remove_service(conn, message, argv[1]);
216 fprintf(stderr, "Command not recognized, please check help\n");
221 dbus_message_unref(message);
226 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
232 error = monitor_connman(conn, "Service", "PropertyChanged");
235 if (dbus_connection_add_filter(conn, service_property_changed,
236 NULL, NULL) == FALSE)
238 printf("Now monitoring the service interface.\n");
241 error = monitor_connman(conn, "Technology", "PropertyChanged");
244 if (dbus_connection_add_filter(conn, tech_property_changed,
245 NULL, NULL) == FALSE)
247 printf("Now monitoring the technology interface.\n");
250 error = monitor_connman(conn, "Manager", "PropertyChanged");
253 error = monitor_connman(conn, "Manager", "TechnologyAdded");
256 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
259 error = monitor_connman(conn, "Manager", "ServicesChanged");
262 if (dbus_connection_add_filter(conn, manager_property_changed,
263 NULL, NULL) == FALSE)
265 if (dbus_connection_add_filter(conn, tech_added_removed,
266 NULL, NULL) == FALSE)
268 if (dbus_connection_add_filter(conn, manager_services_changed,
269 NULL, NULL) == FALSE)
271 printf("Now monitoring the manager interface.\n");
274 fprintf(stderr, "Command not recognized, please check help\n");
281 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
283 DBusMessage *message = NULL;
287 if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0 ||
288 strcmp(argv[0], "h") == 0) {
290 } else if (strcmp(argv[0], "state") == 0) {
292 fprintf(stderr, "State cannot accept an argument, "
296 error = list_properties(connection,
297 "GetProperties", NULL);
298 } else if (strcmp(argv[0], "technologies") == 0) {
300 fprintf(stderr, "Tech cannot accept an argument, "
304 error = list_properties(connection,
305 "GetTechnologies", NULL);
306 } else if (strcmp(argv[0], "connect") == 0) {
308 fprintf(stderr, "Connect requires a service name or "
312 error = connect_service(connection,
313 strip_service_path(argv[1]));
315 printf("Connected to: %s\n",
316 strip_service_path(argv[1]));
317 } else if (strcmp(argv[0], "disconnect") == 0) {
319 fprintf(stderr, "Disconnect requires a service name or "
323 error = disconnect_service(connection,
324 strip_service_path(argv[1]));
326 printf("Disconnected from: %s\n",
327 strip_service_path(argv[1]));
328 } else if (strcmp(argv[0], "scan") == 0) {
330 fprintf(stderr, "Scan requires a service name or path, "
334 message = get_message(connection, "GetTechnologies");
338 error = scan_technology(connection, message, argv[1]);
339 } else if (strcmp(argv[0], "enable") == 0) {
341 fprintf(stderr, "Enable requires a technology name or "
342 "the argument 'offlinemode', see help\n");
344 } else if (strcmp(argv[1], "offlinemode") == 0) {
345 error = set_manager(connection, "OfflineMode", TRUE);
347 printf("OfflineMode is now enabled\n");
349 message = get_message(connection, "GetTechnologies");
353 error = set_technology(connection, message,
354 "Powered", argv[1], TRUE);
356 printf("Enabled %s technology\n", argv[1]);
358 } else if (strcmp(argv[0], "disable") == 0) {
360 fprintf(stderr, "Disable requires a technology name or "
361 "the argument 'offlinemode' see help\n");
363 } else if (strcmp(argv[1], "offlinemode") == 0) {
364 error = set_manager(connection, "OfflineMode", FALSE);
366 printf("OfflineMode is now disabled\n");
368 message = get_message(connection, "GetTechnologies");
372 error = set_technology(connection, message,
373 "Powered", argv[1], FALSE);
375 printf("Disabled %s technology\n", argv[1]);
381 dbus_message_unref(message);
386 int commands_options(DBusConnection *connection, char *argv[], int argc)
389 int option_index = 0;
390 struct service_data service;
392 static struct option service_options[] = {
393 {"properties", required_argument, 0, 'p'},
397 static struct option config_options[] = {
398 {"nameservers", required_argument, 0, 'n'},
399 {"timeservers", required_argument, 0, 't'},
400 {"domains", required_argument, 0, 'd'},
401 {"ipv6", required_argument, 0, 'v'},
402 {"proxy", required_argument, 0, 'x'},
403 {"autoconnect", required_argument, 0, 'a'},
404 {"ipv4", required_argument, 0, 'i'},
405 {"remove", 0, 0, 'r'},
409 static struct option monitor_options[] = {
410 {"services", no_argument, 0, 's'},
411 {"tech", no_argument, 0, 'c'},
412 {"manager", no_argument, 0, 'm'},
416 if (strcmp(argv[0], "services") == 0) {
418 fprintf(stderr, "Too many arguments for services, "
423 printf("List of all services:\n");
424 error = list_properties(connection, "GetServices", NULL);
428 while ((c = getopt_long(argc, argv, "", service_options,
431 if (option_index == 0) {
432 printf("Services takes an "
433 "option, see help.\n");
438 error = service_switch(argc, argv, c,
446 } else if (strcmp(argv[0], "config") == 0) {
448 fprintf(stderr, "Config requires an option, "
452 while ((c = getopt_long(argc, argv, "", config_options,
455 if (option_index == 0) {
456 printf("Config requires an option, "
462 error = config_switch(argc, argv, c, connection);
467 } else if (strcmp(argv[0], "monitor") == 0) {
469 fprintf(stderr, "Too many arguments for monitor, "
474 error = monitor_connman(connection, "Service",
478 error = monitor_connman(connection, "Technology",
482 error = monitor_connman(connection, "Manager",
486 error = monitor_connman(connection, "Manager",
490 error = monitor_connman(connection, "Manager",
491 "TechnologyRemoved");
494 error = monitor_connman(connection, "Manager",
498 if (dbus_connection_add_filter(connection,
499 service_property_changed, NULL, NULL)
502 if (dbus_connection_add_filter(connection,
503 tech_property_changed, NULL, NULL)
506 if (dbus_connection_add_filter(connection,
507 tech_added_removed, NULL, NULL)
510 if (dbus_connection_add_filter(connection,
511 manager_property_changed, NULL, NULL)
514 if (dbus_connection_add_filter(connection,
515 manager_services_changed, NULL, NULL)
518 printf("Now monitoring all interfaces.\n");
520 while ((c = getopt_long(argc, argv, "", monitor_options,
523 if (option_index == 0) {
524 printf("Monitor takes an "
525 "option, see help\n");
530 error = monitor_switch(argc, argv, c, connection);