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:
156 #if defined TIZEN_EXT_WIFI_MESH
157 case CONNMAN_SERVICE_TYPE_MESH:
162 if (__sync_fetch_and_add(&connected[type], 1) == 0) {
163 __connman_technology_set_connected(type, true);
168 void __connman_notifier_enter_online(enum connman_service_type type)
170 DBG("type %d", type);
172 if (__sync_fetch_and_add(&online[type], 1) == 0)
176 void __connman_notifier_leave_online(enum connman_service_type type)
178 DBG("type %d", type);
180 if (__sync_fetch_and_sub(&online[type], 1) == 1)
184 void __connman_notifier_disconnect(enum connman_service_type type)
186 DBG("type %d", type);
188 __sync_synchronize();
189 if (connected[type] == 0) {
190 connman_error("notifier disconnect underflow");
195 case CONNMAN_SERVICE_TYPE_UNKNOWN:
196 case CONNMAN_SERVICE_TYPE_SYSTEM:
197 case CONNMAN_SERVICE_TYPE_GPS:
198 case CONNMAN_SERVICE_TYPE_VPN:
200 case CONNMAN_SERVICE_TYPE_ETHERNET:
201 case CONNMAN_SERVICE_TYPE_GADGET:
202 case CONNMAN_SERVICE_TYPE_WIFI:
203 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
204 case CONNMAN_SERVICE_TYPE_CELLULAR:
205 case CONNMAN_SERVICE_TYPE_P2P:
206 #if defined TIZEN_EXT_WIFI_MESH
207 case CONNMAN_SERVICE_TYPE_MESH:
212 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
215 __connman_technology_set_connected(type, false);
219 void __connman_notifier_default_changed(struct connman_service *service)
223 for (list = notifier_list; list; list = list->next) {
224 struct connman_notifier *notifier = list->data;
226 if (notifier->default_changed)
227 notifier->default_changed(service);
231 void __connman_notifier_service_add(struct connman_service *service,
236 for (list = notifier_list; list; list = list->next) {
237 struct connman_notifier *notifier = list->data;
239 if (notifier->service_add)
240 notifier->service_add(service, name);
244 void __connman_notifier_service_remove(struct connman_service *service)
248 if (g_hash_table_lookup(service_hash, service)) {
250 * This is a tempory check for consistency. It can be
251 * removed when there are no reports for the following
254 connman_error("Service state machine inconsistency detected.");
256 g_hash_table_remove(service_hash, service);
259 for (list = notifier_list; list; list = list->next) {
260 struct connman_notifier *notifier = list->data;
262 if (notifier->service_remove)
263 notifier->service_remove(service);
267 void __connman_notifier_proxy_changed(struct connman_service *service)
271 for (list = notifier_list; list; list = list->next) {
272 struct connman_notifier *notifier = list->data;
274 if (notifier->proxy_changed)
275 notifier->proxy_changed(service);
279 static void offlinemode_changed(dbus_bool_t enabled)
281 DBG("enabled %d", enabled);
283 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
284 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
285 DBUS_TYPE_BOOLEAN, &enabled);
288 void __connman_notifier_offlinemode(bool enabled)
292 DBG("enabled %d", enabled);
294 offlinemode_changed(enabled);
297 for (list = notifier_list; list; list = list->next) {
298 struct connman_notifier *notifier = list->data;
300 if (notifier->offline_mode)
301 notifier->offline_mode(enabled);
305 static void notify_idle_state(bool idle)
309 DBG("idle %d", idle);
311 for (list = notifier_list; list; list = list->next) {
312 struct connman_notifier *notifier = list->data;
314 if (notifier->idle_state)
315 notifier->idle_state(idle);
319 void __connman_notifier_service_state_changed(struct connman_service *service,
320 enum connman_service_state state)
323 unsigned int old_size;
326 for (list = notifier_list; list; list = list->next) {
327 struct connman_notifier *notifier = list->data;
329 if (notifier->service_state_changed)
330 notifier->service_state_changed(service, state);
333 old_size = g_hash_table_size(service_hash);
334 found = g_hash_table_lookup(service_hash, service);
337 case CONNMAN_SERVICE_STATE_UNKNOWN:
338 case CONNMAN_SERVICE_STATE_FAILURE:
339 case CONNMAN_SERVICE_STATE_DISCONNECT:
340 case CONNMAN_SERVICE_STATE_IDLE:
344 g_hash_table_remove(service_hash, service);
346 notify_idle_state(true);
349 case CONNMAN_SERVICE_STATE_ASSOCIATION:
350 case CONNMAN_SERVICE_STATE_CONFIGURATION:
351 case CONNMAN_SERVICE_STATE_READY:
352 case CONNMAN_SERVICE_STATE_ONLINE:
356 g_hash_table_insert(service_hash, service, service);
358 notify_idle_state(false);
364 void __connman_notifier_ipconfig_changed(struct connman_service *service,
365 struct connman_ipconfig *ipconfig)
369 for (list = notifier_list; list; list = list->next) {
370 struct connman_notifier *notifier = list->data;
372 if (notifier->ipconfig_changed)
373 notifier->ipconfig_changed(service, ipconfig);
377 int __connman_notifier_init(void)
381 connection = connman_dbus_get_connection();
383 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
386 notifier_state = evaluate_notifier_state();
391 void __connman_notifier_cleanup(void)
395 g_hash_table_destroy(service_hash);
398 dbus_connection_unref(connection);