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 guint netconfig_wifi_indicator_timer = 0;
62 int netconfig_wifi_get_rssi(void)
64 return netconfig_wifi_rssi;
67 static int __netconfig_wifi_update_and_get_rssi(void)
71 char *p_ifname = NULL, *p_entry = NULL;
72 int rssi_dbm = VCONFKEY_WIFI_SNR_MIN;
74 fp = fopen(NETCONFIG_PROCWIRELESS, "r");
76 ERR("Failed to open %s", NETCONFIG_PROCWIRELESS);
80 /* skip the first and second line */
81 if (fgets(buf, sizeof(buf), fp) == NULL ||
82 fgets(buf, sizeof(buf), fp) == NULL)
85 while (fgets(buf, sizeof(buf), fp)) {
86 unsigned int status = 0;
91 unsigned long nwid, crypt, frag, retry, misc, missed;
95 while (*p_ifname == ' ') p_ifname++;
96 p_entry = strchr(p_ifname, ':');
101 if (g_strcmp0(p_ifname, WIFI_IFNAME) != 0)
104 /* read wireless status */
106 p_entry = strtok_r(p_entry, " .", &saveptr);
110 status = (int)strtol(p_entry, NULL, 16);
112 p_entry = strtok_r(NULL, " .", &saveptr);
114 /* Quality link "%d" */
116 link = (int)strtol(p_entry, NULL, 10);
118 p_entry = strtok_r(NULL, " .", &saveptr);
120 /* Quality level "%d" */
122 rssi_dbm = (int)strtol(p_entry, NULL, 10);
124 p_entry = strtok_r(NULL, " .", &saveptr);
126 /* Quality noise "%d" */
128 noise = (int)strtol(p_entry, NULL, 10);
130 DBG("status(%x) link(%d) rssi_dbm(%d) noise(%d)",
131 status, link, rssi_dbm, noise);
133 /* No need to read */
135 p_entry = strtok(NULL, " ."); // Discarded nwid "%lu"
136 sscanf(p_entry, "%lu", &nwid);
137 p_entry = strtok(NULL, " ."); // Discarded crypt "%lu"
138 sscanf(p_entry, "%lu", &crypt);
139 p_entry = strtok(NULL, " ."); // Discarded frag "%lu"
140 sscanf(p_entry, "%lu", &frag);
141 p_entry = strtok(NULL, " ."); // Discarded retry "%lu"
142 sscanf(p_entry, "%lu", &retry);
143 p_entry = strtok(NULL, " ."); // Discarded misc "%lu"
144 sscanf(p_entry, "%lu", &misc);
145 p_entry = strtok(NULL, " ."); // Discarded missed "%lu"
146 sscanf(p_entry, "%lu", &missed);
154 netconfig_wifi_rssi = rssi_dbm;
159 int netconfig_wifi_rssi_level(const int rssi_dbm)
163 /* Wi-Fi Signal Strength Display
172 else if (rssi_dbm >= -74)
174 else if (rssi_dbm >= -82)
182 static void __netconfig_wifi_set_rssi_level(const int snr_level)
184 static int last_snr_level = 0;
186 if (snr_level != last_snr_level) {
187 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, snr_level);
188 last_snr_level = snr_level;
192 static void __netconfig_wifi_data_activity_booster(int level)
194 gboolean reply = FALSE;
195 GVariant *params = NULL;
203 static int old_level = 0;
212 params = g_variant_new("(ii)", level1, lock);
215 params = g_variant_new("(ii)", level2, lock);
218 params = g_variant_new("(ii)", level3, lock);
221 ERR("Invalid level");
225 reply = netconfig_invoke_dbus_method_nonblock(
226 "org.tizen.system.deviced",
227 "/Org/Tizen/System/DeviceD/PmQos",
228 "org.tizen.system.deviced.PmQos",
236 /* disable previous booster */
237 if (old_level == 0 || old_level == level)
242 params = g_variant_new("(ii)", level1, unlock);
245 params = g_variant_new("(ii)", level2, unlock);
248 params = g_variant_new("(ii)", level3, unlock);
251 ERR("Invalid level");
255 reply = netconfig_invoke_dbus_method_nonblock(
256 "org.tizen.system.deviced",
257 "/Org/Tizen/System/DeviceD/PmQos",
258 "org.tizen.system.deviced.PmQos",
268 static void __netconfig_wifi_update_indicator(void)
270 static int last_transfer_state = 0;
271 static guint64 netconfig_wifi_tx_bytes = 0;
272 static guint64 netconfig_wifi_rx_bytes = 0;
273 static int booster_tic = 0;
274 static int old_level = 0;
275 int booster_level = 0;
276 guint64 tx, rx, tx_diff, rx_diff;
279 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE) {
280 tx_diff = tx - netconfig_wifi_tx_bytes;
281 rx_diff = rx - netconfig_wifi_rx_bytes;
285 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TXRX;
287 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TX;
290 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_RX;
292 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_NONE;
295 if (transfer_state != last_transfer_state) {
296 netconfig_set_vconf_int(VCONFKEY_WIFI_TRANSFER_STATE, transfer_state);
297 last_transfer_state = transfer_state;
300 /* NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER */
301 if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 ||
302 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1)
304 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 ||
305 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2)
307 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 ||
308 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3)
311 if (old_level == booster_level) {
312 if (--booster_tic <= 0) {
313 __netconfig_wifi_data_activity_booster(booster_level);
318 __netconfig_wifi_data_activity_booster(booster_level);
320 if (booster_level > 0)
326 old_level = booster_level;
328 netconfig_wifi_tx_bytes = tx;
329 netconfig_wifi_rx_bytes = rx;
333 static gboolean __wifi_indicator_monitor(gpointer data)
337 int pm_state = VCONFKEY_PM_STATE_NORMAL;
339 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
342 /* In case of LCD off, we don't need to update Wi-Fi indicator */
343 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
344 if (pm_state >= VCONFKEY_PM_STATE_LCDOFF)
347 rssi_dbm = __netconfig_wifi_update_and_get_rssi();
348 /* INFO("%d dbm", rssi_dbm); */
349 snr_level = netconfig_wifi_rssi_level(rssi_dbm);
350 __netconfig_wifi_set_rssi_level(snr_level);
352 __netconfig_wifi_update_indicator();
357 void netconfig_wifi_indicator_start(void)
359 INFO("Start Wi-Fi indicator");
361 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, VCONFKEY_WIFI_STRENGTH_MAX);
362 netconfig_start_timer_seconds(WIFI_INDICATOR_INTERVAL, __wifi_indicator_monitor, NULL, &netconfig_wifi_indicator_timer);
365 void netconfig_wifi_indicator_stop(void)
367 INFO("Stop Wi-Fi indicator");
369 netconfig_stop_timer(&netconfig_wifi_indicator_timer);
371 netconfig_wifi_rssi = VCONFKEY_WIFI_SNR_MIN;