This library controls the WNC. There is a derived class for usage from the K64F board.

Fork of WncControllerLibrary by Fred Kellerman

Revision:
4:c5720f4d13ff
Parent:
1:ac2de545b981
Child:
5:20207cc5502e
--- a/WncController.cpp	Fri Sep 02 16:37:14 2016 +0000
+++ b/WncController.cpp	Tue Sep 06 18:45:28 2016 +0000
@@ -448,6 +448,65 @@
  *
  *  \details DO NOT use the same string as is passed to the auto poll setup method!
  */
+ 
+size_t WncController::read(uint16_t numSock, const uint8_t ** readBuf)
+{
+    static string theBuf;
+    string readStr;
+    
+    theBuf.erase();  // Clean-up from last time
+
+    if (numSock < MAX_NUM_WNC_SOCKETS) {
+        if (m_sSock[numSock].open == true) {
+            uint8_t   i = m_sSock[numSock].readRetries;
+            uint16_t to = m_sSock[numSock].readRetryWaitMs;
+            bool foundData = false;
+            do {
+                AtCmdErr_e cmdRes;
+                cmdRes = at_sockread_wnc(&readStr, numSock, m_sSock[numSock].isTcp);
+                if (WNC_AT_CMD_OK == cmdRes) {
+                    // This will let this loop read until the socket data is
+                    //  empty.  If no data, then wait the retry amount of time.
+                    if (readStr.size() > 0) {
+                        theBuf += readStr;
+                        foundData = true;
+                        i = 1;
+                    }
+                    else {
+                        // Once data is found start returning it asap
+                        if (foundData == false)
+                            waitMs(to);
+                    }
+                }
+                else {
+                    theBuf += readStr; // Append what if any we got before it errored.
+                    dbgPuts("Sockread failed!");
+                    if (cmdRes == WNC_AT_CMD_ERREXT || cmdRes == WNC_AT_CMD_TIMEOUT)
+                    {
+                        // This may throw away any data that hasn't been read out of the WNC
+                        //  but at this point with the way the WNC currently works we have
+                        //  no choice.
+                        closeOpenSocket(numSock);
+                        i = 0;
+                    }
+                    else
+                        waitMs(to);
+                }
+            } while (i-- > 0);
+        }
+        else {
+            dbgPuts("Socket is closed for read");
+        }
+    }
+    else {
+        dbgPuts("Bad socket num!");
+    }
+
+    *readBuf = (const uint8_t *)theBuf.c_str();
+
+    return (theBuf.size());
+}
+
 size_t WncController::read(uint16_t numSock, uint8_t * readBuf, uint32_t maxReadBufLen)
 {
     uint32_t numCopied = 0;
@@ -1362,6 +1421,59 @@
     return (result);
 }
 
+WncController::AtCmdErr_e WncController::at_sockread_wnc(string * pS, uint16_t numSock, bool isTcp)
+{
+    AtCmdErr_e result = WNC_AT_CMD_OK;
+
+    string * pRespStr;
+    string cmd_str;
+    size_t pos_start, pos_end;
+    int i;
+
+    if (isTcp == true)
+        cmd_str="AT@SOCKREAD=";
+    else
+        cmd_str="AT@SOCKREAD="; // "AT@SOCKRECV=";
+
+    cmd_str += _to_string(numSock + 1);
+    cmd_str += ",";
+    cmd_str += _to_string(MAX_WNC_READ_BYTES);
+            
+    // Experimental: read should not need to check cell net status
+    result = at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs);
+    if (result == WNC_AT_CMD_OK) {
+        pos_start = pRespStr->find("\"")  + 1;
+        pos_end   = pRespStr->rfind("\"") - 1;
+        
+        // Make sure search finds what it's looking for!
+        if (pos_start != string::npos && pos_end != string::npos)
+            i = (pos_end - pos_start + 1);  // Num hex chars, 2 per byte
+        else
+            i = 0;
+            
+        if (i < 0)
+            dbgPuts("Invalid READ string!");
+        else if (i > 2*MAX_WNC_READ_BYTES) {
+            // Bound the ill formated WNC read string!
+            i = 2*MAX_WNC_READ_BYTES;
+            dbgPuts("TRUNCATING read data!");
+        }
+            
+        // If data, convert the hex string into byte values
+        if (i > 0)
+        {
+            string byte;
+            while (pos_start < pos_end) {
+                byte = pRespStr->substr(pos_start, 2);
+                *pS += (uint8_t)strtol(byte.c_str(), NULL, 16);
+                pos_start += 2;
+            }
+        }
+    }
+
+    return (result);
+}
+
 WncController::AtCmdErr_e WncController::at_sockread_wnc(uint8_t * pS, uint32_t * numRead, uint16_t n, uint16_t numSock, bool isTcp)
 {
     AtCmdErr_e result = WNC_AT_CMD_OK;
@@ -1394,13 +1506,21 @@
             else
                 i = 0;
             
+            if (i < 0)
+                dbgPuts("Invalid READ string!");
+            else if (i > 2*MAX_WNC_READ_BYTES) {
+                // Bound the ill formated WNC read string!
+                i = 2*MAX_WNC_READ_BYTES;
+                dbgPuts("TRUNCATING read data!");
+            }
+
             // If data, convert the hex string into byte values
-            if (i > 0 && i <= (2*MAX_WNC_READ_BYTES))
+            if (i > 0)
             {
                 string byte;
                 while (pos_start < pos_end) {
                     byte = pRespStr->substr(pos_start, 2);
-                    *pS += (uint8_t)strtol(byte.c_str(), NULL, 16);
+                    *pS++ = (uint8_t)strtol(byte.c_str(), NULL, 16);
                     pos_start += 2;
                     (*numRead)++;
                 }