Skip to content

Commit 90cd978

Browse files
c1728p9geky
authored andcommitted
Separate interface from stack in NSAPI
This patch consists of: -Add NetworkInterface to wrap objects bound to a stack and update socket code to handle this in addition to NetworkStacks -Update MeshInterface to inherit from NetworkInterface -Update NanostackInterface so it only inherits from NetworkStack -Add MeshInterfaceNanostack and update LoWPANNDInterface and ThreadInterface to inherit from this
1 parent 3ee8e30 commit 90cd978

13 files changed

+198
-33
lines changed

EthernetInterface.h renamed to EthInterface.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* EthernetInterface
1+
/* EthInterface
22
* Copyright (c) 2015 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,16 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef ETHERNET_INTERFACE_H
18-
#define ETHERNET_INTERFACE_H
17+
#ifndef ETH_INTERFACE_H
18+
#define ETH_INTERFACE_H
1919

20-
#include "NetworkStack.h"
20+
#include "NetworkInterface.h"
2121

22-
/** EthernetInterface class
22+
/** EthInterface class
2323
*
2424
* Common interface that is shared between ethernet hardware.
2525
*/
26-
class EthernetInterface
26+
class EthInterface : public NetworkInterface
2727
{
2828
public:
2929
/** Start the interface

MeshInterface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
#ifndef MESH_INTERFACE_H
1818
#define MESH_INTERFACE_H
1919

20-
#include "NetworkStack.h"
20+
#include "NetworkInterface.h"
2121

2222
/** MeshInterface class
2323
*
2424
* Common interface that is shared between mesh hardware
2525
*/
26-
class MeshInterface
26+
class MeshInterface : public NetworkInterface
2727
{
2828
public:
2929
/** Start the interface

NetworkInterface.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* NetworkStack
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef NETWORK_INTERFACE_H
18+
#define NETWORK_INTERFACE_H
19+
20+
#include "mbed.h"
21+
#include "SocketAddress.h"
22+
23+
class NetworkInterface {
24+
public:
25+
virtual ~NetworkInterface() {};
26+
NetworkInterface() {}
27+
28+
/** Get the internally stored IP address
29+
/return IP address of the interface or null if not yet connected
30+
*/
31+
virtual const char *get_ip_address() = 0;
32+
protected:
33+
friend class Socket;
34+
friend class UDPSocket;
35+
friend class TCPSocket;
36+
friend class TCPServer;
37+
friend class SocketAddress;
38+
virtual NetworkStack * get_stack(void) = 0;
39+
};
40+
41+
#endif

NetworkStack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef NETWORK_INTERFACE_H
18-
#define NETWORK_INTERFACE_H
17+
#ifndef NETWORK_STACK_H
18+
#define NETWORK_STACK_H
1919

2020
#include "mbed.h"
2121
#include "SocketAddress.h"

SocketAddress.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "SocketAddress.h"
18+
#include "NetworkInterface.h"
1819
#include "NetworkStack.h"
1920
#include <string.h>
2021
#include "mbed.h"
@@ -144,28 +145,12 @@ static void ipv6_to_address(char *addr, const uint8_t *bytes)
144145

145146
SocketAddress::SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
146147
{
147-
memset(&_ip_address, 0, sizeof _ip_address);
148+
_SocketAddress(iface, host, port);
149+
}
148150

149-
// Check for valid IP addresses
150-
if (host && ipv4_is_valid(host)) {
151-
_ip_version = NSAPI_IPv4;
152-
ipv4_from_address(_ip_bytes, host);
153-
set_port(port);
154-
} else if (host && ipv6_is_valid(host)) {
155-
_ip_version = NSAPI_IPv6;
156-
ipv6_from_address(_ip_bytes, host);
157-
set_port(port);
158-
} else {
159-
// DNS lookup
160-
int err = iface->gethostbyname(this, host);
161-
if (!err) {
162-
set_port(port);
163-
} else {
164-
_ip_version = NSAPI_IPv4;
165-
memset(_ip_bytes, 0, NSAPI_IPv4_BYTES);
166-
set_port(0);
167-
}
168-
}
151+
SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port)
152+
{
153+
_SocketAddress(iface->get_stack(), host, port);
169154
}
170155

171156
SocketAddress::SocketAddress(const char *addr, uint16_t port)
@@ -273,3 +258,29 @@ SocketAddress::operator bool() const
273258

274259
return false;
275260
}
261+
262+
void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
263+
{
264+
memset(&_ip_address, 0, sizeof _ip_address);
265+
266+
// Check for valid IP addresses
267+
if (host && ipv4_is_valid(host)) {
268+
_ip_version = NSAPI_IPv4;
269+
ipv4_from_address(_ip_bytes, host);
270+
set_port(port);
271+
} else if (host && ipv6_is_valid(host)) {
272+
_ip_version = NSAPI_IPv6;
273+
ipv6_from_address(_ip_bytes, host);
274+
set_port(port);
275+
} else {
276+
// DNS lookup
277+
int err = iface->gethostbyname(this, host);
278+
if (!err) {
279+
set_port(port);
280+
} else {
281+
_ip_version = NSAPI_IPv4;
282+
memset(_ip_bytes, 0, NSAPI_IPv4_BYTES);
283+
set_port(0);
284+
}
285+
}
286+
}

