NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 21:35ed15069189, committed 2016-02-18
- Comitter:
- Christopher Haster
- Date:
- Thu Feb 18 03:11:58 2016 -0600
- Branch:
- api-changes
- Parent:
- 20:3ad72f5406bf
- Child:
- 22:4fca633c0633
- Commit message:
- Added UDPSocket and TCPSocket API and changes to core API
Changed in this revision
--- a/EthernetInterface.h Fri Dec 25 19:51:33 2015 +0000 +++ b/EthernetInterface.h Thu Feb 18 03:11:58 2016 -0600 @@ -14,29 +14,29 @@ * limitations under the License. */ -#ifndef ETHERNETINTERFACE_H -#define ETHERNETINTERFACE_H +#ifndef ETHERNET_INTERFACE_H +#define ETHERNET_INTERFACE_H #include "NetworkInterface.h" -///* wifi_security_t enum for encryption types -// */ -//typedef enum wifi_security_t { -// WI_NONE = 0, /*!< No security for connection */ -// WI_WEP, /*!< WEP encryption */ -// WI_WPA, /*!< WPA encryption */ -// WI_WPA2, /*!< WPA2 encryption */ -//} wifi_security_t; -/** EthernetInterface class. - This is a common interface to handle how ethernet connects to a router +/** EthernetInterface class + * Common interface that is shared between ethernet hardware */ class EthernetInterface : public NetworkInterface { public: - // make sure to import the base symbol that needs an implementation for classes that have ap and phrase in the constructor - using NetworkInterface::connect; + /** Start the interface + * @param timeout_ms Time in milliseconds to wait for a connection + * @return 0 on success + */ + virtual int32_t connect(uint32_t timeout_ms = 15000) = 0; + + /** Stop the interface + * @return 0 on success + */ + virtual int32_t disconnect() = 0; }; #endif
--- a/NetworkInterface.h Fri Dec 25 19:51:33 2015 +0000 +++ b/NetworkInterface.h Thu Feb 18 03:11:58 2016 -0600 @@ -14,92 +14,90 @@ * limitations under the License. */ -#ifndef NETWORKINTERFACE_H -#define NETWORKINTERFACE_H +#ifndef NETWORK_INTERFACE_H +#define NETWORK_INTERFACE_H #include "stdint.h" #include "SocketInterface.h" -#include <map> + -/** NetworkInterface class. - This is a common interface that is shared between all hardware that connect - to a network over IP. +/** NetworkInterface class + * Common interface that is shared between all hardware that + * can connect to a network over IP. */ class NetworkInterface { public: - - /** Initialize the network interface with DHCP. - @returns 0 on success, a negative number on failure - */ - virtual int32_t init(void) = 0; - - /** Initialize the network interface with a static IP address. - @param ip The static IP address to use - @param mask The IP address mask - @param gateway The gateway to use - @returns 0 on success, a negative number on failure - */ - virtual int32_t init(const char *ip, const char *mask, const char *gateway) = 0; - - - /** Start the interface, using DHCP if needed. - @param timeout_ms Time in miliseconds to wait while attempting to connect before timing out - @returns 0 on success, a negative number on failure - */ - virtual int32_t connect(uint32_t timeout_ms = 15000) = 0; - - /** Stop the interface, bringing down dhcp if necessary. - @returns 0 on success, a negative number on failure - */ - virtual int32_t disconnect(void) = 0; - - /** Get the current IP address. - @returns a pointer to a string containing the IP address. - */ - virtual char *getIPAddress(void) = 0; - - /** Get the current gateway address. - @returns a pointer to a string containing the gateway address. - */ - virtual char *getGateway(void) const = 0; + virtual ~NetworkInterface() = default; - /** Get the current network mask. - @returns a pointer to a string containing the network mask. + /** Enables DHCP and clears any static address + * DHCP is enabled by default + * @return 0 on success + */ + virtual int32_t useDHCP() = 0; + + /** Set the static IP address of the network interface + * @param ip Static IP address, copied internally */ - virtual char *getNetworkMask(void) const = 0; + virtual void setIPAddress(const char *ip); + + /** Set the static network mask of the network interface + * @param mask Static network mask, copied internally + */ + virtual void setNetworkMask(const char *mask); + + /** Set the static gateway of the network interface + * @param gateway Gateway address, copied internally + */ + virtual void setGateway(const char *gateway); + + /** Get the IP address + * @return IP address of the interface + */ + virtual const char *getIPAddress(); - /** Get the devices MAC address. - @returns a pointer to a string containing the mac address. + /** Get the network mask + * @return Network mask of the interface */ - virtual char *getMACAddress(void) const = 0; + virtual const char *getNetworkMask(); + + /** Get the gateway + * @return Gateway address of the interface + */ + virtual const char *getGateway(); + + /** Get the current MAC address + * @return String MAC address of the interface + */ + virtual const char *getMACAddress() = 0; - /** Get the current status of the interface connection. - @returns true if connected, a negative number on failure + /** Get the current status of the interface + @return true if connected */ - virtual int32_t isConnected(void) = 0; + virtual bool isConnected(void) = 0; + + +private: + friend class TCPSocket; + friend class UDPSocket; + + /** Internally create a socket + * @param proto The type of socket to open, SOCK_TCP or SOCK_UDP + * @return The allocated socket + */ + virtual SocketInterface *createSocket(socket_protocol_t proto) = 0; - /** Allocate space for a socket. - @param The type of socket you want to open, SOCK_TCP or SOCK_UDP - @returns a pointer to the allocated socket. - */ - virtual SocketInterface* allocateSocket(socket_protocol_t socketProtocol) = 0; - - /** Deallocate space for a socket. - @param An allocated SocketInterface - @returns 0 if object is destroyed, -1 otherwise + /** Internally destroy a socket + * @param socket An allocated SocketInterface + * @returns 0 on success */ - virtual int deallocateSocket(SocketInterface* socket) = 0; - -protected: - /** Map used to keep track of all SocketInterface instances */ - std::map<uint32_t, SocketInterface*> sockets; - - /** Counter used to create unique handles for new sockets. - Should be incremented whenever a new socket is created. - */ - uint32_t uuidCounter; + virtual void destroySocket(SocketInterface *socket) = 0; + +private: + const char *_ip_address; + const char *_network_mask; + const char *_gateway; }; #endif
--- a/SocketInterface.h Fri Dec 25 19:51:33 2015 +0000 +++ b/SocketInterface.h Thu Feb 18 03:11:58 2016 -0600 @@ -14,139 +14,83 @@ * limitations under the License. */ -#ifndef SOCKETINTERFACE_H -#define SOCKETINTERFACE_H +#ifndef SOCKET_INTERFACE_H +#define SOCKET_INTERFACE_H #include "stdint.h" -/** This enum defines the possible socket domain types. - */ -typedef enum { - SOCK_IPV4, /*!< IPv4 */ - SOCK_IPV6, /*!< IPV6 */ -} socket_domain_t; -/** This enum defines the possible socket types. - */ -typedef enum { - SOCK_STREAM, /*!< Reliable stream-oriented service or Stream Sockets */ - SOCK_DGRAM, /**< Datagram service or Datagram Sockets */ - SOCK_SEQPACKET, /*!< Reliable sequenced packet service */ - SOCK_RAW /*!< Raw protocols atop the network layer */ -} socket_type_t; - -/** This enum defines the ip protocols +/** Enum of socket protocols */ typedef enum { SOCK_TCP, /*!< Socket connection over TCP */ SOCK_UDP, /*!< Socket connection over UDP */ } socket_protocol_t; -/** Base class that defines an endpoint (TCP/UDP/Server/Client Socket) + +/** SocketInterface class + * Common interface for implementation specific sockets created through + * network interfaces. This class is used internally by the + * TCPSocket and UDPSocket classes */ -class Endpoint +class SocketInterface { public: + virtual ~SocketInterface() = default; - /** Get the ip address of a host by DNS lookup - @param name The name of a host you need an ip address for - @return The ip address of the host otherwise NULL + + /** Set the IP address of the socket + * @param ip IP address to connect to, copied internally */ - virtual const char *getHostByName(const char *name) const = 0; + virtual void setIPAddress(const char *ip); - /** Set the address of this endpoint - @param addr The endpoint address - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). + /** Set the port of the socket + * @param port Port to connect to */ - virtual void setAddress(const char* addr) = 0; + virtual void setPort(uint16_t port); - /** Set the port of this endpoint - @param port The endpoint port - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). + /** Get the IP address + * @return IP address to connect to */ - virtual void setPort(uint16_t port) = 0; + virtual const char *getIPAddress(); + + /** Get the port + * @return Port to connect to + */ + virtual uint16_t getPort(); + - /** Set the address and port of this endpoint - @param addr The endpoint address (supplied as an ip address). - @param port The endpoint port - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). + /** Open a connection to the underlying address + * Only used for TCP sockets + * @return 0 on success */ - virtual void setAddressPort(const char* addr, uint16_t port) = 0; + virtual int32_t open() = 0; + + /** Close an open connection + * Only used for TCP sockets + * @return 0 on success + */ + virtual int32_t close() = 0; - /** Get the IP address of this endpoint - @return The IP address of this endpoint. + /** Send data + * @param data Buffer of data to send + * @param len Size of data to send + * @param timeout_ms Maximum amount of time to wait + * @return 0 on success */ - virtual const char *getAddress(void) const = 0; + virtual int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000) = 0; - /** Get the port of this endpoint - @return The port of this socket + /** In client or server mode receive data + * @param data a buffer to store the data in + * @param amount The amount of data to receive + * @param timeout_ms The longest time to wait for the data + * @return Number of bytes sent or a negative value on failure */ - virtual uint16_t getPort(void) const = 0; -protected: - const char* _addr; + virtual int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000) = 0; + +private: + char *_ip_address; uint16_t _port; }; -/** SocketInterface class. - * This is a common interface that is shared between all sockets that connect - * using the NetworkInterface. - */ -class SocketInterface : public Endpoint -{ -public: - /** In server mode, set which port to listen on - @param port The endpoint port - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - virtual int32_t bind(uint16_t port) const = 0; - - /** In server mode, start listening to a port - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - virtual int32_t listen(void) const = 0; - - /** In server mode, accept an incoming connection - @param endpoint The endpoint we are listening to - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - virtual int32_t accept() const = 0; - - /** In client mode, open a connection to a remote host - @param endpoint The endpoint we want to connect to - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - virtual int32_t open() = 0; - - /** In client or server mode send data - @param data A buffer of data to send - @param amount The amount of data to send - @param timeout_ms The longest amount of time this send can take - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - virtual int32_t send(const void *data, uint32_t amount, uint32_t timeout_ms = 15000) = 0; - - /** In client or server mode receive data - @param data a buffer to store the data in - @param amount The amount of data to receive - @param timeout_ms The longest time to wait for the data - @return The amount of data received - */ - virtual uint32_t recv(void *data, uint32_t amount, uint32_t timeout_ms = 15000) = 0; - - /** In client or server mode, close an open connection - @param endpoint The endpoint we want to connect to - @return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - virtual int32_t close() const = 0; - - /** Get the socket's unique handle - @return socket's handle number - */ - virtual uint32_t getHandle() const = 0; - -protected: - socket_protocol_t _type; - uint32_t _handle; -}; - #endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TCPSocket.h Thu Feb 18 03:11:58 2016 -0600 @@ -0,0 +1,93 @@ +/* TCPSocket + * 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 TCP_SOCKET_H +#define TCP_SOCKET_H + +#include "NetworkInterface.h" + +/** TCPSocket class + * API for handling TCP sockets. The implementation is determined + * by the interface passed during construction. + */ +class TCPSocket +{ +public: + /** Create a socket using the specified network interface + * @param iface The network interface to use + * @param ip Optional ip address to connect to, copied internally + * @param port Optional port to connect to + */ + TCPSocket(NetworkInterface *iface, const char *ip = 0, uint16_t port = 0); + + /** Closes and destroys the socket + */ + ~TCPSocket(); + + + /** Set the IP address of the socket + * @param ip IP address to connect to, copied internally + */ + void setIPAddress(const char *ip); + + /** Set the port of the socket + * @param port Port to connect to + */ + void setPort(uint16_t port); + + /** Gets the IP address + * @return IP address to connect to + */ + const char *getIPAddress(); + + /** Gets the port + * @return Port to connect to + */ + uint16_t getPort(); + + + /** Open a connection to the underlying address + * @return 0 on success + */ + int32_t open(); + + /** Close an open connection + * @return 0 on success + */ + int32_t close(); + + /** Send data over TCP + * @param data Buffer of data to send + * @param len Size of data to send + * @param timeout_ms Maximum amount of time to wait + * @return 0 on success + */ + int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000); + + /** Recieve data over TCP + * @param data Buffer to store recieved data + * @param len Size of provided buffer + * @param timeout_ms Maximum amount of time to wait + * @return Number of bytes sent or a negative value on failure + */ + int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000); + +private: + NetworkInterface *_iface; + SocketInterface *_socket; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UDPSocket.h Thu Feb 18 03:11:58 2016 -0600 @@ -0,0 +1,83 @@ +/* UDPSocket + * 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 UDP_SOCKET_H +#define UDP_SOCKET_H + +#include "NetworkInterface.h" + +/** UDPSocket class + * API for handling UDP sockets. The implementation is determined + * by the interface passed during construction. + */ +class UDPSocket +{ +public: + /** Create a socket using the specified network interface + * @param iface The network interface to use + * @param ip Optional ip address to connect to, copied internally + * @param port Optional port to connect to + */ + UDPSocket(NetworkInterface *iface, const char *ip = 0, uint16_t port = 0); + + /** Closes and destroys the socket + */ + ~UDPSocket(); + + + /** Set the IP address of the socket + * @param ip IP address to connect to, copied internally + */ + void setIPAddress(const char *ip); + + /** Set the port of the socket + * @param port Port to connect to + */ + void setPort(uint16_t port); + + /** Gets the IP address + * @return IP address to connect to + */ + const char *getIPAddress(); + + /** Gets the port + * @return Port to connect to + */ + uint16_t getPort(); + + + /** Send a UDP packet + * @param data Buffer of data to send + * @param len Size of data to send + * @param timeout_ms Maximum amount of time to wait + * @return 0 on success + */ + int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000); + + /** Recieve a UDP packet + * @param data Buffer to store recieved data + * @param len Size of provided buffer + * @param timeout_ms Maximum amount of time to wait + * @return Number of bytes sent or a negative value on failure + */ + int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000); + +private: + NetworkInterface *_iface; + SocketInterface *_socket; +}; + +#endif
--- a/WiFiInterface.h Fri Dec 25 19:51:33 2015 +0000 +++ b/WiFiInterface.h Thu Feb 18 03:11:58 2016 -0600 @@ -14,12 +14,12 @@ * limitations under the License. */ -#ifndef WIFIINTERFACE_H -#define WIFIINTERFACE_H +#ifndef WIFI_INTERFACE_H +#define WIFI_INTERFACE_H #include "NetworkInterface.h" -/* wifi_security_t enum for encryption types +/** Enum for WiFi encryption types */ typedef enum wifi_security_t { WI_NONE = 0, /*!< No security for connection */ @@ -28,25 +28,31 @@ WI_WPA2, /*!< WPA2 encryption */ } wifi_security_t; -/** WiFiInterface class. - This is a common interface to handle how WiFi connects to an access point + +/** WiFiInterface class + * Common interface that is shared between WiFi devices */ class WiFiInterface : public NetworkInterface { public: - // make sure to import the base symbol that needs an implementation for classes that have ap and phrase in the constructor - using NetworkInterface::connect; - - /** Start the interface using ap name, phrase and security attributes - @param ap Name of the network the radio should connect to - @param pass_pharase The security phrase needed for connecting to the network - @param security Type of encryption the network requires for connection - @param timeout_ms Time in miliseconds to wait while attempting to connect before timing out - @returns 0 on success, a negative number on failure + /** Start the interface + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption to connect with + * @param timeout_ms Time in milliseconds to wait for a connection + * @return 0 on success */ - virtual int32_t connect(const char *ap, const char *pass_phrase = 0, wifi_security_t security = WI_NONE, uint32_t timeout_ms = 15000) = 0; - + virtual int32_t connect( + const char *ssid, + const char *pass, + wifi_security_t security = WI_NONE, + uint32_t timeout_ms = 15000) = 0; + + /** Stop the interface + * @return 0 on success + */ + virtual int32_t disconnect() = 0; }; #endif