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_disconnect(enum connman_service_type type)
155 DBG("type %d", type);
157 __sync_synchronize();
158 if (connected[type] == 0) {
159 connman_error("notifier disconnect underflow");
164 case CONNMAN_SERVICE_TYPE_UNKNOWN:
165 case CONNMAN_SERVICE_TYPE_SYSTEM:
166 case CONNMAN_SERVICE_TYPE_GPS:
167 case CONNMAN_SERVICE_TYPE_VPN:
168 case CONNMAN_SERVICE_TYPE_GADGET:
170 case CONNMAN_SERVICE_TYPE_ETHERNET:
171 case CONNMAN_SERVICE_TYPE_WIFI:
172 case CONNMAN_SERVICE_TYPE_WIMAX:
173 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
174 case CONNMAN_SERVICE_TYPE_CELLULAR:
178 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
181 technology_connected(type, FALSE);
184 static void technology_default(enum connman_service_type type)
188 str = __connman_service_type2string(type);
192 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
193 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
194 DBUS_TYPE_STRING, &str);
197 void __connman_notifier_default_changed(struct connman_service *service)
199 enum connman_service_type type = connman_service_get_type(service);
203 technology_default(type);
205 interface = connman_service_get_interface(service);
206 __connman_tethering_update_interface(interface);
209 for (list = notifier_list; list; list = list->next) {
210 struct connman_notifier *notifier = list->data;
212 if (notifier->default_changed)
213 notifier->default_changed(service);
217 void __connman_notifier_service_add(struct connman_service *service,
222 for (list = notifier_list; list; list = list->next) {
223 struct connman_notifier *notifier = list->data;
225 if (notifier->service_add)
226 notifier->service_add(service, name);
230 void __connman_notifier_service_remove(struct connman_service *service)
234 if (g_hash_table_lookup(service_hash, service) != NULL) {
236 * This is a tempory check for consistency. It can be
237 * removed when there are no reports for the following
240 connman_error("Service state machine inconsistency detected.");
242 g_hash_table_remove(service_hash, service);
245 for (list = notifier_list; list; list = list->next) {
246 struct connman_notifier *notifier = list->data;
248 if (notifier->service_remove)
249 notifier->service_remove(service);
253 void __connman_notifier_proxy_changed(struct connman_service *service)
257 for (list = notifier_list; list; list = list->next) {
258 struct connman_notifier *notifier = list->data;
260 if (notifier->proxy_changed)
261 notifier->proxy_changed(service);
265 static void offlinemode_changed(dbus_bool_t enabled)
267 DBG("enabled %d", enabled);
269 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
270 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
271 DBUS_TYPE_BOOLEAN, &enabled);
274 void __connman_notifier_offlinemode(connman_bool_t enabled)
278 DBG("enabled %d", enabled);
280 offlinemode_changed(enabled);
282 for (list = notifier_list; list; list = list->next) {
283 struct connman_notifier *notifier = list->data;
285 if (notifier->offline_mode)
286 notifier->offline_mode(enabled);
290 static void notify_idle_state(connman_bool_t idle)
294 DBG("idle %d", idle);
296 for (list = notifier_list; list; list = list->next) {
297 struct connman_notifier *notifier = list->data;
299 if (notifier->idle_state)
300 notifier->idle_state(idle);
304 void __connman_notifier_service_state_changed(struct connman_service *service,
305 enum connman_service_state state)
308 unsigned int old_size;
309 connman_bool_t found;
311 for (list = notifier_list; list; list = list->next) {
312 struct connman_notifier *notifier = list->data;
314 if (notifier->service_state_changed)
315 notifier->service_state_changed(service, state);
318 old_size = g_hash_table_size(service_hash);
319 found = g_hash_table_lookup(service_hash, service) != NULL;
322 case CONNMAN_SERVICE_STATE_UNKNOWN:
323 case CONNMAN_SERVICE_STATE_FAILURE:
324 case CONNMAN_SERVICE_STATE_DISCONNECT:
325 case CONNMAN_SERVICE_STATE_IDLE:
329 g_hash_table_remove(service_hash, service);
331 notify_idle_state(TRUE);
334 case CONNMAN_SERVICE_STATE_ASSOCIATION:
335 case CONNMAN_SERVICE_STATE_CONFIGURATION:
336 case CONNMAN_SERVICE_STATE_READY:
337 case CONNMAN_SERVICE_STATE_ONLINE:
341 g_hash_table_insert(service_hash, service, service);
343 notify_idle_state(FALSE);
349 void __connman_notifier_ipconfig_changed(struct connman_service *service,
350 struct connman_ipconfig *ipconfig)
354 for (list = notifier_list; list; list = list->next) {
355 struct connman_notifier *notifier = list->data;
357 if (notifier->ipconfig_changed)
358 notifier->ipconfig_changed(service, ipconfig);
362 int __connman_notifier_init(void)
366 connection = connman_dbus_get_connection();
368 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);