2 * Network Configuration Module
4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <dbus/dbus-glib-lowlevel.h>
25 #include <vconf-keys.h>
30 #include "netsupplicant.h"
31 #include "wifi-state.h"
32 #include "wifi-indicator.h"
33 #include "wifi-ssid-scan.h"
34 #include "wifi-background-scan.h"
35 #include "network-state.h"
39 #define SIGNAL_SCAN_DONE "ScanDone"
40 #define SIGNAL_BSS_ADDED "BSSAdded"
41 #define SIGNAL_PROPERTIES_CHANGED "PropertiesChanged"
43 #define CONNMAN_SIGNAL_SERVICES_CHANGED "ServicesChanged"
44 #define CONNMAN_SIGNAL_PROPERTY_CHANGED "PropertyChanged"
46 #define CONNMAN_MANAGER_SIGNAL_FILTER "type='signal',interface='net.connman.Manager'"
47 #define CONNMAN_TECHNOLOGY_SIGNAL_FILTER "type='signal',interface='net.connman.Technology'"
48 #define CONNMAN_SERVICE_SIGNAL_FILTER "type='signal',interface='net.connman.Service'"
49 #define SUPPLICANT_INTERFACE_SIGNAL_FILTER "type='signal',interface='fi.w1.wpa_supplicant1.Interface'"
52 static DBusConnection *signal_connection = NULL;
54 static char *__netconfig_get_property(DBusMessage *msg, int *prop_value)
56 DBusMessageIter args, variant;
57 char *property = NULL;
60 /** read these parameters */
61 if (!dbus_message_iter_init(msg, &args)) {
62 ERR("Message does not have parameters");
63 } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) {
64 ERR("Argument is not string");
66 dbus_message_iter_get_basic(&args, &property);
67 dbus_message_iter_next(&args);
68 dbus_message_iter_recurse(&args, &variant);
69 /* Right now, checking for only 'Powered' property which has
72 if (dbus_message_iter_get_arg_type(&variant) == DBUS_TYPE_BOOLEAN) {
73 dbus_message_iter_get_basic(&variant, &data);
86 static void __netconfig_technology_signal_handler(DBusMessage *msg)
89 const char *tech = NULL;
90 dbus_bool_t value = FALSE;
92 if (netconfig_dbus_get_basic_params_string(msg,
93 &key, DBUS_TYPE_BOOLEAN, &value) != TRUE)
96 tech = dbus_message_get_path(msg);
97 if (key == NULL || tech == NULL)
100 if (g_str_has_prefix(tech, CONNMAN_WIFI_TECHNOLOGY_PREFIX) == TRUE) {
101 INFO("Wi-Fi Technology %s, property %d", key, value);
103 if (g_strcmp0(key, "Powered") == 0) {
106 netconfig_wifi_update_power_state(TRUE);
108 netconfig_wifi_update_power_state(FALSE);
109 } else if (g_strcmp0(key, "Connected") == 0) {
110 /* Connection state */
111 } else if (g_strcmp0(key, "Tethering") == 0) {
112 /* Tethering state */
114 } else if (g_str_has_prefix(tech,
115 CONNMAN_CELLULAR_TECHNOLOGY_PREFIX) == TRUE) {
116 /* Cellular technology state */
120 static void __netconfig_wifi_service_state_signal_handler(DBusMessage *msg)
122 char *sigvalue = NULL;
123 char *property = NULL;
124 char *service_profile = NULL;
125 DBusMessageIter args, variant;
127 service_profile = (char *)dbus_message_get_path(msg);
128 if (service_profile == NULL)
131 dbus_message_iter_init(msg, &args);
132 dbus_message_iter_get_basic(&args, &sigvalue);
133 if (sigvalue == NULL)
136 if (g_str_equal(sigvalue, "State") == TRUE) {
137 dbus_message_iter_next(&args);
138 dbus_message_iter_recurse(&args, &variant);
139 dbus_message_iter_get_basic(&variant, &property);
141 DBG("[%s] %s", property, service_profile);
142 if (netconfig_is_wifi_profile(service_profile) == TRUE) {
145 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
146 if (wifi_state == VCONFKEY_WIFI_OFF)
149 if (g_str_equal(property, "ready") == TRUE ||
150 g_str_equal(property, "online") == TRUE) {
151 if (wifi_state >= VCONFKEY_WIFI_CONNECTED)
154 netconfig_set_default_profile(service_profile);
156 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTED);
158 } else if (g_str_equal(property, "failure") == TRUE ||
159 g_str_equal(property, "disconnect") == TRUE ||
160 g_str_equal(property, "idle") == TRUE) {
161 if (netconfig_get_default_profile() == NULL ||
162 netconfig_is_wifi_profile(netconfig_get_default_profile())
164 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
168 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
171 netconfig_set_default_profile(NULL);
173 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
175 } else if (g_str_equal(property, "association") == TRUE ||
176 g_str_equal(property, "configuration") == TRUE) {
177 if (netconfig_get_default_profile() == NULL ||
178 netconfig_is_wifi_profile(netconfig_get_default_profile())
180 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTING);
184 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
187 netconfig_set_default_profile(NULL);
189 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTING);
192 if (g_str_equal(property, "ready") == TRUE ||
193 g_str_equal(property, "online") == TRUE) {
194 if (netconfig_get_default_profile() == NULL)
195 netconfig_set_default_profile(service_profile);
197 } else if (g_str_equal(property, "failure") == TRUE ||
198 g_str_equal(property, "disconnect") == TRUE ||
199 g_str_equal(property, "idle") == TRUE) {
200 if (netconfig_get_default_profile() == NULL)
203 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
206 netconfig_set_default_profile(NULL);
208 } else if (g_str_equal(property, "association") == TRUE ||
209 g_str_equal(property, "configuration") == TRUE) {
210 if (netconfig_get_default_profile() == NULL)
213 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
216 netconfig_set_default_profile(NULL);
222 static DBusHandlerResult __netconfig_signal_filter_handler(
223 DBusConnection *conn, DBusMessage *msg, void *user_data)
225 char *sigvalue = NULL;
228 DBG("Invalid Message. Ignore");
229 return DBUS_HANDLER_RESULT_HANDLED;
232 if (dbus_message_is_signal(msg, CONNMAN_MANAGER_INTERFACE,
233 CONNMAN_SIGNAL_PROPERTY_CHANGED)) {
234 /* We have handled this message, don't pass it on */
235 return DBUS_HANDLER_RESULT_HANDLED;
236 } else if (dbus_message_is_signal(msg, CONNMAN_TECHNOLOGY_INTERFACE,
237 CONNMAN_SIGNAL_PROPERTY_CHANGED)) {
238 __netconfig_technology_signal_handler(msg);
240 return DBUS_HANDLER_RESULT_HANDLED;
241 } else if (dbus_message_is_signal(msg, CONNMAN_SERVICE_INTERFACE,
242 CONNMAN_SIGNAL_PROPERTY_CHANGED)) {
243 __netconfig_wifi_service_state_signal_handler(msg);
245 return DBUS_HANDLER_RESULT_HANDLED;
246 } else if (dbus_message_is_signal(msg, CONNMAN_MANAGER_INTERFACE,
247 CONNMAN_SIGNAL_SERVICES_CHANGED)) {
248 DBG("Received CONNMAN_SIGNAL_SERVICES_CHANGED message");
249 netconfig_wifi_check_network_notification(msg);
251 return DBUS_HANDLER_RESULT_HANDLED;
252 } else if (dbus_message_is_signal(msg, SUPPLICANT_INTERFACE ".Interface",
253 SIGNAL_PROPERTIES_CHANGED)) {
254 dbus_bool_t scanning = FALSE;
255 void *property = &scanning;
257 if (netconfig_dbus_get_basic_params_array(msg,
258 &sigvalue, &property) != TRUE)
259 return DBUS_HANDLER_RESULT_HANDLED;
261 if (sigvalue == NULL)
262 return DBUS_HANDLER_RESULT_HANDLED;
264 if (scanning == TRUE)
265 netconfig_wifi_set_scanning(TRUE);
267 return DBUS_HANDLER_RESULT_HANDLED;
268 } else if (dbus_message_is_signal(msg, SUPPLICANT_INTERFACE ".Interface",
270 if (netconfig_wifi_get_ssid_scan_state() == TRUE)
271 netconfig_wifi_bss_added(msg);
273 return DBUS_HANDLER_RESULT_HANDLED;
274 } else if (dbus_message_is_signal(msg, SUPPLICANT_INTERFACE ".Interface",
276 netconfig_wifi_set_scanning(FALSE);
278 if (netconfig_wifi_get_bgscan_state() != TRUE) {
279 if (netconfig_wifi_get_ssid_scan_state() == TRUE)
280 netconfig_wifi_notify_ssid_scan_done();
282 netconfig_wifi_ssid_scan(NULL);
285 return DBUS_HANDLER_RESULT_HANDLED;
288 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
291 void netconfig_register_signal(void)
293 DBusConnection *conn = NULL;
296 DBG("Register DBus signal filters");
298 dbus_error_init(&err);
299 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
301 ERR("Error! Failed to connect to the D-BUS daemon: [%s]",
303 dbus_error_free(&err);
307 signal_connection = conn;
309 dbus_connection_setup_with_g_main(conn, NULL);
311 /* listening to messages from all objects as no path is specified */
312 /* see signals from the given interface */
313 dbus_bus_add_match(conn, CONNMAN_MANAGER_SIGNAL_FILTER, &err);
314 dbus_connection_flush(conn);
315 if (dbus_error_is_set(&err)) {
316 ERR("Error! Match Error (%s)", err.message);
317 dbus_error_free(&err);
321 dbus_bus_add_match(conn, CONNMAN_TECHNOLOGY_SIGNAL_FILTER, &err);
322 dbus_connection_flush(conn);
323 if (dbus_error_is_set(&err)) {
324 ERR("Error! Match Error (%s)", err.message);
325 dbus_error_free(&err);
329 dbus_bus_add_match(conn, CONNMAN_SERVICE_SIGNAL_FILTER, &err);
330 dbus_connection_flush(conn);
331 if (dbus_error_is_set(&err)) {
332 ERR("Error! Match Error (%s)", err.message);
333 dbus_error_free(&err);
337 dbus_bus_add_match(conn, SUPPLICANT_INTERFACE_SIGNAL_FILTER, &err);
338 dbus_connection_flush(conn);
339 if (dbus_error_is_set(&err)) {
340 ERR("Error! Match Error (%s)", err.message);
341 dbus_error_free(&err);
345 if (dbus_connection_add_filter(conn,
346 __netconfig_signal_filter_handler, NULL, NULL) == FALSE) {
347 ERR("Error! dbus_connection_add_filter() failed");
351 INFO("Successfully register signal filters");
354 void netconfig_deregister_signal(void)
356 if (signal_connection == NULL) {
357 ERR("Error! Already de-registered. Nothing to be done");
361 dbus_connection_remove_filter(signal_connection,
362 __netconfig_signal_filter_handler, NULL);
363 INFO("Successfully remove DBus signal filters");
365 dbus_connection_unref(signal_connection);
366 signal_connection = NULL;
368 netconfig_wifi_deinit_bgscan();