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 */
103 p_entry = strtok(p_entry, " ."); // status "%x"
105 sscanf(p_entry, "%x", &status);
106 p_entry = strtok(NULL, " ."); // Quality link "%d"
108 sscanf(p_entry, "%d", &link);
109 p_entry = strtok(NULL, " ."); // Quality level "%d"
111 sscanf(p_entry, "%d", &rssi_dbm);
112 p_entry = strtok(NULL, " ."); // Quality noise "%d"
114 sscanf(p_entry, "%d", &noise);
116 /* No need to read */
118 p_entry = strtok(NULL, " ."); // Discarded nwid "%lu"
119 sscanf(p_entry, "%lu", &nwid);
120 p_entry = strtok(NULL, " ."); // Discarded crypt "%lu"
121 sscanf(p_entry, "%lu", &crypt);
122 p_entry = strtok(NULL, " ."); // Discarded frag "%lu"
123 sscanf(p_entry, "%lu", &frag);
124 p_entry = strtok(NULL, " ."); // Discarded retry "%lu"
125 sscanf(p_entry, "%lu", &retry);
126 p_entry = strtok(NULL, " ."); // Discarded misc "%lu"
127 sscanf(p_entry, "%lu", &misc);
128 p_entry = strtok(NULL, " ."); // Discarded missed "%lu"
129 sscanf(p_entry, "%lu", &missed);
137 netconfig_wifi_rssi = rssi_dbm;
142 int netconfig_wifi_rssi_level(const int rssi_dbm)
146 /* Wi-Fi Signal Strength Display
155 else if (rssi_dbm >= -74)
157 else if (rssi_dbm >= -82)
165 static void __netconfig_wifi_set_rssi_level(const int snr_level)
167 static int last_snr_level = 0;
169 if (snr_level != last_snr_level) {
170 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, snr_level);
171 last_snr_level = snr_level;
175 static void __netconfig_wifi_data_activity_booster(int level)
177 gboolean reply = FALSE;
178 GVariant *params = NULL;
186 static int old_level = 0;
195 params = g_variant_new("(ii)", level1, lock);
198 params = g_variant_new("(ii)", level2, lock);
201 params = g_variant_new("(ii)", level3, lock);
204 ERR("Invalid level");
208 reply = netconfig_invoke_dbus_method_nonblock(
209 "org.tizen.system.deviced",
210 "/Org/Tizen/System/DeviceD/PmQos",
211 "org.tizen.system.deviced.PmQos",
219 /* disable previous booster */
220 if (old_level == 0 || old_level == level)
225 params = g_variant_new("(ii)", level1, unlock);
228 params = g_variant_new("(ii)", level2, unlock);
231 params = g_variant_new("(ii)", level3, unlock);
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",
251 static void __netconfig_wifi_update_indicator(void)
253 static int last_transfer_state = 0;
254 static guint64 netconfig_wifi_tx_bytes = 0;
255 static guint64 netconfig_wifi_rx_bytes = 0;
256 static int booster_tic = 0;
257 static int old_level = 0;
258 int booster_level = 0;
259 guint64 tx, rx, tx_diff, rx_diff;
262 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE) {
263 tx_diff = tx - netconfig_wifi_tx_bytes;
264 rx_diff = rx - netconfig_wifi_rx_bytes;
268 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TXRX;
270 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_TX;
273 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_RX;
275 transfer_state = VCONFKEY_WIFI_TRANSFER_STATE_NONE;
278 if (transfer_state != last_transfer_state) {
279 netconfig_set_vconf_int(VCONFKEY_WIFI_TRANSFER_STATE, transfer_state);
280 last_transfer_state = transfer_state;
283 /* NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER */
284 if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1 ||
285 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL1)
287 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2 ||
288 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL2)
290 else if (tx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3 ||
291 rx_diff >= NETCONFIG_WIFI_DATA_ACTIVITY_BOOSTER_LEVEL3)
294 if (old_level == booster_level) {
295 if (--booster_tic <= 0) {
296 __netconfig_wifi_data_activity_booster(booster_level);
301 __netconfig_wifi_data_activity_booster(booster_level);
303 if (booster_level > 0)
309 old_level = booster_level;
311 netconfig_wifi_tx_bytes = tx;
312 netconfig_wifi_rx_bytes = rx;
316 static gboolean __wifi_indicator_monitor(gpointer data)
320 int pm_state = VCONFKEY_PM_STATE_NORMAL;
322 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
325 /* In case of LCD off, we don't need to update Wi-Fi indicator */
326 vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
327 if (pm_state >= VCONFKEY_PM_STATE_LCDOFF)
330 rssi_dbm = __netconfig_wifi_update_and_get_rssi();
331 //INFO("%d dbm", rssi_dbm);
332 snr_level = netconfig_wifi_rssi_level(rssi_dbm);
333 __netconfig_wifi_set_rssi_level(snr_level);
335 __netconfig_wifi_update_indicator();
340 void netconfig_wifi_indicator_start(void)
342 INFO("Start Wi-Fi indicator");
344 netconfig_set_vconf_int(VCONFKEY_WIFI_STRENGTH, VCONFKEY_WIFI_STRENGTH_MAX);
345 netconfig_start_timer_seconds(WIFI_INDICATOR_INTERVAL, __wifi_indicator_monitor, NULL, &netconfig_wifi_indicator_timer);
348 void netconfig_wifi_indicator_stop(void)
350 INFO("Stop Wi-Fi indicator");
352 netconfig_stop_timer(&netconfig_wifi_indicator_timer);
354 netconfig_wifi_rssi = VCONFKEY_WIFI_SNR_MIN;