client: Update Makefile.am and add new command line client main file
authorCeara Chewning <ceara.k.chewning@intel.com>
Wed, 12 Sep 2012 22:06:00 +0000 (15:06 -0700)
committerPatrik Flykt <patrik.flykt@linux.intel.com>
Fri, 14 Sep 2012 13:32:00 +0000 (16:32 +0300)
The new main.c uses the previously added services, technology, manager,
interactive, and monitor files to create the connmanctl command line
interface. It is able to display properties of services and technologies,
and set/enable them.

Makefile.am
client/main.c

index 5c5b95c..fe8e75d 100644 (file)
@@ -135,10 +135,21 @@ script_LTLIBRARIES =
 include Makefile.plugins
 
 if CLIENT
-noinst_PROGRAMS += client/cm
+noinst_PROGRAMS += client/connmanctl
 
-client_cm_SOURCES = client/main.c
-client_cm_LDADD = @DBUS_LIBS@
+dist_man_MANS = doc/connmanctl.1
+
+client_connmanctl_SOURCES =  $(gdbus_sources) src/connman.h \
+                       include/log.h src/log.c \
+                       include/dbus.h src/dbus.c \
+                       client/data_manager.h client/data_manager.c \
+                       client/services.h client/services.c \
+                       client/technology.h client/technology.c \
+                       client/interactive.h client/interactive.c \
+                       client/monitor.h client/monitor.c \
+                       client/commands.c client/main.c
+
+client_connmanctl_LDADD = @DBUS_LIBS@ @GLIB_LIBS@ -lreadline -ldl
 endif
 
 if WISPR
index 4a88d6e..9e456f1 100644 (file)
@@ -2,7 +2,8 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
+ *  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
  *
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <stdio.h>
-#include <errno.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
 #include <string.h>
+#include <errno.h>
+#include <readline/readline.h>
 
-#include <dbus/dbus.h>
+#include <glib.h>
 
-#define CONNMAN_SERVICE                        "net.connman"
+#include <dbus/dbus.h>
+#include <gdbus.h>
 
-#define CONNMAN_MANAGER_INTERFACE      CONNMAN_SERVICE ".Manager"
-#define CONNMAN_MANAGER_PATH           "/"
+#include "client/data_manager.h"
+#include "client/services.h"
+#include "client/technology.h"
+#include "client/interactive.h"
+#include "client/monitor.h"
 
-struct service_data {
-       const char *path;
-       const char *name;
-       dbus_bool_t favorite;
-};
+static GMainLoop *main_loop;
 
-static DBusMessage *get_services(DBusConnection *connection)
+static gboolean timeout_wait(gpointer data)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
-
-       message = dbus_message_new_method_call(CONNMAN_SERVICE,
-                                               CONNMAN_MANAGER_PATH,
-                                               CONNMAN_MANAGER_INTERFACE,
-                                                       "GetServices");
-       if (message == NULL)
-               return NULL;
-
-       dbus_error_init(&error);
-
-       reply = dbus_connection_send_with_reply_and_block(connection,
-                                                       message, -1, &error);
-       if (reply == NULL) {
-               if (dbus_error_is_set(&error) == TRUE) {
-                       fprintf(stderr, "%s\n", error.message);
-                       dbus_error_free(&error);
-               } else
-                       fprintf(stderr, "Failed to get properties\n");
-               dbus_message_unref(message);
-               return NULL;
+       static int i;
+       i++;
+       /* Set to whatever number of retries is wanted/needed */
+       if (i == 1) {
+               g_main_loop_quit(data);
+               return FALSE;
        }
-
-       dbus_message_unref(message);
-
-       return reply;
+       return TRUE;
 }
 
-static DBusMessage *lookup_service(DBusConnection *connection,
-                                                       const char *pattern)
+static void rl_handler(char *input)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
-
-       message = dbus_message_new_method_call(CONNMAN_SERVICE,
-                                               CONNMAN_MANAGER_PATH,
-                                               CONNMAN_MANAGER_INTERFACE,
-                                                       "LookupService");
-       if (message == NULL)
-               return NULL;
-
-       dbus_message_append_args(message, DBUS_TYPE_STRING, &pattern,
-                                                       DBUS_TYPE_INVALID);
-
-       dbus_error_init(&error);
-
-       reply = dbus_connection_send_with_reply_and_block(connection,
-                                                       message, -1, &error);
-       if (reply == NULL) {
-               if (dbus_error_is_set(&error) == TRUE) {
-                       fprintf(stderr, "%s\n", error.message);
-                       dbus_error_free(&error);
-               } else
-                       fprintf(stderr, "Failed to get properties\n");
-               dbus_message_unref(message);
-               return NULL;
-       }
 
-       dbus_message_unref(message);
-
-       return reply;
+       if (input == NULL)
+               exit(EXIT_FAILURE);
+       else
+               printf("Use ctrl-d to exit\n");
 }
 
