modified by ohneta

Dependencies:   ESP8266

Dependents:   HelloESP8266Interface_mine

Fork of ESP8266Interface by NetworkSocketAPI

Revision:
22:312453862371
Parent:
20:5d0762aa4680
Child:
23:fd0f3197c30b
--- a/ESP8266Interface.cpp	Tue Jul 21 20:18:55 2015 +0000
+++ b/ESP8266Interface.cpp	Wed Jul 22 20:53:09 2015 +0000
@@ -20,9 +20,9 @@
 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx)
 : serial(tx, rx), atParser(&serial)
 {
+    uuidCounter = 0;
     serial.baud(115200);
-    availableID = new int[5];
-    for(int i = 0; i<4; i++){
+    for(int i = 0; i<numSockets; i++){
         availableID[i] = -1;
     }
 }
@@ -49,7 +49,7 @@
 
 int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) 
 {
-    return -1;
+
 }
 
 int32_t ESP8266Interface::connect(uint32_t timeout_ms) 
@@ -69,9 +69,16 @@
     return 0;    
 }
 
-int32_t ESP8266Interface::disconnect(void) const
+int32_t ESP8266Interface::disconnect(void)
 {
-    return -1;
+    if (!(atParser.send("AT+CWQAP") && atParser.recv("OK")))
+        return -1;
+    for(int i=0; i<numSockets; i++){
+        SocketInterface *socket = sockets[availableID[i]];
+        deallocateSocket(socket);
+    }
+    return 0;
+    
 }
 
 char *ESP8266Interface::getIPAddress(void)
@@ -104,9 +111,8 @@
 SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol) 
 {   
     int id = -1;
-    uuidCounter += 1;
     //Look through the array of available sockets for an unused ID
-    for(int i=0; i<sizeof(availableID); i++){
+    for(int i=0; i<numSockets; i++){
         if (availableID[i] == -1){
             id = i;   
             availableID[i] = uuidCounter;
@@ -116,7 +122,8 @@
     if (id == -1){
         return NULL;//tried  to allocate more than the maximum 5 sockets   
     }
-    ESP8266Socket *socket = new ESP8266Socket(uuidCounter, &atParser, socketProtocol, (uint8_t)id);
+    ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, &atParser, socketProtocol, (uint8_t)id);
+    sockets[socket->getHandle()] = socket;
     return socket;
 }
 
@@ -124,12 +131,27 @@
 {
     int id = (int)static_cast<ESP8266Socket*>(socket)->getID();
     availableID[id] = -1;
-    return id;
+    
+    std::map<uint32_t, SocketInterface*>::iterator it;
+ 
+    // Check if socket is owned by WiFiRadioInterface
+    it = sockets.find(socket->getHandle());
+    
+    if (it != sockets.end()) {
+        // If so, erase it from the internal socket map and deallocate the socket
+        sockets.erase(it);
+        delete socket;
+        return 0;
+    } else {
+        // Socket is not owned by WiFiRadioInterface, so return -1 error
+        return -1;
+    }
 }
 
 ESP8266Socket::ESP8266Socket(uint32_t handle, ATParser *atParser, socket_protocol_t type, uint8_t id)
-: _handle(handle), atParser(atParser), _id(id)
+: atParser(atParser), _id(id)
 {
+    _handle = handle;
     SocketInterface::_type = type;
 }
 
@@ -241,6 +263,12 @@
     }
     return 0;
 }
+
+uint32_t ESP8266Socket::getHandle()const
+{
+    return _handle;
+}
+
 uint8_t ESP8266Socket::getID()
 {
     return _id;