2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 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.
24 #include <sys/ioctl.h>
25 #include <netinet/in.h>
26 #include <netinet/ip.h>
27 #include <linux/wireless.h>
29 #include <vconf-keys.h>
34 #include "wifi-state.h"
35 #include "network-state.h"
36 #include "network-statistics.h"
37 #include "netsupplicant.h"
38 #include "wifi-indicator.h"
40 #define VCONFKEY_WIFI_SNR_MIN -89
42 #if !defined TIZEN_WEARABLE
43 #define WIFI_INDICATOR_INTERVAL 1
45 #define WIFI_INDICATOR_INTERVAL 10
48 #if defined TIZEN_WEARABLE
49 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 (19200 * 1024)
50 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 (2560 * 1024)
51 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 (1536 * 1024)
53 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 (19200 * 1024)
54 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 (7680 * 1024)
55 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 (3840 * 1024)
57 #define NETCONFIG_PROCWIRELESS "/proc/net/wireless"
59 static int netconfig_wifi_rssi = VCONFKEY_WIFI_SNR_MIN;
60 static guint32 netconfig_wifi_freq = 0;
61 static guint netconfig_wifi_indicator_timer = 0;
63 int netconfig_wifi_get_rssi(void)
65 return netconfig_wifi_rssi;
68 unsigned int netconfig_wifi_get_freq(void)
70 return netconfig_wifi_freq;
73 static int __netconfig_wifi_update_and_get_rssi(void)
76 GVariant *message = NULL;
77 GVariant *value = NULL;
82 int rssi_dbm = VCONFKEY_WIFI_SNR_MIN;
84 if_path = netconfig_wifi_get_supplicant_interface();
85 if (if_path == NULL) {
86 ERR("Fail to get wpa_supplicant DBus path");
90 message = netconfig_invoke_dbus_method(SUPPLICANT_SERVICE, if_path,
91 SUPPLICANT_INTERFACE ".Interface", "SignalPoll", NULL);
92 if (message == NULL) {
93 ERR("Fail to get SignalPoll from wpa_supplicant");
98 g_variant_get(message, "(v)", &value);
100 g_variant_get(value, "a{sv}", &iter);
101 while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
103 if (g_strcmp0(key, "rssi") == 0) {
104 key_value = g_variant_get_int32(variant);
105 rssi_dbm = (int)key_value;
106 DBG("Currently signal dbm value [%d]", rssi_dbm);
107 } else if (g_strcmp0(key, "frequency") == 0) {
108 netconfig_wifi_freq = g_variant_get_uint32(variant);
109 DBG("Currently frequency [%u]", netconfig_wifi_freq);
113 g_variant_iter_free(iter);
115 g_variant_unref(message);
119 netconfig_wifi_rssi = rssi_dbm;
124 static int __netconfig_wifi_convert_dbm_to_level_24(int rssi_dbm)
128 /* Wi-Fi Signal Strength Display (for 2.4G (dB))
133 * Very weak : -83 ~ -88
139 else if (rssi_dbm >= -74)
141 else if (rssi_dbm >= -82)
143 else if (rssi_dbm >= -88)
151 static int __netconfig_wifi_convert_dbm_to_level_50(int rssi_dbm)
155 /* Wi-Fi Signal Strength Display (for 5G (dB))
160 * Very weak : -84 ~ -88
166 else if (rssi_dbm >= -78)
168 else if (rssi_dbm >= -83)
170 else if (rssi_dbm >= -88)
178 int netconfig_wifi_rssi_level(const int rssi_dbm)
184 ret = netconfig_vconf_get_int("memory/private/wifi/frequency", &freq);
186 if (!ret && freq > 4900)
187 snr_level = __netconfig_wifi_convert_dbm_to_level_50(rssi_dbm);
189 snr_level = __netconfig_wifi_convert_dbm_to_level_24(rssi_dbm);
194 static void __netconfig_wifi_set_rssi_level(const int snr_level)
196 static int last_snr_level = 0;
198 if (snr_level != last_snr_level) {
199 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, snr_level);
200 netconfig_battery_update_wifi_rssi(snr_level);
201 last_snr_level = snr_level;
205 static void __netconfig_wifi_data_activity_booster(int level)
207 gboolean reply = FALSE;
208 GVariant *params = NULL;
216 static int old_level = 0;
225 params = g_variant_new("(ii)", level1, lock);
228 params = g_variant_new("(ii)", level2, lock);
231 params = g_variant_new("(ii)", level3, lock);
234 ERR("Invalid level");
238 reply = netconfig_invoke_dbus_method_nonblock(
239 "org.tizen.system.deviced",
240 "/Org/Tizen/System/DeviceD/PmQos",
241 "org.tizen.system.deviced.PmQos",
249 /* disable previous booster */
250 if (old_level == 0 || old_level == level)
255 params = g_variant_new("(ii)", level1, unlock);
258 params = g_variant_new("(ii)", level2, unlock);
261 params = g_variant_new("(ii)", level3, unlock);
264 ERR("Invalid level");
268 reply = netconfig_invoke_dbus_method_nonblock(
269 "org.tizen.system.deviced",
270 "/Org/Tizen/System/DeviceD/PmQos",
271 "org.tizen.system.deviced.PmQos",
281 static void __netconfig_wifi_update_indicator(void)
283 static int last_transfer_state = 0;
284 static guint64 netconfig_wifi_tx_bytes = 0;
285 static guint64 netconfig_wifi_rx_bytes = 0;
286 static int booster_tic = 0;
287 static int old_level = 0;
288 int booster_level = 0;
289 guint64 tx, rx, tx_diff, rx_diff;
292 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE) {
293 tx_diff = tx - netconfig_wifi_tx_bytes;
294 rx_diff = rx - netconfig_wifi_rx_bytes;
298 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TXRX;
300 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TX;
303 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_RX;
305 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_NONE;
308 if (transfer_state != last_transfer_state) {
309 netconfig_set_vconf_int(VCONFKEY_WIFI_TRANSFER_STATE, transfer_state);
310 last_transfer_state = transfer_state;
313 /* NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER */
314 if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 ||
315 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1)
317 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 ||
318 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2)
320 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 ||
321 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3)
324 if (old_level == booster_level) {
325 if (--booster_tic <= 0) {
326 __netconfig_wifi_data_activity_booster(booster_level);
331 __netconfig_wifi_data_activity_booster(booster_level);
333 if (booster_level > 0)
339 old_level = booster_level;
341 netconfig_wifi_tx_bytes = tx;
342 netconfig_wifi_rx_bytes = rx;
346 static gboolean __wifi_indicator_monitor(gpointer data)
350 int pm_state = VCONFKEY_PM_STATE_NORMAL;
352 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
355 /* In case of LCD off, we don't need to update Wi-Fi indicator */
356 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
357 if (pm_state >= VCONFKEY_PM_STATE_LCDOFF)
360 rssi_dbm = __netconfig_wifi_update_and_get_rssi();
361 /* INFO("%d dbm", rssi_dbm); */
362 snr_level = netconfig_wifi_rssi_level(rssi_dbm);
363 __netconfig_wifi_set_rssi_level(snr_level);
365 __netconfig_wifi_update_indicator();
370 void netconfig_wifi_indicator_start(void)
372 INFO("Start Wi-Fi indicator");
374 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, VCONFKEY_WIFI_STRENGTH_MAX);
375 __wifi_indicator_monitor(&netconfig_wifi_indicator_timer);
376 netconfig_battery_update_wifi_rssi(VCONFKEY_WIFI_STRENGTH_MAX);
377 netconfig_start_timer_seconds(WIFI_INDICATOR_INTERVAL, __wifi_indicator_monitor, NULL, &netconfig_wifi_indicator_timer);
380 void netconfig_wifi_indicator_stop(void)
382 INFO("Stop Wi-Fi indicator");
384 netconfig_stop_timer(&netconfig_wifi_indicator_timer);
386 netconfig_wifi_rssi = VCONFKEY_WIFI_SNR_MIN;
387 netconfig_wifi_freq = 0;