Skip to content

Commit 97380a7

Browse files
committed
Restructured nsapi headers
- Provide nsapi.h as central entry point - Provide nsapi_types.h as c-compatible destination for nsapi types - Standardize include paths
1 parent 04e4d9f commit 97380a7

13 files changed

+237
-134
lines changed

CellularInterface.h

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

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

22+
2223
/** CellularInterface class
2324
*
2425
* Common interface that is shared between ethernet hardware
2526
*/
26-
class CellularInterface
27+
class CellularInterface : public NetworkInterface
2728
{
2829
public:
2930
/** Start the interface
@@ -47,5 +48,6 @@ class CellularInterface
4748
*/
4849
virtual const char *get_mac_address() = 0;
4950
};
51+
5052

5153
#endif

EthInterface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#ifndef ETH_INTERFACE_H
1818
#define ETH_INTERFACE_H
1919

20-
#include "NetworkInterface.h"
20+
#include "NetworkSocketAPI/NetworkInterface.h"
21+
2122

2223
/** EthInterface class
2324
*
@@ -45,4 +46,5 @@ class EthInterface : public NetworkInterface
4546
virtual const char *get_mac_address() = 0;
4647
};
4748

49+
4850
#endif

MeshInterface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#ifndef MESH_INTERFACE_H
1818
#define MESH_INTERFACE_H
1919

20-
#include "NetworkInterface.h"
20+
#include "NetworkSocketAPI/NetworkInterface.h"
21+
2122

2223
/** MeshInterface class
2324
*
@@ -45,4 +46,5 @@ class MeshInterface : public NetworkInterface
4546
virtual const char *get_mac_address() = 0;
4647
};
4748

49+
4850
#endif

NetworkInterface.h

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

20-
#include "mbed.h"
21-
#include "SocketAddress.h"
20+
#include "NetworkSocketAPI/NetworkStack.h"
21+
2222

2323
class NetworkInterface {
2424
public:
@@ -38,4 +38,5 @@ class NetworkInterface {
3838
virtual NetworkStack * get_stack(void) = 0;
3939
};
4040

41+
4142
#endif

NetworkStack.h

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,8 @@
1717
#ifndef NETWORK_STACK_H
1818
#define NETWORK_STACK_H
1919

20-
#include "mbed.h"
21-
#include "SocketAddress.h"
22-
23-
24-
/** Enum of standardized error codes
25-
*
26-
* Valid error codes have negative values and may
27-
* be returned by any network operation.
28-
*
29-
* @enum nsapi_error_t
30-
*/
31-
enum nsapi_error_t {
32-
NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
33-
NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
34-
NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
35-
NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */
36-
NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */
37-
NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */
38-
NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
39-
NSAPI_ERROR_DNS_FAILURE = -3008, /*!< DNS failed to complete successfully */
40-
NSAPI_ERROR_DHCP_FAILURE = -3009, /*!< DHCP failed to complete successfully */
41-
NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point faield */
42-
NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network procesor */
43-
};
44-
45-
/** Enum of socket protocols
46-
*
47-
* The socket protocol specifies a particular protocol to
48-
* be used with a newly created socket.
49-
*
50-
* @enum nsapi_protocol_t
51-
*/
52-
enum nsapi_protocol_t {
53-
NSAPI_TCP, /*!< Socket is of TCP type */
54-
NSAPI_UDP, /*!< Socket is of UDP type */
55-
};
56-
57-
/* Enum of standardized stack option levels
58-
*
59-
* @enum nsapi_level_t
60-
*/
61-
enum nsapi_level_t {
62-
NSAPI_STACK, /*!< Stack option level */
63-
NSAPI_SOCKET, /*!< Socket option level */
64-
};
65-
66-
/* Enum of standardized stack options
67-
*
68-
* These options may not be supported on all stacks, in which
69-
* case NSAPI_ERROR_UNSUPPORTED may be returned from setsockopt.
70-
*
71-
* @enum nsapi_option_t
72-
*/
73-
enum nsapi_option_t {
74-
NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */
75-
NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */
76-
NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */
77-
NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */
78-
NSAPI_LINGER, /*!< Keeps close from returning until queues empty */
79-
NSAPI_SNDBUF, /*!< Sets send buffer size */
80-
NSAPI_RCVBUF, /*!< Sets recv buffer size */
81-
};
20+
#include "nsapi_types.h"
21+
#include "NetworkSocketAPI/SocketAddress.h"
8222

