Add support for changing default connections
[connman] / src / device.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 <errno.h>
27
28 #include "connman.h"
29
30 static GSList *driver_list = NULL;
31
32 #if 0
33 static gboolean match_driver(struct connman_device *device,
34                                         struct connman_device_driver *driver)
35 {
36         if (device->element->subtype == driver->type ||
37                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
38                 return TRUE;
39
40         return FALSE;
41 }
42
43 static int device_probe(struct connman_element *element)
44 {
45         struct connman_device *device;
46         GSList *list;
47
48         DBG("element %p name %s", element, element->name);
49
50         device = g_try_new0(struct connman_device, 1);
51         if (device == NULL)
52                 return -ENOMEM;
53
54         device->element = element;
55
56         for (list = driver_list; list; list = list->next) {
57                 struct connman_device_driver *driver = list->data;
58
59                 if (match_driver(device, driver) == FALSE)
60                         continue;
61
62                 DBG("driver %p name %s", driver, driver->name);
63
64                 if (driver->probe(device) == 0) {
65                         device->driver = driver;
66                         connman_element_set_data(element, device);
67                         return 0;
68                 }
69         }
70
71         g_free(device);
72
73         return -ENODEV;
74 }
75
76 static void device_remove(struct connman_element *element)
77 {
78         struct connman_device *device = connman_element_get_data(element);
79
80         DBG("element %p name %s", element, element->name);
81
82         if (device->driver && device->driver->remove)
83                 device->driver->remove(device);
84
85         connman_element_set_data(element, NULL);
86
87         g_free(device);
88 }
89
90 static struct connman_driver device_driver = {
91         .name           = "device",
92         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
93         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
94         .probe          = device_probe,
95         .remove         = device_remove,
96 };
97 #endif
98
99 int __connman_device_init(void)
100 {
101         DBG("");
102
103         //return connman_driver_register(&device_driver);
104         return 0;
105 }
106
107 void __connman_device_cleanup(void)
108 {
109         DBG("");
110
111         //connman_driver_unregister(&device_driver);
112 }
113
114 static gint compare_priority(gconstpointer a, gconstpointer b)
115 {
116         const struct connman_device_driver *driver1 = a;
117         const struct connman_device_driver *driver2 = b;
118
119         return driver2->priority - driver1->priority;
120 }
121
122 /**
123  * connman_device_driver_register:
124  * @driver: device driver definition
125  *
126  * Register a new device driver
127  *
128  * Returns: %0 on success
129  */
130 int connman_device_driver_register(struct connman_device_driver *driver)
131 {
132         DBG("driver %p name %s", driver, driver->name);
133
134         driver_list = g_slist_insert_sorted(driver_list, driver,
135                                                         compare_priority);
136
137         //__connman_driver_rescan(&device_driver);
138
139         return 0;
140 }
141
142 /**
143  * connman_device_driver_unregister:
144  * @driver: device driver definition
145  *
146  * Remove a previously registered device driver
147  */
148 void connman_device_driver_unregister(struct connman_device_driver *driver)
149 {
150         DBG("driver %p name %s", driver, driver->name);
151
152         driver_list = g_slist_remove(driver_list, driver);
153 }