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"
34 static Network_statistics *netconfigstatistics = NULL;
36 static wifi_device_data_s *__find_wifi_interface_name(const char *interface_name)
38 GSList *list = wifi_state_get_device_list();
40 for ( ; list; list = list->next) {
41 wifi_device_data_s *device_data = list->data;
42 if (g_strcmp0(device_data->interface_name, interface_name) == 0)
49 gboolean netconfig_wifi_get_bytes_statistics(guint64 *tx, guint64 *rx, gboolean update)
54 gchar *p_ifname = NULL, *p_entry = NULL;
57 wifi_device_data_s *device_data = NULL;
62 fp = fopen(NETCONFIG_PROCDEV, "r");
64 ERR("Failed to open %s", NETCONFIG_PROCDEV);
68 /* skip the first and second line */
69 if (fgets(buf, sizeof(buf), fp) == NULL ||
70 fgets(buf, sizeof(buf), fp) == NULL)
73 while (fgets(buf, sizeof(buf), fp)) {
74 long long unsigned int llval;
78 while (*p_ifname == ' ') p_ifname++;
79 p_entry = strchr(p_ifname, ':');
85 device_data = __find_wifi_interface_name(p_ifname);
86 if (device_data == NULL)
89 /* read interface statistics */
91 "%llu %llu %lu %lu %lu %lu %lu %lu "
92 "%llu %llu %lu %lu %lu %lu %lu %lu",
93 (long long unsigned int *)&curr_rx, /* rx bytes */
94 &llval, /* rx packet */
95 &lval, /* rx errors */
96 &lval, /* rx dropped */
97 &lval, /* rx fifo errors */
98 &lval, /* rx frame errors */
99 &lval, /* rx compressed */
100 &lval, /* rx multicast */
102 (long long unsigned int *)&curr_tx, /* tx bytes */
103 &llval, /* tx packet */
104 &lval, /* tx errors */
105 &lval, /* tx dropped */
106 &lval, /* tx fifo errors */
107 &lval, /* collisions */
108 &lval, /* tx carrier errors */
109 &lval /* tx compressed */
112 if (update == TRUE) {
113 device_data->tx_diff = curr_tx - device_data->tx;
114 device_data->rx_diff = curr_rx - device_data->rx;
115 device_data->tx = curr_tx;
116 device_data->rx = curr_rx;
119 *tx += device_data->tx_diff;
120 *rx += device_data->rx_diff;
130 void netconfig_wifi_get_bytes_default_iface(guint64 *tx, guint64 *rx)
132 wifi_device_data_s *device_data = NULL;
137 device_data = __find_wifi_interface_name(netconfig_get_default_ifname());
138 if (device_data == NULL)
141 *tx = device_data->tx_diff;
142 *rx = device_data->rx_diff;
145 void netconfig_wifi_set_bytes_pkt_vconf(guint64 tx_diff, guint64 rx_diff)
148 guint64 last_tx = 0, last_rx = 0;
149 guint64 total_tx = 0, total_rx = 0;
152 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
153 last_tx = (guint64)val + tx_diff;
155 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
156 last_rx = (guint64)val + rx_diff;
158 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, (int)last_tx, FALSE);
159 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, (int)last_rx, FALSE);
162 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
163 total_tx = (guint64)val + tx_diff;
165 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
166 total_rx = (guint64)val + rx_diff;
168 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, (int)total_tx, FALSE);
169 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, (int)total_rx, FALSE);
172 void netconfig_wifi_reset_last_bytes(void)
174 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0, FALSE);
175 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0, FALSE);
178 static gboolean handle_get_wifi_total_tx_bytes(
179 Network_statistics *object,
180 GDBusMethodInvocation *context)
183 guint64 tx = 0, rx = 0;
184 guint64 tx_bytes = 0;
185 guint64 total_bytes = 0;
188 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
190 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, &val);
191 tx_bytes = (guint64)val;
193 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
194 total_bytes = tx_bytes;
195 network_statistics_complete_get_wifi_total_tx_bytes(object, context, total_bytes);
199 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
200 total_bytes = tx + tx_bytes;
202 total_bytes = tx_bytes;
204 network_statistics_complete_get_wifi_total_tx_bytes(object, context, total_bytes);
208 static gboolean handle_get_wifi_total_rx_bytes(
209 Network_statistics *object,
210 GDBusMethodInvocation *context)
213 guint64 tx = 0, rx = 0;
214 guint64 rx_bytes = 0;
215 guint64 total_bytes = 0;
218 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
220 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, &val);
221 rx_bytes = (guint64)val;
223 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
224 total_bytes = rx_bytes;
225 network_statistics_complete_get_wifi_total_rx_bytes(object, context, total_bytes);
229 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
230 total_bytes = rx + rx_bytes;
232 total_bytes = rx_bytes;
234 network_statistics_complete_get_wifi_total_rx_bytes(object, context, total_bytes);
238 static gboolean handle_get_wifi_last_tx_bytes(
239 Network_statistics *object,
240 GDBusMethodInvocation *context)
243 guint64 tx = 0, rx = 0;
244 guint64 tx_bytes = 0;
245 guint64 last_bytes = 0;
248 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
250 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, &val);
251 tx_bytes = (guint64)val;
253 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
254 last_bytes = tx_bytes;
255 network_statistics_complete_get_wifi_last_tx_bytes(object, context, last_bytes);
259 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
260 last_bytes = tx + tx_bytes;
262 last_bytes = tx_bytes;
264 network_statistics_complete_get_wifi_last_tx_bytes(object, context, last_bytes);
268 static gboolean handle_get_wifi_last_rx_bytes(
269 Network_statistics *object,
270 GDBusMethodInvocation *context)
273 guint64 tx = 0, rx = 0;
274 guint64 rx_bytes = 0;
275 guint64 last_bytes = 0;
278 netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
280 netconfig_vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, &val);
281 rx_bytes = (guint64)val;
283 if (wifi_state != VCONFKEY_WIFI_CONNECTED) {
284 last_bytes = rx_bytes;
285 network_statistics_complete_get_wifi_last_rx_bytes(object, context, last_bytes);
289 if (netconfig_wifi_get_bytes_statistics(&tx, &rx, FALSE) == TRUE)
290 last_bytes = rx + rx_bytes;
292 last_bytes = rx_bytes;
294 network_statistics_complete_get_wifi_last_rx_bytes(object, context, last_bytes);
298 static gboolean handle_reset_cellular_total_tx_bytes(
299 Network_statistics *object,
300 GDBusMethodInvocation *context)
302 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT, 0, TRUE);
303 network_statistics_complete_reset_cellular_total_tx_bytes(object, context);
307 static gboolean handle_reset_cellular_total_rx_bytes(
308 Network_statistics *object,
309 GDBusMethodInvocation *context)
311 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV, 0, TRUE);
312 network_statistics_complete_reset_cellular_total_rx_bytes(object, context);
316 static gboolean handle_reset_cellular_last_tx_bytes(
317 Network_statistics *object,
318 GDBusMethodInvocation *context)
320 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT, 0, TRUE);
321 network_statistics_complete_reset_cellular_last_tx_bytes(object, context);
325 static gboolean handle_reset_cellular_last_rx_bytes(
326 Network_statistics *object,
327 GDBusMethodInvocation *context)
329 netconfig_set_vconf_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV, 0, TRUE);
330 network_statistics_complete_reset_cellular_last_rx_bytes(object, context);
334 static gboolean handle_reset_wifi_total_tx_bytes(
335 Network_statistics *object,
336 GDBusMethodInvocation *context)
338 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, 0, TRUE);
339 network_statistics_complete_reset_wifi_total_tx_bytes(object, context);
343 static gboolean handle_reset_wifi_total_rx_bytes(
344 Network_statistics *object,
345 GDBusMethodInvocation *context)
347 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, 0, TRUE);
348 network_statistics_complete_reset_wifi_total_rx_bytes(object, context);
352 static gboolean handle_reset_wifi_last_tx_bytes(
353 Network_statistics *object,
354 GDBusMethodInvocation *context)
356 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, 0, TRUE);
357 network_statistics_complete_reset_wifi_last_tx_bytes(object, context);
361 static gboolean handle_reset_wifi_last_rx_bytes(
362 Network_statistics *object,
363 GDBusMethodInvocation *context)
365 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, 0, TRUE);
366 network_statistics_complete_reset_wifi_last_rx_bytes(object, context);
370 static void wifi_statistics_update_state(void)
374 netconfig_wifi_get_bytes_statistics(&tx, &rx, TRUE);
377 void statistics_object_create_and_init(void)
379 DBG("Creating statistics object");
380 GDBusInterfaceSkeleton *interface_statistics = NULL;
381 GDBusConnection *connection = NULL;
382 GDBusObjectManagerServer *server = netdbus_get_statistics_manager();
386 connection = netdbus_get_connection();
387 g_dbus_object_manager_server_set_connection(server, connection);
389 /*Interface netconfig.network_statistics*/
390 netconfigstatistics = network_statistics_skeleton_new();
392 interface_statistics = 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_statistics, connection,
419 NETCONFIG_NETWORK_STATISTICS_PATH, NULL)) {
420 ERR("Export with path failed");
423 wifi_statistics_update_state();
428 void statistics_object_deinit(void)
430 g_object_unref(netconfigstatistics);