From 61d0b6297c0f8e96a5ce79d03009a2d436e742f4 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 14 Oct 2008 15:12:01 +0200 Subject: [PATCH] Add really simple RTNL device detection support --- plugins/rtnllink.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/plugins/rtnllink.c b/plugins/rtnllink.c index 3d81428..abe6811 100644 --- a/plugins/rtnllink.c +++ b/plugins/rtnllink.c @@ -28,16 +28,92 @@ #include #include +#include "inet.h" + +static GStaticMutex device_mutex = G_STATIC_MUTEX_INIT; +static GSList *device_list = NULL; + static void rtnllink_newlink(unsigned short type, int index, unsigned flags, unsigned change) { + struct connman_element *device; + enum connman_element_subtype subtype; + GSList *list; + gboolean exists = FALSE; + gchar *name; + DBG("index %d", index); + + g_static_mutex_lock(&device_mutex); + + for (list = device_list; list; list = list->next) { + struct connman_element *device = list->data; + + if (device->index == index) { + exists = TRUE; + break; + } + } + + g_static_mutex_unlock(&device_mutex); + + if (exists == TRUE) + return; + + name = inet_index2name(index); + + if (g_str_has_prefix(name, "eth") == TRUE) + subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET; + else if (g_str_has_prefix(name, "wlan") == TRUE) + subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI; + else if (g_str_has_prefix(name, "wmx") == TRUE) + subtype = CONNMAN_ELEMENT_SUBTYPE_WIMAX; + else if (g_str_has_prefix(name, "bnep") == TRUE) + subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH; + else + subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN; + + if (subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN) { + g_free(name); + return; + } + + device = connman_element_create(NULL); + device->type = CONNMAN_ELEMENT_TYPE_DEVICE; + device->subtype = subtype; + + device->index = index; + device->name = name; + + g_static_mutex_lock(&device_mutex); + + connman_element_register(device, NULL); + device_list = g_slist_append(device_list, device); + + g_static_mutex_unlock(&device_mutex); } static void rtnllink_dellink(unsigned short type, int index, unsigned flags, unsigned change) { + GSList *list; + DBG("index %d", index); + + g_static_mutex_lock(&device_mutex); + + for (list = device_list; list; list = list->next) { + struct connman_element *device = list->data; + + if (device->index == index) { + device_list = g_slist_remove(device_list, device); + connman_element_unregister(device); + connman_element_unref(device); + break; + } + } + + g_static_mutex_unlock(&device_mutex); } static struct connman_rtnl rtnllink_rtnl = { @@ -48,12 +124,36 @@ static struct connman_rtnl rtnllink_rtnl = { static int rtnllink_init(void) { - return connman_rtnl_register(&rtnllink_rtnl); + int err; + + err = connman_rtnl_register(&rtnllink_rtnl); + if (err < 0) + return err; + + connman_rtnl_send_getlink(); + + return 0; } static void rtnllink_exit(void) { + GSList *list; + connman_rtnl_unregister(&rtnllink_rtnl); + + g_static_mutex_lock(&device_mutex); + + for (list = device_list; list; list = list->next) { + struct connman_element *device = list->data; + + connman_element_unregister(device); + connman_element_unref(device); + } + + g_slist_free(device_list); + device_list = NULL; + + g_static_mutex_unlock(&device_mutex); } CONNMAN_PLUGIN_DEFINE("rtnllink", "RTNL link detection plugin", VERSION, -- 1.7.9.5