5 * Copyright (C) 2007-2009 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;
34 static gint compare_priority(gconstpointer a, gconstpointer b)
36 const struct connman_notifier *notifier1 = a;
37 const struct connman_notifier *notifier2 = b;
39 return notifier2->priority - notifier1->priority;
43 * connman_notifier_register:
44 * @notifier: notifier module
46 * Register a new notifier module
48 * Returns: %0 on success
50 int connman_notifier_register(struct connman_notifier *notifier)
52 DBG("notifier %p name %s", notifier, notifier->name);
54 notifier_list = g_slist_insert_sorted(notifier_list, notifier,
61 * connman_notifier_unregister:
62 * @notifier: notifier module
64 * Remove a previously registered notifier module
66 void connman_notifier_unregister(struct connman_notifier *notifier)
68 DBG("notifier %p name %s", notifier, notifier->name);
70 notifier_list = g_slist_remove(notifier_list, notifier);
73 static void technology_enabled(enum connman_device_type type,
74 connman_bool_t enabled)
78 DBusMessageIter entry, value, iter;
79 const char *key = "EnabledTechnologies";
81 DBG("type %d enabled %d", type, enabled);
83 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
84 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
88 dbus_message_iter_init_append(signal, &entry);
90 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
92 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
93 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
96 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
97 DBUS_TYPE_STRING_AS_STRING, &iter);
98 __connman_notifier_list(TRUE, &iter);
99 dbus_message_iter_close_container(&value, &iter);
101 dbus_message_iter_close_container(&entry, &value);
103 g_dbus_send_message(connection, signal);
106 for (list = notifier_list; list; list = list->next) {
107 struct connman_notifier *notifier = list->data;
109 if (notifier->device_enabled)
110 notifier->device_enabled(type, enabled);
114 static void technology_registered(enum connman_service_type type,
115 connman_bool_t registered)
118 DBusMessageIter entry, value, iter;
119 const char *key = "AvailableTechnologies";
121 DBG("type %d registered %d", type, registered);
123 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
124 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
128 dbus_message_iter_init_append(signal, &entry);
130 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
132 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
133 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
136 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
137 DBUS_TYPE_STRING_AS_STRING, &iter);
138 __connman_notifier_list(FALSE, &iter);
139 dbus_message_iter_close_container(&value, &iter);
141 dbus_message_iter_close_container(&entry, &value);
143 g_dbus_send_message(connection, signal);
146 static const char *type2string(enum connman_service_type type)
149 case CONNMAN_SERVICE_TYPE_UNKNOWN:
151 case CONNMAN_SERVICE_TYPE_ETHERNET:
153 case CONNMAN_SERVICE_TYPE_WIFI:
155 case CONNMAN_SERVICE_TYPE_WIMAX:
157 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
159 case CONNMAN_SERVICE_TYPE_CELLULAR:
166 static volatile gint registered[10];
167 static volatile gint enabled[10];
169 void __connman_notifier_list(gboolean powered, DBusMessageIter *iter)
173 for (i = 0; i < 10; i++) {
174 const char *type = type2string(i);
181 count = g_atomic_int_get(&enabled[i]);
183 count = g_atomic_int_get(®istered[i]);
186 dbus_message_iter_append_basic(iter,
187 DBUS_TYPE_STRING, &type);
191 void __connman_notifier_register(enum connman_service_type type)
193 DBG("type %d", type);
196 case CONNMAN_SERVICE_TYPE_UNKNOWN:
198 case CONNMAN_SERVICE_TYPE_ETHERNET:
199 case CONNMAN_SERVICE_TYPE_WIFI:
200 case CONNMAN_SERVICE_TYPE_WIMAX:
201 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
202 case CONNMAN_SERVICE_TYPE_CELLULAR:
206 if (g_atomic_int_exchange_and_add(®istered[type], 1) == 0)
207 technology_registered(type, TRUE);
210 void __connman_notifier_unregister(enum connman_service_type type)
212 DBG("type %d", type);
215 case CONNMAN_SERVICE_TYPE_UNKNOWN:
217 case CONNMAN_SERVICE_TYPE_ETHERNET:
218 case CONNMAN_SERVICE_TYPE_WIFI:
219 case CONNMAN_SERVICE_TYPE_WIMAX:
220 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
221 case CONNMAN_SERVICE_TYPE_CELLULAR:
225 if (g_atomic_int_dec_and_test(®istered[type]) == TRUE)
226 technology_registered(type, FALSE);
229 void __connman_notifier_enable(enum connman_service_type type)
231 DBG("type %d", type);
234 case CONNMAN_SERVICE_TYPE_UNKNOWN:
236 case CONNMAN_SERVICE_TYPE_ETHERNET:
237 case CONNMAN_SERVICE_TYPE_WIFI:
238 case CONNMAN_SERVICE_TYPE_WIMAX:
239 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
240 case CONNMAN_SERVICE_TYPE_CELLULAR:
244 if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
245 technology_enabled(type, TRUE);
248 void __connman_notifier_disable(enum connman_service_type type)
250 DBG("type %d", type);
253 case CONNMAN_SERVICE_TYPE_UNKNOWN:
255 case CONNMAN_SERVICE_TYPE_ETHERNET:
256 case CONNMAN_SERVICE_TYPE_WIFI:
257 case CONNMAN_SERVICE_TYPE_WIMAX:
258 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
259 case CONNMAN_SERVICE_TYPE_CELLULAR:
263 if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
264 technology_enabled(type, FALSE);
267 void __connman_notifier_offline_mode(connman_bool_t enabled)
271 DBG("enabled %d", enabled);
273 __connman_profile_changed(FALSE);
275 for (list = notifier_list; list; list = list->next) {
276 struct connman_notifier *notifier = list->data;
278 if (notifier->offline_mode)
279 notifier->offline_mode(enabled);
283 int __connman_notifier_init(void)
287 connection = connman_dbus_get_connection();
292 void __connman_notifier_cleanup(void)
296 dbus_connection_unref(connection);