From 0b5dea5f99831a5888817821e69f9fec3f941ddf Mon Sep 17 00:00:00 2001 From: Balaji Date: Tue, 27 Aug 2019 13:40:25 -0700 Subject: [PATCH 01/12] Changes to the PR 10978 (LWIP: Add RAWIPSocket support) --- features/lwipstack/LWIPStack.cpp | 26 +++- features/lwipstack/lwipopts.h | 4 +- features/lwipstack/mbed_lib.json | 4 + features/netsocket/NetworkStack.h | 1 + features/netsocket/RAWIPSocket.cpp | 196 +++++++++++++++++++++++++++++ features/netsocket/RAWIPSocket.h | 179 ++++++++++++++++++++++++++ features/netsocket/UDPSocket.cpp | 163 ------------------------ features/netsocket/UDPSocket.h | 115 +---------------- features/netsocket/nsapi_types.h | 1 + 9 files changed, 408 insertions(+), 281 deletions(-) create mode 100644 features/netsocket/RAWIPSocket.cpp create mode 100644 features/netsocket/RAWIPSocket.h diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index 902fc8620fb..8f8ba7b5581 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -230,14 +230,34 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) return NSAPI_ERROR_NO_SOCKET; } - enum netconn_type lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP; + enum netconn_type netconntype; + if ( proto == NSAPI_TCP) + { + netconntype = NETCONN_TCP; + } + else if ( proto == NSAPI_UDP ) + { + netconntype = NETCONN_UDP; + } + else + { + netconntype = NETCONN_RAW; + } #if LWIP_IPV6 // Enable IPv6 (or dual-stack) - lwip_proto = (enum netconn_type)(lwip_proto | NETCONN_TYPE_IPV6); + netconntype = (enum netconn_type)(netconntype | NETCONN_TYPE_IPV6); #endif - s->conn = netconn_new_with_callback(lwip_proto, &LWIP::socket_callback); + if (proto == NSAPI_ICMP ) + { + s->conn = netconn_new_with_proto_and_callback(NETCONN_RAW, + (u8_t)IP_PROTO_ICMP, &LWIP::socket_callback); + } + else + { + s->conn = netconn_new_with_callback(netconntype, &LWIP::socket_callback); + } if (!s->conn) { arena_dealloc(s); diff --git a/features/lwipstack/lwipopts.h b/features/lwipstack/lwipopts.h index 4f720de3db2..e38aae6da79 100644 --- a/features/lwipstack/lwipopts.h +++ b/features/lwipstack/lwipopts.h @@ -81,9 +81,7 @@ #define SYS_LIGHTWEIGHT_PROT 1 -#ifndef LWIP_RAW -#define LWIP_RAW 0 -#endif +#define LWIP_RAW MBED_CONF_LWIP_RAWIPSOCKET_ENABLED #define MEMP_NUM_TCPIP_MSG_INPKT MBED_CONF_LWIP_MEMP_NUM_TCPIP_MSG_INPKT #define TCPIP_MBOX_SIZE MBED_CONF_LWIP_TCPIP_MBOX_SIZE diff --git a/features/lwipstack/mbed_lib.json b/features/lwipstack/mbed_lib.json index 0ac9c9f6b48..a5bab310728 100644 --- a/features/lwipstack/mbed_lib.json +++ b/features/lwipstack/mbed_lib.json @@ -149,6 +149,10 @@ "help": "Thread stack size for PPP (obsolete: use netsocket/ppp configuration instead)", "value": 768 }, + "rawipsocket-enabled": { + "help": "Enable ICMP RAW", + "value": false + }, "num-pbuf": { "help": "Number of non-pool pbufs, each needs 92 bytes of RAM, see LWIP's opt.h for more information. Current default is 8.", "value": 8 diff --git a/features/netsocket/NetworkStack.h b/features/netsocket/NetworkStack.h index a9cc6e5bc4a..2e5358b1843 100644 --- a/features/netsocket/NetworkStack.h +++ b/features/netsocket/NetworkStack.h @@ -183,6 +183,7 @@ class NetworkStack: public DNS { protected: friend class InternetSocket; + friend class RAWIPSocket; friend class UDPSocket; friend class TCPSocket; friend class TCPServer; diff --git a/features/netsocket/RAWIPSocket.cpp b/features/netsocket/RAWIPSocket.cpp new file mode 100644 index 00000000000..15935ca936b --- /dev/null +++ b/features/netsocket/RAWIPSocket.cpp @@ -0,0 +1,196 @@ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "RAWIPSocket.h" +#include "Timer.h" +#include "mbed_assert.h" + +RAWIPSocket::RAWIPSocket() +{ + _socket_stats.stats_update_proto(this, NSAPI_ICMP); +} + +RAWIPSocket::~RAWIPSocket() +{ +} + +nsapi_protocol_t RAWIPSocket::get_proto() +{ + return NSAPI_ICMP; +} + +nsapi_error_t RAWIPSocket::connect(const SocketAddress &address) +{ + _remote_peer = address; + _socket_stats.stats_update_peer(this, _remote_peer); + _socket_stats.stats_update_socket_state(this, SOCK_CONNECTED); + return NSAPI_ERROR_OK; +} + +nsapi_size_or_error_t RAWIPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) +{ + SocketAddress address; + nsapi_size_or_error_t err; + + if (!strcmp(_interface_name, "")) { + err = _stack->gethostbyname(host, &address); + } else { + err = _stack->gethostbyname(host, &address, NSAPI_UNSPEC, _interface_name); + } + + if (err) { + return NSAPI_ERROR_DNS_FAILURE; + } + + address.set_port(port); + + // sendto is thread safe + return sendto(address, data, size); +} + +nsapi_size_or_error_t RAWIPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) +{ + _lock.lock(); + nsapi_size_or_error_t ret; + + _writers++; + if (_socket) { + _socket_stats.stats_update_socket_state(this, SOCK_OPEN); + _socket_stats.stats_update_peer(this, address); + } + while (true) { + if (!_socket) { + ret = NSAPI_ERROR_NO_SOCKET; + break; + } + + core_util_atomic_flag_clear(&_pending); + nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size); + if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { + _socket_stats.stats_update_sent_bytes(this, sent); + ret = sent; + break; + } else { + uint32_t flag; + + // Release lock before blocking so other threads + // accessing this object aren't blocked + _lock.unlock(); + flag = _event_flag.wait_any(WRITE_FLAG, _timeout); + _lock.lock(); + + if (flag & osFlagsError) { + // Timeout break + ret = NSAPI_ERROR_WOULD_BLOCK; + break; + } + } + } + + _writers--; + if (!_socket || !_writers) { + _event_flag.set(FINISHED_FLAG); + } + _lock.unlock(); + return ret; +} + +nsapi_size_or_error_t RAWIPSocket::send(const void *data, nsapi_size_t size) +{ + if (!_remote_peer) { + return NSAPI_ERROR_NO_ADDRESS; + } + return sendto(_remote_peer, data, size); +} + +nsapi_size_or_error_t RAWIPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) +{ + _lock.lock(); + nsapi_size_or_error_t ret; + SocketAddress ignored; + + if (!address) { + address = &ignored; + } + + _readers++; + + if (_socket) { + _socket_stats.stats_update_socket_state(this, SOCK_OPEN); + } + while (true) { + if (!_socket) { + ret = NSAPI_ERROR_NO_SOCKET; + break; + } + + core_util_atomic_flag_clear(&_pending); + nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size); + + // Filter incomming packets using connected peer address + if (recv >= 0 && _remote_peer && _remote_peer != *address) { + continue; + } + + _socket_stats.stats_update_peer(this, _remote_peer); + // Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK + if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { + ret = recv; + _socket_stats.stats_update_recv_bytes(this, recv); + break; + } else { + uint32_t flag; + + // Release lock before blocking so other threads + // accessing this object aren't blocked + _lock.unlock(); + flag = _event_flag.wait_any(READ_FLAG, _timeout); + _lock.lock(); + + if (flag & osFlagsError) { + // Timeout break + ret = NSAPI_ERROR_WOULD_BLOCK; + break; + } + } + } + + _readers--; + if (!_socket || !_readers) { + _event_flag.set(FINISHED_FLAG); + } + + _lock.unlock(); + return ret; +} + +nsapi_size_or_error_t RAWIPSocket::recv(void *buffer, nsapi_size_t size) +{ + return recvfrom(NULL, buffer, size); +} + +Socket *RAWIPSocket::accept(nsapi_error_t *error) +{ + if (error) { + *error = NSAPI_ERROR_UNSUPPORTED; + } + return NULL; +} + +nsapi_error_t RAWIPSocket::listen(int) +{ + return NSAPI_ERROR_UNSUPPORTED; +} diff --git a/features/netsocket/RAWIPSocket.h b/features/netsocket/RAWIPSocket.h new file mode 100644 index 00000000000..788774bb151 --- /dev/null +++ b/features/netsocket/RAWIPSocket.h @@ -0,0 +1,179 @@ +/** \addtogroup netsocket */ +/** @{*/ +/* RAWIPSocket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RAWIPSOCKET_H +#define RAWIPSOCKET_H + +#include "netsocket/InternetSocket.h" +#include "netsocket/NetworkStack.h" +#include "netsocket/NetworkInterface.h" +#include "rtos/EventFlags.h" + + +/** RAW socket implementation. + */ +class RAWIPSocket : public InternetSocket { +public: + /** Create an uninitialized socket. + * + * @note Must call open to initialize the socket on a network stack. + */ + RAWIPSocket(); + + /** Create and open a socket on the network stack of the given + * network interface. + * + * @tparam S Type of the Network stack. + * @param stack Network stack as target for socket. + * @deprecated since mbed-os-5.11 + */ + template + MBED_DEPRECATED_SINCE("mbed-os-5.11", + "The RAWSocket(S *stack) constructor is deprecated" + "It discards the open() call return value." + "Use another constructor and call open() explicitly, instead.") + RAWIPSocket(S *stack) + { + open(stack); + } + + /** Destroy a socket. + * + * @note Closes socket if the socket is still open. + */ + virtual ~RAWIPSocket(); + + /** Send data to the specified host and port. + * + * By default, sendto blocks until data is sent. If socket is set to + * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned + * immediately. + * + * @param host Domain name of the remote host or a dotted notation IP address. + * @param port Port of the remote host. + * @param data Buffer of data to send to the host. + * @param size Size of the buffer in bytes. + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port, + const void *data, nsapi_size_t size); + + /** Send data to the specified address. + * + * By default, sendto blocks until data is sent. If socket is set to + * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned + * immediately. + * + * @param address The SocketAddress of the remote host. + * @param data Buffer of data to send to the host. + * @param size Size of the buffer in bytes. + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t sendto(const SocketAddress &address, + const void *data, nsapi_size_t size); + + /** Receive a datagram and store the source address in address if it's not NULL. + * + * By default, recvfrom blocks until a datagram is received. If socket is set to + * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK + * is returned. + * + * @note If the datagram is larger than the buffer, the excess data is silently discarded. + * + * @note If socket is connected, only packets coming from connected peer address + * are accepted. + * + * @note recvfrom() is allowed write to address and data buffers even if error occurs. + * + * @param address Destination for the source address or NULL. + * @param data Destination buffer for RAW data to be received from the host. + * @param size Size of the buffer in bytes. + * @return Number of received bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, + void *data, nsapi_size_t size); + + /** Set the remote address for next send() call and filtering + * of incoming packets. To reset the address, zero initialized + * SocketAddress must be in the address parameter. + * + * @param address The SocketAddress of the remote host. + * @return 0 on success, negative error code on failure. + */ + virtual nsapi_error_t connect(const SocketAddress &address); + + /** Send a raw data to connected remote address. + * + * By default, send blocks until all data is sent. If socket is set to + * nonblocking or times out, a partial amount can be written. + * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. + * + * @note The socket must be connected to a remote host before send() call. + * + * @param data Buffer of data to send to the host. + * @param size Size of the buffer in bytes. + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); + + /** Receive data from a socket. + * + * This is equivalent to calling recvfrom(NULL, data, size). + * + * By default, recv blocks until some data is received. If socket is set to + * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to + * indicate no data. + * + * @note recv() is allowed write to data buffer even if error occurs. + * + * @param data Pointer to buffer for data received from the host. + * @param size Size of the buffer in bytes. + * @return Number of received bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); + + /** Not implemented for RAWIPSocket. + * + * @param error Not used. + * @return NSAPI_ERROR_UNSUPPORTED + */ + virtual Socket *accept(nsapi_error_t *error = NULL); + + /** Not implemented for RAWIPSocket. + * + * @param backlog Not used. + * @return NSAPI_ERROR_UNSUPPORTED + */ + virtual nsapi_error_t listen(int backlog = 1); +#if !defined(DOXYGEN_ONLY) + +protected: + virtual nsapi_protocol_t get_proto(); + +#endif //!defined(DOXYGEN_ONLY) +}; + + +#endif + +/** @}*/ diff --git a/features/netsocket/UDPSocket.cpp b/features/netsocket/UDPSocket.cpp index d5041144dc9..6f37685567b 100644 --- a/features/netsocket/UDPSocket.cpp +++ b/features/netsocket/UDPSocket.cpp @@ -31,166 +31,3 @@ nsapi_protocol_t UDPSocket::get_proto() { return NSAPI_UDP; } - -nsapi_error_t UDPSocket::connect(const SocketAddress &address) -{ - _remote_peer = address; - _socket_stats.stats_update_peer(this, _remote_peer); - _socket_stats.stats_update_socket_state(this, SOCK_CONNECTED); - return NSAPI_ERROR_OK; -} - -nsapi_size_or_error_t UDPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) -{ - SocketAddress address; - nsapi_size_or_error_t err; - - if (!strcmp(_interface_name, "")) { - err = _stack->gethostbyname(host, &address); - } else { - err = _stack->gethostbyname(host, &address, NSAPI_UNSPEC, _interface_name); - } - - if (err) { - return NSAPI_ERROR_DNS_FAILURE; - } - - address.set_port(port); - - // sendto is thread safe - return sendto(address, data, size); -} - -nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) -{ - _lock.lock(); - nsapi_size_or_error_t ret; - - _writers++; - if (_socket) { - _socket_stats.stats_update_socket_state(this, SOCK_OPEN); - _socket_stats.stats_update_peer(this, address); - } - while (true) { - if (!_socket) { - ret = NSAPI_ERROR_NO_SOCKET; - break; - } - - core_util_atomic_flag_clear(&_pending); - nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size); - if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { - _socket_stats.stats_update_sent_bytes(this, sent); - ret = sent; - break; - } else { - uint32_t flag; - - // Release lock before blocking so other threads - // accessing this object aren't blocked - _lock.unlock(); - flag = _event_flag.wait_any(WRITE_FLAG, _timeout); - _lock.lock(); - - if (flag & osFlagsError) { - // Timeout break - ret = NSAPI_ERROR_WOULD_BLOCK; - break; - } - } - } - - _writers--; - if (!_socket || !_writers) { - _event_flag.set(FINISHED_FLAG); - } - _lock.unlock(); - return ret; -} - -nsapi_size_or_error_t UDPSocket::send(const void *data, nsapi_size_t size) -{ - if (!_remote_peer) { - return NSAPI_ERROR_NO_ADDRESS; - } - return sendto(_remote_peer, data, size); -} - -nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) -{ - _lock.lock(); - nsapi_size_or_error_t ret; - SocketAddress ignored; - - if (!address) { - address = &ignored; - } - - _readers++; - - if (_socket) { - _socket_stats.stats_update_socket_state(this, SOCK_OPEN); - } - while (true) { - if (!_socket) { - ret = NSAPI_ERROR_NO_SOCKET; - break; - } - - core_util_atomic_flag_clear(&_pending); - nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size); - - // Filter incomming packets using connected peer address - if (recv >= 0 && _remote_peer && _remote_peer != *address) { - continue; - } - - _socket_stats.stats_update_peer(this, _remote_peer); - // Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK - if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { - ret = recv; - _socket_stats.stats_update_recv_bytes(this, recv); - break; - } else { - uint32_t flag; - - // Release lock before blocking so other threads - // accessing this object aren't blocked - _lock.unlock(); - flag = _event_flag.wait_any(READ_FLAG, _timeout); - _lock.lock(); - - if (flag & osFlagsError) { - // Timeout break - ret = NSAPI_ERROR_WOULD_BLOCK; - break; - } - } - } - - _readers--; - if (!_socket || !_readers) { - _event_flag.set(FINISHED_FLAG); - } - - _lock.unlock(); - return ret; -} - -nsapi_size_or_error_t UDPSocket::recv(void *buffer, nsapi_size_t size) -{ - return recvfrom(NULL, buffer, size); -} - -Socket *UDPSocket::accept(nsapi_error_t *error) -{ - if (error) { - *error = NSAPI_ERROR_UNSUPPORTED; - } - return NULL; -} - -nsapi_error_t UDPSocket::listen(int) -{ - return NSAPI_ERROR_UNSUPPORTED; -} diff --git a/features/netsocket/UDPSocket.h b/features/netsocket/UDPSocket.h index 44025389d22..59ef0021bd8 100644 --- a/features/netsocket/UDPSocket.h +++ b/features/netsocket/UDPSocket.h @@ -24,11 +24,12 @@ #include "netsocket/NetworkStack.h" #include "netsocket/NetworkInterface.h" #include "rtos/EventFlags.h" +#include "RAWIPSocket.h" /** UDP socket implementation. */ -class UDPSocket : public InternetSocket { +class UDPSocket : public RAWIPSocket { public: /** Create an uninitialized socket. * @@ -59,123 +60,13 @@ class UDPSocket : public InternetSocket { */ virtual ~UDPSocket(); - /** Send data to the specified host and port. - * - * By default, sendto blocks until data is sent. If socket is set to - * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned - * immediately. - * - * @param host Domain name of the remote host or a dotted notation IP address. - * @param port Port of the remote host. - * @param data Buffer of data to send to the host. - * @param size Size of the buffer in bytes. - * @return Number of sent bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port, - const void *data, nsapi_size_t size); - - /** Send data to the specified address. - * - * By default, sendto blocks until data is sent. If socket is set to - * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned - * immediately. - * - * @param address The SocketAddress of the remote host. - * @param data Buffer of data to send to the host. - * @param size Size of the buffer in bytes. - * @return Number of sent bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t sendto(const SocketAddress &address, - const void *data, nsapi_size_t size); - - /** Receive a datagram and store the source address in address if it's not NULL. - * - * By default, recvfrom blocks until a datagram is received. If socket is set to - * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK - * is returned. - * - * @note If the datagram is larger than the buffer, the excess data is silently discarded. - * - * @note If socket is connected, only packets coming from connected peer address - * are accepted. - * - * @note recvfrom() is allowed write to address and data buffers even if error occurs. - * - * @param address Destination for the source address or NULL. - * @param data Destination buffer for datagram received from the host. - * @param size Size of the buffer in bytes. - * @return Number of received bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, - void *data, nsapi_size_t size); - - /** Set the remote address for next send() call and filtering - * of incoming packets. To reset the address, zero initialized - * SocketAddress must be in the address parameter. - * - * @param address The SocketAddress of the remote host. - * @return 0 on success, negative error code on failure. - */ - virtual nsapi_error_t connect(const SocketAddress &address); - - /** Send a datagram to connected remote address. - * - * By default, send blocks until all data is sent. If socket is set to - * nonblocking or times out, a partial amount can be written. - * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. - * - * @note The socket must be connected to a remote host before send() call. - * - * @param data Buffer of data to send to the host. - * @param size Size of the buffer in bytes. - * @return Number of sent bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); - - /** Receive data from a socket. - * - * This is equivalent to calling recvfrom(NULL, data, size). - * - * If the socket is connected, only packets coming from a connected peer address - * are accepted. - * - * By default, recv blocks until some data is received. If socket is set to - * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to - * indicate no data. - * - * @note recv() is allowed write to data buffer even if error occurs. - * - * @param data Pointer to buffer for data received from the host. - * @param size Size of the buffer in bytes. - * @return Number of received bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); - - /** Not implemented for UDP. - * - * @param error Not used. - * @return NSAPI_ERROR_UNSUPPORTED - */ - virtual Socket *accept(nsapi_error_t *error = NULL); - - /** Not implemented for UDP. - * - * @param backlog Not used. - * @return NSAPI_ERROR_UNSUPPORTED - */ - virtual nsapi_error_t listen(int backlog = 1); - #if !defined(DOXYGEN_ONLY) protected: virtual nsapi_protocol_t get_proto(); #endif //!defined(DOXYGEN_ONLY) + }; diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index c640aa7dcd4..ee2657d3dc4 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -215,6 +215,7 @@ typedef void *nsapi_socket_t; typedef enum nsapi_protocol { NSAPI_TCP, /*!< Socket is of TCP type */ NSAPI_UDP, /*!< Socket is of UDP type */ + NSAPI_ICMP, /*!< Socket is of ICMP type */ } nsapi_protocol_t; /** Enum of standardized stack option levels From d92a946b78947866e63074de01199ac1e7935032 Mon Sep 17 00:00:00 2001 From: Balaji Date: Tue, 27 Aug 2019 14:54:42 -0700 Subject: [PATCH 02/12] fix astyle convention --- features/lwipstack/LWIPStack.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index 8f8ba7b5581..8529f743282 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -231,16 +231,13 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) } enum netconn_type netconntype; - if ( proto == NSAPI_TCP) - { + if ( proto == NSAPI_TCP) { netconntype = NETCONN_TCP; } - else if ( proto == NSAPI_UDP ) - { + else if ( proto == NSAPI_UDP ) { netconntype = NETCONN_UDP; } - else - { + else { netconntype = NETCONN_RAW; } @@ -249,13 +246,11 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) netconntype = (enum netconn_type)(netconntype | NETCONN_TYPE_IPV6); #endif - if (proto == NSAPI_ICMP ) - { + if (proto == NSAPI_ICMP ) { s->conn = netconn_new_with_proto_and_callback(NETCONN_RAW, (u8_t)IP_PROTO_ICMP, &LWIP::socket_callback); } - else - { + else { s->conn = netconn_new_with_callback(netconntype, &LWIP::socket_callback); } From 1a7288d220f60fa0bc0bcfcf4102e92f5cd1e4b8 Mon Sep 17 00:00:00 2001 From: Balaji Date: Tue, 27 Aug 2019 17:22:16 -0700 Subject: [PATCH 03/12] fix astyle convention --- features/lwipstack/LWIPStack.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index 8529f743282..dc24729eeb0 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -233,11 +233,9 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) enum netconn_type netconntype; if ( proto == NSAPI_TCP) { netconntype = NETCONN_TCP; - } - else if ( proto == NSAPI_UDP ) { + } else if ( proto == NSAPI_UDP ) { netconntype = NETCONN_UDP; - } - else { + } else { netconntype = NETCONN_RAW; } @@ -249,8 +247,7 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) if (proto == NSAPI_ICMP ) { s->conn = netconn_new_with_proto_and_callback(NETCONN_RAW, (u8_t)IP_PROTO_ICMP, &LWIP::socket_callback); - } - else { + } else { s->conn = netconn_new_with_callback(netconntype, &LWIP::socket_callback); } From b91836a94d1a0dba88a39239fa4f8ab694e1a296 Mon Sep 17 00:00:00 2001 From: Balaji Date: Tue, 27 Aug 2019 17:30:36 -0700 Subject: [PATCH 04/12] fix astyle convention --- features/lwipstack/LWIPStack.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index dc24729eeb0..bb32248f733 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -231,12 +231,12 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) } enum netconn_type netconntype; - if ( proto == NSAPI_TCP) { - netconntype = NETCONN_TCP; - } else if ( proto == NSAPI_UDP ) { - netconntype = NETCONN_UDP; + if (proto == NSAPI_TCP) { + netconntype = NETCONN_TCP; + } else if (proto == NSAPI_UDP) { + netconntype = NETCONN_UDP; } else { - netconntype = NETCONN_RAW; + netconntype = NETCONN_RAW; } #if LWIP_IPV6 @@ -244,9 +244,9 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) netconntype = (enum netconn_type)(netconntype | NETCONN_TYPE_IPV6); #endif - if (proto == NSAPI_ICMP ) { + if (proto == NSAPI_ICMP) { s->conn = netconn_new_with_proto_and_callback(NETCONN_RAW, - (u8_t)IP_PROTO_ICMP, &LWIP::socket_callback); + (u8_t)IP_PROTO_ICMP, &LWIP::socket_callback); } else { s->conn = netconn_new_with_callback(netconntype, &LWIP::socket_callback); } From 90e188b23ad5aa8e00216603d949bfaa561e99d2 Mon Sep 17 00:00:00 2001 From: Balaji Date: Tue, 27 Aug 2019 17:35:29 -0700 Subject: [PATCH 05/12] fix a style convention --- features/netsocket/RAWIPSocket.h | 6 +++--- features/netsocket/nsapi_types.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/features/netsocket/RAWIPSocket.h b/features/netsocket/RAWIPSocket.h index 788774bb151..d5ad95a28b0 100644 --- a/features/netsocket/RAWIPSocket.h +++ b/features/netsocket/RAWIPSocket.h @@ -33,7 +33,7 @@ class RAWIPSocket : public InternetSocket { * * @note Must call open to initialize the socket on a network stack. */ - RAWIPSocket(); + RAWIPSocket(); /** Create and open a socket on the network stack of the given * network interface. @@ -150,8 +150,8 @@ class RAWIPSocket : public InternetSocket { * @return Number of received bytes on success, negative error * code on failure. */ - virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); - + virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); + /** Not implemented for RAWIPSocket. * * @param error Not used. diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index ee2657d3dc4..fa0ef691da6 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -215,7 +215,7 @@ typedef void *nsapi_socket_t; typedef enum nsapi_protocol { NSAPI_TCP, /*!< Socket is of TCP type */ NSAPI_UDP, /*!< Socket is of UDP type */ - NSAPI_ICMP, /*!< Socket is of ICMP type */ + NSAPI_ICMP, /*!< Socket is of ICMP type */ } nsapi_protocol_t; /** Enum of standardized stack option levels From ab883350a0600b249156fb67e4c0e8c98c2d16d4 Mon Sep 17 00:00:00 2001 From: Balaji Date: Wed, 28 Aug 2019 12:28:55 -0700 Subject: [PATCH 06/12] Incorporate review comments from @kjbracey-arm --- features/lwipstack/LWIPStack.cpp | 4 ++- .../{RAWIPSocket.cpp => ICMPSocket.cpp} | 24 +++++++------- .../netsocket/{RAWIPSocket.h => ICMPSocket.h} | 33 +++++-------------- features/netsocket/NetworkStack.h | 2 +- features/netsocket/UDPSocket.h | 4 +-- features/netsocket/nsapi_types.h | 1 + 6 files changed, 27 insertions(+), 41 deletions(-) rename features/netsocket/{RAWIPSocket.cpp => ICMPSocket.cpp} (85%) rename features/netsocket/{RAWIPSocket.h => ICMPSocket.h} (87%) diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index bb32248f733..36dea27dfd2 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -235,8 +235,10 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) netconntype = NETCONN_TCP; } else if (proto == NSAPI_UDP) { netconntype = NETCONN_UDP; - } else { + } else if (proto == NSAPI_ICMP) { netconntype = NETCONN_RAW; + } else { + return NSAPI_ERROR_PROTO_UNKNOWN; } #if LWIP_IPV6 diff --git a/features/netsocket/RAWIPSocket.cpp b/features/netsocket/ICMPSocket.cpp similarity index 85% rename from features/netsocket/RAWIPSocket.cpp rename to features/netsocket/ICMPSocket.cpp index 15935ca936b..8595f8dc345 100644 --- a/features/netsocket/RAWIPSocket.cpp +++ b/features/netsocket/ICMPSocket.cpp @@ -14,25 +14,25 @@ * limitations under the License. */ -#include "RAWIPSocket.h" +#include "ICMPSocket.h" #include "Timer.h" #include "mbed_assert.h" -RAWIPSocket::RAWIPSocket() +ICMPSocket::ICMPSocket() { _socket_stats.stats_update_proto(this, NSAPI_ICMP); } -RAWIPSocket::~RAWIPSocket() +ICMPSocket::~ICMPSocket() { } -nsapi_protocol_t RAWIPSocket::get_proto() +nsapi_protocol_t ICMPSocket::get_proto() { return NSAPI_ICMP; } -nsapi_error_t RAWIPSocket::connect(const SocketAddress &address) +nsapi_error_t ICMPSocket::connect(const SocketAddress &address) { _remote_peer = address; _socket_stats.stats_update_peer(this, _remote_peer); @@ -40,7 +40,7 @@ nsapi_error_t RAWIPSocket::connect(const SocketAddress &address) return NSAPI_ERROR_OK; } -nsapi_size_or_error_t RAWIPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) +nsapi_size_or_error_t ICMPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) { SocketAddress address; nsapi_size_or_error_t err; @@ -61,7 +61,7 @@ nsapi_size_or_error_t RAWIPSocket::sendto(const char *host, uint16_t port, const return sendto(address, data, size); } -nsapi_size_or_error_t RAWIPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) +nsapi_size_or_error_t ICMPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) { _lock.lock(); nsapi_size_or_error_t ret; @@ -108,7 +108,7 @@ nsapi_size_or_error_t RAWIPSocket::sendto(const SocketAddress &address, const vo return ret; } -nsapi_size_or_error_t RAWIPSocket::send(const void *data, nsapi_size_t size) +nsapi_size_or_error_t ICMPSocket::send(const void *data, nsapi_size_t size) { if (!_remote_peer) { return NSAPI_ERROR_NO_ADDRESS; @@ -116,7 +116,7 @@ nsapi_size_or_error_t RAWIPSocket::send(const void *data, nsapi_size_t size) return sendto(_remote_peer, data, size); } -nsapi_size_or_error_t RAWIPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) +nsapi_size_or_error_t ICMPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) { _lock.lock(); nsapi_size_or_error_t ret; @@ -177,12 +177,12 @@ nsapi_size_or_error_t RAWIPSocket::recvfrom(SocketAddress *address, void *buffer return ret; } -nsapi_size_or_error_t RAWIPSocket::recv(void *buffer, nsapi_size_t size) +nsapi_size_or_error_t ICMPSocket::recv(void *buffer, nsapi_size_t size) { return recvfrom(NULL, buffer, size); } -Socket *RAWIPSocket::accept(nsapi_error_t *error) +Socket *ICMPSocket::accept(nsapi_error_t *error) { if (error) { *error = NSAPI_ERROR_UNSUPPORTED; @@ -190,7 +190,7 @@ Socket *RAWIPSocket::accept(nsapi_error_t *error) return NULL; } -nsapi_error_t RAWIPSocket::listen(int) +nsapi_error_t ICMPSocket::listen(int) { return NSAPI_ERROR_UNSUPPORTED; } diff --git a/features/netsocket/RAWIPSocket.h b/features/netsocket/ICMPSocket.h similarity index 87% rename from features/netsocket/RAWIPSocket.h rename to features/netsocket/ICMPSocket.h index d5ad95a28b0..5e887dd481f 100644 --- a/features/netsocket/RAWIPSocket.h +++ b/features/netsocket/ICMPSocket.h @@ -1,6 +1,6 @@ /** \addtogroup netsocket */ /** @{*/ -/* RAWIPSocket +/* ICMPSocket * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,8 +16,8 @@ * limitations under the License. */ -#ifndef RAWIPSOCKET_H -#define RAWIPSOCKET_H +#ifndef ICMPSOCKET_H +#define ICMPSOCKET_H #include "netsocket/InternetSocket.h" #include "netsocket/NetworkStack.h" @@ -27,36 +27,19 @@ /** RAW socket implementation. */ -class RAWIPSocket : public InternetSocket { +class ICMPSocket : public InternetSocket { public: /** Create an uninitialized socket. * * @note Must call open to initialize the socket on a network stack. */ - RAWIPSocket(); - - /** Create and open a socket on the network stack of the given - * network interface. - * - * @tparam S Type of the Network stack. - * @param stack Network stack as target for socket. - * @deprecated since mbed-os-5.11 - */ - template - MBED_DEPRECATED_SINCE("mbed-os-5.11", - "The RAWSocket(S *stack) constructor is deprecated" - "It discards the open() call return value." - "Use another constructor and call open() explicitly, instead.") - RAWIPSocket(S *stack) - { - open(stack); - } + ICMPSocket(); /** Destroy a socket. * * @note Closes socket if the socket is still open. */ - virtual ~RAWIPSocket(); + virtual ~ICMPSocket(); /** Send data to the specified host and port. * @@ -152,14 +135,14 @@ class RAWIPSocket : public InternetSocket { */ virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); - /** Not implemented for RAWIPSocket. + /** Not implemented for ICMPSocket. * * @param error Not used. * @return NSAPI_ERROR_UNSUPPORTED */ virtual Socket *accept(nsapi_error_t *error = NULL); - /** Not implemented for RAWIPSocket. + /** Not implemented for ICMPSocket. * * @param backlog Not used. * @return NSAPI_ERROR_UNSUPPORTED diff --git a/features/netsocket/NetworkStack.h b/features/netsocket/NetworkStack.h index 2e5358b1843..4feb3a03ec6 100644 --- a/features/netsocket/NetworkStack.h +++ b/features/netsocket/NetworkStack.h @@ -183,7 +183,7 @@ class NetworkStack: public DNS { protected: friend class InternetSocket; - friend class RAWIPSocket; + friend class ICMPSocket; friend class UDPSocket; friend class TCPSocket; friend class TCPServer; diff --git a/features/netsocket/UDPSocket.h b/features/netsocket/UDPSocket.h index 59ef0021bd8..060970dfaef 100644 --- a/features/netsocket/UDPSocket.h +++ b/features/netsocket/UDPSocket.h @@ -24,12 +24,12 @@ #include "netsocket/NetworkStack.h" #include "netsocket/NetworkInterface.h" #include "rtos/EventFlags.h" -#include "RAWIPSocket.h" +#include "ICMPSocket.h" /** UDP socket implementation. */ -class UDPSocket : public RAWIPSocket { +class UDPSocket : public ICMPSocket { public: /** Create an uninitialized socket. * diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index fa0ef691da6..005fd2df802 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -56,6 +56,7 @@ enum nsapi_error { NSAPI_ERROR_ADDRESS_IN_USE = -3018, /*!< Address already in use */ NSAPI_ERROR_TIMEOUT = -3019, /*!< operation timed out */ NSAPI_ERROR_BUSY = -3020, /*!< device is busy and cannot accept new operation */ + NSAPI_ERROR_PROTO_UNKNOWN = -3021, /*!< unknown protocol */ }; From 5168bbdcd0f86a5633047035d4243a55946ed49f Mon Sep 17 00:00:00 2001 From: Balaji Date: Wed, 28 Aug 2019 15:49:00 -0700 Subject: [PATCH 07/12] Add InternetDatagram Base Class --- features/netsocket/ICMPSocket.cpp | 163 -------------------- features/netsocket/ICMPSocket.h | 112 +------------- features/netsocket/InternetDatagram.cpp | 196 ++++++++++++++++++++++++ features/netsocket/InternetDatagram.h | 162 ++++++++++++++++++++ features/netsocket/NetworkStack.h | 1 + features/netsocket/UDPSocket.h | 3 +- features/netsocket/nsapi_types.h | 1 + 7 files changed, 365 insertions(+), 273 deletions(-) create mode 100644 features/netsocket/InternetDatagram.cpp create mode 100644 features/netsocket/InternetDatagram.h diff --git a/features/netsocket/ICMPSocket.cpp b/features/netsocket/ICMPSocket.cpp index 8595f8dc345..32e69cfb3e1 100644 --- a/features/netsocket/ICMPSocket.cpp +++ b/features/netsocket/ICMPSocket.cpp @@ -31,166 +31,3 @@ nsapi_protocol_t ICMPSocket::get_proto() { return NSAPI_ICMP; } - -nsapi_error_t ICMPSocket::connect(const SocketAddress &address) -{ - _remote_peer = address; - _socket_stats.stats_update_peer(this, _remote_peer); - _socket_stats.stats_update_socket_state(this, SOCK_CONNECTED); - return NSAPI_ERROR_OK; -} - -nsapi_size_or_error_t ICMPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) -{ - SocketAddress address; - nsapi_size_or_error_t err; - - if (!strcmp(_interface_name, "")) { - err = _stack->gethostbyname(host, &address); - } else { - err = _stack->gethostbyname(host, &address, NSAPI_UNSPEC, _interface_name); - } - - if (err) { - return NSAPI_ERROR_DNS_FAILURE; - } - - address.set_port(port); - - // sendto is thread safe - return sendto(address, data, size); -} - -nsapi_size_or_error_t ICMPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) -{ - _lock.lock(); - nsapi_size_or_error_t ret; - - _writers++; - if (_socket) { - _socket_stats.stats_update_socket_state(this, SOCK_OPEN); - _socket_stats.stats_update_peer(this, address); - } - while (true) { - if (!_socket) { - ret = NSAPI_ERROR_NO_SOCKET; - break; - } - - core_util_atomic_flag_clear(&_pending); - nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size); - if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { - _socket_stats.stats_update_sent_bytes(this, sent); - ret = sent; - break; - } else { - uint32_t flag; - - // Release lock before blocking so other threads - // accessing this object aren't blocked - _lock.unlock(); - flag = _event_flag.wait_any(WRITE_FLAG, _timeout); - _lock.lock(); - - if (flag & osFlagsError) { - // Timeout break - ret = NSAPI_ERROR_WOULD_BLOCK; - break; - } - } - } - - _writers--; - if (!_socket || !_writers) { - _event_flag.set(FINISHED_FLAG); - } - _lock.unlock(); - return ret; -} - -nsapi_size_or_error_t ICMPSocket::send(const void *data, nsapi_size_t size) -{ - if (!_remote_peer) { - return NSAPI_ERROR_NO_ADDRESS; - } - return sendto(_remote_peer, data, size); -} - -nsapi_size_or_error_t ICMPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) -{ - _lock.lock(); - nsapi_size_or_error_t ret; - SocketAddress ignored; - - if (!address) { - address = &ignored; - } - - _readers++; - - if (_socket) { - _socket_stats.stats_update_socket_state(this, SOCK_OPEN); - } - while (true) { - if (!_socket) { - ret = NSAPI_ERROR_NO_SOCKET; - break; - } - - core_util_atomic_flag_clear(&_pending); - nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size); - - // Filter incomming packets using connected peer address - if (recv >= 0 && _remote_peer && _remote_peer != *address) { - continue; - } - - _socket_stats.stats_update_peer(this, _remote_peer); - // Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK - if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { - ret = recv; - _socket_stats.stats_update_recv_bytes(this, recv); - break; - } else { - uint32_t flag; - - // Release lock before blocking so other threads - // accessing this object aren't blocked - _lock.unlock(); - flag = _event_flag.wait_any(READ_FLAG, _timeout); - _lock.lock(); - - if (flag & osFlagsError) { - // Timeout break - ret = NSAPI_ERROR_WOULD_BLOCK; - break; - } - } - } - - _readers--; - if (!_socket || !_readers) { - _event_flag.set(FINISHED_FLAG); - } - - _lock.unlock(); - return ret; -} - -nsapi_size_or_error_t ICMPSocket::recv(void *buffer, nsapi_size_t size) -{ - return recvfrom(NULL, buffer, size); -} - -Socket *ICMPSocket::accept(nsapi_error_t *error) -{ - if (error) { - *error = NSAPI_ERROR_UNSUPPORTED; - } - return NULL; -} - -nsapi_error_t ICMPSocket::listen(int) -{ - return NSAPI_ERROR_UNSUPPORTED; -} diff --git a/features/netsocket/ICMPSocket.h b/features/netsocket/ICMPSocket.h index 5e887dd481f..26778daa067 100644 --- a/features/netsocket/ICMPSocket.h +++ b/features/netsocket/ICMPSocket.h @@ -20,14 +20,15 @@ #define ICMPSOCKET_H #include "netsocket/InternetSocket.h" +#include "netsocket/InternetDatagram.h" #include "netsocket/NetworkStack.h" #include "netsocket/NetworkInterface.h" #include "rtos/EventFlags.h" -/** RAW socket implementation. +/** ICMP socket implementation. */ -class ICMPSocket : public InternetSocket { +class ICMPSocket : public InternetDatagram { public: /** Create an uninitialized socket. * @@ -41,113 +42,6 @@ class ICMPSocket : public InternetSocket { */ virtual ~ICMPSocket(); - /** Send data to the specified host and port. - * - * By default, sendto blocks until data is sent. If socket is set to - * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned - * immediately. - * - * @param host Domain name of the remote host or a dotted notation IP address. - * @param port Port of the remote host. - * @param data Buffer of data to send to the host. - * @param size Size of the buffer in bytes. - * @return Number of sent bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port, - const void *data, nsapi_size_t size); - - /** Send data to the specified address. - * - * By default, sendto blocks until data is sent. If socket is set to - * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned - * immediately. - * - * @param address The SocketAddress of the remote host. - * @param data Buffer of data to send to the host. - * @param size Size of the buffer in bytes. - * @return Number of sent bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t sendto(const SocketAddress &address, - const void *data, nsapi_size_t size); - - /** Receive a datagram and store the source address in address if it's not NULL. - * - * By default, recvfrom blocks until a datagram is received. If socket is set to - * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK - * is returned. - * - * @note If the datagram is larger than the buffer, the excess data is silently discarded. - * - * @note If socket is connected, only packets coming from connected peer address - * are accepted. - * - * @note recvfrom() is allowed write to address and data buffers even if error occurs. - * - * @param address Destination for the source address or NULL. - * @param data Destination buffer for RAW data to be received from the host. - * @param size Size of the buffer in bytes. - * @return Number of received bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, - void *data, nsapi_size_t size); - - /** Set the remote address for next send() call and filtering - * of incoming packets. To reset the address, zero initialized - * SocketAddress must be in the address parameter. - * - * @param address The SocketAddress of the remote host. - * @return 0 on success, negative error code on failure. - */ - virtual nsapi_error_t connect(const SocketAddress &address); - - /** Send a raw data to connected remote address. - * - * By default, send blocks until all data is sent. If socket is set to - * nonblocking or times out, a partial amount can be written. - * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. - * - * @note The socket must be connected to a remote host before send() call. - * - * @param data Buffer of data to send to the host. - * @param size Size of the buffer in bytes. - * @return Number of sent bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); - - /** Receive data from a socket. - * - * This is equivalent to calling recvfrom(NULL, data, size). - * - * By default, recv blocks until some data is received. If socket is set to - * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to - * indicate no data. - * - * @note recv() is allowed write to data buffer even if error occurs. - * - * @param data Pointer to buffer for data received from the host. - * @param size Size of the buffer in bytes. - * @return Number of received bytes on success, negative error - * code on failure. - */ - virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); - - /** Not implemented for ICMPSocket. - * - * @param error Not used. - * @return NSAPI_ERROR_UNSUPPORTED - */ - virtual Socket *accept(nsapi_error_t *error = NULL); - - /** Not implemented for ICMPSocket. - * - * @param backlog Not used. - * @return NSAPI_ERROR_UNSUPPORTED - */ - virtual nsapi_error_t listen(int backlog = 1); #if !defined(DOXYGEN_ONLY) protected: diff --git a/features/netsocket/InternetDatagram.cpp b/features/netsocket/InternetDatagram.cpp new file mode 100644 index 00000000000..23131b739da --- /dev/null +++ b/features/netsocket/InternetDatagram.cpp @@ -0,0 +1,196 @@ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "InternetDatagram.h" +#include "Timer.h" +#include "mbed_assert.h" + +InternetDatagram::InternetDatagram() +{ + _socket_stats.stats_update_proto(this, NSAPI_ICMP); +} + +InternetDatagram::~InternetDatagram() +{ +} + +nsapi_protocol_t InternetDatagram::get_proto() +{ + return NSAPI_PROTO_UNKNOWN; +} + +nsapi_error_t InternetDatagram::connect(const SocketAddress &address) +{ + _remote_peer = address; + _socket_stats.stats_update_peer(this, _remote_peer); + _socket_stats.stats_update_socket_state(this, SOCK_CONNECTED); + return NSAPI_ERROR_OK; +} + +nsapi_size_or_error_t InternetDatagram::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) +{ + SocketAddress address; + nsapi_size_or_error_t err; + + if (!strcmp(_interface_name, "")) { + err = _stack->gethostbyname(host, &address); + } else { + err = _stack->gethostbyname(host, &address, NSAPI_UNSPEC, _interface_name); + } + + if (err) { + return NSAPI_ERROR_DNS_FAILURE; + } + + address.set_port(port); + + // sendto is thread safe + return sendto(address, data, size); +} + +nsapi_size_or_error_t InternetDatagram::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) +{ + _lock.lock(); + nsapi_size_or_error_t ret; + + _writers++; + if (_socket) { + _socket_stats.stats_update_socket_state(this, SOCK_OPEN); + _socket_stats.stats_update_peer(this, address); + } + while (true) { + if (!_socket) { + ret = NSAPI_ERROR_NO_SOCKET; + break; + } + + core_util_atomic_flag_clear(&_pending); + nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size); + if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { + _socket_stats.stats_update_sent_bytes(this, sent); + ret = sent; + break; + } else { + uint32_t flag; + + // Release lock before blocking so other threads + // accessing this object aren't blocked + _lock.unlock(); + flag = _event_flag.wait_any(WRITE_FLAG, _timeout); + _lock.lock(); + + if (flag & osFlagsError) { + // Timeout break + ret = NSAPI_ERROR_WOULD_BLOCK; + break; + } + } + } + + _writers--; + if (!_socket || !_writers) { + _event_flag.set(FINISHED_FLAG); + } + _lock.unlock(); + return ret; +} + +nsapi_size_or_error_t InternetDatagram::send(const void *data, nsapi_size_t size) +{ + if (!_remote_peer) { + return NSAPI_ERROR_NO_ADDRESS; + } + return sendto(_remote_peer, data, size); +} + +nsapi_size_or_error_t InternetDatagram::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) +{ + _lock.lock(); + nsapi_size_or_error_t ret; + SocketAddress ignored; + + if (!address) { + address = &ignored; + } + + _readers++; + + if (_socket) { + _socket_stats.stats_update_socket_state(this, SOCK_OPEN); + } + while (true) { + if (!_socket) { + ret = NSAPI_ERROR_NO_SOCKET; + break; + } + + core_util_atomic_flag_clear(&_pending); + nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size); + + // Filter incomming packets using connected peer address + if (recv >= 0 && _remote_peer && _remote_peer != *address) { + continue; + } + + _socket_stats.stats_update_peer(this, _remote_peer); + // Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK + if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { + ret = recv; + _socket_stats.stats_update_recv_bytes(this, recv); + break; + } else { + uint32_t flag; + + // Release lock before blocking so other threads + // accessing this object aren't blocked + _lock.unlock(); + flag = _event_flag.wait_any(READ_FLAG, _timeout); + _lock.lock(); + + if (flag & osFlagsError) { + // Timeout break + ret = NSAPI_ERROR_WOULD_BLOCK; + break; + } + } + } + + _readers--; + if (!_socket || !_readers) { + _event_flag.set(FINISHED_FLAG); + } + + _lock.unlock(); + return ret; +} + +nsapi_size_or_error_t InternetDatagram::recv(void *buffer, nsapi_size_t size) +{ + return recvfrom(NULL, buffer, size); +} + +Socket *InternetDatagram::accept(nsapi_error_t *error) +{ + if (error) { + *error = NSAPI_ERROR_UNSUPPORTED; + } + return NULL; +} + +nsapi_error_t InternetDatagram::listen(int) +{ + return NSAPI_ERROR_UNSUPPORTED; +} diff --git a/features/netsocket/InternetDatagram.h b/features/netsocket/InternetDatagram.h new file mode 100644 index 00000000000..17f79e655fc --- /dev/null +++ b/features/netsocket/InternetDatagram.h @@ -0,0 +1,162 @@ +/** \addtogroup netsocket */ +/** @{*/ +/* InternetDatagram + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef InternetDatagram_H +#define InternetDatagram_H + +#include "netsocket/InternetSocket.h" +#include "netsocket/NetworkStack.h" +#include "netsocket/NetworkInterface.h" +#include "rtos/EventFlags.h" + + +/** InternetDatagram socket implementation. + */ +class InternetDatagram : public InternetSocket { +public: + /** Create an uninitialized socket. + * + * @note Must call open to initialize the socket on a network stack. + */ + InternetDatagram(); + + /** Destroy a socket. + * + * @note Closes socket if the socket is still open. + */ + virtual ~InternetDatagram(); + + /** Send data to the specified host and port. + * + * By default, sendto blocks until data is sent. If socket is set to + * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned + * immediately. + * + * @param host Domain name of the remote host or a dotted notation IP address. + * @param port Port of the remote host. + * @param data Buffer of data to send to the host. + * @param size Size of the buffer in bytes. + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port, + const void *data, nsapi_size_t size); + + /** Send data to the specified address. + * + * By default, sendto blocks until data is sent. If socket is set to + * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned + * immediately. + * + * @param address The SocketAddress of the remote host. + * @param data Buffer of data to send to the host. + * @param size Size of the buffer in bytes. + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t sendto(const SocketAddress &address, + const void *data, nsapi_size_t size); + + /** Receive a datagram and store the source address in address if it's not NULL. + * + * By default, recvfrom blocks until a datagram is received. If socket is set to + * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK + * is returned. + * + * @note If the datagram is larger than the buffer, the excess data is silently discarded. + * + * @note If socket is connected, only packets coming from connected peer address + * are accepted. + * + * @note recvfrom() is allowed write to address and data buffers even if error occurs. + * + * @param address Destination for the source address or NULL. + * @param data Destination buffer for RAW data to be received from the host. + * @param size Size of the buffer in bytes. + * @return Number of received bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, + void *data, nsapi_size_t size); + + /** Set the remote address for next send() call and filtering + * of incoming packets. To reset the address, zero initialized + * SocketAddress must be in the address parameter. + * + * @param address The SocketAddress of the remote host. + * @return 0 on success, negative error code on failure. + */ + virtual nsapi_error_t connect(const SocketAddress &address); + + /** Send a raw data to connected remote address. + * + * By default, send blocks until all data is sent. If socket is set to + * nonblocking or times out, a partial amount can be written. + * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. + * + * @note The socket must be connected to a remote host before send() call. + * + * @param data Buffer of data to send to the host. + * @param size Size of the buffer in bytes. + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); + + /** Receive data from a socket. + * + * This is equivalent to calling recvfrom(NULL, data, size). + * + * By default, recv blocks until some data is received. If socket is set to + * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to + * indicate no data. + * + * @note recv() is allowed write to data buffer even if error occurs. + * + * @param data Pointer to buffer for data received from the host. + * @param size Size of the buffer in bytes. + * @return Number of received bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); + + /** Not implemented for InternetDatagram. + * + * @param error Not used. + * @return NSAPI_ERROR_UNSUPPORTED + */ + virtual Socket *accept(nsapi_error_t *error = NULL); + + /** Not implemented for InternetDatagram. + * + * @param backlog Not used. + * @return NSAPI_ERROR_UNSUPPORTED + */ + virtual nsapi_error_t listen(int backlog = 1); +#if !defined(DOXYGEN_ONLY) + +protected: + virtual nsapi_protocol_t get_proto(); + +#endif //!defined(DOXYGEN_ONLY) +}; + + +#endif + +/** @}*/ diff --git a/features/netsocket/NetworkStack.h b/features/netsocket/NetworkStack.h index 4feb3a03ec6..560da81e95f 100644 --- a/features/netsocket/NetworkStack.h +++ b/features/netsocket/NetworkStack.h @@ -183,6 +183,7 @@ class NetworkStack: public DNS { protected: friend class InternetSocket; + friend class InternetDatagram; friend class ICMPSocket; friend class UDPSocket; friend class TCPSocket; diff --git a/features/netsocket/UDPSocket.h b/features/netsocket/UDPSocket.h index 060970dfaef..783ca6a197b 100644 --- a/features/netsocket/UDPSocket.h +++ b/features/netsocket/UDPSocket.h @@ -23,13 +23,14 @@ #include "netsocket/InternetSocket.h" #include "netsocket/NetworkStack.h" #include "netsocket/NetworkInterface.h" +#include "netsocket/InternetDatagram.h" #include "rtos/EventFlags.h" #include "ICMPSocket.h" /** UDP socket implementation. */ -class UDPSocket : public ICMPSocket { +class UDPSocket : public InternetDatagram { public: /** Create an uninitialized socket. * diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index 005fd2df802..e2180abf1c1 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -217,6 +217,7 @@ typedef enum nsapi_protocol { NSAPI_TCP, /*!< Socket is of TCP type */ NSAPI_UDP, /*!< Socket is of UDP type */ NSAPI_ICMP, /*!< Socket is of ICMP type */ + NSAPI_PROTO_UNKNOWN, /*!< Socket Protocol type UKNOWN */ } nsapi_protocol_t; /** Enum of standardized stack option levels From 6905b1b5472c2e3f887e1398c63917206774c6b0 Mon Sep 17 00:00:00 2001 From: Balaji Date: Thu, 29 Aug 2019 11:34:20 -0700 Subject: [PATCH 08/12] Incorporate review comments from @kjbracey-arm --- features/lwipstack/LWIPStack.cpp | 2 +- features/lwipstack/lwipopts.h | 2 +- features/lwipstack/mbed_lib.json | 331 ++++++++++++------ features/netsocket/ICMPSocket.cpp | 4 - features/netsocket/ICMPSocket.h | 10 +- ...atagram.cpp => InternetDatagramSocket.cpp} | 32 +- ...netDatagram.h => InternetDatagramSocket.h} | 32 +- features/netsocket/NetworkStack.h | 4 +- features/netsocket/UDPSocket.cpp | 4 - features/netsocket/UDPSocket.h | 10 +- features/netsocket/nsapi_types.h | 2 - 11 files changed, 246 insertions(+), 187 deletions(-) rename features/netsocket/{InternetDatagram.cpp => InternetDatagramSocket.cpp} (82%) rename features/netsocket/{InternetDatagram.h => InternetDatagramSocket.h} (92%) diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index 36dea27dfd2..34345dc0c94 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -238,7 +238,7 @@ nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) } else if (proto == NSAPI_ICMP) { netconntype = NETCONN_RAW; } else { - return NSAPI_ERROR_PROTO_UNKNOWN; + return NSAPI_ERROR_UNSUPPORTED; } #if LWIP_IPV6 diff --git a/features/lwipstack/lwipopts.h b/features/lwipstack/lwipopts.h index e38aae6da79..0a866f391bb 100644 --- a/features/lwipstack/lwipopts.h +++ b/features/lwipstack/lwipopts.h @@ -81,7 +81,7 @@ #define SYS_LIGHTWEIGHT_PROT 1 -#define LWIP_RAW MBED_CONF_LWIP_RAWIPSOCKET_ENABLED +#define LWIP_RAW MBED_CONF_LWIP_RAW_SOCKET_ENABLED #define MEMP_NUM_TCPIP_MSG_INPKT MBED_CONF_LWIP_MEMP_NUM_TCPIP_MSG_INPKT #define TCPIP_MBOX_SIZE MBED_CONF_LWIP_TCPIP_MBOX_SIZE diff --git a/features/lwipstack/mbed_lib.json b/features/lwipstack/mbed_lib.json index a5bab310728..6c42dc113fd 100644 --- a/features/lwipstack/mbed_lib.json +++ b/features/lwipstack/mbed_lib.json @@ -1,83 +1,125 @@ { - "name": "lwip", - "config": { - "ipv4-enabled": { - "help": "Enable IPv4", - "value": true - }, - "ipv6-enabled": { - "help": "Enable IPv6", - "value": false - }, - "ip-ver-pref": { - "help": "On dual-stack system the preferred stack: 4 for IPv4 and 6 for IPv6", +"name": "lwip" + , +"config": + { +"ipv4-enabled": + { +"help": "Enable IPv4" + , +"value": + true + }, +"ipv6-enabled": + { +"help": "Enable IPv6" + , +"value": + false + }, +"ip-ver-pref": + { +"help": "On dual-stack system the preferred stack: 4 for IPv4 and 6 for IPv6" + , "value": 4 }, - "addr-timeout": { - "help": "On dual-stack system how long to additionally wait for other stack's address in seconds", +"addr-timeout": + { +"help": "On dual-stack system how long to additionally wait for other stack's address in seconds" + , "value": 5 }, - "addr-timeout-mode": { - "help": "Address timeout mode; true: wait both stack's addresses; false: wait for preferred stack's address", - "value": true - }, - "dhcp-timeout": { - "help": "DHCP timeout value", - "value": 60 - }, - "ethernet-enabled": { - "help": "Enable support for Ethernet interfaces", - "value": true - }, - "l3ip-enabled": { - "help": "Enable support for L3IP interfaces", - "value": false - }, - "debug-enabled": { - "help": "Enable debug trace support", - "value": false - }, - "ppp-enabled": { - "help": "Enable support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)", - "value": false - }, - "ppp-ipv4-enabled": { - "help": "Enable support for ipv4 PPP interface (obsolete: use netsocket/ppp configuration instead)", - "value": false - }, - "ppp-ipv6-enabled": { - "help": "Enable support for ipv6 PPP interface (obsolete: use netsocket/ppp configuration instead)", - "value": false - }, - "use-mbed-trace": { - "help": "Use mbed trace for debug, rather than printf", - "value": false - }, - "enable-ppp-trace": { - "help": "Enable trace support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)", - "value": false - }, - "socket-max": { - "help": "Maximum number of open TCPServer, TCPSocket and UDPSocket instances allowed, including one used internally for DNS. Each requires 236 bytes of pre-allocated RAM", +"addr-timeout-mode": + { +"help": "Address timeout mode; true: wait both stack's addresses; false: wait for preferred stack's address" + , +"value": + true + }, +"value": + true + }, +"l3ip-enabled": + { +"help": "Enable support for L3IP interfaces" + , +"value": + false + }, +"debug-enabled": + { +"help": "Enable debug trace support" + , +"value": + false + }, +"ppp-enabled": + { +"help": "Enable support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)" + , +"value": + false + }, +"ppp-ipv4-enabled": + { +"help": "Enable support for ipv4 PPP interface (obsolete: use netsocket/ppp configuration instead)" + , +"value": + false + }, +"ppp-ipv6-enabled": + { +"help": "Enable support for ipv6 PPP interface (obsolete: use netsocket/ppp configuration instead)" + , +"value": + false + }, +"use-mbed-trace": + { +"help": "Use mbed trace for debug, rather than printf" + , +"value": + false + }, +"enable-ppp-trace": + { +"help": "Enable trace support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)" + , +"value": + false + }, +"socket-max": + { +"help": "Maximum number of open TCPServer, TCPSocket and UDPSocket instances allowed, including one used internally for DNS. Each requires 236 bytes of pre-allocated RAM" + , "value": 4 }, - "tcp-enabled": { - "help": "Enable TCP", - "value": true - }, - "tcp-server-max": { - "help": "Maximum number of open TCPServer instances allowed. Each requires 72 bytes of pre-allocated RAM", +"tcp-enabled": + { +"help": "Enable TCP" + , +"value": + true + }, +"tcp-server-max": + { +"help": "Maximum number of open TCPServer instances allowed. Each requires 72 bytes of pre-allocated RAM" + , "value": 4 }, - "tcp-socket-max": { - "help": "Maximum number of open TCPSocket instances allowed. Each requires 196 bytes of pre-allocated RAM", +"tcp-socket-max": + { +"help": "Maximum number of open TCPSocket instances allowed. Each requires 196 bytes of pre-allocated RAM" + , "value": 4 }, - "udp-socket-max": { - "help": "Maximum number of open UDPSocket instances allowed, including one used internally for DNS. Each requires 84 bytes of pre-allocated RAM", +"udp-socket-max": + { +"help": "Maximum number of open UDPSocket instances allowed, including one used internally for DNS. Each requires 84 bytes of pre-allocated RAM" + , "value": 4 }, - "memp-num-tcp-seg": { +"memp-num-tcp-seg": "help": "Number of simultaneously queued TCP segments, see LWIP opt.h for more information. Current default is 16.", "value": 16 }, @@ -85,7 +127,7 @@ "help": "Number of simultaneously queued TCP messages that are received", "value": 8 }, - "tcp-mss": { +"tcp-mss": "help": "TCP Maximum segment size, see LWIP opt.h for more information. Current default is 536.", "value": 536 }, @@ -101,57 +143,87 @@ "help": "mailbox size", "value": 8 }, - "tcp-snd-buf": { +"tcp-snd-buf": "help": "TCP sender buffer space (bytes), see LWIP's opt.h for more information. Current default is (2 * TCP_MSS).", "value": "(2 * TCP_MSS)" + , +"value": + null }, - "tcp-wnd": { +"tcp-wnd": "help": "TCP sender buffer space (bytes), see LWIP's opt.h for more information. Current default is (4 * TCP_MSS).", "value": "(4 * TCP_MSS)" + , +"value": + null }, - "tcp-maxrtx": { +"tcp-maxrtx": "help": "Maximum number of retransmissions of data segments, see LWIP's opt.h for more information. Current default is 6.", +"help": "Maximum number of retransmissions of data segments." + , "value": 6 }, - "tcp-synmaxrtx": { +"tcp-synmaxrtx": "help": "Maximum number of retransmissions of SYN segments, see LWIP's opt.h for more information. Current default is 6.", "value": 6 - }, - "tcp-close-timeout": { - "help": "Maximum timeout (ms) for TCP close handshaking timeout", + , +"value": + null + }, +"tcp-close-timeout": + { +"help": "Maximum timeout (ms) for TCP close handshaking timeout" + , "value": 1000 }, - "tcpip-thread-priority": { - "help": "Priority of lwip TCPIP thread", - "value": "osPriorityNormal" +"tcpip-thread-priority": + { +"help": "Priority of lwip TCPIP thread" + , +"value": "osPriorityNormal" }, - "pbuf-pool-size": { +"pbuf-pool-size": "help": "Number of pbufs in pool - usually used for received packets, so this determines how much data can be buffered between reception and the application reading, see LWIP's opt.h for more information. If a driver uses PBUF_RAM for reception, less pool may be needed. Current default is 5.", "value": 5 + , +"value": + null }, - "pbuf-pool-bufsize": { +"pbuf-pool-bufsize": "help": "Size of pbufs in pool, see LWIP's opt.h for more information.", - "value": null +"help": "Size of pbufs in pool. If set to null, lwIP will base the size on the TCP MSS, which is 536 unless overridden by the target" + , +"value": + null }, - "mem-size": { +"mem-size": "help": "Size of heap (bytes) - used for outgoing packets, and also used by some drivers for reception, see LWIP's opt.h for more information. Current default is 1600.", "value": 1600 - }, - "tcpip-thread-stacksize": { - "help": "Stack size for lwip TCPIP thread", + , +"value": + null + }, +"tcpip-thread-stacksize": + { +"help": "Stack size for lwip TCPIP thread" + , "value": 1200 }, - "default-thread-stacksize": { - "help": "Stack size for lwip system threads", +"default-thread-stacksize": + { +"help": "Stack size for lwip system threads" + , "value": 512 }, - "ppp-thread-stacksize": { - "help": "Thread stack size for PPP (obsolete: use netsocket/ppp configuration instead)", +"ppp-thread-stacksize": + { +"help": "Thread stack size for PPP (obsolete: use netsocket/ppp configuration instead)" + , "value": 768 }, - "rawipsocket-enabled": { - "help": "Enable ICMP RAW", - "value": false +"raw-socket-enabled": + { +"help": "Enable RAW socket" }, "num-pbuf": { "help": "Number of non-pool pbufs, each needs 92 bytes of RAM, see LWIP's opt.h for more information. Current default is 8.", @@ -162,71 +234,102 @@ "value": 8 } }, - "target_overrides": { - "REALTEK_RTL8195AM": { +"target_overrides": + { +"REALTEK_RTL8195AM": + { "tcpip-thread-stacksize": 1600, "mem-size": 12800 }, - "UBLOX_EVK_ODIN_W2": { +"UBLOX_EVK_ODIN_W2": + { "pbuf-pool-size" : 10 }, - "STM": { +"STM": + { "mem-size": 2310 }, - "Freescale": { +"Freescale": + { "mem-size": 33270 }, - "LPC1768": { +"LPC1768": + { "mem-size": 16362 }, - "LPC4088": { +"LPC4088": + { "mem-size": 15360 }, - "LPC4088_DM": { +"LPC4088_DM": + { "mem-size": 15360 }, - "UBLOX_C027": { +"UBLOX_C027": + { "mem-size": 16362 }, - "ARCH_PRO": { +"ARCH_PRO": + { "mem-size": 16362 }, - "LPC546XX": { +"LPC546XX": + { "mem-size": 36496 }, - "EFM32GG11_STK3701": { +"EFM32GG11_STK3701": + { "mem-size": 36560 }, - "RZ_A1_EMAC": { +"RZ_A1_EMAC": + { "tcpip-thread-stacksize": 1328, "default-thread-stacksize": 640, "memp-num-tcp-seg": 32, "tcp-mss": 1440, - "tcp-snd-buf": "(8 * TCP_MSS)", - "tcp-wnd": "(TCP_MSS * 8)", +"tcp-snd-buf": "(8 * TCP_MSS)" + , +"tcp-wnd": "(TCP_MSS * 8)" + , "pbuf-pool-size": 16, "mem-size": 51200 }, "MCU_PSOC6": { + { +"tcp-wnd": "(TCP_MSS * 6)" + , + { "tcpip-thread-stacksize": 8192, "default-thread-stacksize": 640, "memp-num-tcp-seg": 24, "tcp-socket-max": 10, - "udp-socket-max":10, - "socket-max":18, + "udp-socket-max": 10, + "socket-max": 18, "tcp-mss": 1540, - "tcp-snd-buf": "(6 * TCP_MSS)", - "tcp-wnd": "(TCP_MSS * 6)", +"tcp-snd-buf": "(6 * TCP_MSS)" + , "pbuf-pool-size": 14, + , + { +"tcp-wnd": "(TCP_MSS * 6)" + , "mem-size": 65536 }, - "MIMXRT1050_EVK": { + { +"tcp-snd-buf": "(6 * TCP_MSS)" + , +"tcp-wnd": "(TCP_MSS * 6)" + , +"MIMXRT1050_EVK": + { "mem-size": 36560 }, - "FVP_MPS2_M3": { +"FVP_MPS2_M3": + { "mem-size": 36560 }, - "MTS_DRAGONFLY_F411RE": { +"MTS_DRAGONFLY_F411RE": + { "tcpip-thread-stacksize": 1600 } } diff --git a/features/netsocket/ICMPSocket.cpp b/features/netsocket/ICMPSocket.cpp index 32e69cfb3e1..6e564bea387 100644 --- a/features/netsocket/ICMPSocket.cpp +++ b/features/netsocket/ICMPSocket.cpp @@ -23,10 +23,6 @@ ICMPSocket::ICMPSocket() _socket_stats.stats_update_proto(this, NSAPI_ICMP); } -ICMPSocket::~ICMPSocket() -{ -} - nsapi_protocol_t ICMPSocket::get_proto() { return NSAPI_ICMP; diff --git a/features/netsocket/ICMPSocket.h b/features/netsocket/ICMPSocket.h index 26778daa067..6d3223d4079 100644 --- a/features/netsocket/ICMPSocket.h +++ b/features/netsocket/ICMPSocket.h @@ -20,7 +20,7 @@ #define ICMPSOCKET_H #include "netsocket/InternetSocket.h" -#include "netsocket/InternetDatagram.h" +#include "netsocket/InternetDatagramSocket.h" #include "netsocket/NetworkStack.h" #include "netsocket/NetworkInterface.h" #include "rtos/EventFlags.h" @@ -28,7 +28,7 @@ /** ICMP socket implementation. */ -class ICMPSocket : public InternetDatagram { +class ICMPSocket : public InternetDatagramSocket { public: /** Create an uninitialized socket. * @@ -36,12 +36,6 @@ class ICMPSocket : public InternetDatagram { */ ICMPSocket(); - /** Destroy a socket. - * - * @note Closes socket if the socket is still open. - */ - virtual ~ICMPSocket(); - #if !defined(DOXYGEN_ONLY) protected: diff --git a/features/netsocket/InternetDatagram.cpp b/features/netsocket/InternetDatagramSocket.cpp similarity index 82% rename from features/netsocket/InternetDatagram.cpp rename to features/netsocket/InternetDatagramSocket.cpp index 23131b739da..7526a5e86ce 100644 --- a/features/netsocket/InternetDatagram.cpp +++ b/features/netsocket/InternetDatagramSocket.cpp @@ -14,25 +14,11 @@ * limitations under the License. */ -#include "InternetDatagram.h" +#include "InternetDatagramSocket.h" #include "Timer.h" #include "mbed_assert.h" -InternetDatagram::InternetDatagram() -{ - _socket_stats.stats_update_proto(this, NSAPI_ICMP); -} - -InternetDatagram::~InternetDatagram() -{ -} - -nsapi_protocol_t InternetDatagram::get_proto() -{ - return NSAPI_PROTO_UNKNOWN; -} - -nsapi_error_t InternetDatagram::connect(const SocketAddress &address) +nsapi_error_t InternetDatagramSocket::connect(const SocketAddress &address) { _remote_peer = address; _socket_stats.stats_update_peer(this, _remote_peer); @@ -40,7 +26,7 @@ nsapi_error_t InternetDatagram::connect(const SocketAddress &address) return NSAPI_ERROR_OK; } -nsapi_size_or_error_t InternetDatagram::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) +nsapi_size_or_error_t InternetDatagramSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size) { SocketAddress address; nsapi_size_or_error_t err; @@ -61,7 +47,7 @@ nsapi_size_or_error_t InternetDatagram::sendto(const char *host, uint16_t port, return sendto(address, data, size); } -nsapi_size_or_error_t InternetDatagram::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) +nsapi_size_or_error_t InternetDatagramSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size) { _lock.lock(); nsapi_size_or_error_t ret; @@ -108,7 +94,7 @@ nsapi_size_or_error_t InternetDatagram::sendto(const SocketAddress &address, con return ret; } -nsapi_size_or_error_t InternetDatagram::send(const void *data, nsapi_size_t size) +nsapi_size_or_error_t InternetDatagramSocket::send(const void *data, nsapi_size_t size) { if (!_remote_peer) { return NSAPI_ERROR_NO_ADDRESS; @@ -116,7 +102,7 @@ nsapi_size_or_error_t InternetDatagram::send(const void *data, nsapi_size_t size return sendto(_remote_peer, data, size); } -nsapi_size_or_error_t InternetDatagram::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) +nsapi_size_or_error_t InternetDatagramSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size) { _lock.lock(); nsapi_size_or_error_t ret; @@ -177,12 +163,12 @@ nsapi_size_or_error_t InternetDatagram::recvfrom(SocketAddress *address, void *b return ret; } -nsapi_size_or_error_t InternetDatagram::recv(void *buffer, nsapi_size_t size) +nsapi_size_or_error_t InternetDatagramSocket::recv(void *buffer, nsapi_size_t size) { return recvfrom(NULL, buffer, size); } -Socket *InternetDatagram::accept(nsapi_error_t *error) +Socket *InternetDatagramSocket::accept(nsapi_error_t *error) { if (error) { *error = NSAPI_ERROR_UNSUPPORTED; @@ -190,7 +176,7 @@ Socket *InternetDatagram::accept(nsapi_error_t *error) return NULL; } -nsapi_error_t InternetDatagram::listen(int) +nsapi_error_t InternetDatagramSocket::listen(int) { return NSAPI_ERROR_UNSUPPORTED; } diff --git a/features/netsocket/InternetDatagram.h b/features/netsocket/InternetDatagramSocket.h similarity index 92% rename from features/netsocket/InternetDatagram.h rename to features/netsocket/InternetDatagramSocket.h index 17f79e655fc..75c1223ba89 100644 --- a/features/netsocket/InternetDatagram.h +++ b/features/netsocket/InternetDatagramSocket.h @@ -1,6 +1,6 @@ /** \addtogroup netsocket */ /** @{*/ -/* InternetDatagram +/* InternetDatagramSocket * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,8 +16,8 @@ * limitations under the License. */ -#ifndef InternetDatagram_H -#define InternetDatagram_H +#ifndef INTERNETDATAGRAMSOCKET_H +#define INTERNETDATAGRAMSOCKET_H #include "netsocket/InternetSocket.h" #include "netsocket/NetworkStack.h" @@ -25,21 +25,10 @@ #include "rtos/EventFlags.h" -/** InternetDatagram socket implementation. +/** InternetDatagramSocket socket implementation. */ -class InternetDatagram : public InternetSocket { +class InternetDatagramSocket : public InternetSocket { public: - /** Create an uninitialized socket. - * - * @note Must call open to initialize the socket on a network stack. - */ - InternetDatagram(); - - /** Destroy a socket. - * - * @note Closes socket if the socket is still open. - */ - virtual ~InternetDatagram(); /** Send data to the specified host and port. * @@ -135,14 +124,14 @@ class InternetDatagram : public InternetSocket { */ virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); - /** Not implemented for InternetDatagram. + /** Not implemented for InternetDatagramSocket. * * @param error Not used. * @return NSAPI_ERROR_UNSUPPORTED */ virtual Socket *accept(nsapi_error_t *error = NULL); - /** Not implemented for InternetDatagram. + /** Not implemented for InternetDatagramSocket. * * @param backlog Not used. * @return NSAPI_ERROR_UNSUPPORTED @@ -151,7 +140,12 @@ class InternetDatagram : public InternetSocket { #if !defined(DOXYGEN_ONLY) protected: - virtual nsapi_protocol_t get_proto(); + + /** Create an uninitialized socket. + * + * @note Must call open to initialize the socket on a network stack. + */ + InternetDatagramSocket() = default; #endif //!defined(DOXYGEN_ONLY) }; diff --git a/features/netsocket/NetworkStack.h b/features/netsocket/NetworkStack.h index 560da81e95f..a8cf469760e 100644 --- a/features/netsocket/NetworkStack.h +++ b/features/netsocket/NetworkStack.h @@ -183,9 +183,7 @@ class NetworkStack: public DNS { protected: friend class InternetSocket; - friend class InternetDatagram; - friend class ICMPSocket; - friend class UDPSocket; + friend class InternetDatagramSocket; friend class TCPSocket; friend class TCPServer; diff --git a/features/netsocket/UDPSocket.cpp b/features/netsocket/UDPSocket.cpp index 6f37685567b..a21d099cfe0 100644 --- a/features/netsocket/UDPSocket.cpp +++ b/features/netsocket/UDPSocket.cpp @@ -23,10 +23,6 @@ UDPSocket::UDPSocket() _socket_stats.stats_update_proto(this, NSAPI_UDP); } -UDPSocket::~UDPSocket() -{ -} - nsapi_protocol_t UDPSocket::get_proto() { return NSAPI_UDP; diff --git a/features/netsocket/UDPSocket.h b/features/netsocket/UDPSocket.h index 783ca6a197b..bde85b987aa 100644 --- a/features/netsocket/UDPSocket.h +++ b/features/netsocket/UDPSocket.h @@ -23,14 +23,14 @@ #include "netsocket/InternetSocket.h" #include "netsocket/NetworkStack.h" #include "netsocket/NetworkInterface.h" -#include "netsocket/InternetDatagram.h" +#include "netsocket/InternetDatagramSocket.h" #include "rtos/EventFlags.h" #include "ICMPSocket.h" /** UDP socket implementation. */ -class UDPSocket : public InternetDatagram { +class UDPSocket : public InternetDatagramSocket { public: /** Create an uninitialized socket. * @@ -55,12 +55,6 @@ class UDPSocket : public InternetDatagram { open(stack); } - /** Destroy a socket. - * - * @note Closes socket if the socket is still open. - */ - virtual ~UDPSocket(); - #if !defined(DOXYGEN_ONLY) protected: diff --git a/features/netsocket/nsapi_types.h b/features/netsocket/nsapi_types.h index e2180abf1c1..fa0ef691da6 100644 --- a/features/netsocket/nsapi_types.h +++ b/features/netsocket/nsapi_types.h @@ -56,7 +56,6 @@ enum nsapi_error { NSAPI_ERROR_ADDRESS_IN_USE = -3018, /*!< Address already in use */ NSAPI_ERROR_TIMEOUT = -3019, /*!< operation timed out */ NSAPI_ERROR_BUSY = -3020, /*!< device is busy and cannot accept new operation */ - NSAPI_ERROR_PROTO_UNKNOWN = -3021, /*!< unknown protocol */ }; @@ -217,7 +216,6 @@ typedef enum nsapi_protocol { NSAPI_TCP, /*!< Socket is of TCP type */ NSAPI_UDP, /*!< Socket is of UDP type */ NSAPI_ICMP, /*!< Socket is of ICMP type */ - NSAPI_PROTO_UNKNOWN, /*!< Socket Protocol type UKNOWN */ } nsapi_protocol_t; /** Enum of standardized stack option levels From d9045b4840093956faba2fdc121daaa06bd87bce Mon Sep 17 00:00:00 2001 From: Balaji Date: Fri, 25 Oct 2019 11:00:55 -0700 Subject: [PATCH 09/12] index on lwip-rawsocket: 6905b1b547 Incorporate review comments from @kjbracey-arm --- features/lwipstack/mbed_lib.json | 397 +++++++++++-------------------- 1 file changed, 133 insertions(+), 264 deletions(-) diff --git a/features/lwipstack/mbed_lib.json b/features/lwipstack/mbed_lib.json index 6c42dc113fd..5b93e132df1 100644 --- a/features/lwipstack/mbed_lib.json +++ b/features/lwipstack/mbed_lib.json @@ -1,335 +1,204 @@ { -"name": "lwip" - , -"config": - { -"ipv4-enabled": - { -"help": "Enable IPv4" - , -"value": - true - }, -"ipv6-enabled": - { -"help": "Enable IPv6" - , -"value": - false - }, -"ip-ver-pref": - { -"help": "On dual-stack system the preferred stack: 4 for IPv4 and 6 for IPv6" - , + "name": "lwip", + "config": { + "ipv4-enabled": { + "help": "Enable IPv4", + "value": true + }, + "ipv6-enabled": { + "help": "Enable IPv6", + "value": false + }, + "ip-ver-pref": { + "help": "On dual-stack system the preferred stack: 4 for IPv4 and 6 for IPv6", "value": 4 }, -"addr-timeout": - { -"help": "On dual-stack system how long to additionally wait for other stack's address in seconds" - , + "addr-timeout": { + "help": "On dual-stack system how long to additionally wait for other stack's address in seconds", "value": 5 }, -"addr-timeout-mode": - { -"help": "Address timeout mode; true: wait both stack's addresses; false: wait for preferred stack's address" - , -"value": - true - }, -"value": - true - }, -"l3ip-enabled": - { -"help": "Enable support for L3IP interfaces" - , -"value": - false - }, -"debug-enabled": - { -"help": "Enable debug trace support" - , -"value": - false - }, -"ppp-enabled": - { -"help": "Enable support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)" - , -"value": - false - }, -"ppp-ipv4-enabled": - { -"help": "Enable support for ipv4 PPP interface (obsolete: use netsocket/ppp configuration instead)" - , -"value": - false - }, -"ppp-ipv6-enabled": - { -"help": "Enable support for ipv6 PPP interface (obsolete: use netsocket/ppp configuration instead)" - , -"value": - false - }, -"use-mbed-trace": - { -"help": "Use mbed trace for debug, rather than printf" - , -"value": - false - }, -"enable-ppp-trace": - { -"help": "Enable trace support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)" - , -"value": - false - }, -"socket-max": - { -"help": "Maximum number of open TCPServer, TCPSocket and UDPSocket instances allowed, including one used internally for DNS. Each requires 236 bytes of pre-allocated RAM" - , + "addr-timeout-mode": { + "help": "Address timeout mode; true: wait both stack's addresses; false: wait for preferred stack's address", + "value": true + }, + "ethernet-enabled": { + "help": "Enable support for Ethernet interfaces", + "value": true + }, + "l3ip-enabled": { + "help": "Enable support for L3IP interfaces", + "value": false + }, + "debug-enabled": { + "help": "Enable debug trace support", + "value": false + }, + "ppp-enabled": { + "help": "Enable support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)", + "value": false + }, + "ppp-ipv4-enabled": { + "help": "Enable support for ipv4 PPP interface (obsolete: use netsocket/ppp configuration instead)", + "value": false + }, + "ppp-ipv6-enabled": { + "help": "Enable support for ipv6 PPP interface (obsolete: use netsocket/ppp configuration instead)", + "value": false + }, + "use-mbed-trace": { + "help": "Use mbed trace for debug, rather than printf", + "value": false + }, + "enable-ppp-trace": { + "help": "Enable trace support for PPP interfaces (obsolete: use netsocket/ppp configuration instead)", + "value": false + }, + "socket-max": { + "help": "Maximum number of open TCPServer, TCPSocket and UDPSocket instances allowed, including one used internally for DNS. Each requires 236 bytes of pre-allocated RAM", "value": 4 }, -"tcp-enabled": - { -"help": "Enable TCP" - , -"value": - true - }, -"tcp-server-max": - { -"help": "Maximum number of open TCPServer instances allowed. Each requires 72 bytes of pre-allocated RAM" - , + "tcp-enabled": { + "help": "Enable TCP", + "value": true + }, + "tcp-server-max": { + "help": "Maximum number of open TCPServer instances allowed. Each requires 72 bytes of pre-allocated RAM", "value": 4 }, -"tcp-socket-max": - { -"help": "Maximum number of open TCPSocket instances allowed. Each requires 196 bytes of pre-allocated RAM" - , + "tcp-socket-max": { + "help": "Maximum number of open TCPSocket instances allowed. Each requires 196 bytes of pre-allocated RAM", "value": 4 }, -"udp-socket-max": - { -"help": "Maximum number of open UDPSocket instances allowed, including one used internally for DNS. Each requires 84 bytes of pre-allocated RAM" - , + "udp-socket-max": { + "help": "Maximum number of open UDPSocket instances allowed, including one used internally for DNS. Each requires 84 bytes of pre-allocated RAM", "value": 4 }, -"memp-num-tcp-seg": - "help": "Number of simultaneously queued TCP segments, see LWIP opt.h for more information. Current default is 16.", - "value": 16 - }, - "memp-num-tcpip-msg-inpkt": { - "help": "Number of simultaneously queued TCP messages that are received", - "value": 8 - }, -"tcp-mss": - "help": "TCP Maximum segment size, see LWIP opt.h for more information. Current default is 536.", - "value": 536 - }, - "tcpip-mbox-size": { - "help": "TCPIP mailbox size", - "value": 8 - }, - "default-tcp-recvmbox-size": { - "help": "Default TCPIP receive mailbox size", - "value": 8 - }, - "mbox-size": { - "help": "mailbox size", - "value": 8 - }, -"tcp-snd-buf": - "help": "TCP sender buffer space (bytes), see LWIP's opt.h for more information. Current default is (2 * TCP_MSS).", - "value": "(2 * TCP_MSS)" - , -"value": - null - }, -"tcp-wnd": - "help": "TCP sender buffer space (bytes), see LWIP's opt.h for more information. Current default is (4 * TCP_MSS).", - "value": "(4 * TCP_MSS)" - , -"value": - null - }, -"tcp-maxrtx": - "help": "Maximum number of retransmissions of data segments, see LWIP's opt.h for more information. Current default is 6.", -"help": "Maximum number of retransmissions of data segments." - , - "value": 6 + "memp-num-tcp-seg": { + "help": "Number of simultaneously queued TCP segments. Current default (used if null here) is set to 16 in opt.h, unless overridden by target Ethernet drivers.", + "value": null + }, + "tcp-mss": { + "help": "TCP Maximum segment size. Current default (used if null here) is set to 536 in opt.h, unless overridden by target Ethernet drivers.", + "value": null }, -"tcp-synmaxrtx": - "help": "Maximum number of retransmissions of SYN segments, see LWIP's opt.h for more information. Current default is 6.", + "tcp-snd-buf": { + "help": "TCP sender buffer space (bytes). Current default (used if null here) is set to (2 * TCP_MSS) in opt.h, unless overridden by target Ethernet drivers.", + "value": null + }, + "tcp-wnd": { + "help": "TCP sender buffer space (bytes). Current default (used if null here) is set to (4 * TCP_MSS) in opt.h, unless overridden by target Ethernet drivers.", + "value": null + }, + "tcp-maxrtx": { + "help": "Maximum number of retransmissions of data segments.", "value": 6 - , -"value": - null - }, -"tcp-close-timeout": - { -"help": "Maximum timeout (ms) for TCP close handshaking timeout" - , + }, + "tcp-synmaxrtx": { + "help": "Maximum number of retransmissions of SYN segments. Current default (used if null here) is set to 6 in opt.h", + "value": null + }, + "tcp-close-timeout": { + "help": "Maximum timeout (ms) for TCP close handshaking timeout", "value": 1000 }, -"tcpip-thread-priority": - { -"help": "Priority of lwip TCPIP thread" - , -"value": "osPriorityNormal" + "tcpip-thread-priority": { + "help": "Priority of lwip TCPIP thread", + "value": "osPriorityNormal" }, -"pbuf-pool-size": - "help": "Number of pbufs in pool - usually used for received packets, so this determines how much data can be buffered between reception and the application reading, see LWIP's opt.h for more information. If a driver uses PBUF_RAM for reception, less pool may be needed. Current default is 5.", - "value": 5 - , -"value": - null - }, -"pbuf-pool-bufsize": - "help": "Size of pbufs in pool, see LWIP's opt.h for more information.", -"help": "Size of pbufs in pool. If set to null, lwIP will base the size on the TCP MSS, which is 536 unless overridden by the target" - , -"value": - null - }, -"mem-size": - "help": "Size of heap (bytes) - used for outgoing packets, and also used by some drivers for reception, see LWIP's opt.h for more information. Current default is 1600.", - "value": 1600 - , -"value": - null - }, -"tcpip-thread-stacksize": - { -"help": "Stack size for lwip TCPIP thread" - , - "value": 1200 + "pbuf-pool-size": { + "help": "Number of pbufs in pool - usually used for received packets, so this determines how much data can be buffered between reception and the application reading. If a driver uses PBUF_RAM for reception, less pool may be needed. Current default (used if null here) is set to 5 in lwipopts.h, unless overridden by target Ethernet drivers.", + "value": null }, -"default-thread-stacksize": - { -"help": "Stack size for lwip system threads" - , - "value": 512 + "pbuf-pool-bufsize": { + "help": "Size of pbufs in pool. If set to null, lwIP will base the size on the TCP MSS, which is 536 unless overridden by the target", + "value": null }, -"ppp-thread-stacksize": - { -"help": "Thread stack size for PPP (obsolete: use netsocket/ppp configuration instead)" - , - "value": 768 + "mem-size": { + "help": "Size of heap (bytes) - used for outgoing packets, and also used by some drivers for reception. Current default (used if null here) is set to 1600 in opt.h, unless overridden by target Ethernet drivers.", + "value": null }, -"raw-socket-enabled": - { -"help": "Enable RAW socket" + "tcpip-thread-stacksize": { + "help": "Stack size for lwip TCPIP thread", + "value": 1200 }, - "num-pbuf": { - "help": "Number of non-pool pbufs, each needs 92 bytes of RAM, see LWIP's opt.h for more information. Current default is 8.", - "value": 8 + "default-thread-stacksize": { + "help": "Stack size for lwip system threads", + "value": 512 }, - "num-netbuf": { - "help": "Number of netbufs, each netbuf requires 64 bytes of RAM, see LWIP's opt.h for more information. Current default is 8.", - "value": 8 + "ppp-thread-stacksize": { + "help": "Thread stack size for PPP (obsolete: use netsocket/ppp configuration instead)", + "value": 768 } + "raw-socket-enabled": { + "help": "Enable lwip raw sockets, required for Mbed OS ICMPSocket", + "value": false + } }, -"target_overrides": - { -"REALTEK_RTL8195AM": - { + "target_overrides": { + "REALTEK_RTL8195AM": { "tcpip-thread-stacksize": 1600, "mem-size": 12800 }, -"UBLOX_EVK_ODIN_W2": - { + "UBLOX_EVK_ODIN_W2": { "pbuf-pool-size" : 10 }, -"STM": - { + "STM": { "mem-size": 2310 }, -"Freescale": - { + "Freescale": { "mem-size": 33270 }, -"LPC1768": - { + "LPC1768": { "mem-size": 16362 }, -"LPC4088": - { + "LPC4088": { "mem-size": 15360 }, -"LPC4088_DM": - { + "LPC4088_DM": { "mem-size": 15360 }, -"UBLOX_C027": - { + "UBLOX_C027": { "mem-size": 16362 }, -"ARCH_PRO": - { + "ARCH_PRO": { "mem-size": 16362 }, -"LPC546XX": - { + "LPC546XX": { "mem-size": 36496 }, -"EFM32GG11_STK3701": - { + "EFM32GG11_STK3701": { "mem-size": 36560 }, -"RZ_A1_EMAC": - { + "RZ_A1_EMAC": { "tcpip-thread-stacksize": 1328, "default-thread-stacksize": 640, "memp-num-tcp-seg": 32, "tcp-mss": 1440, -"tcp-snd-buf": "(8 * TCP_MSS)" - , -"tcp-wnd": "(TCP_MSS * 8)" - , + "tcp-snd-buf": "(8 * TCP_MSS)", + "tcp-wnd": "(TCP_MSS * 8)", "pbuf-pool-size": 16, "mem-size": 51200 }, "MCU_PSOC6": { - { -"tcp-wnd": "(TCP_MSS * 6)" - , - { "tcpip-thread-stacksize": 8192, "default-thread-stacksize": 640, "memp-num-tcp-seg": 24, "tcp-socket-max": 10, - "udp-socket-max": 10, - "socket-max": 18, + "udp-socket-max":10, + "socket-max":18, "tcp-mss": 1540, -"tcp-snd-buf": "(6 * TCP_MSS)" - , + "tcp-snd-buf": "(6 * TCP_MSS)", + "tcp-wnd": "(TCP_MSS * 6)", "pbuf-pool-size": 14, - , - { -"tcp-wnd": "(TCP_MSS * 6)" - , "mem-size": 65536 }, - { -"tcp-snd-buf": "(6 * TCP_MSS)" - , -"tcp-wnd": "(TCP_MSS * 6)" - , -"MIMXRT1050_EVK": - { + "MIMXRT1050_EVK": { "mem-size": 36560 }, -"FVP_MPS2_M3": - { + "FVP_MPS2_M3": { "mem-size": 36560 }, -"MTS_DRAGONFLY_F411RE": - { + "MTS_DRAGONFLY_F411RE": { "tcpip-thread-stacksize": 1600 } } From 948e989d271e3c0e42177b93696f277a3d2d1168 Mon Sep 17 00:00:00 2001 From: Balaji Date: Fri, 25 Oct 2019 12:14:31 -0700 Subject: [PATCH 10/12] fix mbed_lib.json add , --- features/lwipstack/mbed_lib.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/lwipstack/mbed_lib.json b/features/lwipstack/mbed_lib.json index 5b93e132df1..cc6154697fe 100644 --- a/features/lwipstack/mbed_lib.json +++ b/features/lwipstack/mbed_lib.json @@ -128,7 +128,7 @@ "ppp-thread-stacksize": { "help": "Thread stack size for PPP (obsolete: use netsocket/ppp configuration instead)", "value": 768 - } + }, "raw-socket-enabled": { "help": "Enable lwip raw sockets, required for Mbed OS ICMPSocket", "value": false From 5b9291628ebeefbbb2527c1671a590e89f3cf37b Mon Sep 17 00:00:00 2001 From: Balaji Date: Fri, 25 Oct 2019 12:39:27 -0700 Subject: [PATCH 11/12] rebase mbed_lib.json to upstream/master --- features/lwipstack/mbed_lib.json | 66 +++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/features/lwipstack/mbed_lib.json b/features/lwipstack/mbed_lib.json index cc6154697fe..5ac827ac825 100644 --- a/features/lwipstack/mbed_lib.json +++ b/features/lwipstack/mbed_lib.json @@ -21,6 +21,10 @@ "help": "Address timeout mode; true: wait both stack's addresses; false: wait for preferred stack's address", "value": true }, + "dhcp-timeout": { + "help": "DHCP timeout value", + "value": 60 + }, "ethernet-enabled": { "help": "Enable support for Ethernet interfaces", "value": true @@ -74,28 +78,44 @@ "value": 4 }, "memp-num-tcp-seg": { - "help": "Number of simultaneously queued TCP segments. Current default (used if null here) is set to 16 in opt.h, unless overridden by target Ethernet drivers.", - "value": null + "help": "Number of simultaneously queued TCP segments, see LWIP opt.h for more information. Current default is 16.", + "value": 16 + }, + "memp-num-tcpip-msg-inpkt": { + "help": "Number of simultaneously queued TCP messages that are received", + "value": 8 }, "tcp-mss": { - "help": "TCP Maximum segment size. Current default (used if null here) is set to 536 in opt.h, unless overridden by target Ethernet drivers.", - "value": null + "help": "TCP Maximum segment size, see LWIP opt.h for more information. Current default is 536.", + "value": 536 + }, + "tcpip-mbox-size": { + "help": "TCPIP mailbox size", + "value": 8 + }, + "default-tcp-recvmbox-size": { + "help": "Default TCPIP receive mailbox size", + "value": 8 + }, + "mbox-size": { + "help": "mailbox size", + "value": 8 }, "tcp-snd-buf": { - "help": "TCP sender buffer space (bytes). Current default (used if null here) is set to (2 * TCP_MSS) in opt.h, unless overridden by target Ethernet drivers.", - "value": null + "help": "TCP sender buffer space (bytes), see LWIP's opt.h for more information. Current default is (2 * TCP_MSS).", + "value": "(2 * TCP_MSS)" }, "tcp-wnd": { - "help": "TCP sender buffer space (bytes). Current default (used if null here) is set to (4 * TCP_MSS) in opt.h, unless overridden by target Ethernet drivers.", - "value": null + "help": "TCP sender buffer space (bytes), see LWIP's opt.h for more information. Current default is (4 * TCP_MSS).", + "value": "(4 * TCP_MSS)" }, "tcp-maxrtx": { - "help": "Maximum number of retransmissions of data segments.", + "help": "Maximum number of retransmissions of data segments, see LWIP's opt.h for more information. Current default is 6.", "value": 6 }, "tcp-synmaxrtx": { - "help": "Maximum number of retransmissions of SYN segments. Current default (used if null here) is set to 6 in opt.h", - "value": null + "help": "Maximum number of retransmissions of SYN segments, see LWIP's opt.h for more information. Current default is 6.", + "value": 6 }, "tcp-close-timeout": { "help": "Maximum timeout (ms) for TCP close handshaking timeout", @@ -106,16 +126,16 @@ "value": "osPriorityNormal" }, "pbuf-pool-size": { - "help": "Number of pbufs in pool - usually used for received packets, so this determines how much data can be buffered between reception and the application reading. If a driver uses PBUF_RAM for reception, less pool may be needed. Current default (used if null here) is set to 5 in lwipopts.h, unless overridden by target Ethernet drivers.", - "value": null + "help": "Number of pbufs in pool - usually used for received packets, so this determines how much data can be buffered between reception and the application reading, see LWIP's opt.h for more information. If a driver uses PBUF_RAM for reception, less pool may be needed. Current default is 5.", + "value": 5 }, "pbuf-pool-bufsize": { - "help": "Size of pbufs in pool. If set to null, lwIP will base the size on the TCP MSS, which is 536 unless overridden by the target", + "help": "Size of pbufs in pool, see LWIP's opt.h for more information.", "value": null }, "mem-size": { - "help": "Size of heap (bytes) - used for outgoing packets, and also used by some drivers for reception. Current default (used if null here) is set to 1600 in opt.h, unless overridden by target Ethernet drivers.", - "value": null + "help": "Size of heap (bytes) - used for outgoing packets, and also used by some drivers for reception, see LWIP's opt.h for more information. Current default is 1600.", + "value": 1600 }, "tcpip-thread-stacksize": { "help": "Stack size for lwip TCPIP thread", @@ -129,10 +149,18 @@ "help": "Thread stack size for PPP (obsolete: use netsocket/ppp configuration instead)", "value": 768 }, + "num-pbuf": { + "help": "Number of non-pool pbufs, each needs 92 bytes of RAM, see LWIP's opt.h for more information. Current default is 8.", + "value": 8 + }, + "num-netbuf": { + "help": "Number of netbufs, each netbuf requires 64 bytes of RAM, see LWIP's opt.h for more information. Current default is 8.", + "value": 8 + }, "raw-socket-enabled": { - "help": "Enable lwip raw sockets, required for Mbed OS ICMPSocket", - "value": false - } + "help": "Enable lwip raw sockets, required for Mbed OS ICMPSocket", + "value": false + } }, "target_overrides": { "REALTEK_RTL8195AM": { From 2d410e3c0553e3a747110a2a8741ac1877025bab Mon Sep 17 00:00:00 2001 From: Balaji Date: Mon, 28 Oct 2019 12:10:12 -0700 Subject: [PATCH 12/12] Rebase the code Unittests aware of InternetDatagramSocket --- UNITTESTS/features/netsocket/DTLSSocket/unittest.cmake | 1 + UNITTESTS/features/netsocket/DTLSSocketWrapper/unittest.cmake | 1 + UNITTESTS/features/netsocket/UDPSocket/unittest.cmake | 1 + 3 files changed, 3 insertions(+) diff --git a/UNITTESTS/features/netsocket/DTLSSocket/unittest.cmake b/UNITTESTS/features/netsocket/DTLSSocket/unittest.cmake index 8b66364f37b..fa43a292ee4 100644 --- a/UNITTESTS/features/netsocket/DTLSSocket/unittest.cmake +++ b/UNITTESTS/features/netsocket/DTLSSocket/unittest.cmake @@ -7,6 +7,7 @@ set(unittest-sources ../features/netsocket/SocketAddress.cpp ../features/netsocket/NetworkStack.cpp ../features/netsocket/InternetSocket.cpp + ../features/netsocket/InternetDatagramSocket.cpp ../features/netsocket/UDPSocket.cpp ../features/netsocket/DTLSSocket.cpp ../features/netsocket/DTLSSocketWrapper.cpp diff --git a/UNITTESTS/features/netsocket/DTLSSocketWrapper/unittest.cmake b/UNITTESTS/features/netsocket/DTLSSocketWrapper/unittest.cmake index 5fc1e82ac17..98f011a071e 100644 --- a/UNITTESTS/features/netsocket/DTLSSocketWrapper/unittest.cmake +++ b/UNITTESTS/features/netsocket/DTLSSocketWrapper/unittest.cmake @@ -7,6 +7,7 @@ set(unittest-sources ../features/netsocket/SocketAddress.cpp ../features/netsocket/NetworkStack.cpp ../features/netsocket/InternetSocket.cpp + ../features/netsocket/InternetDatagramSocket.cpp ../features/netsocket/UDPSocket.cpp ../features/netsocket/DTLSSocketWrapper.cpp ../features/netsocket/TLSSocketWrapper.cpp diff --git a/UNITTESTS/features/netsocket/UDPSocket/unittest.cmake b/UNITTESTS/features/netsocket/UDPSocket/unittest.cmake index 99a5900b240..b23122f4b32 100644 --- a/UNITTESTS/features/netsocket/UDPSocket/unittest.cmake +++ b/UNITTESTS/features/netsocket/UDPSocket/unittest.cmake @@ -7,6 +7,7 @@ set(unittest-sources ../features/netsocket/SocketAddress.cpp ../features/netsocket/NetworkStack.cpp ../features/netsocket/InternetSocket.cpp + ../features/netsocket/InternetDatagramSocket.cpp ../features/netsocket/UDPSocket.cpp ../features/frameworks/nanostack-libservice/source/libip4string/ip4tos.c ../features/frameworks/nanostack-libservice/source/libip6string/ip6tos.c