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 netconfig_wifi_update_power_state(FALSE);
116 netconfig_wifi_update_power_state(TRUE);
118 } else if (g_str_has_prefix(tech,
119 CONNMAN_CELLULAR_TECHNOLOGY_PREFIX) == TRUE) {
120 /* Cellular technology state */
124 static void __netconfig_wifi_service_state_signal_handler(DBusMessage *msg)
126 char *sigvalue = NULL;
127 char *property = NULL;
128 char *service_profile = NULL;
129 DBusMessageIter args, variant;
131 service_profile = (char *)dbus_message_get_path(msg);
132 if (service_profile == NULL)
135 dbus_message_iter_init(msg, &args);
136 dbus_message_iter_get_basic(&args, &sigvalue);
137 if (sigvalue == NULL)
140 if (g_str_equal(sigvalue, "State") == TRUE) {
141 dbus_message_iter_next(&args);
142 dbus_message_iter_recurse(&args, &variant);
143 dbus_message_iter_get_basic(&variant, &property);
145 DBG("[%s] %s", property, service_profile);
146 if (netconfig_is_wifi_profile(service_profile) == TRUE) {
149 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
150 if (wifi_state == VCONFKEY_WIFI_OFF)
153 if (g_str_equal(property, "ready") == TRUE ||
154 g_str_equal(property, "online") == TRUE) {
155 if (wifi_state >= VCONFKEY_WIFI_CONNECTED)
158 netconfig_set_default_profile(service_profile);
160 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTED);
162 } else if (g_str_equal(property, "failure") == TRUE ||
163 g_str_equal(property, "disconnect") == TRUE ||
164 g_str_equal(property, "idle") == TRUE) {
165 if (netconfig_get_default_profile() == NULL ||
166 netconfig_is_wifi_profile(netconfig_get_default_profile())
168 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
172 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
175 netconfig_set_default_profile(NULL);
177 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
179 } else if (g_str_equal(property, "association") == TRUE ||
180 g_str_equal(property, "configuration") == TRUE) {
181 if (netconfig_get_default_profile() == NULL ||
182 netconfig_is_wifi_profile(netconfig_get_default_profile())
184 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTING);
188 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
191 netconfig_set_default_profile(NULL);
193 netconfig_wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTING);
196 if (g_str_equal(property, "ready") == TRUE ||
197 g_str_equal(property, "online") == TRUE) {
198 if (netconfig_get_default_profile() == NULL)
199 netconfig_set_default_profile(service_profile);
201 } else if (g_str_equal(property, "failure") == TRUE ||
202 g_str_equal(property, "disconnect") == TRUE ||
203 g_str_equal(property, "idle") == TRUE) {
204 if (netconfig_get_default_profile() == NULL)
207 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
210 netconfig_set_default_profile(NULL);
212 } else if (g_str_equal(property, "association") == TRUE ||
213 g_str_equal(property, "configuration") == TRUE) {
214 if (netconfig_get_default_profile() == NULL)
217 if (g_str_equal(service_profile, netconfig_get_default_profile()) != TRUE)
220 netconfig_set_default_profile(NULL);
226 static DBusHandlerResult __netconfig_signal_filter_handler(
227 DBusConnection *conn, DBusMessage *msg, void *user_data)
229 char *sigvalue = NULL;
232 DBG("Invalid Message. Ignore");
233 return DBUS_HANDLER_RESULT_HANDLED;
236 if (dbus_message_is_signal(msg, CONNMAN_MANAGER_INTERFACE,
237 CONNMAN_SIGNAL_PROPERTY_CHANGED)) {
238 /* We have handled this message, don't pass it on */
239 return DBUS_HANDLER_RESULT_HANDLED;
240 } else if (dbus_message_is_signal(msg, CONNMAN_TECHNOLOGY_INTERFACE,
241 CONNMAN_SIGNAL_PROPERTY_CHANGED)) {
242 __netconfig_technology_signal_handler(msg);
244 return DBUS_HANDLER_RESULT_HANDLED;
245 } else if (dbus_message_is_signal(msg, CONNMAN_SERVICE_INTERFACE,
246 CONNMAN_SIGNAL_PROPERTY_CHANGED)) {
247 __netconfig_wifi_service_state_signal_handler(msg);
249 return DBUS_HANDLER_RESULT_HANDLED;
250 } else if (dbus_message_is_signal(msg, CONNMAN_MANAGER_INTERFACE,
251 CONNMAN_SIGNAL_SERVICES_CHANGED)) {
252 DBG("Received CONNMAN_SIGNAL_SERVICES_CHANGED message");
253 netconfig_wifi_check_network_notification(msg);
255 return DBUS_HANDLER_RESULT_HANDLED;
256 } else if (dbus_message_is_signal(msg, SUPPLICANT_INTERFACE ".Interface",
257 SIGNAL_PROPERTIES_CHANGED)) {
258 dbus_bool_t scanning = FALSE;
259 void *property = &scanning;
261 if (netconfig_dbus_get_basic_params_array(msg,
262 &sigvalue, &property) != TRUE)
263 return DBUS_HANDLER_RESULT_HANDLED;
265 if (sigvalue == NULL)
266 return DBUS_HANDLER_RESULT_HANDLED;
268 if (scanning == TRUE)
269 netconfig_wifi_set_scanning(TRUE);
271 return DBUS_HANDLER_RESULT_HANDLED;
272 } else if (dbus_message_is_signal(msg, SUPPLICANT_INTERFACE ".Interface",
274 if (netconfig_wifi_get_ssid_scan_state() == TRUE)
275 netconfig_wifi_bss_added(msg);
277 return DBUS_HANDLER_RESULT_HANDLED;
278 } else if (dbus_message_is_signal(msg, SUPPLICANT_INTERFACE ".Interface",
280 netconfig_wifi_set_scanning(FALSE);
282 if (netconfig_wifi_get_bgscan_state() != TRUE) {
283 if (netconfig_wifi_get_ssid_scan_state() == TRUE)
284 netconfig_wifi_notify_ssid_scan_done();
286 netconfig_wifi_ssid_scan(NULL);
289 return DBUS_HANDLER_RESULT_HANDLED;
292 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
295 void netconfig_register_signal(void)
297 DBusConnection *conn = NULL;
300 DBG("Register DBus signal filters");
302 dbus_error_init(&err);
303 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
305 ERR("Error! Failed to connect to the D-BUS daemon: [%s]",
307 dbus_error_free(&err);
311 signal_connection = conn;
313 dbus_connection_setup_with_g_main(conn, NULL);
315 /* listening to messages from all objects as no path is specified */
316 /* see signals from the given interface */
317 dbus_bus_add_match(conn, CONNMAN_MANAGER_SIGNAL_FILTER, &err);
318 dbus_connection_flush(conn);
319 if (dbus_error_is_set(&err)) {
320 ERR("Error! Match Error (%s)", err.message);
321 dbus_error_free(&err);
325 dbus_bus_add_match(conn, CONNMAN_TECHNOLOGY_SIGNAL_FILTER, &err);
326 dbus_connection_flush(conn);
327 if (dbus_error_is_set(&err)) {
328 ERR("Error! Match Error (%s)", err.message);
329 dbus_error_free(&err);
333 dbus_bus_add_match(conn, CONNMAN_SERVICE_SIGNAL_FILTER, &err);
334 dbus_connection_flush(conn);
335 if (dbus_error_is_set(&err)) {
336 ERR("Error! Match Error (%s)", err.message);
337 dbus_error_free(&err);
341 dbus_bus_add_match(conn, SUPPLICANT_INTERFACE_SIGNAL_FILTER, &err);
342 dbus_connection_flush(conn);
343 if (dbus_error_is_set(&err)) {
344 ERR("Error! Match Error (%s)", err.message);
345 dbus_error_free(&err);
349 if (dbus_connection_add_filter(conn,
350 __netconfig_signal_filter_handler, NULL, NULL) == FALSE) {
351 ERR("Error! dbus_connection_add_filter() failed");
355 INFO("Successfully register signal filters");
358 void netconfig_deregister_signal(void)
360 if (signal_connection == NULL) {
361 ERR("Error! Already de-registered. Nothing to be done");
365 dbus_connection_remove_filter(signal_connection,
366 __netconfig_signal_filter_handler, NULL);
367 INFO("Successfully remove DBus signal filters");
369 dbus_connection_unref(signal_connection);
370 signal_connection = NULL;
372 netconfig_wifi_deinit_bgscan();