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
30 struct connman_location {
32 struct connman_service *service;
33 enum connman_location_result result;
35 struct connman_location_driver *driver;
40 * connman_location_ref:
41 * @location: Location structure
43 * Increase reference counter of location
45 struct connman_location *connman_location_ref(struct connman_location *location)
47 g_atomic_int_inc(&location->refcount);
53 * connman_location_unref:
54 * @location: Location structure
56 * Decrease reference counter of location
58 void connman_location_unref(struct connman_location *location)
60 if (g_atomic_int_dec_and_test(&location->refcount) == FALSE)
63 if (location->driver) {
64 location->driver->finish(location);
65 location->driver = NULL;
72 * connman_location_get_type:
73 * @location: Location structure
75 * Get the service type of location
77 enum connman_service_type connman_location_get_type(struct connman_location *location)
80 return CONNMAN_SERVICE_TYPE_UNKNOWN;
82 return connman_service_get_type(location->service);
86 * connman_location_get_interface:
87 * @location: location structure
89 * Get network interface of location
91 char *connman_location_get_interface(struct connman_location *location)
96 return connman_service_get_interface(location->service);
99 struct connman_service *connman_location_get_service(
100 struct connman_location *location)
102 return location->service;
105 * connman_location_get_data:
106 * @location: Location structure
108 * Get private location data pointer
110 void *connman_location_get_data(struct connman_location *location)
112 return location->driver_data;
116 * connman_location_set_data:
117 * @location: Location structure
118 * @data: data pointer
120 * Set private location data pointer
122 void connman_location_set_data(struct connman_location *location, void *data)
124 location->driver_data = data;
127 static GSList *driver_list = NULL;
129 static gint compare_priority(gconstpointer a, gconstpointer b)
131 const struct connman_location_driver *driver1 = a;
132 const struct connman_location_driver *driver2 = b;
134 return driver2->priority - driver1->priority;
138 * connman_location_driver_register:
139 * @driver: Location driver definition
141 * Register a new Location driver
143 * Returns: %0 on success
145 int connman_location_driver_register(struct connman_location_driver *driver)
147 DBG("driver %p name %s", driver, driver->name);
149 driver_list = g_slist_insert_sorted(driver_list, driver,
156 * connman_location_driver_unregister:
157 * @driver: Location driver definition
159 * Remove a previously registered Location driver
161 void connman_location_driver_unregister(struct connman_location_driver *driver)
163 DBG("driver %p name %s", driver, driver->name);
165 driver_list = g_slist_remove(driver_list, driver);
169 * connman_location_report_result:
170 * @location: location structure
171 * @result: result information
173 * Report result of a location detection
175 void connman_location_report_result(struct connman_location *location,
176 enum connman_location_result result)
178 DBG("location %p result %d", location, result);
180 if (location == NULL)
183 if (location->result == result)
186 location->result = result;
188 switch (location->result) {
189 case CONNMAN_LOCATION_RESULT_UNKNOWN:
191 case CONNMAN_LOCATION_RESULT_PORTAL:
192 __connman_service_request_login(location->service);
194 case CONNMAN_LOCATION_RESULT_ONLINE:
195 __connman_service_ipconfig_indicate_state(location->service,
196 CONNMAN_SERVICE_STATE_ONLINE,
197 CONNMAN_IPCONFIG_TYPE_IPV4);
202 struct connman_location *__connman_location_create(struct connman_service *service)
204 struct connman_location *location;
206 DBG("service %p", service);
211 location = g_try_new0(struct connman_location, 1);
212 if (location == NULL)
215 DBG("location %p", location);
217 location->refcount = 1;
219 location->service = service;
220 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
225 int __connman_location_detect(struct connman_service *service)
227 struct connman_location *location;
230 DBG("service %p", service);
232 location = __connman_service_get_location(service);
233 if (location == NULL)
236 if (location->driver) {
237 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
238 location->driver->finish(location);
240 if (location->driver->detect(location) == 0)
243 location->driver = NULL;
246 for (list = driver_list; list; list = list->next) {
247 struct connman_location_driver *driver = list->data;
249 DBG("driver %p name %s", driver, driver->name);
251 if (driver->detect(location) == 0) {
252 location->driver = driver;
257 if (location->driver == NULL)
258 connman_location_report_result(location,
259 CONNMAN_LOCATION_RESULT_ONLINE);
264 int __connman_location_finish(struct connman_service *service)
266 struct connman_location *location;
268 DBG("service %p", service);
270 location = __connman_service_get_location(service);
271 if (location == NULL)
274 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
276 if (location->driver) {
277 location->driver->finish(location);
278 location->driver = NULL;
284 int __connman_location_init(void)
291 void __connman_location_cleanup(void)