Skip to content

C++11-ify virtualisation in netsocket #12487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(unittest-test-sources
stubs/Mutex_stub.cpp
stubs/CellularContext_stub.cpp
stubs/mbed_assert_stub.cpp
stubs/mbed_atomic_stub.c
)

set(unittest-test-flags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,22 @@ class TestPPPInterface: public testing::Test {
}
};

#if 0
/* Test is invalid, as it's working on a PPPInterface pointer rather than a NetworkInterface pointer. */
/* NetworkInterface does not yet offer the pppInterface method for a dynamic cast */
TEST_F(TestPPPInterface, constructor_default)
{
EXPECT_TRUE(iface);
// Test that this clas presents itself correctly
EXPECT_NE(nullptr, iface->pppInterface());
EXPECT_EQ(iface, iface->pppInterface());

EXPECT_EQ(nullptr, iface->emacInterface());
EXPECT_EQ(nullptr, iface->ethInterface());
EXPECT_EQ(nullptr, iface->wifiInterface());
EXPECT_EQ(nullptr, iface->cellularInterface());
EXPECT_EQ(nullptr, iface->meshInterface());
}
#endif

TEST_F(TestPPPInterface, connect)
{
Expand Down
10 changes: 5 additions & 5 deletions features/netsocket/CellularInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class CellularInterface: public NetworkInterface {
*
* @return NSAPI_ERROR_OK on success, or negative error code on failure.
*/
virtual nsapi_error_t connect() = 0;
nsapi_error_t connect() override = 0;

/** Stop the interface.
*
* @return NSAPI_ERROR_OK on success, or error code on failure.
*/
virtual nsapi_error_t disconnect() = 0;
nsapi_error_t disconnect() override = 0;

/** Check if the connection is currently established.
*
Expand All @@ -96,11 +96,11 @@ class CellularInterface: public NetworkInterface {
virtual bool is_connected() = 0;

/** @copydoc NetworkInterface::get_ip_address */
virtual nsapi_error_t get_ip_address(SocketAddress *address) = 0;
nsapi_error_t get_ip_address(SocketAddress *address) override = 0;

/** @copydoc NetworkInterface::cellularInterface
*/
virtual CellularInterface *cellularInterface()
CellularInterface *cellularInterface() final
{
return this;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ class CellularInterface: public NetworkInterface {
* NetworkInterface::get_default_instance() (see nsapi JSON
* configuration).
*/
virtual void set_default_parameters();
void set_default_parameters() override;
};

#endif // CELLULAR_INTERFACE_H_
23 changes: 9 additions & 14 deletions features/netsocket/CellularNonIPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ using namespace mbed;

ControlPlane_netif *CellularNonIPSocket::_cp_netif;

CellularNonIPSocket::CellularNonIPSocket()
: _timeout(osWaitForever),
_readers(0), _writers(0), _pending(0),
_opened(false)
{}
CellularNonIPSocket::CellularNonIPSocket() = default;

nsapi_error_t CellularNonIPSocket::open(CellularContext *cellular_context)
{
if (cellular_context == NULL) {
if (cellular_context == nullptr) {
return NSAPI_ERROR_PARAMETER;
}

Expand All @@ -47,7 +43,7 @@ nsapi_error_t CellularNonIPSocket::open(ControlPlane_netif *cp_netif)
{
_lock.lock();

if (cp_netif == NULL || _opened) {
if (cp_netif == nullptr || _opened) {
_lock.unlock();
return NSAPI_ERROR_PARAMETER;
}
Expand Down Expand Up @@ -76,9 +72,9 @@ nsapi_error_t CellularNonIPSocket::close()
}

// Just in case - tell the stack not to callback any more, then remove this socket.
_cp_netif->attach(0, 0);
_cp_netif->attach(nullptr, nullptr);
_opened = false;
_cp_netif = 0; // Invalidate the cp_netif pointer - otherwise open() fails.
_cp_netif = nullptr; // Invalidate the cp_netif pointer - otherwise open() fails.

// Wakeup anything in a blocking operation
// on this socket
Expand Down Expand Up @@ -112,7 +108,7 @@ nsapi_size_or_error_t CellularNonIPSocket::send(const void *data, nsapi_size_t s
break;
}

_pending = 0;
core_util_atomic_flag_clear(&_pending);
nsapi_size_or_error_t sent = _cp_netif->send(data, size);
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
ret = sent;
Expand Down Expand Up @@ -154,7 +150,7 @@ nsapi_size_or_error_t CellularNonIPSocket::recv(void *buffer, nsapi_size_t size)
break;
}

_pending = 0;
core_util_atomic_flag_clear(&_pending);
nsapi_size_or_error_t recv = _cp_netif->recv(buffer, size);

// Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK
Expand Down Expand Up @@ -210,8 +206,7 @@ void CellularNonIPSocket::event()
{
_event_flag.set(READ_FLAG | WRITE_FLAG);

_pending += 1;
if (_callback && _pending == 1) {
if (_callback && !core_util_atomic_flag_test_and_set(&_pending)) {
_callback();
}
}
Expand All @@ -233,7 +228,7 @@ Socket *CellularNonIPSocket::accept(nsapi_error_t *error)
if (error) {
*error = NSAPI_ERROR_UNSUPPORTED;
}
return NULL;
return nullptr;
}

nsapi_error_t CellularNonIPSocket::listen(int backlog)
Expand Down
53 changes: 27 additions & 26 deletions features/netsocket/CellularNonIPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "rtos/Mutex.h"
#include "rtos/EventFlags.h"
#include "Callback.h"
#include "mbed_atomic.h"
#include "mbed_toolchain.h"
#include "ControlPlane_netif.h"
#include "CellularContext.h"
Expand All @@ -40,7 +41,7 @@ class CellularNonIPSocket : public Socket {
*
* @note Closes socket if it's still open.
*/
virtual ~CellularNonIPSocket();
~CellularNonIPSocket() override;

/** Creates a socket.
*/
Expand All @@ -54,7 +55,7 @@ class CellularNonIPSocket : public Socket {
* @return NSAPI_ERROR_OK on success
* NSAPI_ERROR_PARAMETER otherwise
*/
virtual nsapi_error_t open(mbed::CellularContext *cellular_context);
nsapi_error_t open(mbed::CellularContext *cellular_context);

/** Opens a socket that will use the given control plane interface for data delivery.
* Attaches the event as callback to the control plane interface.
Expand All @@ -64,15 +65,15 @@ class CellularNonIPSocket : public Socket {
* NSAPI_ERROR_PARAMETER otherwise
*
*/
virtual nsapi_error_t open(mbed::ControlPlane_netif *cp_netif);
nsapi_error_t open(mbed::ControlPlane_netif *cp_netif);

/** Closes socket
*
* @return NSAPI_ERROR_OK on success
* NSAPI_ERROR_NO_SOCKET otherwise
*/

virtual nsapi_error_t close();
nsapi_error_t close() override;

/** Send data over a control plane cellular context.
*
Expand All @@ -85,7 +86,7 @@ class CellularNonIPSocket : public Socket {
* @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);
nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override;

/** Receive data from a socket.
*
Expand All @@ -98,60 +99,60 @@ class CellularNonIPSocket : public Socket {
* @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);
nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override;

/** @copydoc Socket::set_blocking
*/
virtual void set_blocking(bool blocking);
void set_blocking(bool blocking) override;

/** @copydoc Socket::set_timeout
*/
virtual void set_timeout(int timeout);
void set_timeout(int timeout) override;

/** @copydoc Socket::sigio
*/
virtual void sigio(mbed::Callback<void()> func);
void sigio(mbed::Callback<void()> func) override;

/// NOT APPLICABLE
virtual nsapi_error_t connect(const SocketAddress &address);
nsapi_error_t connect(const SocketAddress &address) override;
/// NOT APPLICABLE
virtual Socket *accept(nsapi_error_t *error = NULL);
Socket *accept(nsapi_error_t *error = NULL) override;
/// NOT APPLICABLE
virtual nsapi_error_t listen(int backlog = 1);
nsapi_error_t listen(int backlog = 1) override;
/// NOT APPLICABLE
virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen) override;
/// NOT APPLICABLE
virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen) override;
/// NOT APPLICABLE
virtual nsapi_error_t getpeername(SocketAddress *address);
nsapi_error_t getpeername(SocketAddress *address) override;
/// NOT APPLICABLE
virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
const void *data, nsapi_size_t size);
nsapi_size_or_error_t sendto(const SocketAddress &address,
const void *data, nsapi_size_t size) override;
/// NOT APPLICABLE
virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,
void *data, nsapi_size_t size);
nsapi_size_or_error_t recvfrom(SocketAddress *address,
void *data, nsapi_size_t size) override;
/// NOT APPLICABLE
virtual nsapi_error_t bind(const SocketAddress &address);
nsapi_error_t bind(const SocketAddress &address) override;

protected:
virtual void event();
void event();

uint32_t _timeout;
uint32_t _timeout = osWaitForever;
mbed::Callback<void()> _event;
mbed::Callback<void()> _callback;
rtos::EventFlags _event_flag;
rtos::Mutex _lock;
uint8_t _readers;
uint8_t _writers;
volatile unsigned _pending;
uint8_t _readers = 0;
uint8_t _writers = 0;
core_util_atomic_flag _pending = CORE_UTIL_ATOMIC_FLAG_INIT;

// Event flags
static const int READ_FLAG = 0x1u;
static const int WRITE_FLAG = 0x2u;
static const int FINISHED_FLAG = 0x3u;

static ControlPlane_netif *_cp_netif; // there can be only one Non-IP socket
bool _opened;
bool _opened = false;
};

/** @}*/
Expand Down
3 changes: 1 addition & 2 deletions features/netsocket/ControlPlane_netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ namespace mbed {
*/
class ControlPlane_netif {
public:
ControlPlane_netif() {}
virtual ~ControlPlane_netif() {}
virtual ~ControlPlane_netif() = default;

protected:
friend class CellularNonIPSocket;
Expand Down
3 changes: 3 additions & 0 deletions features/netsocket/DNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ class DNS {
* @return NSAPI_ERROR_OK on success, negative error code on failure.
*/
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name = NULL) = 0;

protected:
~DNS() = default;
};

#endif
Expand Down
4 changes: 2 additions & 2 deletions features/netsocket/DTLSSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DTLSSocket : public DTLSSocketWrapper {

/** Destroy the DTLSSocket and closes the transport.
*/
virtual ~DTLSSocket();
~DTLSSocket() override;

/** Create a socket on a network interface.
*
Expand Down Expand Up @@ -74,7 +74,7 @@ class DTLSSocket : public DTLSSocketWrapper {
* @return NSAPI_ERROR_OK on success, negative error code on failure.
* See @ref UDPSocket::open.
*/
virtual nsapi_error_t open(NetworkStack *stack)
nsapi_error_t open(NetworkStack *stack)
{
return _udp_socket.open(stack);
}
Expand Down
5 changes: 1 addition & 4 deletions features/netsocket/DTLSSocketWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
#if defined(MBEDTLS_SSL_CLI_C)

DTLSSocketWrapper::DTLSSocketWrapper(Socket *transport, const char *hostname, control_transport control) :
TLSSocketWrapper(transport, hostname, control),
_int_ms_tick(0),
_timer_event_id(0),
_timer_expired(false)
TLSSocketWrapper(transport, hostname, control)
{
mbedtls_ssl_conf_transport(get_ssl_config(), MBEDTLS_SSL_TRANSPORT_DATAGRAM);
mbedtls_ssl_set_timer_cb(get_ssl_context(), this, timing_set_delay, timing_get_delay);
Expand Down
6 changes: 3 additions & 3 deletions features/netsocket/DTLSSocketWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class DTLSSocketWrapper : public TLSSocketWrapper {
static void timing_set_delay(void *ctx, uint32_t int_ms, uint32_t fin_ms);
static int timing_get_delay(void *ctx);
void timer_event();
uint64_t _int_ms_tick;
int _timer_event_id;
bool _timer_expired : 1;
uint64_t _int_ms_tick = 0;
int _timer_event_id = 0;
bool _timer_expired = false;
};

#endif
Expand Down
8 changes: 1 addition & 7 deletions features/netsocket/EMACInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ using namespace mbed;
/* Interface implementation */
EMACInterface::EMACInterface(EMAC &emac, OnboardNetworkStack &stack) :
_emac(emac),
_stack(stack),
_interface(NULL),
_dhcp(true),
_blocking(true),
_ip_address(),
_netmask(),
_gateway()
_stack(stack)
{
}

Expand Down
Loading