SocketAddress.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ enum nsapi_version_t {
6565

6666
// Predeclared classes
6767
class NetworkStack;
68+
class NetworkInterface;
6869

6970

7071
/** SocketAddress class
@@ -86,6 +87,19 @@ class SocketAddress {
8687
*/
8788
SocketAddress(NetworkStack *iface, const char *host, uint16_t port = 0);
8889

90+
/** Create a SocketAddress from a hostname and port
91+
*
92+
* The hostname may be either a domain name or an IP address. If the
93+
* hostname is an IP address, no network transactions will be performed.
94+
*
95+
* On failure, the IP address and port will be set to zero
96+
*
97+
* @param iface Network interface to use for DNS resolution
98+
* @param host Hostname to resolve
99+
* @param port Optional 16-bit port
100+
*/
101+
SocketAddress(NetworkInterface *iface, const char *host, uint16_t port = 0);
102+
89103
/** Create a SocketAddress from an IP address and port
90104
*
91105
* @param host Null-terminated representation of the IP address
@@ -157,6 +171,7 @@ class SocketAddress {
157171
operator bool() const;
158172

159173
private:
174+
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
160175
char _ip_address[NSAPI_IP_SIZE];
161176
uint8_t _ip_bytes[NSAPI_IP_BYTES];
162177
nsapi_version_t _ip_version;

TCPServer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ TCPServer::TCPServer(NetworkStack *iface)
2828
open(iface);
2929
}
3030

31+
TCPServer::TCPServer(NetworkInterface *iface)
32+
: _pending(0), _accept_sem(0)
33+
{
34+
open(iface->get_stack());
35+
}
36+
3137
TCPServer::~TCPServer()
3238
{
3339
close();
@@ -38,6 +44,11 @@ int TCPServer::open(NetworkStack *iface)
3844
return Socket::open(iface, NSAPI_TCP);
3945
}
4046

47+
int TCPServer::open(NetworkInterface *iface)
48+
{
49+
return TCPServer::open(iface->get_stack());
50+
}
51+
4152
int TCPServer::listen(int backlog)
4253
{
4354
_lock.lock();

TCPServer.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "Socket.h"
2121
#include "TCPSocket.h"
22+
#include "NetworkInterface.h"
2223
#include "NetworkStack.h"
2324
#include "Semaphore.h"
2425

@@ -46,6 +47,15 @@ class TCPServer : public Socket {
4647
*/
4748
TCPServer(NetworkStack *iface);
4849

50+
/** Create a socket on a network interface
51+
*
52+
* Creates and opens a socket on the network stack of the given
53+
* network interface.
54+
*
55+
* @param iface Network interface as target for socket
56+
*/
57+
TCPServer(NetworkInterface *iface);
58+
4959
/** Opens a socket
5060
*
5161
* Creates a network socket on the specified network stack.
@@ -56,6 +66,17 @@ class TCPServer : public Socket {
5666
*/
5767
virtual int open(NetworkStack *iface);
5868

69+
/** Opens a socket
70+
*
71+
* Creates a network socket on the network stack of the given
72+
* network interface. Not needed if stack is passed to the
73+
* socket's constructor.
74+
*
75+
* @param iface Network interface as target for socket
76+
* @return 0 on success, negative error code on failure
77+
*/
78+
virtual int open(NetworkInterface *iface);
79+
5980
/** Listen for connections on a TCP socket
6081
*
6182
* Marks the socket as a passive socket that can be used to accept

TCPSocket.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ TCPSocket::TCPSocket(NetworkStack *iface)
2929
open(iface);
3030
}
3131

32+
TCPSocket::TCPSocket(NetworkInterface *iface)
33+
: _pending(0), _read_sem(0), _write_sem(0)
34+
{
35+
// TCPSocket::open is thread safe
36+
open(iface->get_stack());
37+
}
38+
3239
TCPSocket::~TCPSocket()
3340
{
3441
close();
@@ -40,6 +47,12 @@ int TCPSocket::open(NetworkStack *iface)
4047
return Socket::open(iface, NSAPI_TCP);
4148
}
4249

50+
int TCPSocket::open(NetworkInterface *iface)
51+
{
52+
// Socket::open is thread safe
53+
return TCPSocket::open(iface->get_stack());
54+
}
55+
4356
int TCPSocket::connect(const SocketAddress &addr)
4457
{
4558
_lock.lock();

TCPSocket.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define TCPSOCKET_H
1919

2020
#include "Socket.h"
21+
#include "NetworkInterface.h"
2122
#include "NetworkStack.h"
2223
#include "Semaphore.h"
2324

@@ -45,6 +46,15 @@ class TCPSocket : public Socket {
4546
*/
4647
TCPSocket(NetworkStack *iface);
4748

49+
/** Create a socket on a network interface
50+
*
51+
* Creates and opens a socket on the network stack of the given
52+
* network interface.
53+
*
54+
* @param iface Network interface as target for socket
55+
*/
56+
TCPSocket(NetworkInterface *iface);
57+
4858
/** Opens a socket
4959
*
5060
* Creates a network socket on the specified network stack.
@@ -55,6 +65,17 @@ class TCPSocket : public Socket {
5565
*/
5666
virtual int open(NetworkStack *iface);
5767

68+
/** Opens a socket
69+
*
70+
* Creates a network socket on the network stack of the given
71+
* network interface. Not needed if stack is passed to the
72+
* socket's constructor.
73+
*
74+
* @param iface Network interface as target for socket
75+
* @return 0 on success, negative error code on failure
76+
*/
77+
virtual int open(NetworkInterface *iface);
78+
5879
/** Connects TCP socket to a remote host
5980
*
6081
* Initiates a connection to a remote server specified by either

0 commit comments

Comments
 (0)