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 connected[MAX_TECHNOLOGIES];
78 unsigned int __connman_notifier_count_connected(void)
80 unsigned int i, count = 0;
83 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
91 const char *__connman_notifier_get_state(void)
93 unsigned int count = __connman_notifier_count_connected();
101 static void state_changed(connman_bool_t connected)
103 unsigned int count = __connman_notifier_count_connected();
104 char *state = "offline";
110 if (connected == FALSE)
116 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
117 CONNMAN_MANAGER_INTERFACE, "State",
118 DBUS_TYPE_STRING, &state);
121 static void technology_connected(enum connman_service_type type,
122 connman_bool_t connected)
124 DBG("type %d connected %d", type, connected);
126 __connman_technology_set_connected(type, connected);
127 state_changed(connected);
130 void __connman_notifier_connect(enum connman_service_type type)
132 DBG("type %d", type);
135 case CONNMAN_SERVICE_TYPE_UNKNOWN:
136 case CONNMAN_SERVICE_TYPE_SYSTEM:
137 case CONNMAN_SERVICE_TYPE_GPS:
138 case CONNMAN_SERVICE_TYPE_VPN:
139 case CONNMAN_SERVICE_TYPE_GADGET:
141 case CONNMAN_SERVICE_TYPE_ETHERNET:
142 case CONNMAN_SERVICE_TYPE_WIFI:
143 case CONNMAN_SERVICE_TYPE_WIMAX:
144 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
145 case CONNMAN_SERVICE_TYPE_CELLULAR:
149 if (__sync_fetch_and_add(&connected[type], 1) == 0)
150 technology_connected(type, TRUE);
153 void __connman_notifier_online(enum connman_service_type type)
155 DBG("type %d", type);
158 void __connman_notifier_disconnect(enum connman_service_type type,
159 enum connman_service_state old_state)
161 DBG("type %d", type);
163 __sync_synchronize();
164 if (connected[type] == 0) {
165 connman_error("notifier disconnect underflow");
170 case CONNMAN_SERVICE_TYPE_UNKNOWN:
171 case CONNMAN_SERVICE_TYPE_SYSTEM:
172 case CONNMAN_SERVICE_TYPE_GPS:
173 case CONNMAN_SERVICE_TYPE_VPN:
174 case CONNMAN_SERVICE_TYPE_GADGET:
176 case CONNMAN_SERVICE_TYPE_ETHERNET:
177 case CONNMAN_SERVICE_TYPE_WIFI:
178 case CONNMAN_SERVICE_TYPE_WIMAX:
179 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
180 case CONNMAN_SERVICE_TYPE_CELLULAR:
184 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
187 technology_connected(type, FALSE);
190 static void technology_default(enum connman_service_type type)
194 str = __connman_service_type2string(type);
198 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
199 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
200 DBUS_TYPE_STRING, &str);
203 void __connman_notifier_default_changed(struct connman_service *service)
205 enum connman_service_type type = connman_service_get_type(service);
208 technology_default(type);
210 for (list = notifier_list; list; list = list->next) {
211 struct connman_notifier *notifier = list->data;
213 if (notifier->default_changed)
214 notifier->default_changed(service);
218 void __connman_notifier_service_add(struct connman_service *service,
223 for (list = notifier_list; list; list = list->next) {
224 struct connman_notifier *notifier = list->data;
226 if (notifier->service_add)
227 notifier->service_add(service, name);
231 void __connman_notifier_service_remove(struct connman_service *service)
235 if (g_hash_table_lookup(service_hash, service) != NULL) {
237 * This is a tempory check for consistency. It can be
238 * removed when there are no reports for the following
241 connman_error("Service state machine inconsistency detected.");
243 g_hash_table_remove(service_hash, service);
246 for (list = notifier_list; list; list = list->next) {
247 struct connman_notifier *notifier = list->data;
249 if (notifier->service_remove)
250 notifier->service_remove(service);
254 void __connman_notifier_proxy_changed(struct connman_service *service)
258 for (list = notifier_list; list; list = list->next) {
259 struct connman_notifier *notifier = list->data;
261 if (notifier->proxy_changed)
262 notifier->proxy_changed(service);
266 static void offlinemode_changed(dbus_bool_t enabled)
268 DBG("enabled %d", enabled);
270 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
271 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
272 DBUS_TYPE_BOOLEAN, &enabled);
275 void __connman_notifier_offlinemode(connman_bool_t enabled)
279 DBG("enabled %d", enabled);
281 offlinemode_changed(enabled);
283 for (list = notifier_list; list; list = list->next) {
284 struct connman_notifier *notifier = list->data;
286 if (notifier->offline_mode)
287 notifier->offline_mode(enabled);
291 static void notify_idle_state(connman_bool_t idle)
295 DBG("idle %d", idle);
297 for (list = notifier_list; list; list = list->next) {
298 struct connman_notifier *notifier = list->data;
300 if (notifier->idle_state)
301 notifier->idle_state(idle);
305 void __connman_notifier_service_state_changed(struct connman_service *service,
306 enum connman_service_state state)
309 unsigned int old_size;
310 connman_bool_t found;
312 for (list = notifier_list; list; list = list->next) {
313 struct connman_notifier *notifier = list->data;
315 if (notifier->service_state_changed)
316 notifier->service_state_changed(service, state);
319 old_size = g_hash_table_size(service_hash);
320 found = g_hash_table_lookup(service_hash, service) != NULL;
323 case CONNMAN_SERVICE_STATE_UNKNOWN:
324 case CONNMAN_SERVICE_STATE_FAILURE:
325 case CONNMAN_SERVICE_STATE_DISCONNECT:
326 case CONNMAN_SERVICE_STATE_IDLE:
330 g_hash_table_remove(service_hash, service);
332 notify_idle_state(TRUE);
335 case CONNMAN_SERVICE_STATE_ASSOCIATION:
336 case CONNMAN_SERVICE_STATE_CONFIGURATION:
337 case CONNMAN_SERVICE_STATE_READY:
338 case CONNMAN_SERVICE_STATE_ONLINE:
342 g_hash_table_insert(service_hash, service, service);
344 notify_idle_state(FALSE);
350 void __connman_notifier_ipconfig_changed(struct connman_service *service,
351 struct connman_ipconfig *ipconfig)
355 for (list = notifier_list; list; list = list->next) {
356 struct connman_notifier *notifier = list->data;
358 if (notifier->ipconfig_changed)
359 notifier->ipconfig_changed(service, ipconfig);
363 int __connman_notifier_init(void)
367 connection = connman_dbus_get_connection();
369 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
375 void __connman_notifier_cleanup(void)
379 g_hash_table_destroy(service_hash);
382 dbus_connection_unref(connection);