When the client is started with the interactive command, it drops into an
interactive shell mode. These files handle all the interactive commands using
readline, and provide quick funtionality that is similar to the plain command
line mode. Invalid input will be ignored, and the input is tokenized to allow
for various input errors.
---
 client/interactive.c |  158 ++++++++++++++++++++++++++++++++++++++++++++++++++
 client/interactive.h |   30 ++++++++++
 2 files changed, 188 insertions(+)
 create mode 100644 client/interactive.c
 create mode 100644 client/interactive.h

diff --git a/client/interactive.c b/client/interactive.c
new file mode 100644
index 0000000..c50d6ae
--- /dev/null
+++ b/client/interactive.c
@@ -0,0 +1,158 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2012  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#include "client/interactive.h"
+#include "client/services.h"
+#include "client/technology.h"
+#include "client/data_manager.h"
+
+static void show_interactive_help(void)
+{
+fprintf(stdout, "\n\t\t\tWelcome to Connman interactive mode.\n\n"
+"Helpful commands:\n"
+"      services                     Display list of all services\n"
+"      state                        Shows if the system is online or offline\n"
+"      tech                         Current technology on the system\n"
+"      scan                         Scan for new networks\n"
+"      enable/disable <technology>  Enables/disables given technology\n"
+"      connect/disconnect <service> Connect/disconnect to a given service\n"
+"      help, h                      Show this menu\n"
+"      quit, exit                Exit program\n");
+}
+
+static void handle_commands(DBusConnection *connection, char *token)
+{
+       DBusMessage *message;
+
+       for (;;) {
+               if (strncmp(token, "quit", strlen("quit")) == 0 ||
+                   strncmp(token, "exit", strlen("exit")) == 0) {
+                       free(token);
+                       exit(EXIT_SUCCESS);
+               }
+
+               if (strncmp(token, "help", strlen("help")) == 0 ||
+                   strncmp(token, "h", strlen("h")) == 0) {
+                       show_interactive_help();
+                       add_history(token);
+               }
+
+               if (strncmp(token, "services", strlen("services")) == 0) {
+                       list_properties(connection, "GetServices", NULL);
+                       add_history(token);
+               }
+
+               if (strncmp(token, "state", strlen("state")) == 0) {
+                       list_properties(connection, "GetProperties", NULL);
+                       add_history(token);
+               }
+
+               if (strncmp(token, "tech", strlen("tech")) == 0) {
+                       list_properties(connection, "GetTechnologies", NULL);
+                       add_history(token);
+               }
+
+               if (strncmp(token, "scan", strlen("scan")) == 0) {
+                       scan_technology(connection);
+                       add_history(token);
+                       /* FIXME: add function to receive and read the
+                        * ServicesChanged signal asynchronously. */
+               }
+
+               if (strncmp(token, "enable", strlen("enable")) == 0) {
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a technology "
+                                               "to enable\n");
+                               break;
+                       }
+                       message = get_message(connection, "GetTechnologies");
+                       if (set_technology(connection, message,  "Powered",
+                                                       token, TRUE) == 0)
+                               fprintf(stdout, "Enabled %s technology\n",
+                                               token);
+                       dbus_message_unref(message);
+                       add_history(token);
+               }
+
+               if (strncmp(token, "disable", strlen("disable")) == 0) {
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a technology "
+                                               "to disable\n");
+                               break;
+                       }
+                       message = get_message(connection, "GetTechnologies");
+                       if (set_technology(connection, message, "Powered",
+                                               token, FALSE) == 0)
+                               fprintf(stdout, "Disabled %s technology\n",
+                                               token);
+                       dbus_message_unref(message);
+                       add_history(token);
+               }
+
+               if (strncmp(token, "connect", strlen("conn")) == 0) {
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a service to "
+                                               "connect to\n");
+                               break;
+                       }
+                       connect_service(connection, strip_service_path(token));
+                       add_history(token);
+               }
+
+               if (strncmp(token, "disconnect", strlen("disconn")) == 0) {
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a service to "
+                                               "disconnect from\n");
+                               break;
+                       }
+                       disconnect_service(connection,
+                                               strip_service_path(token));
+                       add_history(token);
+               } else {
+                       break;
+               }
+       }
+}
+
+void show_interactive(DBusConnection *connection)
+{
+       static char *input;
+       char *token;
+       show_interactive_help();
+       while (TRUE) {
+               input = readline("\r> ");
+               token = strtok(input, " ");
+
+               if (token)
+                       handle_commands(connection, token);
+       }
+}
+
diff --git a/client/interactive.h b/client/interactive.h
new file mode 100644
index 0000000..85faea1
--- /dev/null
+++ b/client/interactive.h
@@ -0,0 +1,30 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2012  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __CLIENT_INTERACTIVE_H
+#define __CLIENT_INTERACTIVE_H
+
+#include <dbus/dbus.h>
+
+void show_interactive(DBusConnection *connection);
+
+#endif
+
-- 
1.7.9.5

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to