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

Fork of WncControllerLibrary by Fred Kellerman

Files at this revision

API Documentation at this revision

Comitter:
fkellermavnet
Date:
Sat Sep 10 02:02:05 2016 +0000
Parent:
13:73629a6e9122
Child:
15:2c8211ef69e1
Commit message:
Added more checks to make sure find is not used on empty string for some reason mbed sometimes crashes when that is done.

Changed in this revision

WncController.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/WncController.cpp	Sat Sep 10 01:10:20 2016 +0000
+++ b/WncController.cpp	Sat Sep 10 02:02:05 2016 +0000
@@ -1001,42 +1001,123 @@
         if (pRespStr->size() > 0) {
             memset((void*)s, '\0', sizeof(*s));  // Clean-up
             string ss;
-            unsigned i;
             size_t pe;
             size_t ps = pRespStr->rfind("\"");
             if (ps != string::npos) {
                 ps += 2;  // Skip the , after the "
                 pe = ps;
-                for(i=0;i<4;i++)
-                    pe = pRespStr->find(".", pe) + 1;
+
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+
                 ss = pRespStr->substr(ps, pe - 1 - ps);
                 strncpy(s->ip, ss.c_str(), MAX_LEN_IP_STR);
                 s->ip[MAX_LEN_IP_STR - 1] = '\0';
                 ps = pe;
-                for(i=0;i<3;i++)
-                    pe = pRespStr->find(".", pe) + 1;
+
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
                 pe = pRespStr->find(",", pe);
+
                 ss = pRespStr->substr(ps, pe - ps);
                 strncpy(s->mask, ss.c_str(), MAX_LEN_IP_STR);
                 s->mask[MAX_LEN_IP_STR - 1] = '\0';
                 ps = pe + 1;
-                for(i=0;i<3;i++)
-                    pe = pRespStr->find(".", pe) + 1;
+
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
                 pe = pRespStr->find(",", pe);
+
                 ss = pRespStr->substr(ps, pe - ps);
                 strncpy(s->gateway, ss.c_str(), MAX_LEN_IP_STR);
                 s->gateway[MAX_LEN_IP_STR - 1] = '\0';
                 ps = pe + 1;
-                for(i=0;i<3;i++)
-                    pe = pRespStr->find(".", pe) + 1;
+
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
                 pe = pRespStr->find(",", pe);
+
+
                 ss = pRespStr->substr(ps, pe - ps);
                 strncpy(s->dnsPrimary, ss.c_str(), MAX_LEN_IP_STR);
                 s->dnsPrimary[MAX_LEN_IP_STR - 1] = '\0';
                 ps = pe + 1;
-                for(i=0;i<3;i++)
-                    pe = pRespStr->find(".", pe) + 1;
+
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
+                pe = pRespStr->find(".", pe);
+                if (pe == string::npos)
+                    return (false);
+                else
+                    pe += 1;
                 pe = pRespStr->find(",", pe);
+
+
                 ss = pRespStr->substr(ps, pe - ps);
                 strncpy(s->dnsSecondary, ss.c_str(), MAX_LEN_IP_STR);
                 s->dnsSecondary[MAX_LEN_IP_STR - 1] = '\0';
@@ -1327,7 +1408,7 @@
 
     cmd_str += ",0";
     res = sendWncCmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs);
-    if (res == WNC_AT_CMD_OK)
+    if (res == WNC_AT_CMD_OK && pRespStr->size() > 0)
     {
         size_t pos1 = pRespStr->find("T:");
         size_t pos2 = pRespStr->rfind("OK");
@@ -1376,11 +1457,13 @@
 {
     string * pRespStr;
     string str(s);
+    AtCmdErr_e r;
 
     ipStr->erase(); // Clear out string until resolved!
     str = "AT@DNSRESVDON=\"" + str;
     str += "\"";
-    if (sendWncCmd(str.c_str(), &pRespStr, WNC_DNS_RESOLVE_WAIT_MS) == WNC_AT_CMD_OK) {
+    r = sendWncCmd(str.c_str(), &pRespStr, WNC_DNS_RESOLVE_WAIT_MS);
+    if (r == WNC_AT_CMD_OK && pRespStr->size() > 0) {
         size_t pos_start = pRespStr->find(":\"") + 2;
         if (pos_start !=  string::npos) {
             size_t pos_end = pRespStr->find("\"", pos_start) - 1;
@@ -1482,12 +1565,16 @@
     // 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("\"");
-        pos_end   = pRespStr->rfind("\"");
-        // Make sure search finds what it's looking for!
-        if (pos_start != string::npos && pos_end != string::npos) {
-            pos_start++;
-            i = pos_end - pos_start;  // Num hex chars, 2 per byte
+        if (pRespStr->size() > 0) {
+            pos_start = pRespStr->find("\"");
+            pos_end   = pRespStr->rfind("\"");
+            // Make sure search finds what it's looking for!
+            if (pos_start != string::npos && pos_end != string::npos) {
+                pos_start++;
+                i = pos_end - pos_start;  // Num hex chars, 2 per byte
+            }
+            else
+                i = 0;
         }
         else
             i = 0;
@@ -1534,12 +1621,16 @@
         // 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("\"");
-            pos_end   = pRespStr->rfind("\"");
-            // Make sure search finds what it's looking for!
-            if (pos_start != string::npos && pos_end != string::npos) {
-                pos_start++;
-                i = pos_end - pos_start;  // Num hex chars, 2 per byte
+            if (pRespStr->size() > 0) {
+                pos_start = pRespStr->find("\"");
+                pos_end   = pRespStr->rfind("\"");
+                // Make sure search finds what it's looking for!
+                if (pos_start != string::npos && pos_end != string::npos) {
+                    pos_start++;
+                    i = pos_end - pos_start;  // Num hex chars, 2 per byte
+                }
+                else
+                    i = 0;
             }
             else
                 i = 0;
@@ -1634,7 +1725,7 @@
 
         if (n == 0)
             continue;
-
+        
         if (rsp->rfind("OK") != string::npos) {
             stopTimerA();
             return (WNC_AT_CMD_OK);
@@ -1745,7 +1836,7 @@
     cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, m_sCmdTimeoutMs);       // Check RSSI,BER
     cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, m_sCmdTimeoutMs);     // Check if SIM locked
     
-    if ((cmdRes1 != WNC_AT_CMD_OK) && (cmdRes2 != WNC_AT_CMD_OK))
+    if ((cmdRes1 != WNC_AT_CMD_OK) || (cmdRes2 != WNC_AT_CMD_OK) || (pRespStr->size() == 0))
     {
         if (m_sMoreDebugEnabled)
         {
@@ -1768,7 +1859,7 @@
 
     // SIM card OK, now check for signal and cellular network registration
     cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, m_sCmdTimeoutMs);      // Check if registered on network
-    if (cmdRes1 != WNC_AT_CMD_OK)
+    if (cmdRes1 != WNC_AT_CMD_OK || pRespStr->size() == 0)
     {
         if (m_sMoreDebugEnabled)
             dbgPuts("------------ WNC +CREG? Fail! --------->");