Skip to content

Commit 5b63cbb

Browse files
committed
Added primitive nsapi_stack_t and nsapi_stack_api_t structures
Provides a primitive structure for explicitly instantiating network stack structures/vtables. - Avoids reoccuring issue with non-gced vtables - Provides thick api layer for additional nsapi features - Provides more explicit seperation between implementation and user api Now implementors have two options: 1. Extend NetworkStack and implement all of the abstract member functions 2. Fill out a nsapi_stack_api_t and provide a NetworkStack with nsapi_create_stack option 1 provides an easy route for porting single drivers such as the esp8266 or the c027 and can easily benefit from other C++ classes. option 2 provides an easy route for porting full network-stacks such as lwip or nanostack, which provide their own host of networking utilities and only need a minimal socket interface.
1 parent 7ab9f4c commit 5b63cbb

File tree

3 files changed

+499
-0
lines changed

3 files changed

+499
-0
lines changed

NetworkStack.cpp

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

17+
#include "NetworkStack.h"
1718
#include "DnsQuery.h"
1819
#include "mbed.h"
20+
#include "stddef.h"
21+
#include <new>
1922

23+
24+
// Default NetworkStack operations
2025
int NetworkStack::gethostbyname(SocketAddress *address, const char *name)
2126
{
2227
char buffer[NSAPI_IP_SIZE];
@@ -48,3 +53,199 @@ int NetworkStack::getsockopt(void *handle, int level, int optname, void *optval,
4853
{
4954
return NSAPI_ERROR_UNSUPPORTED;
5055
}
56+
57+
58+
// NetworkStackWrapper class for encapsulating the raw nsapi_stack structure
59+
class NetworkStackWrapper : public NetworkStack
60+
{
61+
private:
62+
inline nsapi_stack_t *_stack()
63+
{
64+
return reinterpret_cast<nsapi_stack_t *>(
65+
reinterpret_cast<uint8_t *>(this)
66+
- offsetof(nsapi_stack_t, _stack_buffer));
67+
}
68+
69+
inline const nsapi_stack_api_t *_stack_api()
70+
{
71+
return _stack()->stack_api;
72+
}
73+
74+
public:
75+
virtual const char *get_ip_address()
76+
{
77+
if (!_stack_api()->get_ip_address) {
78+
return 0;
79+
}
80+
81+
static uint8_t buffer[sizeof(SocketAddress)];
82+
SocketAddress *address = new (buffer) SocketAddress(_stack_api()->get_ip_address(_stack()));
83+
return address->get_ip_address();
84+
}
85+
86+
virtual int gethostbyname(SocketAddress *address, const char *name)
87+
{
88+
if (!_stack_api()->gethostbyname) {
89+
return NetworkStack::gethostbyname(address, name);
90+
}
91+
92+
nsapi_addr_t addr = {NSAPI_IPv4, 0};
93+
int err = _stack_api()->gethostbyname(_stack(), &addr, name);
94+
address->set_addr(addr);
95+
return err;
96+
}
97+
98+
virtual int setstackopt(int level, int optname, const void *optval, unsigned optlen)
99+
{
100+
if (!_stack_api()->setstackopt) {
101+
return NSAPI_ERROR_UNSUPPORTED;
102+
}
103+
104+
return _stack_api()->setstackopt(_stack(), level, optname, optval, optlen);
105+
}
106+
107+
virtual int getstackopt(int level, int optname, void *optval, unsigned *optlen)
108+
{
109+
if (!_stack_api()->getstackopt) {
110+
return NSAPI_ERROR_UNSUPPORTED;
111+
}
112+
113+
return _stack_api()->getstackopt(_stack(), level, optname, optval, optlen);
114+
}
115+
116+
protected:
117+
virtual int socket_open(nsapi_socket_t *socket, nsapi_protocol_t proto)
118+
{
119+
if (!_stack_api()->socket_open) {
120+
return NSAPI_ERROR_UNSUPPORTED;
121+
}
122+
123+
return _stack_api()->socket_open(_stack(), socket, proto);
124+
}
125+
126+
virtual int socket_close(nsapi_socket_t socket)
127+
{
128+
if (!_stack_api()->socket_close) {
129+
return NSAPI_ERROR_UNSUPPORTED;
130+
}
131+
132+
return _stack_api()->socket_close(_stack(), socket);
133+
}
134+
135+
virtual int socket_bind(nsapi_socket_t socket, const SocketAddress &address)
136+
{
137+
if (!_stack_api()->socket_bind) {
138+
return NSAPI_ERROR_UNSUPPORTED;
139+
}
140+
141+
return _stack_api()->socket_bind(_stack(), socket, address.get_addr(), address.get_port());
142+
}
143+
144+
virtual int socket_listen(nsapi_socket_t socket, int backlog)
145+
{
146+
if (!_stack_api()->socket_listen) {
147+
return NSAPI_ERROR_UNSUPPORTED;
148+
}
149+
150+
return _stack_api()->socket_listen(_stack(), socket, backlog);
151+
}
152+
153+
virtual int socket_connect(nsapi_socket_t socket, const SocketAddress &address)
154+
{
155+
if (!_stack_api()->socket_connect) {
156+
return NSAPI_ERROR_UNSUPPORTED;
157+
}
158+
159+
return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port());
160+
}
161+
162+
virtual int socket_accept(nsapi_socket_t *socket, nsapi_socket_t server)
163+
{
164+
if (!_stack_api()->socket_accept) {
165+
return NSAPI_ERROR_UNSUPPORTED;
166+
}
167+
168+
return _stack_api()->socket_accept(_stack(), socket, server);
169+
}
170+
171+
virtual int socket_send(nsapi_socket_t socket, const void *data, unsigned size)
172+
{
173+
if (!_stack_api()->socket_send) {
174+
return NSAPI_ERROR_UNSUPPORTED;
175+
}
176+
177+
return _stack_api()->socket_send(_stack(), socket, data, size);
178+
}
179+
180+
virtual int socket_recv(nsapi_socket_t socket, void *data, unsigned size)
181+
{
182+
if (!_stack_api()->socket_recv) {
183+
return NSAPI_ERROR_UNSUPPORTED;
184+
}
185+
186+
return _stack_api()->socket_recv(_stack(), socket, data, size);
187+
}
188+
189+
virtual int socket_sendto(nsapi_socket_t socket, const SocketAddress &address, const void *data, unsigned size)
190+
{
191+
if (!_stack_api()->socket_sendto) {
192+
return NSAPI_ERROR_UNSUPPORTED;
193+
}
194+
195+
return _stack_api()->socket_sendto(_stack(), socket, address.get_addr(), address.get_port(), data, size);
196+
}
197+
198+
virtual int socket_recvfrom(nsapi_socket_t socket, SocketAddress *address, void *data, unsigned size)
199+
{
200+
if (!_stack_api()->socket_recvfrom) {
201+
return NSAPI_ERROR_UNSUPPORTED;
202+
}
203+
204+
nsapi_addr_t addr = {NSAPI_IPv4, 0};
205+
uint16_t port = 0;
206+
207+
int err = _stack_api()->socket_recvfrom(_stack(), socket, &addr, &port, data, size);
208+
209+
if (address) {
210+
address->set_addr(addr);
211+
address->set_port(port);
212+
}
213+
214+
return err;
215+
}
216+
217+
virtual void socket_attach(nsapi_socket_t socket, void (*callback)(void *), void *data)
218+
{
219+
if (!_stack_api()->socket_attach) {
220+
return;
221+
}
222+
223+
return _stack_api()->socket_attach(_stack(), socket, callback, data);
224+
}
225+
226+
virtual int setsockopt(nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen)
227+
{
228+
if (!_stack_api()->setsockopt) {
229+
return NSAPI_ERROR_UNSUPPORTED;
230+
}
231+
232+
return _stack_api()->setsockopt(_stack(), socket, level, optname, optval, optlen);
233+
}
234+
235+
virtual int getsockopt(nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen)
236+
{
237+
if (!_stack_api()->getsockopt) {
238+
return NSAPI_ERROR_UNSUPPORTED;
239+
}
240+
241+
return _stack_api()->getsockopt(_stack(), socket, level, optname, optval, optlen);
242+
}
243+
};
244+
245+
246+
// Conversion function for network stacks
247+
NetworkStack *nsapi_create_stack(nsapi_stack_t *stack)
248+
{
249+
MBED_ASSERT(sizeof stack->_stack_buffer >= sizeof(NetworkStackWrapper));
250+
return new (stack->_stack_buffer) NetworkStackWrapper;
251+
}

NetworkStack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,12 @@ class NetworkStack
278278
};
279279

280280

281+
/** Convert a raw nsapi_stack_t object into a C++ NetworkStack object
282+
*
283+
* @param stack Reference to a raw nsapi_stack_t object
284+
* @return Reference to the underlying network stack
285+
*/
286+
NetworkStack *nsapi_create_stack(nsapi_stack_t *stack);
287+
288+
281289
#endif

0 commit comments

Comments
 (0)