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 const char *notifier_state;
37 static gint compare_priority(gconstpointer a, gconstpointer b)
39 const struct connman_notifier *notifier1 = a;
40 const struct connman_notifier *notifier2 = b;
42 return notifier2->priority - notifier1->priority;
46 * connman_notifier_register:
47 * @notifier: notifier module
49 * Register a new notifier module
51 * Returns: %0 on success
53 int connman_notifier_register(struct connman_notifier *notifier)
55 DBG("notifier %p name %s", notifier, notifier->name);
57 notifier_list = g_slist_insert_sorted(notifier_list, notifier,
64 * connman_notifier_unregister:
65 * @notifier: notifier module
67 * Remove a previously registered notifier module
69 void connman_notifier_unregister(struct connman_notifier *notifier)
71 DBG("notifier %p name %s", notifier, notifier->name);
73 notifier_list = g_slist_remove(notifier_list, notifier);
76 #define MAX_TECHNOLOGIES 10
78 static int connected[MAX_TECHNOLOGIES];
79 static int online[MAX_TECHNOLOGIES];
81 static unsigned int notifier_count_online(void)
83 unsigned int i, count = 0;
86 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
94 connman_bool_t __connman_notifier_is_connected(void)
99 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
100 if (connected[i] > 0)
107 static const char *evaluate_notifier_state(void)
111 count = notifier_count_online();
115 if (__connman_notifier_is_connected() == TRUE)
118 if ( __connman_technology_get_offlinemode() == TRUE)
124 const char *__connman_notifier_get_state(void)
126 return notifier_state;
129 static void state_changed(void)
133 state = evaluate_notifier_state();
135 if (g_strcmp0(state, notifier_state) == 0)
138 notifier_state = state;
140 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
141 CONNMAN_MANAGER_INTERFACE, "State",
142 DBUS_TYPE_STRING, ¬ifier_state);
145 static void technology_connected(enum connman_service_type type,
146 connman_bool_t connected)
148 DBG("type %d connected %d", type, connected);
150 __connman_technology_set_connected(type, connected);
154 void __connman_notifier_connect(enum connman_service_type type)
156 DBG("type %d", type);
159 case CONNMAN_SERVICE_TYPE_UNKNOWN:
160 case CONNMAN_SERVICE_TYPE_SYSTEM:
161 case CONNMAN_SERVICE_TYPE_GPS:
162 case CONNMAN_SERVICE_TYPE_VPN:
163 case CONNMAN_SERVICE_TYPE_GADGET:
165 case CONNMAN_SERVICE_TYPE_ETHERNET:
166 case CONNMAN_SERVICE_TYPE_WIFI:
167 case CONNMAN_SERVICE_TYPE_WIMAX:
168 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
169 case CONNMAN_SERVICE_TYPE_CELLULAR:
173 if (__sync_fetch_and_add(&connected[type], 1) == 0)
174 technology_connected(type, TRUE);
177 void __connman_notifier_online(enum connman_service_type type)
179 DBG("type %d", type);
181 if (__sync_fetch_and_add(&online[type], 1) == 0)
185 void __connman_notifier_disconnect(enum connman_service_type type,
186 enum connman_service_state old_state)
188 DBG("type %d", type);
190 __sync_synchronize();
191 if (connected[type] == 0) {
192 connman_error("notifier disconnect underflow");
197 case CONNMAN_SERVICE_TYPE_UNKNOWN:
198 case CONNMAN_SERVICE_TYPE_SYSTEM:
199 case CONNMAN_SERVICE_TYPE_GPS:
200 case CONNMAN_SERVICE_TYPE_VPN:
201 case CONNMAN_SERVICE_TYPE_GADGET:
203 case CONNMAN_SERVICE_TYPE_ETHERNET:
204 case CONNMAN_SERVICE_TYPE_WIFI:
205 case CONNMAN_SERVICE_TYPE_WIMAX:
206 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
207 case CONNMAN_SERVICE_TYPE_CELLULAR:
211 if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
212 __sync_fetch_and_sub(&online[type], 1);
214 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
217 technology_connected(type, FALSE);
220 static void technology_default(enum connman_service_type type)
224 str = __connman_service_type2string(type);
228 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
229 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
230 DBUS_TYPE_STRING, &str);
233 void __connman_notifier_default_changed(struct connman_service *service)
235 enum connman_service_type type = connman_service_get_type(service);
238 technology_default(type);
240 for (list = notifier_list; list; list = list->next) {
241 struct connman_notifier *notifier = list->data;
243 if (notifier->default_changed)
244 notifier->default_changed(service);
248 void __connman_notifier_service_add(struct connman_service *service,
253 for (list = notifier_list; list; list = list->next) {
254 struct connman_notifier *notifier = list->data;
256 if (notifier->service_add)
257 notifier->service_add(service, name);
261 void __connman_notifier_service_remove(struct connman_service *service)
265 if (g_hash_table_lookup(service_hash, service) != NULL) {
267 * This is a tempory check for consistency. It can be
268 * removed when there are no reports for the following
271 connman_error("Service state machine inconsistency detected.");
273 g_hash_table_remove(service_hash, service);
276 for (list = notifier_list; list; list = list->next) {
277 struct connman_notifier *notifier = list->data;
279 if (notifier->service_remove)
280 notifier->service_remove(service);
284 void __connman_notifier_proxy_changed(struct connman_service *service)
288 for (list = notifier_list; list; list = list->next) {
289 struct connman_notifier *notifier = list->data;
291 if (notifier->proxy_changed)
292 notifier->proxy_changed(service);
296 static void offlinemode_changed(dbus_bool_t enabled)
298 DBG("enabled %d", enabled);
300 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
301 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
302 DBUS_TYPE_BOOLEAN, &enabled);
305 void __connman_notifier_offlinemode(connman_bool_t enabled)
309 DBG("enabled %d", enabled);
311 offlinemode_changed(enabled);
314 for (list = notifier_list; list; list = list->next) {
315 struct connman_notifier *notifier = list->data;
317 if (notifier->offline_mode)
318 notifier->offline_mode(enabled);
322 static void notify_idle_state(connman_bool_t idle)
326 DBG("idle %d", idle);
328 for (list = notifier_list; list; list = list->next) {
329 struct connman_notifier *notifier = list->data;
331 if (notifier->idle_state)
332 notifier->idle_state(idle);
336 void __connman_notifier_service_state_changed(struct connman_service *service,
337 enum connman_service_state state)
340 unsigned int old_size;
341 connman_bool_t found;
343 for (list = notifier_list; list; list = list->next) {
344 struct connman_notifier *notifier = list->data;
346 if (notifier->service_state_changed)
347 notifier->service_state_changed(service, state);
350 old_size = g_hash_table_size(service_hash);
351 found = g_hash_table_lookup(service_hash, service) != NULL;
354 case CONNMAN_SERVICE_STATE_UNKNOWN:
355 case CONNMAN_SERVICE_STATE_FAILURE:
356 case CONNMAN_SERVICE_STATE_DISCONNECT:
357 case CONNMAN_SERVICE_STATE_IDLE:
361 g_hash_table_remove(service_hash, service);
363 notify_idle_state(TRUE);
366 case CONNMAN_SERVICE_STATE_ASSOCIATION:
367 case CONNMAN_SERVICE_STATE_CONFIGURATION:
368 case CONNMAN_SERVICE_STATE_READY:
369 case CONNMAN_SERVICE_STATE_ONLINE:
373 g_hash_table_insert(service_hash, service, service);
375 notify_idle_state(FALSE);
381 void __connman_notifier_ipconfig_changed(struct connman_service *service,
382 struct connman_ipconfig *ipconfig)
386 for (list = notifier_list; list; list = list->next) {
387 struct connman_notifier *notifier = list->data;
389 if (notifier->ipconfig_changed)
390 notifier->ipconfig_changed(service, ipconfig);
394 int __connman_notifier_init(void)
398 connection = connman_dbus_get_connection();
400 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
403 notifier_state = evaluate_notifier_state();
408 void __connman_notifier_cleanup(void)
412 g_hash_table_destroy(service_hash);
415 dbus_connection_unref(connection);