Mistake on this page?
Report an issue in GitHub or email us
SocketStats.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef SOCKET_STATS_H
19 #define SOCKET_STATS_H
20 
21 #include "platform/SingletonPtr.h"
22 #include "platform/PlatformMutex.h"
23 #include "netsocket/Socket.h"
24 #include "SocketAddress.h"
25 #include "hal/ticker_api.h"
26 
27 #ifndef MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT
28 #define MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT 10
29 #endif
30 
31 /** Enum of socket states
32  *
33  * Can be used to specify current state of socket - open, closed, connected or listen.
34  *
35  * @enum socket_state
36  */
37 typedef enum {
38  SOCK_CLOSED, /**< Socket is closed and does not exist anymore in the system */
39  SOCK_OPEN, /**< Socket is open but not associated to any peer address */
40  SOCK_CONNECTED, /**< Socket is associated to peer address, either by connect() or sendto()/recvfrom() calls */
41  SOCK_LISTEN, /**< Socket is listening for incoming connections */
42 } socket_state;
43 
44 /** Structure to parse socket statistics
45  */
46 typedef struct {
47  Socket *reference_id; /**< Used for identifying socket */
48  SocketAddress peer; /**< Last associated peername of this socket (Destination address) */
49  socket_state state; /**< State of this socket */
50  nsapi_protocol_t proto; /**< Specifies a protocol used with socket */
51  size_t sent_bytes; /**< Data sent through this socket */
52  size_t recv_bytes; /**< Data received through this socket */
53  us_timestamp_t last_change_tick;/**< osKernelGetTick() when state last changed */
55 
56 /** SocketStats class
57  *
58  * Class to get the network socket statistics
59  */
60 class SocketStats {
61 public:
62 
63 #if !defined(DOXYGEN_ONLY)
64  /** Create a socket statistics object
65  *
66  * Application users must not create class objects.
67  * Entities reporting network statistics create the class object.
68  * Application can fetch network statistics using static `mbed_stats_socket_get_each` API
69  * without creating an object.
70  */
71  constexpr SocketStats() = default;
72 #endif
73  /**
74  * Fill the passed array of structures with the socket statistics for each created socket.
75  *
76  * @param stats A pointer to an array of mbed_stats_socket_t structures to fill
77  * @param count The number of mbed_stats_socket_t structures in the provided array
78  * @return The number of mbed_stats_socket_t structures that have been filled.
79  * If the number of sockets on the system is less than or equal to count,
80  * it will equal the number of sockets created (active or closed).
81  * If the number of sockets on the system is greater than count,
82  * it will equal count.
83  */
84  static size_t mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count);
85 
86 #if !defined(DOXYGEN_ONLY)
87  /** Add entry of newly created socket in statistics array.
88  * API used by socket (TCP or UDP) layers only, not to be used by application.
89  *
90  * @param reference_id ID to identify socket in data array.
91  *
92  * @Note: The entry in the array is maintained even after the socket is closed.
93  * The entry is overwritten for sockets that were closed first, in case
94  * the socket creation count exceeds `MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT`.
95  *
96  */
97  void stats_new_socket_entry(Socket *reference_id);
98 
99  /** Updates the state of the socket and records `tick_last_change`.
100  * API used by socket (TCP or UDP) layers only, not to be used by application.
101  *
102  * @param reference_id ID to identify socket in data array.
103  * @param state Parameter to update the current state of the socket.
104  *
105  */
106  void stats_update_socket_state(const Socket *reference_id, socket_state state);
107 
108  /** Update the peer information of the socket.
109  * API used by socket (TCP or UDP) layers only, not to be used by application.
110  *
111  * @param reference_id ID to identify socket in data array.
112  * @param peer Parameter to update destination peer information.
113  *
114  */
115  void stats_update_peer(const Socket *reference_id, const SocketAddress &peer);
116 
117  /** Update socket protocol.
118  * API used by socket (TCP or UDP) layers only, not to be used by application.
119  *
120  * @param reference_id ID to identify socket in data array.
121  * @param proto Parameter to update the protocol type of socket.
122  *
123  */
124  void stats_update_proto(const Socket *reference_id, nsapi_protocol_t proto);
125 
126  /** Update bytes sent on socket, which is cumulative count per socket.
127  * API used by socket (TCP or UDP) layers only, not to be used by application.
128  *
129  * @param reference_id ID to identify socket in data array.
130  * @param sent_bytes Parameter to append bytes sent over the socket.
131  *
132  */
133  void stats_update_sent_bytes(const Socket *reference_id, size_t sent_bytes);
134 
135  /** Update bytes received on socket, which is cumulative count per socket
136  * API used by socket (TCP or UDP) layers only, not to be used by application.
137  *
138  * @param reference_id ID to identify socket in data array.
139  * @param recv_bytes Parameter to append bytes the socket receives.
140  *
141  */
142  void stats_update_recv_bytes(const Socket *reference_id, size_t recv_bytes);
143 
144 #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
145 private:
146  static mbed_stats_socket_t _stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
147  static SingletonPtr<PlatformMutex> _mutex;
148  static uint32_t _size;
149 
150  /** Internal function to scan the array and get the position of the element in the list.
151  *
152  * @param reference_id ID to identify the socket in the data array.
153  *
154  */
155  int get_entry_position(const Socket *reference_id);
156 #endif
157 #endif
158 };
159 
160 #if !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
162 {
163  return 0;
164 }
165 
166 inline void SocketStats::stats_new_socket_entry(Socket *)
167 {
168 }
169 
170 inline void SocketStats::stats_update_socket_state(const Socket *, socket_state)
171 {
172 }
173 
174 inline void SocketStats::stats_update_peer(const Socket *, const SocketAddress &)
175 {
176 }
177 
178 inline void SocketStats::stats_update_proto(const Socket *, nsapi_protocol_t)
179 {
180 }
181 
182 inline void SocketStats::stats_update_sent_bytes(const Socket *, size_t)
183 {
184 }
185 
186 inline void SocketStats::stats_update_recv_bytes(const Socket *, size_t)
187 {
188 }
189 #endif // !MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
190 
191 #endif
nsapi_protocol_t proto
Specifies a protocol used with socket.
Definition: SocketStats.h:50
SocketAddress class.
size_t recv_bytes
Data received through this socket.
Definition: SocketStats.h:52
us_timestamp_t last_change_tick
osKernelGetTick() when state last changed
Definition: SocketStats.h:53
uint64_t us_timestamp_t
A us timestamp stored in a 64 bit integer.
Definition: ticker_api.h:39
SocketAddress peer
Last associated peername of this socket (Destination address)
Definition: SocketStats.h:48
Abstract Socket interface.
SocketAddress class.
Definition: SocketAddress.h:37
Socket interface.
Definition: Socket.h:40
static size_t mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
Fill the passed array of structures with the socket statistics for each created socket.
Definition: SocketStats.h:161
SocketStats class.
Definition: SocketStats.h:60
socket_state state
State of this socket.
Definition: SocketStats.h:49
size_t sent_bytes
Data sent through this socket.
Definition: SocketStats.h:51
Structure to parse socket statistics.
Definition: SocketStats.h:46
Socket * reference_id
Used for identifying socket.
Definition: SocketStats.h:47
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.