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.
22 #include <vconf-keys.h>
26 #include "netsupplicant.h"
27 #include "network-state.h"
28 #include "network-statistics.h"
30 #include "generated-code.h"
32 #define NETCONFIG_PROCDEV "/proc/net/dev"
33 #define WIFI_VCONF_UPDATE_INTERVAL 3600
38 guint64 last_tx_offset;
39 guint64 last_rx_offset;
42 static guint wifi_vconf_update_timer = 0;
43 static Network_statistics *netconfigstatistics = NULL;
45 static wifi_device_data_s *__find_wifi_interface_name(const char *interface_name)
47 GSList *list = wifi_state_get_device_list();
49 for ( ; list; list = list->next) {
50 wifi_device_data_s *device_data = list->data;
51 if (g_strcmp0(device_data->interface_name, interface_name) == 0)
58 gboolean netconfig_wifi_get_bytes_statistics(guint64 *tx, guint64 *rx, gboolean update)
63 gchar *p_ifname = NULL, *p_entry = NULL;
66 wifi_device_data_s *device_data = NULL;
71 fp = fopen(NETCONFIG_PROCDEV, "r");
73 ERR("Failed to open %s", NETCONFIG_PROCDEV);
77 /* skip the first and second line */
78 if (fgets(buf, sizeof(buf), fp) == NULL ||
79 fgets(buf, sizeof(buf), fp) == NULL)
82 while (fgets(buf, sizeof(buf), fp)) {
83 long long unsigned int llval;
87 while (*p_ifname == ' ') p_ifname++;
88 p_entry = strchr(p_ifname, ':');
94 device_data = __find_wifi_interface_name(p_ifname);
95 if (device_data == NULL)
98 /* read interface statistics */
100 "%llu %llu %lu %lu %lu %lu %lu %lu "
101 "%llu %llu %lu %lu %lu %lu %lu %lu",
102 (long long unsigned int *)&curr_rx, /* rx bytes */
103 &llval, /* rx packet */
104 &lval, /* rx errors */
105 &lval, /* rx dropped */
106 &lval, /* rx fifo errors */
107 &lval, /* rx frame errors */
108 &lval, /* rx compressed */
109 &lval, /* rx multicast */
111 (long long unsigned int *)&curr_tx, /* tx bytes */
112 &llval, /* tx packet */
113 &lval, /* tx errors */
114 &lval, /* tx dropped */
115 &lval, /* tx fifo errors */
116 &lval, /* collisions */
117 &lval, /* tx carrier errors */
118 &lval /* tx compressed */
121 if (update == TRUE) {
122 device_data->tx_diff = curr_tx - device_data->tx;
123 device_data->rx_diff = curr_rx - device_data->rx;
124 device_data->tx = curr_tx;
125 device_data->rx = curr_rx;
128 *tx += device_data->tx_diff;
129 *rx += device_data->rx_diff;
139 void netconfig_wifi_get_bytes_default_iface(guint64 *tx, guint64 *rx)
141 wifi_device_data_s *device_data = NULL;
146 device_data = __find_wifi_interface_name(netconfig_get_default_ifname());
147 if (device_data == NULL)
150 *tx = device_data->tx_diff;
151 *rx = device_data->rx_diff;
154 void netconfig_wifi_set_bytes_pkt_vconf(guint64 tx_diff, guint64 rx_diff, gboolean update)
157 guint64 last_tx = 0, last_rx = 0;
158 guint64 total_tx = 0, total_rx = 0;
160 stat_count.tx_diff += tx_diff;
161 stat_count.rx_diff += rx_diff;
165 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
166 last_tx = (guint64)val + stat_count.tx_diff - stat_count.last_tx_offset;
168 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
169 last_rx = (guint64)val + stat_count.rx_diff - stat_count.last_rx_offset;
171 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)last_tx, FALSE);
172 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)last_rx, FALSE);
174 stat_count.last_tx_offset = 0;
175 stat_count.last_rx_offset = 0;
178 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
179 total_tx = (guint64)val + stat_count.tx_diff;
181 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
182 total_rx = (guint64)val + stat_count.rx_diff;
184 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, (int)total_tx, FALSE);
185 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, (int)total_rx, FALSE);
187 stat_count.rx_diff = 0;
188 stat_count.tx_diff = 0;
192 void netconfig_wifi_reset_last_bytes(void)
196 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
197 stat_count.last_tx_offset = (guint64)val + stat_count.tx_diff;
199 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
200 stat_count.last_rx_offset = (guint64)val + stat_count.rx_diff;
203 static gboolean handle_get_wifi_total_tx_bytes(
204 Network_statistics *object,
205 GDBusMethodInvocation *context)
208 guint64 tx = 0, rx = 0;
209 guint64 tx_bytes = 0;
210 guint64 total_bytes = 0;
213 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
215 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
216 tx_bytes = (guint64)val + stat_count.tx_diff;
218 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
219 total_bytes = tx_bytes;
220 network_statistics_complete_get_wifi_total_tx_bytes(object, context, total_bytes);
224 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
225 total_bytes = tx + tx_bytes;
226 stat_count.tx_diff += tx;
227 stat_count.rx_diff += rx;
229 total_bytes = tx_bytes;
232 network_statistics_complete_get_wifi_total_tx_bytes(object, context, total_bytes);
236 static gboolean handle_get_wifi_total_rx_bytes(
237 Network_statistics *object,
238 GDBusMethodInvocation *context)
241 guint64 tx = 0, rx = 0;
242 guint64 rx_bytes = 0;
243 guint64 total_bytes = 0;
246 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
248 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
249 rx_bytes = (guint64)val + stat_count.rx_diff;
251 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
252 total_bytes = rx_bytes;
253 network_statistics_complete_get_wifi_total_rx_bytes(object, context, total_bytes);
257 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
258 total_bytes = rx + rx_bytes;
259 stat_count.tx_diff += tx;
260 stat_count.rx_diff += rx;
262 total_bytes = rx_bytes;
265 network_statistics_complete_get_wifi_total_rx_bytes(object, context, total_bytes);
269 static gboolean handle_get_wifi_last_tx_bytes(
270 Network_statistics *object,
271 GDBusMethodInvocation *context)
274 guint64 tx = 0, rx = 0;
275 guint64 tx_bytes = 0;
276 guint64 last_bytes = 0;
279 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
281 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
282 tx_bytes = (guint64)val + stat_count.tx_diff - stat_count.last_tx_offset;
284 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
285 last_bytes = tx_bytes;
286 network_statistics_complete_get_wifi_last_tx_bytes(object, context, last_bytes);
290 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
291 last_bytes = tx + tx_bytes;
292 stat_count.tx_diff += tx;
293 stat_count.rx_diff += rx;
295 last_bytes = tx_bytes;
298 network_statistics_complete_get_wifi_last_tx_bytes(object, context, last_bytes);
302 static gboolean handle_get_wifi_last_rx_bytes(
303 Network_statistics *object,
304 GDBusMethodInvocation *context)
307 guint64 tx = 0, rx = 0;
308 guint64 rx_bytes = 0;
309 guint64 last_bytes = 0;
312 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
314 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
315 rx_bytes = (guint64)val + stat_count.rx_diff - stat_count.last_rx_offset;
317 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
318 last_bytes = rx_bytes;
319 network_statistics_complete_get_wifi_last_rx_bytes(object, context, last_bytes);
323 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE) {
324 last_bytes = rx + rx_bytes;
325 stat_count.tx_diff += tx;
326 stat_count.rx_diff += rx;
328 last_bytes = rx_bytes;
331 network_statistics_complete_get_wifi_last_rx_bytes(object, context, last_bytes);
335 static gboolean handle_reset_cellular_total_tx_bytes(
336 Network_statistics *object,
337 GDBusMethodInvocation *context)
339 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT, 0, TRUE);
340 network_statistics_complete_reset_cellular_total_tx_bytes(object, context);
344 static gboolean handle_reset_cellular_total_rx_bytes(
345 Network_statistics *object,
346 GDBusMethodInvocation *context)
348 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV, 0, TRUE);
349 network_statistics_complete_reset_cellular_total_rx_bytes(object, context);
353 static gboolean handle_reset_cellular_last_tx_bytes(
354 Network_statistics *object,
355 GDBusMethodInvocation *context)
357 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT, 0, TRUE);
358 network_statistics_complete_reset_cellular_last_tx_bytes(object, context);
362 static gboolean handle_reset_cellular_last_rx_bytes(
363 Network_statistics *object,
364 GDBusMethodInvocation *context)
366 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV, 0, TRUE);
367 network_statistics_complete_reset_cellular_last_rx_bytes(object, context);
371 static gboolean handle_reset_wifi_total_tx_bytes(
372 Network_statistics *object,
373 GDBusMethodInvocation *context)
378 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
379 last_tx = (guint64)val + stat_count.tx_diff - stat_count.last_tx_offset;
381 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)last_tx, FALSE);
382 stat_count.last_tx_offset = 0;
383 stat_count.tx_diff = 0;
385 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, 0, TRUE);
386 network_statistics_complete_reset_wifi_total_tx_bytes(object, context);
390 static gboolean handle_reset_wifi_total_rx_bytes(
391 Network_statistics *object,
392 GDBusMethodInvocation *context)
397 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
398 last_rx = (guint64)val + stat_count.rx_diff - stat_count.last_rx_offset;
400 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)last_rx, FALSE);
401 stat_count.last_rx_offset = 0;
402 stat_count.rx_diff = 0;
404 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, 0, TRUE);
405 network_statistics_complete_reset_wifi_total_rx_bytes(object, context);
409 static gboolean handle_reset_wifi_last_tx_bytes(
410 Network_statistics *object,
411 GDBusMethodInvocation *context)
414 guint64 total_tx = 0;
416 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
417 total_tx = (guint64)val + stat_count.tx_diff;
419 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, (int)total_tx, FALSE);
420 stat_count.tx_diff = 0;
422 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0, TRUE);
423 stat_count.last_tx_offset = 0;
424 network_statistics_complete_reset_wifi_last_tx_bytes(object, context);
428 static gboolean handle_reset_wifi_last_rx_bytes(
429 Network_statistics *object,
430 GDBusMethodInvocation *context)
433 guint64 total_rx = 0;
435 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
436 total_rx = (guint64)val + stat_count.rx_diff;
438 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, (int)total_rx, FALSE);
439 stat_count.rx_diff = 0;
441 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0, TRUE);
442 stat_count.last_rx_offset = 0;
443 network_statistics_complete_reset_wifi_last_rx_bytes(object, context);
447 static void wifi_statistics_update_state(void)
451 netconfig_wifi_get_bytes_statistics(&tx, &rx, TRUE);
454 static gboolean __statistics_update_wifi_vconf(gpointer data)
456 if (stat_count.tx_diff ||
457 stat_count.rx_diff ||
458 stat_count.last_tx_offset ||
459 stat_count.last_rx_offset)
460 netconfig_wifi_set_bytes_pkt_vconf(0, 0, TRUE);
465 void statistics_object_create_and_init(void)
467 DBG("Creating statistics object");
468 GDBusInterfaceSkeleton *interface_statistics = NULL;
469 GDBusConnection *connection = NULL;
470 GDBusObjectManagerServer *server = netdbus_get_statistics_manager();
474 connection = netdbus_get_connection();
475 g_dbus_object_manager_server_set_connection(server, connection);
477 /*Interface netconfig.network_statistics*/
478 netconfigstatistics = network_statistics_skeleton_new();
480 interface_statistics = G_DBUS_INTERFACE_SKELETON(netconfigstatistics);
481 g_signal_connect(netconfigstatistics, "handle-get-wifi-last-rx-bytes",
482 G_CALLBACK(handle_get_wifi_last_rx_bytes), NULL);
483 g_signal_connect(netconfigstatistics, "handle-get-wifi-last-tx-bytes",
484 G_CALLBACK(handle_get_wifi_last_tx_bytes), NULL);
485 g_signal_connect(netconfigstatistics, "handle-get-wifi-total-rx-bytes",
486 G_CALLBACK(handle_get_wifi_total_rx_bytes), NULL);
487 g_signal_connect(netconfigstatistics, "handle-get-wifi-total-tx-bytes",
488 G_CALLBACK(handle_get_wifi_total_tx_bytes), NULL);
489 g_signal_connect(netconfigstatistics, "handle-reset-cellular-last-rx-bytes",
490 G_CALLBACK(handle_reset_cellular_last_rx_bytes), NULL);
491 g_signal_connect(netconfigstatistics, "handle-reset-cellular-last-tx-bytes",
492 G_CALLBACK(handle_reset_cellular_last_tx_bytes), NULL);
493 g_signal_connect(netconfigstatistics, "handle-reset-cellular-total-rx-bytes",
494 G_CALLBACK(handle_reset_cellular_total_rx_bytes), NULL);
495 g_signal_connect(netconfigstatistics, "handle-reset-cellular-total-tx-bytes",
496 G_CALLBACK(handle_reset_cellular_total_tx_bytes), NULL);
497 g_signal_connect(netconfigstatistics, "handle-reset-wifi-last-rx-bytes",
498 G_CALLBACK(handle_reset_wifi_last_rx_bytes), NULL);
499 g_signal_connect(netconfigstatistics, "handle-reset-wifi-last-tx-bytes",
500 G_CALLBACK(handle_reset_wifi_last_tx_bytes), NULL);
501 g_signal_connect(netconfigstatistics, "handle-reset-wifi-total-rx-bytes",
502 G_CALLBACK(handle_reset_wifi_total_rx_bytes), NULL);
503 g_signal_connect(netconfigstatistics, "handle-reset-wifi-total-tx-bytes",
504 G_CALLBACK(handle_reset_wifi_total_tx_bytes), NULL);
506 if (!g_dbus_interface_skeleton_export(interface_statistics, connection,
507 NETCONFIG_NETWORK_STATISTICS_PATH, NULL)) {
508 ERR("Export with path failed");
511 wifi_statistics_update_state();
512 netconfig_start_timer_seconds(WIFI_VCONF_UPDATE_INTERVAL,
513 __statistics_update_wifi_vconf, NULL, &wifi_vconf_update_timer);
518 void statistics_object_deinit(void)
520 netconfig_wifi_set_bytes_pkt_vconf(0, 0, TRUE);
521 netconfig_stop_timer(&wifi_vconf_update_timer);
522 g_object_unref(netconfigstatistics);