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-statistics.h"
29 #include "generated-code.h"
31 #define NETCONFIG_PROCDEV "/proc/net/dev"
33 static Network_statistics *netconfigstatistics = NULL;
35 gboolean netconfig_wifi_get_bytes_statistics(guint64 *tx, guint64 *rx)
40 gchar *p_ifname = NULL, *p_entry = NULL;
45 fp = fopen(NETCONFIG_PROCDEV, "r");
47 ERR("Failed to open %s", NETCONFIG_PROCDEV);
51 /* skip the first and second line */
52 if (fgets(buf, sizeof(buf), fp) == NULL ||
53 fgets(buf, sizeof(buf), fp) == NULL)
56 while (fgets(buf, sizeof(buf), fp)) {
61 while (*p_ifname == ' ') p_ifname++;
62 p_entry = strchr(p_ifname, ':');
65 if (g_strcmp0(p_ifname, WIFI_IFNAME) != 0)
68 /* read interface statistics */
70 "%llu %llu %lu %lu %lu %lu %lu %lu "
71 "%llu %llu %lu %lu %lu %lu %lu %lu",
73 &llval, /* rx packet */
74 &lval, /* rx errors */
75 &lval, /* rx dropped */
76 &lval, /* rx fifo errors */
77 &lval, /* rx frame errors */
78 &lval, /* rx compressed */
79 &lval, /* rx multicast */
82 &llval, /* tx packet */
83 &lval, /* tx errors */
84 &lval, /* tx dropped */
85 &lval, /* tx fifo errors */
86 &lval, /* collisions */
87 &lval, /* tx carrier errors */
88 &lval /* tx compressed */
100 static gboolean handle_get_wifi_total_tx_bytes(
101 Network_statistics *object,
102 GDBusMethodInvocation *context)
104 guint64 tx = 0, rx = 0;
105 guint64 tx_bytes = 0;
106 guint64 total_bytes = 0;
109 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
110 tx_bytes = (guint64)val;
112 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
113 total_bytes = tx + tx_bytes;
115 total_bytes = tx_bytes;
117 network_statistics_complete_get_wifi_total_tx_bytes(object, context, total_bytes);
121 static gboolean handle_get_wifi_total_rx_bytes(
122 Network_statistics *object,
123 GDBusMethodInvocation *context)
125 guint64 tx = 0, rx = 0;
126 guint64 rx_bytes = 0;
127 guint64 total_bytes = 0;
130 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
131 rx_bytes = (guint64)val;
133 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
134 total_bytes = rx + rx_bytes;
136 total_bytes = rx_bytes;
138 network_statistics_complete_get_wifi_total_rx_bytes(object, context, total_bytes);
142 static gboolean handle_get_wifi_last_tx_bytes(
143 Network_statistics *object,
144 GDBusMethodInvocation *context)
146 guint64 tx = 0, rx = 0;
147 guint64 tx_bytes = 0;
148 guint64 last_bytes = 0;
151 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
152 tx_bytes = (guint64)val;
154 if (netconfig_wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
155 last_bytes = tx_bytes;
156 network_statistics_complete_get_wifi_last_tx_bytes(object, context, last_bytes);
160 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
161 last_bytes = tx < tx_bytes ? 0 : tx - tx_bytes;
163 last_bytes = tx_bytes;
165 network_statistics_complete_get_wifi_last_tx_bytes(object, context, last_bytes);
169 static gboolean handle_get_wifi_last_rx_bytes(
170 Network_statistics *object,
171 GDBusMethodInvocation *context)
173 guint64 tx = 0, rx = 0;
174 guint64 rx_bytes = 0;
175 guint64 last_bytes = 0;
178 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
179 rx_bytes = (guint64)val;
181 if (netconfig_wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
182 last_bytes = rx_bytes;
183 network_statistics_complete_get_wifi_last_rx_bytes(object, context, last_bytes);
187 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
188 last_bytes = rx < rx_bytes ? 0 : rx - rx_bytes;
190 last_bytes = rx_bytes;
192 network_statistics_complete_get_wifi_last_rx_bytes(object, context, last_bytes);
196 static gboolean handle_reset_cellular_total_tx_bytes(
197 Network_statistics *object,
198 GDBusMethodInvocation *context)
200 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT, 0);
201 network_statistics_complete_reset_cellular_total_tx_bytes(object, context);
205 static gboolean handle_reset_cellular_total_rx_bytes(
206 Network_statistics *object,
207 GDBusMethodInvocation *context)
209 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV, 0);
210 network_statistics_complete_reset_cellular_total_rx_bytes(object, context);
214 static gboolean handle_reset_cellular_last_tx_bytes(
215 Network_statistics *object,
216 GDBusMethodInvocation *context)
218 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT, 0);
219 network_statistics_complete_reset_cellular_last_tx_bytes(object, context);
223 static gboolean handle_reset_cellular_last_rx_bytes(
224 Network_statistics *object,
225 GDBusMethodInvocation *context)
227 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV, 0);
228 network_statistics_complete_reset_cellular_last_rx_bytes(object, context);
232 static gboolean handle_reset_wifi_total_tx_bytes(
233 Network_statistics *object,
234 GDBusMethodInvocation *context)
236 guint64 tx = 0, rx = 0;
238 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
239 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, -(int)tx);
241 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, 0);
243 network_statistics_complete_reset_wifi_total_tx_bytes(object, context);
248 static gboolean handle_reset_wifi_total_rx_bytes(
249 Network_statistics *object,
250 GDBusMethodInvocation *context)
252 guint64 tx = 0, rx = 0;
254 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
255 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, -(int)rx);
257 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, 0);
259 network_statistics_complete_reset_wifi_total_rx_bytes(object, context);
263 static gboolean handle_reset_wifi_last_tx_bytes(
264 Network_statistics *object,
265 GDBusMethodInvocation *context)
267 guint64 tx = 0, rx = 0;
269 if (netconfig_wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
270 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0);
271 network_statistics_complete_reset_wifi_last_tx_bytes(object, context);
275 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
276 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)tx);
278 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0);
280 network_statistics_complete_reset_wifi_last_tx_bytes(object, context);
285 static gboolean handle_reset_wifi_last_rx_bytes(
286 Network_statistics *object,
287 GDBusMethodInvocation *context)
289 guint64 tx = 0, rx = 0;
291 if (netconfig_wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
292 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0);
293 network_statistics_complete_reset_wifi_last_rx_bytes(object, context);
297 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) == TRUE)
298 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)rx);
300 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0);
302 network_statistics_complete_reset_wifi_last_rx_bytes(object, context);
307 void netconfig_wifi_statistics_update_powered_off(void)
309 guint64 cur_tx = 0, cur_rx = 0;
310 guint64 prev_tx = 0, prev_rx = 0;
311 guint64 total_tx = 0, total_rx = 0;
314 if (netconfig_wifi_get_bytes_statistics(&cur_tx, &cur_rx) != TRUE)
317 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
318 prev_tx = (guint64)val;
320 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
321 prev_rx = (guint64)val;
323 total_tx = prev_tx + cur_tx;
324 total_rx = prev_rx + cur_rx;
326 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, (int)total_tx);
327 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, (int)total_rx);
330 static void netconfig_wifi_statistics_update_state(
331 enum netconfig_wifi_service_state state, void *user_data)
333 guint64 tx = 0, rx = 0;
334 guint64 last_tx = 0, last_rx = 0;
336 static enum netconfig_wifi_service_state prev_state = NETCONFIG_WIFI_UNKNOWN;
338 if (prev_state == NETCONFIG_WIFI_UNKNOWN) {
339 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0);
340 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0);
342 prev_state = NETCONFIG_WIFI_IDLE;
346 if (netconfig_wifi_get_bytes_statistics(&tx, &rx) != TRUE)
349 if (state == NETCONFIG_WIFI_CONNECTED) {
353 if (prev_state != NETCONFIG_WIFI_CONNECTED)
356 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
357 last_tx = (guint64)val;
359 vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
360 last_rx = (guint64)val;
362 last_tx = tx < last_tx ? 0 : tx - last_tx;
363 last_rx = rx < last_rx ? 0 : rx - last_rx;
366 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)last_tx);
367 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)last_rx);
372 static struct netconfig_wifi_state_notifier state_notifier = {
373 .netconfig_wifi_state_changed = netconfig_wifi_statistics_update_state,
377 void netconfig_network_statistics_create_and_init(void)
379 DBG("Creating statistics object");
380 GDBusInterfaceSkeleton *interface = NULL;
381 GDBusConnection *connection = NULL;
382 GDBusObjectManagerServer *server = netconfig_get_statistics_manager();
386 connection = netconfig_gdbus_get_connection();
387 g_dbus_object_manager_server_set_connection(server, connection);
390 netconfigstatistics = network_statistics_skeleton_new();
392 interface = G_DBUS_INTERFACE_SKELETON(netconfigstatistics);
393 g_signal_connect(netconfigstatistics, "handle-get-wifi-last-rx-bytes",
394 G_CALLBACK(handle_get_wifi_last_rx_bytes), NULL);
395 g_signal_connect(netconfigstatistics, "handle-get-wifi-last-tx-bytes",
396 G_CALLBACK(handle_get_wifi_last_tx_bytes), NULL);
397 g_signal_connect(netconfigstatistics, "handle-get-wifi-total-rx-bytes",
398 G_CALLBACK(handle_get_wifi_total_rx_bytes), NULL);
399 g_signal_connect(netconfigstatistics, "handle-get-wifi-total-tx-bytes",
400 G_CALLBACK(handle_get_wifi_total_tx_bytes), NULL);
401 g_signal_connect(netconfigstatistics, "handle-reset-cellular-last-rx-bytes",
402 G_CALLBACK(handle_reset_cellular_last_rx_bytes), NULL);
403 g_signal_connect(netconfigstatistics, "handle-reset-cellular-last-tx-bytes",
404 G_CALLBACK(handle_reset_cellular_last_tx_bytes), NULL);
405 g_signal_connect(netconfigstatistics, "handle-reset-cellular-total-rx-bytes",
406 G_CALLBACK(handle_reset_cellular_total_rx_bytes), NULL);
407 g_signal_connect(netconfigstatistics, "handle-reset-cellular-total-tx-bytes",
408 G_CALLBACK(handle_reset_cellular_total_tx_bytes), NULL);
409 g_signal_connect(netconfigstatistics, "handle-reset-wifi-last-rx-bytes",
410 G_CALLBACK(handle_reset_wifi_last_rx_bytes), NULL);
411 g_signal_connect(netconfigstatistics, "handle-reset-wifi-last-tx-bytes",
412 G_CALLBACK(handle_reset_wifi_last_tx_bytes), NULL);
413 g_signal_connect(netconfigstatistics, "handle-reset-wifi-total-rx-bytes",
414 G_CALLBACK(handle_reset_wifi_total_rx_bytes), NULL);
415 g_signal_connect(netconfigstatistics, "handle-reset-wifi-total-tx-bytes",
416 G_CALLBACK(handle_reset_wifi_total_tx_bytes), NULL);
418 if (!g_dbus_interface_skeleton_export(interface, connection,
419 NETCONFIG_NETWORK_STATISTICS_PATH, NULL)) {
420 ERR("Export with path failed");
423 netconfig_wifi_statistics_update_state(NETCONFIG_WIFI_IDLE, NULL);
424 netconfig_wifi_state_notifier_register(&state_notifier);