NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Branch:
api-changes
Revision:
21:35ed15069189
Parent:
15:e657a90d9511
Child:
23:1e86d9fb3d86
--- 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