Skip to content

Commit f4df4e7

Browse files
authored
Merge pull request #12487 from kjbracey-arm/override_nsapi
C++11-ify virtualisation in netsocket
2 parents bad9c57 + d8d35ed commit f4df4e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+344
-416
lines changed

UNITTESTS/features/netsocket/CellularNonIPSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(unittest-test-sources
2121
stubs/Mutex_stub.cpp
2222
stubs/CellularContext_stub.cpp
2323
stubs/mbed_assert_stub.cpp
24+
stubs/mbed_atomic_stub.c
2425
)
2526

2627
set(unittest-test-flags

UNITTESTS/features/netsocket/PPPInterface/test_PPPInterface.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,22 @@ class TestPPPInterface: public testing::Test {
114114
}
115115
};
116116

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

123126
EXPECT_EQ(nullptr, iface->emacInterface());
124127
EXPECT_EQ(nullptr, iface->ethInterface());
125128
EXPECT_EQ(nullptr, iface->wifiInterface());
126129
EXPECT_EQ(nullptr, iface->cellularInterface());
127130
EXPECT_EQ(nullptr, iface->meshInterface());
128131
}
132+
#endif
129133

130134
TEST_F(TestPPPInterface, connect)
131135
{

features/netsocket/CellularInterface.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ class CellularInterface: public NetworkInterface {
8080
*
8181
* @return NSAPI_ERROR_OK on success, or negative error code on failure.
8282
*/
83-
virtual nsapi_error_t connect() = 0;
83+
nsapi_error_t connect() override = 0;
8484

8585
/** Stop the interface.
8686
*
8787
* @return NSAPI_ERROR_OK on success, or error code on failure.
8888
*/
89-
virtual nsapi_error_t disconnect() = 0;
89+
nsapi_error_t disconnect() override = 0;
9090

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

9898
/** @copydoc NetworkInterface::get_ip_address */
99-
virtual nsapi_error_t get_ip_address(SocketAddress *address) = 0;
99+
nsapi_error_t get_ip_address(SocketAddress *address) override = 0;
100100

101101
/** @copydoc NetworkInterface::cellularInterface
102102
*/
103-
virtual CellularInterface *cellularInterface()
103+
CellularInterface *cellularInterface() final
104104
{
105105
return this;
106106
}
@@ -130,7 +130,7 @@ class CellularInterface: public NetworkInterface {
130130
* NetworkInterface::get_default_instance() (see nsapi JSON
131131
* configuration).
132132
*/
133-
virtual void set_default_parameters();
133+
void set_default_parameters() override;
134134
};
135135

136136
#endif // CELLULAR_INTERFACE_H_

features/netsocket/CellularNonIPSocket.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ using namespace mbed;
2323

2424
ControlPlane_netif *CellularNonIPSocket::_cp_netif;
2525

26-
CellularNonIPSocket::CellularNonIPSocket()
27-
: _timeout(osWaitForever),
28-
_readers(0), _writers(0), _pending(0),
29-
_opened(false)
30-
{}
26+
CellularNonIPSocket::CellularNonIPSocket() = default;
3127

3228
nsapi_error_t CellularNonIPSocket::open(CellularContext *cellular_context)
3329
{
34-
if (cellular_context == NULL) {
30+
if (cellular_context == nullptr) {
3531
return NSAPI_ERROR_PARAMETER;
3632
}
3733

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

50-
if (cp_netif == NULL || _opened) {
46+
if (cp_netif == nullptr || _opened) {
5147
_lock.unlock();
5248
return NSAPI_ERROR_PARAMETER;
5349
}
@@ -76,9 +72,9 @@ nsapi_error_t CellularNonIPSocket::close()
7672
}
7773

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

8379
// Wakeup anything in a blocking operation
8480
// on this socket
@@ -112,7 +108,7 @@ nsapi_size_or_error_t CellularNonIPSocket::send(const void *data, nsapi_size_t s
112108
break;
113109
}
114110

115-
_pending = 0;
111+
core_util_atomic_flag_clear(&_pending);
116112
nsapi_size_or_error_t sent = _cp_netif->send(data, size);
117113
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
118114
ret = sent;
@@ -154,7 +150,7 @@ nsapi_size_or_error_t CellularNonIPSocket::recv(void *buffer, nsapi_size_t size)
154150
break;
155151
}
156152

157-
_pending = 0;
153+
core_util_atomic_flag_clear(&_pending);
158154
nsapi_size_or_error_t recv = _cp_netif->recv(buffer, size);
159155

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

213-
_pending += 1;
214-
if (_callback && _pending == 1) {
209+
if (_callback && !core_util_atomic_flag_test_and_set(&_pending)) {
215210
_callback();
216211
}
217212
}
@@ -233,7 +228,7 @@ Socket *CellularNonIPSocket::accept(nsapi_error_t *error)
233228
if (error) {
234229
*error = NSAPI_ERROR_UNSUPPORTED;
235230
}
236-
return NULL;
231+
return nullptr;
237232
}
238233

239234
nsapi_error_t CellularNonIPSocket::listen(int backlog)

features/netsocket/CellularNonIPSocket.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "rtos/Mutex.h"
2222
#include "rtos/EventFlags.h"
2323
#include "Callback.h"
24+
#include "mbed_atomic.h"
2425
#include "mbed_toolchain.h"
2526
#include "ControlPlane_netif.h"
2627
#include "CellularContext.h"
@@ -40,7 +41,7 @@ class CellularNonIPSocket : public Socket {
4041
*
4142
* @note Closes socket if it's still open.
4243
*/
43-
virtual ~CellularNonIPSocket();
44+
~CellularNonIPSocket() override;
4445

4546
/** Creates a socket.
4647
*/
@@ -54,7 +55,7 @@ class CellularNonIPSocket : public Socket {
5455
* @return NSAPI_ERROR_OK on success
5556
* NSAPI_ERROR_PARAMETER otherwise
5657
*/
57-
virtual nsapi_error_t open(mbed::CellularContext *cellular_context);
58+
nsapi_error_t open(mbed::CellularContext *cellular_context);
5859

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

6970
/** Closes socket
7071
*
7172
* @return NSAPI_ERROR_OK on success
7273
* NSAPI_ERROR_NO_SOCKET otherwise
7374
*/
7475

75-
virtual nsapi_error_t close();
76+
nsapi_error_t close() override;
7677

7778
/** Send data over a control plane cellular context.
7879
*
@@ -85,7 +86,7 @@ class CellularNonIPSocket : public Socket {
8586
* @return Number of sent bytes on success, negative error
8687
* code on failure.
8788
*/
88-
virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
89+
nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override;
8990

9091
/** Receive data from a socket.
9192
*
@@ -98,60 +99,60 @@ class CellularNonIPSocket : public Socket {
9899
* @return Number of received bytes on success, negative error
99100
* code on failure.
100101
*/
101-
virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
102+
nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override;
102103

103104
/** @copydoc Socket::set_blocking
104105
*/
105-
virtual void set_blocking(bool blocking);
106+
void set_blocking(bool blocking) override;
106107

107108
/** @copydoc Socket::set_timeout
108109
*/
109-
virtual void set_timeout(int timeout);
110+
void set_timeout(int timeout) override;
110111

111112
/** @copydoc Socket::sigio
112113
*/
113-
virtual void sigio(mbed::Callback<void()> func);
114+
void sigio(mbed::Callback<void()> func) override;
114115

115116
/// NOT APPLICABLE
116-
virtual nsapi_error_t connect(const SocketAddress &address);
117+
nsapi_error_t connect(const SocketAddress &address) override;
117118
/// NOT APPLICABLE
118-
virtual Socket *accept(nsapi_error_t *error = NULL);
119+
Socket *accept(nsapi_error_t *error = NULL) override;
119120
/// NOT APPLICABLE
120-
virtual nsapi_error_t listen(int backlog = 1);
121+
nsapi_error_t listen(int backlog = 1) override;
121122
/// NOT APPLICABLE
122-
virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
123+
nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen) override;
123124
/// NOT APPLICABLE
124-
virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
125+
nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen) override;
125126
/// NOT APPLICABLE
126-
virtual nsapi_error_t getpeername(SocketAddress *address);
127+
nsapi_error_t getpeername(SocketAddress *address) override;
127128
/// NOT APPLICABLE
128-
virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
129-
const void *data, nsapi_size_t size);
129+
nsapi_size_or_error_t sendto(const SocketAddress &address,
130+
const void *data, nsapi_size_t size) override;
130131
/// NOT APPLICABLE
131-
virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,
132-
void *data, nsapi_size_t size);
132+
nsapi_size_or_error_t recvfrom(SocketAddress *address,
133+
void *data, nsapi_size_t size) override;
133134
/// NOT APPLICABLE
134-
virtual nsapi_error_t bind(const SocketAddress &address);
135+
nsapi_error_t bind(const SocketAddress &address) override;
135136

