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.
23 #include <sys/ioctl.h>
24 #include <netinet/in.h>
25 #include <netinet/ip.h>
26 #include <linux/wireless.h>
28 #include <vconf-keys.h>
33 #include "wifi-state.h"
34 #include "network-state.h"
35 #include "network-statistics.h"
36 #include "netsupplicant.h"
37 #include "wifi-indicator.h"
39 #define VCONFKEY_WIFI_SNR_MIN -89
41 #if !defined TIZEN_WEARABLE
42 #define WIFI_INDICATOR_INTERVAL 1
44 #define WIFI_INDICATOR_INTERVAL 10
47 #if defined TIZEN_WEARABLE
48 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 (19200 * 1024)
49 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 (2560 * 1024)
50 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 (1536 * 1024)
52 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 (19200 * 1024)
53 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 (7680 * 1024)
54 #define NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 (3840 * 1024)
56 #define NETCONFIG_PROCWIRELESS "/proc/net/wireless"
58 static int netconfig_wifi_rssi = VCONFKEY_WIFI_SNR_MIN;
59 static guint netconfig_wifi_indicator_timer = 0;
61 int netconfig_wifi_get_rssi(void)
63 return netconfig_wifi_rssi;
66 static int __netconfig_wifi_update_and_get_rssi(void)
70 char *p_ifname = NULL, *p_entry = NULL;
71 int rssi_dbm = VCONFKEY_WIFI_SNR_MIN;
73 fp = fopen(NETCONFIG_PROCWIRELESS, "r");
75 ERR("Failed to open %s", NETCONFIG_PROCWIRELESS);
79 /* skip the first and second line */
80 if (fgets(buf, sizeof(buf), fp) == NULL ||
81 fgets(buf, sizeof(buf), fp) == NULL)
84 while (fgets(buf, sizeof(buf), fp)) {
89 unsigned long nwid, crypt, frag, retry, misc, missed;
93 while (*p_ifname == ' ') p_ifname++;
94 p_entry = strchr(p_ifname, ':');
99 if (g_strcmp0(p_ifname, WIFI_IFNAME) != 0)
102 /* read wireless status */
104 p_entry = strtok_r(p_entry, " .", &saveptr); // status "%x"
106 sscanf(p_entry, "%x", &status);
107 p_entry = strtok_r(NULL, " .", &saveptr); // Quality link "%d"
109 sscanf(p_entry, "%d", &link);
110 p_entry = strtok_r(NULL, " .", &saveptr); // Quality level "%d"
112 sscanf(p_entry, "%d", &rssi_dbm);
113 p_entry = strtok_r(NULL, " .", &saveptr); // Quality noise "%d"
115 sscanf(p_entry, "%d", &noise);
117 /* No need to read */
119 p_entry = strtok(NULL, " ."); // Discarded nwid "%lu"
120 sscanf(p_entry, "%lu", &nwid);
121 p_entry = strtok(NULL, " ."); // Discarded crypt "%lu"
122 sscanf(p_entry, "%lu", &crypt);
123 p_entry = strtok(NULL, " ."); // Discarded frag "%lu"
124 sscanf(p_entry, "%lu", &frag);
125 p_entry = strtok(NULL, " ."); // Discarded retry "%lu"
126 sscanf(p_entry, "%lu", &retry);
127 p_entry = strtok(NULL, " ."); // Discarded misc "%lu"
128 sscanf(p_entry, "%lu", &misc);
129 p_entry = strtok(NULL, " ."); // Discarded missed "%lu"
130 sscanf(p_entry, "%lu", &missed);
138 netconfig_wifi_rssi = rssi_dbm;
143 int netconfig_wifi_rssi_level(const int rssi_dbm)
147 /* Wi-Fi Signal Strength Display
156 else if (rssi_dbm >= -74)
158 else if (rssi_dbm >= -82)
166 static void __netconfig_wifi_set_rssi_level(const int snr_level)
168 static int last_snr_level = 0;
170 if (snr_level != last_snr_level) {
171 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, snr_level);
172 last_snr_level = snr_level;
176 static void __netconfig_wifi_data_activity_booster(int level)
178 gboolean reply = FALSE;
179 GVariant *params = NULL;
187 static int old_level = 0;
196 params = g_variant_new("(ii)", level1, lock);
199 params = g_variant_new("(ii)", level2, lock);
202 params = g_variant_new("(ii)", level3, lock);
205 ERR("Invalid level");
209 reply = netconfig_invoke_dbus_method_nonblock(
210 "org.tizen.system.deviced",
211 "/Org/Tizen/System/DeviceD/PmQos",
212 "org.tizen.system.deviced.PmQos",
220 /* disable previous booster */
221 if (old_level == 0 || old_level == level)
226 params = g_variant_new("(ii)", level1, unlock);
229 params = g_variant_new("(ii)", level2, unlock);
232 params = g_variant_new("(ii)", level3, unlock);
235 ERR("Invalid level");
239 reply = netconfig_invoke_dbus_method_nonblock(
240 "org.tizen.system.deviced",
241 "/Org/Tizen/System/DeviceD/PmQos",
242 "org.tizen.system.deviced.PmQos",
252 static void __netconfig_wifi_update_indicator(void)
254 static int last_transfer_state = 0;
255 static guint64 netconfig_wifi_tx_bytes = 0;
256 static guint64 netconfig_wifi_rx_bytes = 0;
257 static int booster_tic = 0;
258 static int old_level = 0;
259 int booster_level = 0;
260 guint64 tx, rx, tx_diff, rx_diff;
263 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE) {
264 tx_diff = tx - netconfig_wifi_tx_bytes;
265 rx_diff = rx - netconfig_wifi_rx_bytes;
269 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TXRX;
271 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TX;
274 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_RX;
276 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_NONE;
279 if (transfer_state != last_transfer_state) {
280 netconfig_set_vconf_int(VCONFKEY_WIFI_TRANSFER_STATE, transfer_state);
281 last_transfer_state = transfer_state;
284 /* NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER */
285 if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 ||
286 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1)
288 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 ||
289 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2)
291 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 ||
292 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3)
295 if (old_level == booster_level) {
296 if (--booster_tic <= 0) {
297 __netconfig_wifi_data_activity_booster(booster_level);
302 __netconfig_wifi_data_activity_booster(booster_level);
304 if (booster_level > 0)
310 old_level = booster_level;
312 netconfig_wifi_tx_bytes = tx;
313 netconfig_wifi_rx_bytes = rx;
317 static gboolean __wifi_indicator_monitor(gpointer data)
321 int pm_state = VCONFKEY_PM_STATE_NORMAL;
323 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
326 /* In case of LCD off, we don't need to update Wi-Fi indicator */
327 vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
328 if (pm_state >= VCONFKEY_PM_STATE_LCDOFF)
331 rssi_dbm = __netconfig_wifi_update_and_get_rssi();
332 //INFO("%d dbm", rssi_dbm);
333 snr_level = netconfig_wifi_rssi_level(rssi_dbm);
334 __netconfig_wifi_set_rssi_level(snr_level);
336 __netconfig_wifi_update_indicator();
341 void netconfig_wifi_indicator_start(void)
343 INFO("Start Wi-Fi indicator");
345 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, VCONFKEY_WIFI_STRENGTH_MAX);
346 netconfig_start_timer_seconds(WIFI_INDICATOR_INTERVAL, __wifi_indicator_monitor, NULL, &netconfig_wifi_indicator_timer);
349 void netconfig_wifi_indicator_stop(void)
351 INFO("Stop Wi-Fi indicator");
353 netconfig_stop_timer(&netconfig_wifi_indicator_timer);
355 netconfig_wifi_rssi = VCONFKEY_WIFI_SNR_MIN;