cc

Dependencies:   ATParser

Revision:
15:e56c9e744ab6
Parent:
14:868b0fd4e84a
Child:
18:fd46c2ca8eb7
--- a/SPWFSA01.cpp	Fri Aug 19 08:50:10 2016 +0000
+++ b/SPWFSA01.cpp	Mon Sep 05 07:09:49 2016 +0000
@@ -285,55 +285,44 @@
 
 int32_t SPWFSA01::recv(int id, void *data, uint32_t amount)
 {
-    uint32_t recv_amount, read_amt;
+    uint32_t recv_amount;
     int recv_id;
-    char _buf[18];
-
+    bool wind_recv = true;
+    
     if (!(_parser.recv("+WIND:55:Pending Data:%d:%u", &recv_id, &recv_amount)
-        && recv_id == id        
-        //&& _parser.send("AT+S.SOCKQ=%d", id)//send a query (will be required for secure sockets)
-        //&& _parser.recv(" DATALEN: %u", &recv_amount)
-        //&& _parser.recv("OK")
-        && recv_amount <= amount)) {
-            return -1;//Block call
+        && recv_id == id
+        && recv_amount <= amount
+        && recv_amount%730
+        && _parser.send("AT+S.SOCKQ=%d", id)  //send a query (will be required for secure sockets)
+        && _parser.recv(" DATALEN: %u", &recv_amount)
+        && _parser.recv("OK") 
+        && recv_amount > 0
+        && _parser.send("AT+S.SOCKR=%d,%d", id, recv_amount)
+        && (_parser.read((char*)data, recv_amount) >0)
+        && _parser.recv("OK"))) {
+            if(!(recv_amount%730))
+            {
+                // receive all the WIND messages
+                do {
+                    if (!(_parser.recv("+WIND:55:Pending Data:%d:%u", &recv_id, &recv_amount)
+                         && recv_id == id 
+                         && recv_amount <= amount
+                         && recv_amount > 0))
+                             wind_recv = false;
+                } while (!(recv_amount%730) && wind_recv);
+
+                // Read all the data pending on a socket
+                if(!( recv_amount > 0
+                    && _parser.send("AT+S.SOCKR=%d,%d", id, recv_amount)
+                    && (_parser.read((char*)data, recv_amount) >0)
+                    && _parser.recv("OK"))) {
+                        return -1;
+                    }
             }
-    //&& sprintf((char*)_buf,"AT+S.SOCKR=%d,%d\r", id, recv_amount)
-    //&& _parser.write((char*)_buf, strlen(_buf))
-    if (!_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount)) {
-        //if error, resend SOCKR again to flush the socket
-        //This should prevent Error: Pending Data errors or the next WIND showing this pending data
-        sprintf((char*)_buf,"AT+S.SOCKR=%d,%d\r", id, recv_amount);
-        _parser.write((char*)_buf, strlen(_buf));
-        debug_if(dbg_on, "SPWF> read send SOCKR error\r\n");
-        return -2;//return NSAPI_ERROR_DEVICE_ERROR
-        }
-            
-    read_amt = _parser.read((char*)data, recv_amount);            
-    if (read_amt<=0) {        
-        debug_if(dbg_on, "SPWF> read recv SOCKR error: read_amt = %d\r\n", read_amt);
-        return -2;//return NSAPI_ERROR_DEVICE_ERROR
-    }
-        
-    //Sometimes OK is not received because _parser.read() reads in ERROR statement. "E.g. ERROR: Command not found".
-    //ERROR statement hence takes up space in recv_amount and hence OK is not read as a result.
-    
-    if (!_parser.recv("OK")) {            
-        debug_if(dbg_on, "SPWF> read recv SOCKR OK error\r\n");
-        
-        if(std::strncmp("\r\nERROR: ", (const char* )data, 9)==0) {
-            debug_if(dbg_on, "SPWF> Module Error Reply\r\n");
-            return -2;
+            else {
+                return -2;
             }
-        
-        /*int i = 0;
-        char * buf = (char*)data;
-        
-        for ( ; i < recv_amount; i++) {
-            debug_if(dbg_on, ("%d = %c\r\n", i, buf[i]);
-            }*/
-        return -2;//return NSAPI_ERROR_DEVICE_ERROR
-    }
-    
+    }    
     return recv_amount;
 }