The new main.c compiles with the previously added services, technology,
data_manager, interactive, and monitor files to create the CLI called
connmanctl. This is the program a user would run to use connman from the
command line. It is able to view properties of services and technologies,
and set/enable connman's properties. The updated Makefile compiles all of
the above source client files.
---
 Makefile.am   |   17 +++-
 client/main.c |  274 +++++++++++++++++----------------------------------------
 2 files changed, 95 insertions(+), 196 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 5c5b95c..51aed19 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -124,7 +124,7 @@ AM_CFLAGS = @DBUS_CFLAGS@ @GLIB_CFLAGS@ @XTABLES_CFLAGS@ \
 
 INCLUDES = -I$(builddir)/include -I$(builddir)/src -I$(srcdir)/gdbus
 
-EXTRA_DIST = src/genbuiltin src/connman-dbus.conf src/connman-polkit.conf \
+EXTRA_DIST = src/genbuiltin src/connman-dbusls.conf src/connman-polkit.conf \
                                                plugins/connman-nmcompat.conf
 
 
@@ -135,10 +135,19 @@ 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@
+man_MANS = doc/connmanctl.1
+
+client_connmanctl_SOURCES =  $(gdbus_sources) src/log.c include/log.h \
+                       src/dbus.c include/dbus.h client/data_manager.c \
+                       client/data_manager.h client/services.c \
+                       client/services.h client/technology.c \
+                       client/technology.h client/interactive.c \
+                       client/interactive.h client/monitor.c \
+                       client/monitor.h client/commands.c client/main.c
+
+client_connmanctl_LDADD = @DBUS_LIBS@ @GLIB_LIBS@ -lreadline -ldl
 endif
 
 if WISPR
diff --git a/client/main.c b/client/main.c
index 4a88d6e..1bf32cc 100644
--- a/client/main.c
+++ b/client/main.c
@@ -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
@@ -19,226 +20,115 @@
  *
  */
 
-#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 *chan, GIOCondition cond,
+                                               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]);
-
-       dbus_connection_unref(conn);
+               show_interactive(connection, main_loop);
+       }
 
+       error = commands_no_options(connection, argv + 1, argc - 1);
+       if (error != -1) {
+               if (error != 0)
+                       return error;
+       } else if (commands_options(connection, argv + 1, argc - 1) != -1) {
+               if (strcmp(argv[1], "monitor") != 0)
+                       return error;
+       } else {
+               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;
 }
+
-- 
1.7.9.5

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

Reply via email to