Add really simple RTNL device detection support
[connman] / plugins / rtnllink.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <connman/plugin.h>
27 #include <connman/element.h>
28 #include <connman/rtnl.h>
29 #include <connman/log.h>
30
31 #include "inet.h"
32
33 static GStaticMutex device_mutex = G_STATIC_MUTEX_INIT;
34 static GSList *device_list = NULL;
35
36 static void rtnllink_newlink(unsigned short type, int index,
37                                         unsigned flags, unsigned change)
38 {
39         struct connman_element *device;
40         enum connman_element_subtype subtype;
41         GSList *list;
42         gboolean exists = FALSE;
43         gchar *name;
44
45         DBG("index %d", index);
46
47         g_static_mutex_lock(&device_mutex);
48
49         for (list = device_list; list; list = list->next) {
50                 struct connman_element *device = list->data;
51
52                 if (device->index == index) {
53                         exists = TRUE;
54                         break;
55                 }
56         }
57
58         g_static_mutex_unlock(&device_mutex);
59
60         if (exists == TRUE)
61                 return;
62
63         name = inet_index2name(index);
64
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;
73         else
74                 subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
75
76         if (subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN) {
77                 g_free(name);
78                 return;
79         }
80
81         device = connman_element_create(NULL);
82         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
83         device->subtype = subtype;
84
85         device->index = index;
86         device->name = name;
87
88         g_static_mutex_lock(&device_mutex);
89
90         connman_element_register(device, NULL);
91         device_list = g_slist_append(device_list, device);
92
93         g_static_mutex_unlock(&device_mutex);
94 }
95
96 static void rtnllink_dellink(unsigned short type, int index,
97                                         unsigned flags, unsigned change)
98 {
99         GSList *list;
100
101         DBG("index %d", index);
102
103         g_static_mutex_lock(&device_mutex);
104
105         for (list = device_list; list; list = list->next) {
106                 struct connman_element *device = list->data;
107
108                 if (device->index == index) {
109                         device_list = g_slist_remove(device_list, device);
110                         connman_element_unregister(device);
111                         connman_element_unref(device);
112                         break;
113                 }
114         }
115
116         g_static_mutex_unlock(&device_mutex);
117 }
118
119 static struct connman_rtnl rtnllink_rtnl = {
120         .name           = "rtnllink",
121         .newlink        = rtnllink_newlink,
122         .dellink        = rtnllink_dellink,
123 };
124
125 static int rtnllink_init(void)
126 {
127         int err;
128
129         err = connman_rtnl_register(&rtnllink_rtnl);
130         if (err < 0)
131                 return err;
132
133         connman_rtnl_send_getlink();
134
135         return 0;
136 }
137
138 static void rtnllink_exit(void)
139 {
140         GSList *list;
141
142         connman_rtnl_unregister(&rtnllink_rtnl);
143
144         g_static_mutex_lock(&device_mutex);
145
146         for (list = device_list; list; list = list->next) {
147                 struct connman_element *device = list->data;
148
149                 connman_element_unregister(device);
150                 connman_element_unref(device);
151         }
152
153         g_slist_free(device_list);
154         device_list = NULL;
155
156         g_static_mutex_unlock(&device_mutex);
157 }
158
159 CONNMAN_PLUGIN_DEFINE("rtnllink", "RTNL link detection plugin", VERSION,
160                                                 rtnllink_init, rtnllink_exit)