5 * Copyright (C) 2007-2010 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
28 struct connman_location {
30 struct connman_service *service;
31 enum connman_location_result result;
33 struct connman_location_driver *driver;
38 * connman_location_ref:
39 * @location: Location structure
41 * Increase reference counter of location
43 struct connman_location *connman_location_ref(struct connman_location *location)
45 g_atomic_int_inc(&location->refcount);
51 * connman_location_unref:
52 * @location: Location structure
54 * Decrease reference counter of location
56 void connman_location_unref(struct connman_location *location)
58 if (g_atomic_int_dec_and_test(&location->refcount) == FALSE)
61 if (location->driver) {
62 location->driver->finish(location);
63 location->driver = NULL;
70 * connman_location_get_type:
71 * @location: Location structure
73 * Get the service type of location
75 enum connman_service_type connman_location_get_type(struct connman_location *location)
78 return CONNMAN_SERVICE_TYPE_UNKNOWN;
80 return connman_service_get_type(location->service);
84 * connman_location_get_interface:
85 * @location: location structure
87 * Get network interface of location
89 char *connman_location_get_interface(struct connman_location *location)
94 return connman_service_get_interface(location->service);
97 struct connman_service *connman_location_get_service(
98 struct connman_location *location)
100 return location->service;
103 * connman_location_get_data:
104 * @location: Location structure
106 * Get private location data pointer
108 void *connman_location_get_data(struct connman_location *location)
110 return location->driver_data;
114 * connman_location_set_data:
115 * @location: Location structure
116 * @data: data pointer
118 * Set private location data pointer
120 void connman_location_set_data(struct connman_location *location, void *data)
122 location->driver_data = data;
125 static GSList *driver_list = NULL;
127 static gint compare_priority(gconstpointer a, gconstpointer b)
129 const struct connman_location_driver *driver1 = a;
130 const struct connman_location_driver *driver2 = b;
132 return driver2->priority - driver1->priority;
136 * connman_location_driver_register:
137 * @driver: Location driver definition
139 * Register a new Location driver
141 * Returns: %0 on success
143 int connman_location_driver_register(struct connman_location_driver *driver)
145 DBG("driver %p name %s", driver, driver->name);
147 driver_list = g_slist_insert_sorted(driver_list, driver,
154 * connman_location_driver_unregister:
155 * @driver: Location driver definition
157 * Remove a previously registered Location driver
159 void connman_location_driver_unregister(struct connman_location_driver *driver)
161 DBG("driver %p name %s", driver, driver->name);
163 driver_list = g_slist_remove(driver_list, driver);
167 * connman_location_report_result:
168 * @location: location structure
169 * @result: result information
171 * Report result of a location detection
173 void connman_location_report_result(struct connman_location *location,
174 enum connman_location_result result)
176 DBG("location %p result %d", location, result);
178 if (location == NULL)
181 if (location->result == result)
184 location->result = result;
186 switch (location->result) {
187 case CONNMAN_LOCATION_RESULT_UNKNOWN:
189 case CONNMAN_LOCATION_RESULT_PORTAL:
190 __connman_service_request_login(location->service);
192 case CONNMAN_LOCATION_RESULT_ONLINE:
193 __connman_service_indicate_state(location->service,
194 CONNMAN_SERVICE_STATE_ONLINE,
195 CONNMAN_IPCONFIG_TYPE_IPV4);
200 struct connman_location *__connman_location_create(struct connman_service *service)
202 struct connman_location *location;
204 DBG("service %p", service);
209 location = g_try_new0(struct connman_location, 1);
210 if (location == NULL)
213 DBG("location %p", location);
215 location->refcount = 1;
217 location->service = service;
218 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
223 int __connman_location_detect(struct connman_service *service)
225 struct connman_location *location;
228 DBG("service %p", service);
230 location = __connman_service_get_location(service);
231 if (location == NULL)
234 if (location->driver) {
235 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
236 location->driver->finish(location);
238 if (location->driver->detect(location) == 0)
241 location->driver = NULL;
244 for (list = driver_list; list; list = list->next) {
245 struct connman_location_driver *driver = list->data;
247 DBG("driver %p name %s", driver, driver->name);
249 if (driver->detect(location) == 0) {
250 location->driver = driver;
255 if (location->driver == NULL)
256 connman_location_report_result(location,
257 CONNMAN_LOCATION_RESULT_ONLINE);
262 int __connman_location_finish(struct connman_service *service)
264 struct connman_location *location;
266 DBG("service %p", service);
268 location = __connman_service_get_location(service);
269 if (location == NULL)
272 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
274 if (location->driver) {
275 location->driver->finish(location);
276 location->driver = NULL;
282 int __connman_location_init(void)
289 void __connman_location_cleanup(void)