diff --git a/CellularInterface.h b/CellularInterface.h index 0f9c28b840..a2b303e228 100644 --- a/CellularInterface.h +++ b/CellularInterface.h @@ -17,13 +17,14 @@ #ifndef CELLULAR_INTERFACE_H #define CELLULAR_INTERFACE_H -#include "NetworkStack.h" +#include "NetworkSocketAPI/NetworkInterface.h" + /** CellularInterface class * * Common interface that is shared between ethernet hardware */ -class CellularInterface +class CellularInterface : public NetworkInterface { public: /** Start the interface @@ -47,5 +48,6 @@ public: */ virtual const char *get_mac_address() = 0; }; + #endif diff --git a/EthInterface.h b/EthInterface.h index e16a71b128..1a0066c685 100644 --- a/EthInterface.h +++ b/EthInterface.h @@ -17,7 +17,8 @@ #ifndef ETH_INTERFACE_H #define ETH_INTERFACE_H -#include "NetworkInterface.h" +#include "NetworkSocketAPI/NetworkInterface.h" + /** EthInterface class * @@ -45,4 +46,5 @@ public: virtual const char *get_mac_address() = 0; }; + #endif diff --git a/MeshInterface.h b/MeshInterface.h index 54d3654b45..6aac499dd2 100644 --- a/MeshInterface.h +++ b/MeshInterface.h @@ -17,7 +17,8 @@ #ifndef MESH_INTERFACE_H #define MESH_INTERFACE_H -#include "NetworkInterface.h" +#include "NetworkSocketAPI/NetworkInterface.h" + /** MeshInterface class * @@ -45,4 +46,5 @@ public: virtual const char *get_mac_address() = 0; }; + #endif diff --git a/NetworkInterface.h b/NetworkInterface.h index af97ee7ffd..6a8b3ea6b4 100644 --- a/NetworkInterface.h +++ b/NetworkInterface.h @@ -17,8 +17,8 @@ #ifndef NETWORK_INTERFACE_H #define NETWORK_INTERFACE_H -#include "mbed.h" -#include "SocketAddress.h" +#include "NetworkSocketAPI/NetworkStack.h" + class NetworkInterface { public: @@ -38,4 +38,5 @@ protected: virtual NetworkStack * get_stack(void) = 0; }; + #endif diff --git a/NetworkStack.h b/NetworkStack.h index 2c5b0f9c5a..dc612a624e 100644 --- a/NetworkStack.h +++ b/NetworkStack.h @@ -17,68 +17,8 @@ #ifndef NETWORK_STACK_H #define NETWORK_STACK_H -#include "mbed.h" -#include "SocketAddress.h" - - -/** Enum of standardized error codes - * - * Valid error codes have negative values and may - * be returned by any network operation. - * - * @enum nsapi_error_t - */ -enum nsapi_error_t { - NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ - NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ - NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ - NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */ - NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ - NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ - NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ - NSAPI_ERROR_DNS_FAILURE = -3008, /*!< DNS failed to complete successfully */ - NSAPI_ERROR_DHCP_FAILURE = -3009, /*!< DHCP failed to complete successfully */ - NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point faield */ - NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network procesor */ -}; - -/** Enum of socket protocols - * - * The socket protocol specifies a particular protocol to - * be used with a newly created socket. - * - * @enum nsapi_protocol_t - */ -enum nsapi_protocol_t { - NSAPI_TCP, /*!< Socket is of TCP type */ - NSAPI_UDP, /*!< Socket is of UDP type */ -}; - -/* Enum of standardized stack option levels - * - * @enum nsapi_level_t - */ -enum nsapi_level_t { - NSAPI_STACK, /*!< Stack option level */ - NSAPI_SOCKET, /*!< Socket option level */ -}; - -/* Enum of standardized stack options - * - * These options may not be supported on all stacks, in which - * case NSAPI_ERROR_UNSUPPORTED may be returned from setsockopt. - * - * @enum nsapi_option_t - */ -enum nsapi_option_t { - NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */ - NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */ - NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */ - NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */ - NSAPI_LINGER, /*!< Keeps close from returning until queues empty */ - NSAPI_SNDBUF, /*!< Sets send buffer size */ - NSAPI_RCVBUF, /*!< Sets recv buffer size */ -}; +#include "nsapi_types.h" +#include "NetworkSocketAPI/SocketAddress.h" /** NetworkStack class @@ -337,4 +277,5 @@ protected: virtual int getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen); }; + #endif diff --git a/Socket.h b/Socket.h index 0050cd2dd2..358a4bb874 100644 --- a/Socket.h +++ b/Socket.h @@ -17,9 +17,15 @@ #ifndef SOCKET_H #define SOCKET_H -#include "SocketAddress.h" -#include "NetworkStack.h" -#include "Mutex.h" +#include "NetworkSocketAPI/SocketAddress.h" +#include "NetworkSocketAPI/NetworkStack.h" +#include "rtos/Mutex.h" +#include "Callback.h" + +#ifndef NSAPI_NO_INCLUDE_MBED +#include "mbed.h" // needed for backwards compatability +#endif + /** Abstract socket class */ @@ -147,7 +153,7 @@ public: * * @param func Function to call on state change */ - void attach(Callback func); + void attach(mbed::Callback func); /** Register a callback on state change of the socket * @@ -163,7 +169,7 @@ public: */ template void attach(T *obj, M method) { - attach(Callback(obj, method)); + attach(mbed::Callback(obj, method)); } protected: @@ -174,9 +180,10 @@ protected: NetworkStack *_iface; void *_socket; uint32_t _timeout; - Callback _event; - Callback _callback; + mbed::Callback _event; + mbed::Callback _callback; rtos::Mutex _lock; }; + #endif diff --git a/SocketAddress.h b/SocketAddress.h index f87653e987..049b3a79b8 100644 --- a/SocketAddress.h +++ b/SocketAddress.h @@ -17,51 +17,7 @@ #ifndef SOCKET_ADDRESS_H #define SOCKET_ADDRESS_H -#include - - -/** Maximum size of IP address representation - */ -#define NSAPI_IP_SIZE NSAPI_IPv6_SIZE - -/** Maximum number of bytes for IP address - */ -#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES - -/** Maximum size of MAC address representation - */ -#define NSAPI_MAC_SIZE 18 - -/** Maximum number of bytes for MAC address - */ -#define NSAPI_MAC_BYTES 6 - -/** Enum of IP address versions - * - * The IP version specifies the type of an IP address. - * - * @enum nsapi_version_t - */ -enum nsapi_version_t { - NSAPI_IPv4, /*!< Address is IPv4 */ - NSAPI_IPv6, /*!< Address is IPv6 */ -}; - -/** Size of IPv4 representation - */ -#define NSAPI_IPv4_SIZE 16 - -/** Number of bytes in IPv4 address - */ -#define NSAPI_IPv4_BYTES 4 - -/** Size of IPv6 representation - */ -#define NSAPI_IPv6_SIZE 40 - -/** Number of bytes in IPv6 address - */ -#define NSAPI_IPv6_BYTES 16 +#include "nsapi_types.h" // Predeclared classes class NetworkStack; @@ -178,4 +134,5 @@ private: uint16_t _port; }; + #endif diff --git a/TCPServer.h b/TCPServer.h index f5a7e2c87a..0cbdc6e3cd 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -17,11 +17,12 @@ #ifndef TCPSERVER_H #define TCPSERVER_H -#include "Socket.h" -#include "TCPSocket.h" -#include "NetworkInterface.h" -#include "NetworkStack.h" -#include "Semaphore.h" +#include "NetworkSocketAPI/Socket.h" +#include "NetworkSocketAPI/TCPSocket.h" +#include "NetworkSocketAPI/NetworkStack.h" +#include "NetworkSocketAPI/NetworkInterface.h" +#include "rtos/Semaphore.h" + /** TCP socket server */ @@ -108,4 +109,5 @@ protected: rtos::Semaphore _accept_sem; }; + #endif diff --git a/TCPSocket.h b/TCPSocket.h index 041aaa7629..b537736b06 100644 --- a/TCPSocket.h +++ b/TCPSocket.h @@ -17,10 +17,11 @@ #ifndef TCPSOCKET_H #define TCPSOCKET_H -#include "Socket.h" -#include "NetworkInterface.h" -#include "NetworkStack.h" -#include "Semaphore.h" +#include "NetworkSocketAPI/Socket.h" +#include "NetworkSocketAPI/NetworkStack.h" +#include "NetworkSocketAPI/NetworkInterface.h" +#include "rtos/Semaphore.h" + /** TCP socket connection */ @@ -139,4 +140,5 @@ protected: friend class TCPServer; }; + #endif diff --git a/UDPSocket.h b/UDPSocket.h index a46a3c0941..235820c5d4 100644 --- a/UDPSocket.h +++ b/UDPSocket.h @@ -17,10 +17,11 @@ #ifndef UDPSOCKET_H #define UDPSOCKET_H -#include "Socket.h" -#include "NetworkInterface.h" -#include "NetworkStack.h" -#include "Semaphore.h" +#include "NetworkSocketAPI/Socket.h" +#include "NetworkSocketAPI/NetworkStack.h" +#include "NetworkSocketAPI/NetworkInterface.h" +#include "rtos/Semaphore.h" + /** UDP socket */ @@ -137,4 +138,5 @@ protected: bool _write_in_progress; }; + #endif diff --git a/WiFiInterface.h b/WiFiInterface.h index f99b411b01..30120e85e8 100644 --- a/WiFiInterface.h +++ b/WiFiInterface.h @@ -17,7 +17,8 @@ #ifndef WIFI_INTERFACE_H #define WIFI_INTERFACE_H -#include "NetworkInterface.h" +#include "NetworkSocketAPI/NetworkInterface.h" + /** Enum of WiFi encryption types * @@ -65,4 +66,5 @@ public: virtual const char *get_mac_address() = 0; }; + #endif diff --git a/nsapi.h b/nsapi.h new file mode 100644 index 0000000000..56dc446037 --- /dev/null +++ b/nsapi.h @@ -0,0 +1,47 @@ +/* nsapi.h - The network socket API + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NSAPI_H +#define NSAPI_H + + +// entry point for nsapi types +#include "nsapi_types.h" + +// disable bug-compatible mbed inclusion +#define NSAPI_NO_INCLUDE_MBED + +#ifdef __cplusplus + +// entry point for C++ api +#include "NetworkSocketAPI/SocketAddress.h" +#include "NetworkSocketAPI/NetworkStack.h" + +#include "NetworkSocketAPI/NetworkInterface.h" +#include "NetworkSocketAPI/EthInterface.h" +#include "NetworkSocketAPI/WiFiInterface.h" +#include "NetworkSocketAPI/CellularInterface.h" +#include "NetworkSocketAPI/MeshInterface.h" + +#include "NetworkSocketAPI/Socket.h" +#include "NetworkSocketAPI/UDPSocket.h" +#include "NetworkSocketAPI/TCPSocket.h" +#include "NetworkSocketAPI/TCPServer.h" + +#endif + + +#endif diff --git a/nsapi_types.h b/nsapi_types.h new file mode 100644 index 0000000000..db647ccfe3 --- /dev/null +++ b/nsapi_types.h @@ -0,0 +1,136 @@ +/* nsapi.h - The network socket API + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NSAPI_TYPES_H +#define NSAPI_TYPES_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** Enum of standardized error codes + * + * Valid error codes have negative values and may + * be returned by any network operation. + * + * @enum nsapi_error_t + */ +typedef enum nsapi_error { + NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ + NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ + NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ + NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */ + NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ + NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ + NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ + NSAPI_ERROR_DNS_FAILURE = -3008, /*!< DNS failed to complete successfully */ + NSAPI_ERROR_DHCP_FAILURE = -3009, /*!< DHCP failed to complete successfully */ + NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point faield */ + NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network procesor */ +} nsapi_error_t; + + +/** Maximum size of IP address representation + */ +#define NSAPI_IP_SIZE NSAPI_IPv6_SIZE + +/** Maximum number of bytes for IP address + */ +#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES + +/** Maximum size of MAC address representation + */ +#define NSAPI_MAC_SIZE 18 + +/** Maximum number of bytes for MAC address + */ +#define NSAPI_MAC_BYTES 6 + +/** Size of IPv4 representation + */ +#define NSAPI_IPv4_SIZE 16 + +/** Number of bytes in IPv4 address + */ +#define NSAPI_IPv4_BYTES 4 + +/** Size of IPv6 representation + */ +#define NSAPI_IPv6_SIZE 40 + +/** Number of bytes in IPv6 address + */ +#define NSAPI_IPv6_BYTES 16 + +/** Enum of IP address versions + * + * The IP version specifies the type of an IP address. + * + * @enum nsapi_version_t + */ +typedef enum nsapi_version { + NSAPI_IPv4, /*!< Address is IPv4 */ + NSAPI_IPv6, /*!< Address is IPv6 */ +} nsapi_version_t; + + +/** Enum of socket protocols + * + * The socket protocol specifies a particular protocol to + * be used with a newly created socket. + * + * @enum nsapi_protocol_t + */ +typedef enum nsapi_protocol { + NSAPI_TCP, /*!< Socket is of TCP type */ + NSAPI_UDP, /*!< Socket is of UDP type */ +} nsapi_protocol_t; + +/* Enum of standardized stack option levels + * + * @enum nsapi_level_t + */ +typedef enum nsapi_level { + NSAPI_STACK, /*!< Stack option level */ + NSAPI_SOCKET, /*!< Socket option level */ +} nsapi_level_t; + +/* Enum of standardized stack options + * + * These options may not be supported on all stacks, in which + * case NSAPI_ERROR_UNSUPPORTED may be returned from setsockopt. + * + * @enum nsapi_option_t + */ +typedef enum nsapi_option { + NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */ + NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */ + NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */ + NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */ + NSAPI_LINGER, /*!< Keeps close from returning until queues empty */ + NSAPI_SNDBUF, /*!< Sets send buffer size */ + NSAPI_RCVBUF, /*!< Sets recv buffer size */ +} nsapi_option_t; + + +#ifdef __cplusplus +} +#endif + +#endif