5 * Copyright (C) 2007-2013 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 static int connected[MAX_CONNMAN_SERVICE_TYPES];
77 static int online[MAX_CONNMAN_SERVICE_TYPES];
79 static bool notifier_is_online(void)
84 for (i = 0; i < MAX_CONNMAN_SERVICE_TYPES; i++) {
92 bool __connman_notifier_is_connected(void)
97 for (i = 0; i < MAX_CONNMAN_SERVICE_TYPES; i++) {
105 static const char *evaluate_notifier_state(void)
107 if (notifier_is_online())
110 if (__connman_notifier_is_connected())
113 if (__connman_technology_get_offlinemode())
119 const char *__connman_notifier_get_state(void)
121 return notifier_state;
124 static void state_changed(void)
128 state = evaluate_notifier_state();
130 if (g_strcmp0(state, notifier_state) == 0)
133 notifier_state = state;
135 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
136 CONNMAN_MANAGER_INTERFACE, "State",
137 DBUS_TYPE_STRING, ¬ifier_state);
140 void __connman_notifier_connect(enum connman_service_type type)
142 DBG("type %d", type);
145 case CONNMAN_SERVICE_TYPE_UNKNOWN:
146 case CONNMAN_SERVICE_TYPE_SYSTEM:
147 case CONNMAN_SERVICE_TYPE_GPS:
148 case CONNMAN_SERVICE_TYPE_VPN:
150 case CONNMAN_SERVICE_TYPE_ETHERNET:
151 case CONNMAN_SERVICE_TYPE_GADGET:
152 case CONNMAN_SERVICE_TYPE_WIFI:
153 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
154 case CONNMAN_SERVICE_TYPE_CELLULAR:
155 case CONNMAN_SERVICE_TYPE_P2P:
159 if (__sync_fetch_and_add(&connected[type], 1) == 0) {
160 __connman_technology_set_connected(type, true);
165 void __connman_notifier_enter_online(enum connman_service_type type)
167 DBG("type %d", type);
169 if (__sync_fetch_and_add(&online[type], 1) == 0)
173 void __connman_notifier_leave_online(enum connman_service_type type)
175 DBG("type %d", type);
177 if (__sync_fetch_and_sub(&online[type], 1) == 1)
181 void __connman_notifier_disconnect(enum connman_service_type type)
183 DBG("type %d", type);
185 __sync_synchronize();
186 if (connected[type] == 0) {
187 connman_error("notifier disconnect underflow");
192 case CONNMAN_SERVICE_TYPE_UNKNOWN:
193 case CONNMAN_SERVICE_TYPE_SYSTEM:
194 case CONNMAN_SERVICE_TYPE_GPS:
195 case CONNMAN_SERVICE_TYPE_VPN:
197 case CONNMAN_SERVICE_TYPE_ETHERNET:
198 case CONNMAN_SERVICE_TYPE_GADGET:
199 case CONNMAN_SERVICE_TYPE_WIFI:
200 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
201 case CONNMAN_SERVICE_TYPE_CELLULAR:
202 case CONNMAN_SERVICE_TYPE_P2P:
206 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
209 __connman_technology_set_connected(type, false);
213 void __connman_notifier_default_changed(struct connman_service *service)
217 for (list = notifier_list; list; list = list->next) {
218 struct connman_notifier *notifier = list->data;
220 if (notifier->default_changed)
221 notifier->default_changed(service);
225 void __connman_notifier_service_add(struct connman_service *service,
230 for (list = notifier_list; list; list = list->next) {
231 struct connman_notifier *notifier = list->data;
233 if (notifier->service_add)
234 notifier->service_add(service, name);
238 void __connman_notifier_service_remove(struct connman_service *service)
242 if (g_hash_table_lookup(service_hash, service)) {
244 * This is a tempory check for consistency. It can be
245 * removed when there are no reports for the following
248 connman_error("Service state machine inconsistency detected.");
250 g_hash_table_remove(service_hash, service);
253 for (list = notifier_list; list; list = list->next) {
254 struct connman_notifier *notifier = list->data;
256 if (notifier->service_remove)
257 notifier->service_remove(service);
261 void __connman_notifier_proxy_changed(struct connman_service *service)
265 for (list = notifier_list; list; list = list->next) {
266 struct connman_notifier *notifier = list->data;
268 if (notifier->proxy_changed)
269 notifier->proxy_changed(service);
273 static void offlinemode_changed(dbus_bool_t enabled)
275 DBG("enabled %d", enabled);
277 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
278 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
279 DBUS_TYPE_BOOLEAN, &enabled);
282 void __connman_notifier_offlinemode(bool enabled)
286 DBG("enabled %d", enabled);
288 offlinemode_changed(enabled);
291 for (list = notifier_list; list; list = list->next) {
292 struct connman_notifier *notifier = list->data;
294 if (notifier->offline_mode)
295 notifier->offline_mode(enabled);
299 static void notify_idle_state(bool idle)
303 DBG("idle %d", idle);
305 for (list = notifier_list; list; list = list->next) {
306 struct connman_notifier *notifier = list->data;
308 if (notifier->idle_state)
309 notifier->idle_state(idle);
313 void __connman_notifier_service_state_changed(struct connman_service *service,
314 enum connman_service_state state)
317 unsigned int old_size;
320 for (list = notifier_list; list; list = list->next) {
321 struct connman_notifier *notifier = list->data;
323 if (notifier->service_state_changed)
324 notifier->service_state_changed(service, state);
327 old_size = g_hash_table_size(service_hash);
328 found = g_hash_table_lookup(service_hash, service);
331 case CONNMAN_SERVICE_STATE_UNKNOWN:
332 case CONNMAN_SERVICE_STATE_FAILURE:
333 case CONNMAN_SERVICE_STATE_DISCONNECT:
334 case CONNMAN_SERVICE_STATE_IDLE:
338 g_hash_table_remove(service_hash, service);
340 notify_idle_state(true);
343 case CONNMAN_SERVICE_STATE_ASSOCIATION:
344 case CONNMAN_SERVICE_STATE_CONFIGURATION:
345 case CONNMAN_SERVICE_STATE_READY:
346 case CONNMAN_SERVICE_STATE_ONLINE:
350 g_hash_table_insert(service_hash, service, service);
352 notify_idle_state(false);
358 void __connman_notifier_ipconfig_changed(struct connman_service *service,
359 struct connman_ipconfig *ipconfig)
363 for (list = notifier_list; list; list = list->next) {
364 struct connman_notifier *notifier = list->data;
366 if (notifier->ipconfig_changed)
367 notifier->ipconfig_changed(service, ipconfig);
371 int __connman_notifier_init(void)
375 connection = connman_dbus_get_connection();
377 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
380 notifier_state = evaluate_notifier_state();
385 void __connman_notifier_cleanup(void)
389 g_hash_table_destroy(service_hash);
392 dbus_connection_unref(connection);