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 static DBusConnection *connection = NULL;
32 static GSList *notifier_list = NULL;
33 static GHashTable *service_hash = NULL;
35 static gint compare_priority(gconstpointer a, gconstpointer b)
37 const struct connman_notifier *notifier1 = a;
38 const struct connman_notifier *notifier2 = b;
40 return notifier2->priority - notifier1->priority;
44 * connman_notifier_register:
45 * @notifier: notifier module
47 * Register a new notifier module
49 * Returns: %0 on success
51 int connman_notifier_register(struct connman_notifier *notifier)
53 DBG("notifier %p name %s", notifier, notifier->name);
55 notifier_list = g_slist_insert_sorted(notifier_list, notifier,
62 * connman_notifier_unregister:
63 * @notifier: notifier module
65 * Remove a previously registered notifier module
67 void connman_notifier_unregister(struct connman_notifier *notifier)
69 DBG("notifier %p name %s", notifier, notifier->name);
71 notifier_list = g_slist_remove(notifier_list, notifier);
74 #define MAX_TECHNOLOGIES 10
76 static volatile int registered[MAX_TECHNOLOGIES];
77 static volatile int enabled[MAX_TECHNOLOGIES];
78 static volatile int connected[MAX_TECHNOLOGIES];
80 static void technology_registered(enum connman_service_type type,
81 connman_bool_t registered)
83 DBG("type %d registered %d", type, registered);
86 static void technology_enabled(enum connman_service_type type,
87 connman_bool_t enabled)
91 DBG("type %d enabled %d", type, enabled);
93 for (list = notifier_list; list; list = list->next) {
94 struct connman_notifier *notifier = list->data;
96 if (notifier->service_enabled)
97 notifier->service_enabled(type, enabled);
101 unsigned int __connman_notifier_count_connected(void)
103 unsigned int i, count = 0;
105 __sync_synchronize();
106 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
107 if (connected[i] > 0)
114 const char *__connman_notifier_get_state(void)
116 unsigned int count = __connman_notifier_count_connected();
124 static void state_changed(connman_bool_t connected)
126 unsigned int count = __connman_notifier_count_connected();
127 char *state = "offline";
133 if (connected == FALSE)
139 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
140 CONNMAN_MANAGER_INTERFACE, "State",
141 DBUS_TYPE_STRING, &state);
144 static void technology_connected(enum connman_service_type type,
145 connman_bool_t connected)
147 DBG("type %d connected %d", type, connected);
149 state_changed(connected);
152 void __connman_notifier_register(enum connman_service_type type)
154 DBG("type %d", type);
157 case CONNMAN_SERVICE_TYPE_UNKNOWN:
158 case CONNMAN_SERVICE_TYPE_SYSTEM:
159 case CONNMAN_SERVICE_TYPE_GPS:
160 case CONNMAN_SERVICE_TYPE_VPN:
161 case CONNMAN_SERVICE_TYPE_GADGET:
163 case CONNMAN_SERVICE_TYPE_ETHERNET:
164 case CONNMAN_SERVICE_TYPE_WIFI:
165 case CONNMAN_SERVICE_TYPE_WIMAX:
166 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
167 case CONNMAN_SERVICE_TYPE_CELLULAR:
171 if (__sync_fetch_and_add(®istered[type], 1) == 0)
172 technology_registered(type, TRUE);
175 void __connman_notifier_unregister(enum connman_service_type type)
177 DBG("type %d", type);
179 __sync_synchronize();
180 if (registered[type] == 0) {
181 connman_error("notifier unregister underflow");
186 case CONNMAN_SERVICE_TYPE_UNKNOWN:
187 case CONNMAN_SERVICE_TYPE_SYSTEM:
188 case CONNMAN_SERVICE_TYPE_GPS:
189 case CONNMAN_SERVICE_TYPE_VPN:
190 case CONNMAN_SERVICE_TYPE_GADGET:
192 case CONNMAN_SERVICE_TYPE_ETHERNET:
193 case CONNMAN_SERVICE_TYPE_WIFI:
194 case CONNMAN_SERVICE_TYPE_WIMAX:
195 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
196 case CONNMAN_SERVICE_TYPE_CELLULAR:
200 if (__sync_fetch_and_sub(®istered[type], 1) != 1)
203 technology_registered(type, FALSE);
206 void __connman_notifier_enable(enum connman_service_type type)
208 DBG("type %d", type);
211 case CONNMAN_SERVICE_TYPE_UNKNOWN:
212 case CONNMAN_SERVICE_TYPE_SYSTEM:
213 case CONNMAN_SERVICE_TYPE_GPS:
214 case CONNMAN_SERVICE_TYPE_VPN:
215 case CONNMAN_SERVICE_TYPE_GADGET:
217 case CONNMAN_SERVICE_TYPE_ETHERNET:
218 case CONNMAN_SERVICE_TYPE_WIFI:
219 case CONNMAN_SERVICE_TYPE_WIMAX:
220 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
221 case CONNMAN_SERVICE_TYPE_CELLULAR:
225 if (__sync_fetch_and_add(&enabled[type], 1) == 0)
226 technology_enabled(type, TRUE);
229 void __connman_notifier_disable(enum connman_service_type type)
231 DBG("type %d", type);
233 __sync_synchronize();
234 if (enabled[type] == 0) {
235 connman_error("notifier disable underflow");
240 case CONNMAN_SERVICE_TYPE_UNKNOWN:
241 case CONNMAN_SERVICE_TYPE_SYSTEM:
242 case CONNMAN_SERVICE_TYPE_GPS:
243 case CONNMAN_SERVICE_TYPE_VPN:
244 case CONNMAN_SERVICE_TYPE_GADGET:
246 case CONNMAN_SERVICE_TYPE_ETHERNET:
247 case CONNMAN_SERVICE_TYPE_WIFI:
248 case CONNMAN_SERVICE_TYPE_WIMAX:
249 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
250 case CONNMAN_SERVICE_TYPE_CELLULAR:
254 if (__sync_fetch_and_sub(&enabled[type], 1) != 1)
257 technology_enabled(type, FALSE);
260 void __connman_notifier_connect(enum connman_service_type type)
262 DBG("type %d", type);
265 case CONNMAN_SERVICE_TYPE_UNKNOWN:
266 case CONNMAN_SERVICE_TYPE_SYSTEM:
267 case CONNMAN_SERVICE_TYPE_GPS:
268 case CONNMAN_SERVICE_TYPE_VPN:
269 case CONNMAN_SERVICE_TYPE_GADGET:
271 case CONNMAN_SERVICE_TYPE_ETHERNET:
272 case CONNMAN_SERVICE_TYPE_WIFI:
273 case CONNMAN_SERVICE_TYPE_WIMAX:
274 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
275 case CONNMAN_SERVICE_TYPE_CELLULAR:
279 if (__sync_fetch_and_add(&connected[type], 1) == 0)
280 technology_connected(type, TRUE);
283 void __connman_notifier_disconnect(enum connman_service_type type)
285 DBG("type %d", type);
287 __sync_synchronize();
288 if (connected[type] == 0) {
289 connman_error("notifier disconnect underflow");
294 case CONNMAN_SERVICE_TYPE_UNKNOWN:
295 case CONNMAN_SERVICE_TYPE_SYSTEM:
296 case CONNMAN_SERVICE_TYPE_GPS:
297 case CONNMAN_SERVICE_TYPE_VPN:
298 case CONNMAN_SERVICE_TYPE_GADGET:
300 case CONNMAN_SERVICE_TYPE_ETHERNET:
301 case CONNMAN_SERVICE_TYPE_WIFI:
302 case CONNMAN_SERVICE_TYPE_WIMAX:
303 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
304 case CONNMAN_SERVICE_TYPE_CELLULAR:
308 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
311 technology_connected(type, FALSE);
314 static void technology_default(enum connman_service_type type)
318 str = __connman_service_type2string(type);
322 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
323 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
324 DBUS_TYPE_STRING, &str);
327 void __connman_notifier_default_changed(struct connman_service *service)
329 enum connman_service_type type = connman_service_get_type(service);
333 technology_default(type);
335 interface = connman_service_get_interface(service);
336 __connman_tethering_update_interface(interface);
339 for (list = notifier_list; list; list = list->next) {
340 struct connman_notifier *notifier = list->data;
342 if (notifier->default_changed)
343 notifier->default_changed(service);
347 void __connman_notifier_service_add(struct connman_service *service,
352 for (list = notifier_list; list; list = list->next) {
353 struct connman_notifier *notifier = list->data;
355 if (notifier->service_add)
356 notifier->service_add(service, name);
360 void __connman_notifier_service_remove(struct connman_service *service)
364 if (g_hash_table_lookup(service_hash, service) != NULL) {
366 * This is a tempory check for consistency. It can be
367 * removed when there are no reports for the following
370 connman_error("Service state machine inconsistency detected.");
372 g_hash_table_remove(service_hash, service);
375 for (list = notifier_list; list; list = list->next) {
376 struct connman_notifier *notifier = list->data;
378 if (notifier->service_remove)
379 notifier->service_remove(service);
383 void __connman_notifier_proxy_changed(struct connman_service *service)
387 for (list = notifier_list; list; list = list->next) {
388 struct connman_notifier *notifier = list->data;
390 if (notifier->proxy_changed)
391 notifier->proxy_changed(service);
395 static void offlinemode_changed(dbus_bool_t enabled)
397 DBG("enabled %d", enabled);
399 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
400 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
401 DBUS_TYPE_BOOLEAN, &enabled);
404 void __connman_notifier_offlinemode(connman_bool_t enabled)
408 DBG("enabled %d", enabled);
410 offlinemode_changed(enabled);
412 for (list = notifier_list; list; list = list->next) {
413 struct connman_notifier *notifier = list->data;
415 if (notifier->offline_mode)
416 notifier->offline_mode(enabled);
420 static void notify_idle_state(connman_bool_t idle)
424 DBG("idle %d", idle);
426 for (list = notifier_list; list; list = list->next) {
427 struct connman_notifier *notifier = list->data;
429 if (notifier->idle_state)
430 notifier->idle_state(idle);
434 void __connman_notifier_service_state_changed(struct connman_service *service,
435 enum connman_service_state state)
438 unsigned int old_size;
439 connman_bool_t found;
441 for (list = notifier_list; list; list = list->next) {
442 struct connman_notifier *notifier = list->data;
444 if (notifier->service_state_changed)
445 notifier->service_state_changed(service, state);
448 old_size = g_hash_table_size(service_hash);
449 found = g_hash_table_lookup(service_hash, service) != NULL;
452 case CONNMAN_SERVICE_STATE_UNKNOWN:
453 case CONNMAN_SERVICE_STATE_FAILURE:
454 case CONNMAN_SERVICE_STATE_DISCONNECT:
455 case CONNMAN_SERVICE_STATE_IDLE:
459 g_hash_table_remove(service_hash, service);
461 notify_idle_state(TRUE);
464 case CONNMAN_SERVICE_STATE_ASSOCIATION:
465 case CONNMAN_SERVICE_STATE_CONFIGURATION:
466 case CONNMAN_SERVICE_STATE_READY:
467 case CONNMAN_SERVICE_STATE_ONLINE:
471 g_hash_table_insert(service_hash, service, service);
473 notify_idle_state(FALSE);
479 void __connman_notifier_ipconfig_changed(struct connman_service *service,
480 struct connman_ipconfig *ipconfig)
484 for (list = notifier_list; list; list = list->next) {
485 struct connman_notifier *notifier = list->data;
487 if (notifier->ipconfig_changed)
488 notifier->ipconfig_changed(service, ipconfig);
492 static connman_bool_t technology_supported(enum connman_service_type type)
495 case CONNMAN_SERVICE_TYPE_UNKNOWN:
496 case CONNMAN_SERVICE_TYPE_SYSTEM:
497 case CONNMAN_SERVICE_TYPE_GPS:
498 case CONNMAN_SERVICE_TYPE_VPN:
499 case CONNMAN_SERVICE_TYPE_GADGET:
501 case CONNMAN_SERVICE_TYPE_ETHERNET:
502 case CONNMAN_SERVICE_TYPE_WIFI:
503 case CONNMAN_SERVICE_TYPE_WIMAX:
504 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
505 case CONNMAN_SERVICE_TYPE_CELLULAR:
512 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
514 DBG("type %d", type);
516 if (technology_supported(type) == FALSE)
519 __sync_synchronize();
520 if (registered[type] > 0)
526 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
528 DBG("type %d", type);
530 if (technology_supported(type) == FALSE)
533 __sync_synchronize();
534 if (enabled[type] > 0)
540 int __connman_notifier_init(void)
544 connection = connman_dbus_get_connection();
546 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
553 void __connman_notifier_cleanup(void)
557 g_hash_table_destroy(service_hash);
560 dbus_connection_unref(connection);