136137
protected:
137-
virtual void event();
138+
void event();
138139

139-
uint32_t _timeout;
140+
uint32_t _timeout = osWaitForever;
140141
mbed::Callback<void()> _event;
141142
mbed::Callback<void()> _callback;
142143
rtos::EventFlags _event_flag;
143144
rtos::Mutex _lock;
144-
uint8_t _readers;
145-
uint8_t _writers;
146-
volatile unsigned _pending;
145+
uint8_t _readers = 0;
146+
uint8_t _writers = 0;
147+
core_util_atomic_flag _pending = CORE_UTIL_ATOMIC_FLAG_INIT;
147148

148149
// Event flags
149150
static const int READ_FLAG = 0x1u;
150151
static const int WRITE_FLAG = 0x2u;
151152
static const int FINISHED_FLAG = 0x3u;
152153

153154
static ControlPlane_netif *_cp_netif; // there can be only one Non-IP socket
154-
bool _opened;
155+
bool _opened = false;
155156
};
156157

157158
/** @}*/

features/netsocket/ControlPlane_netif.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ namespace mbed {
4242
*/
4343
class ControlPlane_netif {
4444
public:
45-
ControlPlane_netif() {}
46-
virtual ~ControlPlane_netif() {}
45+
virtual ~ControlPlane_netif() = default;
4746

4847
protected:
4948
friend class CellularNonIPSocket;

features/netsocket/DNS.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ class DNS {
140140
* @return NSAPI_ERROR_OK on success, negative error code on failure.
141141
*/
142142
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name = NULL) = 0;
143+
144+
protected:
145+
~DNS() = default;
143146
};
144147

145148
#endif

features/netsocket/DTLSSocket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DTLSSocket : public DTLSSocketWrapper {
4646

4747
/** Destroy the DTLSSocket and closes the transport.
4848
*/
49-
virtual ~DTLSSocket();
49+
~DTLSSocket() override;
5050

5151
/** Create a socket on a network interface.
5252
*
@@ -74,7 +74,7 @@ class DTLSSocket : public DTLSSocketWrapper {
7474
* @return NSAPI_ERROR_OK on success, negative error code on failure.
7575
* See @ref UDPSocket::open.
7676
*/
77-
virtual nsapi_error_t open(NetworkStack *stack)
77+
nsapi_error_t open(NetworkStack *stack)
7878
{
7979
return _udp_socket.open(stack);
8080
}

features/netsocket/DTLSSocketWrapper.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
#if defined(MBEDTLS_SSL_CLI_C)
2525

2626
DTLSSocketWrapper::DTLSSocketWrapper(Socket *transport, const char *hostname, control_transport control) :
27-
TLSSocketWrapper(transport, hostname, control),
28-
_int_ms_tick(0),
29-
_timer_event_id(0),
30-
_timer_expired(false)
27+
TLSSocketWrapper(transport, hostname, control)
3128
{
3229
mbedtls_ssl_conf_transport(get_ssl_config(), MBEDTLS_SSL_TRANSPORT_DATAGRAM);
3330
mbedtls_ssl_set_timer_cb(get_ssl_context(), this, timing_set_delay, timing_get_delay);

features/netsocket/DTLSSocketWrapper.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class DTLSSocketWrapper : public TLSSocketWrapper {
4343
static void timing_set_delay(void *ctx, uint32_t int_ms, uint32_t fin_ms);
4444
static int timing_get_delay(void *ctx);
4545
void timer_event();
46-
uint64_t _int_ms_tick;
47-
int _timer_event_id;
48-
bool _timer_expired : 1;
46+
uint64_t _int_ms_tick = 0;
47+
int _timer_event_id = 0;
48+
bool _timer_expired = false;
4949
};
5050

5151
#endif

features/netsocket/EMACInterface.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ using namespace mbed;
2121
/* Interface implementation */
2222
EMACInterface::EMACInterface(EMAC &emac, OnboardNetworkStack &stack) :
2323
_emac(emac),
24-
_stack(stack),
25-
_interface(NULL),
26-
_dhcp(true),
27-
_blocking(true),
28-
_ip_address(),
29-
_netmask(),
30-
_gateway()
24+
_stack(stack)
3125
{
3226
}
3327

0 commit comments

Comments
 (0)