5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <connman/plugin.h>
27 #include <connman/element.h>
28 #include <connman/rtnl.h>
29 #include <connman/log.h>
33 static GStaticMutex device_mutex = G_STATIC_MUTEX_INIT;
34 static GSList *device_list = NULL;
36 static void rtnllink_newlink(unsigned short type, int index,
37 unsigned flags, unsigned change)
39 struct connman_element *device;
40 enum connman_element_subtype subtype;
42 gboolean exists = FALSE;
45 DBG("index %d", index);
47 g_static_mutex_lock(&device_mutex);
49 for (list = device_list; list; list = list->next) {
50 struct connman_element *device = list->data;
52 if (device->index == index) {
58 g_static_mutex_unlock(&device_mutex);
63 name = inet_index2name(index);
65 if (g_str_has_prefix(name, "eth") == TRUE)
66 subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET;
67 else if (g_str_has_prefix(name, "wlan") == TRUE)
68 subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
69 else if (g_str_has_prefix(name, "wmx") == TRUE)
70 subtype = CONNMAN_ELEMENT_SUBTYPE_WIMAX;
71 else if (g_str_has_prefix(name, "bnep") == TRUE)
72 subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
74 subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
76 if (subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN) {
81 device = connman_element_create(NULL);
82 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
83 device->subtype = subtype;
85 device->index = index;
88 g_static_mutex_lock(&device_mutex);
90 connman_element_register(device, NULL);
91 device_list = g_slist_append(device_list, device);
93 g_static_mutex_unlock(&device_mutex);
96 static void rtnllink_dellink(unsigned short type, int index,
97 unsigned flags, unsigned change)
101 DBG("index %d", index);
103 g_static_mutex_lock(&device_mutex);
105 for (list = device_list; list; list = list->next) {
106 struct connman_element *device = list->data;
108 if (device->index == index) {
109 device_list = g_slist_remove(device_list, device);
110 connman_element_unregister(device);
111 connman_element_unref(device);
116 g_static_mutex_unlock(&device_mutex);
119 static struct connman_rtnl rtnllink_rtnl = {
121 .newlink = rtnllink_newlink,
122 .dellink = rtnllink_dellink,
125 static int rtnllink_init(void)
129 err = connman_rtnl_register(&rtnllink_rtnl);
133 connman_rtnl_send_getlink();
138 static void rtnllink_exit(void)
142 connman_rtnl_unregister(&rtnllink_rtnl);
144 g_static_mutex_lock(&device_mutex);
146 for (list = device_list; list; list = list->next) {
147 struct connman_element *device = list->data;
149 connman_element_unregister(device);
150 connman_element_unref(device);
153 g_slist_free(device_list);
156 g_static_mutex_unlock(&device_mutex);
159 CONNMAN_PLUGIN_DEFINE("rtnllink", "RTNL link detection plugin", VERSION,
160 rtnllink_init, rtnllink_exit)