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);
98 * connman_location_get_data:
99 * @location: Location structure
101 * Get private location data pointer
103 void *connman_location_get_data(struct connman_location *location)
105 return location->driver_data;
109 * connman_location_set_data:
110 * @location: Location structure
111 * @data: data pointer
113 * Set private location data pointer
115 void connman_location_set_data(struct connman_location *location, void *data)
117 location->driver_data = data;
120 static GSList *driver_list = NULL;
122 static gint compare_priority(gconstpointer a, gconstpointer b)
124 const struct connman_location_driver *driver1 = a;
125 const struct connman_location_driver *driver2 = b;
127 return driver2->priority - driver1->priority;
131 * connman_location_driver_register:
132 * @driver: Location driver definition
134 * Register a new Location driver
136 * Returns: %0 on success
138 int connman_location_driver_register(struct connman_location_driver *driver)
140 DBG("driver %p name %s", driver, driver->name);
142 driver_list = g_slist_insert_sorted(driver_list, driver,
149 * connman_location_driver_unregister:
150 * @driver: Location driver definition
152 * Remove a previously registered Location driver
154 void connman_location_driver_unregister(struct connman_location_driver *driver)
156 DBG("driver %p name %s", driver, driver->name);
158 driver_list = g_slist_remove(driver_list, driver);
162 * connman_location_report_result:
163 * @location: location structure
164 * @result: result information
166 * Report result of a location detection
168 void connman_location_report_result(struct connman_location *location,
169 enum connman_location_result result)
171 DBG("location %p result %d", location, result);
173 if (location == NULL)
176 if (location->result == result)
179 location->result = result;
181 switch (location->result) {
182 case CONNMAN_LOCATION_RESULT_UNKNOWN:
184 case CONNMAN_LOCATION_RESULT_PORTAL:
185 __connman_service_request_login(location->service);
187 case CONNMAN_LOCATION_RESULT_ONLINE:
188 __connman_service_indicate_state(location->service,
189 CONNMAN_SERVICE_STATE_ONLINE);
194 struct connman_location *__connman_location_create(struct connman_service *service)
196 struct connman_location *location;
198 DBG("service %p", service);
203 location = g_try_new0(struct connman_location, 1);
204 if (location == NULL)
207 DBG("location %p", location);
209 location->refcount = 1;
211 location->service = service;
212 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
217 int __connman_location_detect(struct connman_service *service)
219 struct connman_location *location;
222 DBG("service %p", service);
224 location = __connman_service_get_location(service);
225 if (location == NULL)
228 if (location->driver) {
229 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
230 location->driver->finish(location);
232 if (location->driver->detect(location) == 0)
235 location->driver = NULL;
238 for (list = driver_list; list; list = list->next) {
239 struct connman_location_driver *driver = list->data;
241 DBG("driver %p name %s", driver, driver->name);
243 if (driver->detect(location) == 0) {
244 location->driver = driver;
249 if (location->driver == NULL)
250 connman_location_report_result(location,
251 CONNMAN_LOCATION_RESULT_ONLINE);
256 int __connman_location_finish(struct connman_service *service)
258 struct connman_location *location;
260 DBG("service %p", service);
262 location = __connman_service_get_location(service);
263 if (location == NULL)
266 location->result = CONNMAN_LOCATION_RESULT_UNKNOWN;
268 if (location->driver) {
269 location->driver->finish(location);
270 location->driver = NULL;
276 int __connman_location_init(void)
281 void __connman_location_cleanup(void)