|
| 1 | +/**************************************************************************** |
| 2 | + * apps/netutils/netlib/netlib_getifstatistics.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/config.h> |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | +#include <errno.h> |
| 30 | +#include <debug.h> |
| 31 | + |
| 32 | +#include <sys/types.h> |
| 33 | + |
| 34 | +#include "netutils/netlib.h" |
| 35 | + |
| 36 | +#ifdef CONFIG_NETDEV_STATISTICS |
| 37 | + |
| 38 | +/**************************************************************************** |
| 39 | + * Pre-processor Definitions |
| 40 | + ****************************************************************************/ |
| 41 | + |
| 42 | +/* Determines the size of an intermediate buffer that must be large enough |
| 43 | + * to handle the longest line generated by this logic. |
| 44 | + */ |
| 45 | + |
| 46 | +#define PROCFS_LINELEN 80 |
| 47 | +#define PROCFS_NET_PATH "/proc/net/" |
| 48 | + |
| 49 | +/* The form of the entry from the netstat file: |
| 50 | + * |
| 51 | + * wlan0 Link encap:Ethernet HWaddr 42:37:46:02:16:07 at UP mtu 1500 |
| 52 | + * inet addr:10.0.1.2 DRaddr:10.0.1.1 Mask:255.255.255.0 |
| 53 | + * inet6 DRaddr: :: |
| 54 | + * |
| 55 | + * RX: Received Fragment Errors Bytes |
| 56 | + * 000008c1 00000000 00000000 331ca8 |
| 57 | + * IPv4 IPv6 ARP Dropped |
| 58 | + * 000008a7 00000018 00000002 00000000 |
| 59 | + * TX: Queued Sent Errors Timeouts Bytes |
| 60 | + * 00000973 00000973 00000000 00000000 1b8d3 |
| 61 | + * Total Errors: 00000000 |
| 62 | + */ |
| 63 | + |
| 64 | +/**************************************************************************** |
| 65 | + * Public Functions |
| 66 | + ****************************************************************************/ |
| 67 | + |
| 68 | +/**************************************************************************** |
| 69 | + * Name: netlib_getifstatistics |
| 70 | + * |
| 71 | + * Description: |
| 72 | + * Read the DEV RX/TX statistics. |
| 73 | + * |
| 74 | + * Input Parameters: |
| 75 | + * ifname - The interface to read. |
| 76 | + * stat - The pointer to return dev statistics. |
| 77 | + * |
| 78 | + * Returned Value: |
| 79 | + * 0 on success. A negated errno value is returned |
| 80 | + * on any failure. |
| 81 | + ****************************************************************************/ |
| 82 | + |
| 83 | +int netlib_getifstatistics(FAR const char *ifname, |
| 84 | + FAR struct netdev_statistics_s *stat) |
| 85 | +{ |
| 86 | + FAR FILE *stream; |
| 87 | + char path[32]; |
| 88 | + char line[PROCFS_LINELEN]; |
| 89 | + int ret = OK; |
| 90 | + |
| 91 | + snprintf(path, sizeof(path), "%s%s", PROCFS_NET_PATH, ifname); |
| 92 | + ninfo("get statistics from %s \n", path); |
| 93 | + |
| 94 | + stream = fopen(path, "r"); |
| 95 | + if (stream == NULL) |
| 96 | + { |
| 97 | + fprintf(stderr, "ERROR: Failed to open path:%s \n", path); |
| 98 | + return -ENOTDIR; |
| 99 | + } |
| 100 | + |
| 101 | + while (fgets(line, sizeof(line), stream) != NULL) |
| 102 | + { |
| 103 | + /* Read RX header and next is the data */ |
| 104 | + |
| 105 | + if (strstr(line, "RX:") != NULL) |
| 106 | + { |
| 107 | + if (fgets(line, sizeof(line), stream) == NULL) |
| 108 | + { |
| 109 | + ret = -EINVAL; |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + if (sscanf(line, "%" SCNx32 "%" SCNx32 "%" SCNx32 "%" SCNx64, |
| 114 | + &stat->rx_packets, |
| 115 | + &stat->rx_fragments, |
| 116 | + &stat->rx_errors, |
| 117 | + &stat->rx_bytes) <= 0) |
| 118 | + { |
| 119 | + ret = -EINVAL; |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /* Read TX header and next is the data */ |
| 125 | + |
| 126 | + if (strstr(line, "TX:") != NULL) |
| 127 | + { |
| 128 | + if (fgets(line, sizeof(line), stream) == NULL) |
| 129 | + { |
| 130 | + ret = -EINVAL; |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + if (sscanf(line, "%" SCNx32 "%" SCNx32 "%" SCNx32 \ |
| 135 | + "%" SCNx32 "%" SCNx64, |
| 136 | + &stat->tx_packets, |
| 137 | + &stat->tx_done, |
| 138 | + &stat->tx_errors, |
| 139 | + &stat->tx_timeouts, |
| 140 | + &stat->tx_bytes) <= 0) |
| 141 | + { |
| 142 | + ret = -EINVAL; |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + fclose(stream); |
| 149 | + return ret; |
| 150 | +} |
| 151 | + |
| 152 | +#endif /* CONFIG_NETDEV_STATISTICS */ |
0 commit comments