-static void extract_properties(DBusMessageIter *dict,
-                                       struct service_data *service)
-{
-       while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
-               DBusMessageIter entry, value;
-               const char *key;
-
-               dbus_message_iter_recurse(dict, &entry);
-               dbus_message_iter_get_basic(&entry, &key);
-
-               dbus_message_iter_next(&entry);
-
-               dbus_message_iter_recurse(&entry, &value);
-
-               //type = dbus_message_iter_get_arg_type(&value);
-               //dbus_message_iter_get_basic(&value, &val);
-
-               if (strcmp(key, "Name") == 0)
-                       dbus_message_iter_get_basic(&value, &service->name);
-               else if (strcmp(key, "Favorite") == 0)
-                       dbus_message_iter_get_basic(&value, &service->favorite);
-
-               dbus_message_iter_next(dict);
-       }
+static gboolean readmonitor(GIOChannel *channel, GIOCondition condition,
+                                               gpointer user_data){
+       rl_callback_read_char();
+       return TRUE;
 }
 
-static void extract_services(DBusMessage *message)
+int main(int argc, char *argv[])
 {
-       DBusMessageIter iter, array;
-
-       dbus_message_iter_init(message, &iter);
-       dbus_message_iter_recurse(&iter, &array);
-
-       while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
-               DBusMessageIter entry, dict;
-               struct service_data service;
-               const char *path;
+       DBusConnection *connection;
+       DBusError err;
+       int events, error;
+       GIOChannel *gchan;
+       main_loop = g_main_loop_new(NULL, FALSE);
 
-               dbus_message_iter_recurse(&array, &entry);
-               dbus_message_iter_get_basic(&entry, &path);
+       dbus_error_init(&err);
 
-               service.path = strrchr(path, '/') + 1;
+       connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
 
-               dbus_message_iter_next(&entry);
-
-               dbus_message_iter_recurse(&entry, &dict);
-               extract_properties(&dict, &service);
-
-               printf("%c %-20s { %-50s }\n",
-                               service.favorite == TRUE ? '*' : ' ',
-                                               service.name, service.path);
-
-               dbus_message_iter_next(&array);
+       if (dbus_error_is_set(&err)) {
+               fprintf(stderr, "Connection Error: %s\n", err.message);
+               dbus_error_free(&err);
        }
-}
-
-static int cmd_list_services(DBusConnection *connection)
-{
-       DBusMessage *message;
 
-       message = get_services(connection);
-       if (message == NULL)
-               return -1;
-
-       extract_services(message);
-
-       dbus_message_unref(message);
-
-       return 0;
-}
-
-static int cmd_show_service(DBusConnection *connection, const char *pattern)
-{
-       DBusMessage *message;
-       const char *path;
-
-       message = lookup_service(connection, pattern);
-       if (message == NULL)
-               return -1;
-
-       dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
-                                                       DBUS_TYPE_INVALID);
-
-       printf("Service: %s\n", path);
-
-       dbus_message_unref(message);
-
-       return 0;
-}
-
-static void usage(const char *program)
-{
-       printf("ConnMan utility ver %s\n\n", VERSION);
-
-       printf("Usage:\n"
-               "\t%s <command> [options]\n\n", program);
-
-       printf("Commands:\n"
-               "\thelp\n"
-               "\tlist\n"
-               "\tshow <service>\n"
-               "\n");
-}
-
-int main(int argc, char *argv[])
-{
-       DBusConnection *conn;
-
-       if (argc > 1 && strcmp(argv[1], "help") == 0) {
-               usage(argv[0]);
-               exit(0);
+       if (connection == NULL) {
+               fprintf(stderr, "Could not connect to system bus...exiting\n");
+               exit(EXIT_FAILURE);
        }
 
-       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
-       if (!conn) {
-               fprintf(stderr, "Can't get on system bus\n");
-               exit(1);
+       if (argc < 2) {
+               show_help();
+               exit(EXIT_SUCCESS);
        }
 
-       if (argc > 1) {
-               if (strcmp(argv[1], "list") == 0)
-                       cmd_list_services(conn);
-               else if (strcmp(argv[1], "show") == 0) {
-                       if (argc > 2)
-                               cmd_show_service(conn, argv[2]);
-                       else
-                               usage(argv[0]);
+       if (strcmp(argv[1], "interactive") == 0) {
+               if (argc != 2) {
+                       fprintf(stderr, "Interactive cannot accept an argument,"
+                                                               " see help\n");
+                       return -EINVAL;
                }
-       } else
-               usage(argv[0]);
+               show_interactive(connection, main_loop);
+       }
 
-       dbus_connection_unref(conn);
+       error = commands_no_options(connection, argv + 1, argc - 1);
+       if (error == -1) {
+               error = commands_options(connection, argv + 1, argc - 1);
+               if (strcmp(argv[1], "monitor") != 0)
+                       return error;
+       } else {
+               return error;
+       }
 
+       if (error == -1) {
+               fprintf(stderr, "%s is not a valid command, check help.\n",
+                                                       argv[1]);
+               return -EINVAL;
+       }
+       gchan = g_io_channel_unix_new(fileno(stdin));
+       events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+       g_io_add_watch(gchan, events, readmonitor, NULL);
+       rl_callback_handler_install("", rl_handler);
+
+       if (strcmp(argv[1], "monitor") != 0)
+               g_timeout_add_full(G_PRIORITY_DEFAULT, 100, timeout_wait,
+                                                              main_loop, NULL);
+       g_main_loop_run(main_loop);
+       rl_callback_handler_remove();
+       g_io_channel_unref(gchan);
+       if (main_loop != NULL)
+               g_main_loop_unref(main_loop);
        return 0;
 }