When the client is started with the interactive option, it should drop into an
interactive shell mode. These files handle all the interactive commands
using readline, and provide quick functionality that is similar to plain
synchronous command. Invalid input will be ignored, and the input is tokenized
to allow for various input errors.
---
 client/interactive.c |  202 ++++++++++++++++++++++++++++++++++++++++++++++++++
 client/interactive.h |   30 ++++++++
 2 files changed, 232 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..96cc02b
--- /dev/null
+++ b/client/interactive.c
@@ -0,0 +1,202 @@
+/*
+ *
+ *  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 <glib.h>
+
+#include "client/interactive.h"
+#include "client/services.h"
+#include "client/technology.h"
+#include "client/data_manager.h"
+
+static void show_help(void)
+{
+       printf("\n\t\t\t[Welcome to Connman interactive mode]\n\n"
+       "  enable                             Enables given technology\n"
+       "        <technology>\n"
+       "        offlinemode                  Enables OfflineMode\n"
+       "  disable                            Disables given technology\n"
+       "        <technology>\n"
+       "        offlinemode                  Disables OfflineMode\n"
+       "  state                              Shows if the system is online or 
offline\n"
+       "  services                           Display list of all services\n"
+       "  tech                               Current technology on the 
system\n"
+       "  scan <technology>                  Scans for new services on the 
given technology\n"
+       "  connect <service>                  Connect to a given service\n"
+       "  disconnect <service>               Disconnect from service\n"
+       "  help, h                            Show this dialogue\n"
+       "  quit, q, exit                      Exit program\n");
+}
+
+static int handle_commands(DBusConnection *connection, char *token)
+{
+       DBusMessage *message;
+       gchar *prev_command = NULL;
+
+       for (;;) {
+               if (strcmp(token, "quit") == 0 || strcmp(token, "exit") == 0 ||
+                                               strcmp(token, "q") == 0) {
+                       g_free(prev_command);
+                       free(token);
+                       return 0;
+               }
+
+               if (strcmp(token, "help") == 0 || strcmp(token, "h") == 0) {
+                       show_help();
+                       add_history(token);
+               }
+
+               if (strcmp(token, "services") == 0) {
+                       list_properties(connection, "GetServices", NULL);
+                       add_history(token);
+               }
+
+               if (strcmp(token, "state") == 0) {
+                       list_properties(connection, "GetProperties", NULL);
+                       add_history(token);
+               }
+
+               if (strcmp(token, "tech") == 0) {
+                       list_properties(connection, "GetTechnologies", NULL);
+                       add_history(token);
+               }
+
+               if (strcmp(token, "scan") == 0) {
+                       prev_command = g_strdup(token);
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a technology "
+                                               "to scan\n");
+                               break;
+                       }
+                       message = get_message(connection, "GetTechnologies");
+                       scan_technology(connection, message, token);
+                       dbus_message_unref(message);
+                       prev_command = g_strdup_printf("%s %s", prev_command,
+                                                       token);
+                       add_history(prev_command);
+                       /* FIXME: add function to receive and read the
+                        * ServicesChanged signal asynchronously. */
+               }
+
+               if (strcmp(token, "enable") == 0) {
+                       prev_command = g_strdup(token);
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a technology "
+                                               "to enable\n");
+                               break;
+                       }
+                       if (strcmp(token, "offlinemode") == 0) {
+                               set_manager(connection, "OfflineMode", TRUE);
+                               printf("OfflineMode is now enabled\n");
+                               break;
+                       }
+                       message = get_message(connection, "GetTechnologies");
+                       if (set_technology(connection, message,  "Powered",
+                                                       token, TRUE) == 0)
+                               printf("Enabled %s technology\n",
+                                               token);
+                       dbus_message_unref(message);
+                       prev_command = g_strdup_printf("%s %s", prev_command,
+                                                       token);
+                       add_history(prev_command);
+               }
+
+               if (strcmp(token, "disable") == 0) {
+                       prev_command = g_strdup(token);
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a technology "
+                                               "to disable\n");
+                               break;
+                       }
+                       if (strcmp(token, "offlinemode") == 0) {
+                               set_manager(connection, "OfflineMode", FALSE);
+                               printf("OfflineMode is now disabled\n");
+                               break;
+                       }
+                       message = get_message(connection, "GetTechnologies");
+                       if (set_technology(connection, message, "Powered",
+                                               token, FALSE) == 0)
+                               printf("Disabled %s technology\n",
+                                               token);
+                       dbus_message_unref(message);
+                       prev_command = g_strdup_printf("%s %s", prev_command,
+                                                       token);
+                       add_history(prev_command);
+               }
+
+               if (strcmp(token, "connect") == 0) {
+                       prev_command = g_strdup(token);
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a service to "
+                                               "connect to\n");
+                               break;
+                       }
+                       connect_service(connection, strip_service_path(token));
+                       prev_command = g_strdup_printf("%s %s", prev_command,
+                                                       token);
+                       add_history(prev_command);
+               }
+
+               if (strcmp(token, "disconnect") == 0) {
+                       prev_command = g_strdup(token);
+                       token = strtok(NULL, " ");
+                       if (token == NULL) {
+                               fprintf(stderr, "  ! Please input a service to "
+                                               "disconnect from\n");
+                               break;
+                       }
+                       disconnect_service(connection,
+                                               strip_service_path(token));
+                       prev_command = g_strdup_printf("%s %s", prev_command,
+                                                       token);
+                       add_history(prev_command);
+               } else {
+                       break;
+               }
+       }
+       return 1;
+}
+
+void show_interactive(DBusConnection *connection)
+{
+       static char *input;
+       char *token;
+
+       show_help();
+       while (TRUE) {
+               input = readline("> ");
+               token = strtok(input, " ");
+
+               if (token)
+                       if (handle_commands(connection, token) != 1)
+                               break;
+       }
+}
+
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