8323

8424
/** NetworkStack class
@@ -337,4 +277,5 @@ class NetworkStack
337277
virtual int getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen);
338278
};
339279

280+
340281
#endif

Socket.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717
#ifndef SOCKET_H
1818
#define SOCKET_H
1919

20-
#include "SocketAddress.h"
21-
#include "NetworkStack.h"
22-
#include "Mutex.h"
20+
#include "NetworkSocketAPI/SocketAddress.h"
21+
#include "NetworkSocketAPI/NetworkStack.h"
22+
#include "rtos/Mutex.h"
23+
#include "Callback.h"
24+
25+
#ifndef NSAPI_NO_INCLUDE_MBED
26+
#include "mbed.h" // needed for backwards compatability
27+
#endif
28+
2329

2430
/** Abstract socket class
2531
*/
@@ -147,7 +153,7 @@ class Socket {
147153
*
148154
* @param func Function to call on state change
149155
*/
150-
void attach(Callback<void()> func);
156+
void attach(mbed::Callback<void()> func);
151157

152158
/** Register a callback on state change of the socket
153159
*
@@ -163,7 +169,7 @@ class Socket {
163169
*/
164170
template <typename T, typename M>
165171
void attach(T *obj, M method) {
166-
attach(Callback<void()>(obj, method));
172+
attach(mbed::Callback<void()>(obj, method));
167173
}
168174

169175
protected:
@@ -174,9 +180,10 @@ class Socket {
174180
NetworkStack *_iface;
175181
void *_socket;
176182
uint32_t _timeout;
177-
Callback<void()> _event;
178-
Callback<void()> _callback;
183+
mbed::Callback<void()> _event;
184+
mbed::Callback<void()> _callback;
179185
rtos::Mutex _lock;
180186
};
181187

188+
182189
#endif

SocketAddress.h

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,7 @@
1717
#ifndef SOCKET_ADDRESS_H
1818
#define SOCKET_ADDRESS_H
1919

20-
#include <stdint.h>
21-
22-
23-
/** Maximum size of IP address representation
24-
*/
25-
#define NSAPI_IP_SIZE NSAPI_IPv6_SIZE
26-
27-
/** Maximum number of bytes for IP address
28-
*/
29-
#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES
30-
31-
/** Maximum size of MAC address representation
32-
*/
33-
#define NSAPI_MAC_SIZE 18
34-
35-
/** Maximum number of bytes for MAC address
36-
*/
37-
#define NSAPI_MAC_BYTES 6
38-
39-
/** Enum of IP address versions
40-
*
41-
* The IP version specifies the type of an IP address.
42-
*
43-
* @enum nsapi_version_t
44-
*/
45-
enum nsapi_version_t {
46-
NSAPI_IPv4, /*!< Address is IPv4 */
47-
NSAPI_IPv6, /*!< Address is IPv6 */
48-
};
49-
50-
/** Size of IPv4 representation
51-
*/
52-
#define NSAPI_IPv4_SIZE 16
53-
54-
/** Number of bytes in IPv4 address
55-
*/
56-
#define NSAPI_IPv4_BYTES 4
57-
58-
/** Size of IPv6 representation
59-
*/
60-
#define NSAPI_IPv6_SIZE 40
61-
62-
/** Number of bytes in IPv6 address
63-
*/
64-
#define NSAPI_IPv6_BYTES 16
20+
#include "nsapi_types.h"
6521

6622
// Predeclared classes
6723
class NetworkStack;
@@ -178,4 +134,5 @@ class SocketAddress {
178134
uint16_t _port;
179135
};
180136

137+
181138
#endif

TCPServer.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#ifndef TCPSERVER_H
1818
#define TCPSERVER_H
1919

20-
#include "Socket.h"
21-
#include "TCPSocket.h"
22-
#include "NetworkInterface.h"
23-
#include "NetworkStack.h"
24-
#include "Semaphore.h"
20+
#include "NetworkSocketAPI/Socket.h"
21+
#include "NetworkSocketAPI/TCPSocket.h"
22+
#include "NetworkSocketAPI/NetworkStack.h"
23+
#include "NetworkSocketAPI/NetworkInterface.h"
24+
#include "rtos/Semaphore.h"
25+
2526

2627
/** TCP socket server
2728
*/
@@ -108,4 +109,5 @@ class TCPServer : public Socket {
108109
rtos::Semaphore _accept_sem;
109110
};
110111

112+
111113
#endif

TCPSocket.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
#ifndef TCPSOCKET_H
1818
#define TCPSOCKET_H
1919

20-
#include "Socket.h"
21-
#include "NetworkInterface.h"
22-
#include "NetworkStack.h"
23-
#include "Semaphore.h"
20+
#include "NetworkSocketAPI/Socket.h"
21+
#include "NetworkSocketAPI/NetworkStack.h"
22+
#include "NetworkSocketAPI/NetworkInterface.h"
23+
#include "rtos/Semaphore.h"
24+
2425

2526
/** TCP socket connection
2627
*/
@@ -139,4 +140,5 @@ class TCPSocket : public Socket {
139140
friend class TCPServer;
140141
};
141142

143+
142144
#endif

UDPSocket.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
#ifndef UDPSOCKET_H
1818
#define UDPSOCKET_H
1919

20-
#include "Socket.h"
21-
#include "NetworkInterface.h"
22-
#include "NetworkStack.h"
23-
#include "Semaphore.h"
20+
#include "NetworkSocketAPI/Socket.h"
21+
#include "NetworkSocketAPI/NetworkStack.h"
22+
#include "NetworkSocketAPI/NetworkInterface.h"
23+
#include "rtos/Semaphore.h"
24+
2425

2526
/** UDP socket
2627
*/
@@ -137,4 +138,5 @@ class UDPSocket : public Socket {
137138
bool _write_in_progress;
138139
};
139140

141+
140142
#endif

WiFiInterface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#ifndef WIFI_INTERFACE_H
1818
#define WIFI_INTERFACE_H
1919

20-
#include "NetworkInterface.h"
20+
#include "NetworkSocketAPI/NetworkInterface.h"
21+
2122

2223
/** Enum of WiFi encryption types
2324
*
@@ -65,4 +66,5 @@ class WiFiInterface: public NetworkInterface
6566
virtual const char *get_mac_address() = 0;
6667
};
6768

69+
6870
#endif

nsapi.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* nsapi.h - The network socket API
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 NSAPI_H
18+
#define NSAPI_H
19+
20+
21+
// entry point for nsapi types
22+
#include "nsapi_types.h"
23+
24+
// disable bug-compatible mbed inclusion
25+
#define NSAPI_NO_INCLUDE_MBED
26+
27+
#ifdef __cplusplus
28+
29+
// entry point for C++ api
30+
#include "NetworkSocketAPI/SocketAddress.h"
31+
#include "NetworkSocketAPI/NetworkStack.h"
32+
33+
#include "NetworkSocketAPI/NetworkInterface.h"
34+
#include "NetworkSocketAPI/EthInterface.h"
35+
#include "NetworkSocketAPI/WiFiInterface.h"
36+
#include "NetworkSocketAPI/CellularInterface.h"
37+
#include "NetworkSocketAPI/MeshInterface.h"
38+
39+
#include "NetworkSocketAPI/Socket.h"
40+
#include "NetworkSocketAPI/UDPSocket.h"
41+
#include "NetworkSocketAPI/TCPSocket.h"
42+
#include "NetworkSocketAPI/TCPServer.h"
43+
44+
#endif
45+
46+
47+
#endif

0 commit comments

Comments
 (0)