Add preparation for Network Manager compatibility
authorMarcel Holtmann <marcel@holtmann.org>
Thu, 3 Jan 2008 21:56:58 +0000 (22:56 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Thu, 3 Jan 2008 21:56:58 +0000 (22:56 +0100)
src/connman.h
src/main.c
src/manager.c

index fdb9fa8..ec7a1f3 100644 (file)
 
 #define CONNMAN_SERVICE  "org.freedesktop.connman"
 
-#define CONNMAN_MANAGER_PATH "/"
+#define CONNMAN_MANAGER_PATH       "/"
 #define CONNMAN_MANAGER_INTERFACE  CONNMAN_SERVICE ".Manager"
 
 #define CONNMAN_IFACE_BASEPATH  "/interface"
 #define CONNMAN_IFACE_INTERFACE  CONNMAN_SERVICE ".Interface"
 
-int __connman_manager_init(DBusConnection *conn);
+#define NM_SERVICE    "org.freedesktop.NetworkManager"
+#define NM_PATH       "/org/freedesktop/NetworkManager"
+#define NM_INTERFACE  NM_SERVICE
+#define NM_DEVICE     NM_SERVICE ".Devices"
+
+int __connman_manager_init(DBusConnection *conn, int compat);
 void __connman_manager_cleanup(void);
 
 #include <connman/plugin.h>
index 14198cd..1f30cb4 100644 (file)
@@ -27,7 +27,9 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <syslog.h>
 #include <signal.h>
+#include <getopt.h>
 #include <sys/stat.h>
 
 #include <gdbus.h>
@@ -41,10 +43,63 @@ static void sig_term(int sig)
        g_main_loop_quit(main_loop);
 }
 
+static void usage(void)
+{
+       printf("Connection Manager version %s\n\n", VERSION);
+
+       printf("Usage:\n"
+               "\tconnmand [options]\n"
+               "\n");
+
+       printf("Options:\n"
+               "\t-c, --compat         Enable Network Manager compatibility\n"
+               "\t-n, --nodaemon       Don't fork daemon to background\n"
+               "\t-h, --help           Display help\n"
+               "\n");
+}
+
+static struct option options[] = {
+       { "nodaemon", 0, 0, 'n' },
+       { "compat",   0, 0, 'c' },
+       { "help",     0, 0, 'h' },
+       { }
+};
+
 int main(int argc, char *argv[])
 {
        DBusConnection *conn;
        struct sigaction sa;
+       int log_option = LOG_NDELAY | LOG_PID;
+       int opt, detach = 1, compat = 0;
+
+       while ((opt = getopt_long(argc, argv, "+nch", options, NULL)) != EOF) {
+               switch(opt) {
+               case 'n':
+                       detach = 0;
+                       break;
+               case 'c':
+                       compat = 1;
+                       break;
+               case 'h':
+               default:
+                       usage();
+                       exit(0);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+       optind = 0;
+
+       if (detach) {
+               if (daemon(0, 0)) {
+                       perror("Can't start daemon");
+                       exit(1);
+               }
+       } else
+               log_option |= LOG_PERROR;
+
+       openlog("connmand", log_option, LOG_DAEMON);
 
        mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
                        S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
@@ -57,7 +112,12 @@ int main(int argc, char *argv[])
                exit(1);
        }
 
-       __connman_manager_init(conn);
+       if (compat) {
+               if (g_dbus_request_name(conn, NM_SERVICE) == FALSE)
+                       compat = 0;
+       }
+
+       __connman_manager_init(conn, compat);
 
        __connman_plugin_init();
 
@@ -86,5 +146,7 @@ int main(int argc, char *argv[])
 
        rmdir(STATEDIR);
 
+       closelog();
+
        return 0;
 }
index 1afecc8..d8c6a5a 100644 (file)
@@ -62,9 +62,139 @@ static GDBusSignalTable manager_signals[] = {
        { },
 };
 
+static DBusMessage *activate_device(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       DBusMessage *reply;
+       const char *path;
+
+       DBG("conn %p", conn);
+
+       dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
+                                                       DBUS_TYPE_INVALID);
+
+       DBG("device %s", path);
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+       return reply;
+}
+
+static DBusMessage *set_wireless(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       DBusMessage *reply;
+       dbus_bool_t enabled;
+
+       DBG("conn %p", conn);
+
+       dbus_message_get_args(msg, NULL, DBUS_TYPE_BOOLEAN, &enabled,
+                                                       DBUS_TYPE_INVALID);
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+       return reply;
+}
+
+static DBusMessage *get_wireless(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       DBusMessage *reply;
+       dbus_bool_t enabled = TRUE;
+
+       DBG("conn %p", conn);
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &enabled,
+                                                       DBUS_TYPE_INVALID);
+
+       return reply;
+}
+
+static DBusMessage *do_sleep(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       DBusMessage *reply;
+
+       DBG("conn %p", conn);
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+       return reply;
+}
+
+static DBusMessage *do_wake(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       DBusMessage *reply;
+
+       DBG("conn %p", conn);
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+       return reply;
+}
+
+enum {
+       NM_STATE_UNKNOWN = 0,
+       NM_STATE_ASLEEP,
+       NM_STATE_CONNECTING,
+       NM_STATE_CONNECTED,
+       NM_STATE_DISCONNECTED
+};
+
+static DBusMessage *get_state(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       DBusMessage *reply;
+       dbus_uint32_t state = NM_STATE_DISCONNECTED;
+
+       DBG("conn %p", conn);
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
+                                                       DBUS_TYPE_INVALID);
+
+       return reply;
+}
+
+static GDBusMethodTable nm_methods[] = {
+       { "getDevices",            "",  "ao", list_interfaces },
+       { "setActiveDevice",       "o", "",   activate_device },
+       { "setWirelessEnabled",    "b", "",   set_wireless    },
+       { "getWirelessEnabled",    "",  "b",  get_wireless    },
+       { "sleep",                 "",  "",   do_sleep        },
+       { "wake",                  "",  "",   do_wake         },
+       { "state",                 "",  "u",  get_state       },
+       { },
+};
+
 static DBusConnection *connection = NULL;
+static int nm_compat = 0;
 
-int __connman_manager_init(DBusConnection *conn)
+int __connman_manager_init(DBusConnection *conn, int compat)
 {
        DBG("conn %p", conn);
 
@@ -79,6 +209,15 @@ int __connman_manager_init(DBusConnection *conn)
                                                manager_methods,
                                                manager_signals, NULL);
 
+       if (compat) {
+               g_dbus_register_object(connection, NM_PATH, NULL, NULL);
+
+               g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
+                                               nm_methods, NULL, NULL);
+
+               nm_compat = 1;
+       }
+
        return 0;
 }
 
@@ -86,6 +225,12 @@ void __connman_manager_cleanup(void)
 {
        DBG("conn %p", connection);
 
+       if (nm_compat) {
+               g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
+
+               g_dbus_unregister_object(connection, NM_PATH);
+       }
+
        g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
                                                CONNMAN_MANAGER_INTERFACE);