mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
mbed-os5 only for TYBLE16

Who changed what in which revision?

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