Remote I/O Sensor bus with AT&T flow and M2X cloud

Dependencies:   DHT11 FXOS8700CQ MODSERIAL mbed

Fork of Avnet_ATT_Cellular_IOT by Avnet

Revision:
46:da9d788f5d5a
Parent:
45:a836eecd5d12
Child:
53:dcccf2881fa0
--- a/wnc_control.cpp	Sun Jul 24 17:50:42 2016 +0000
+++ b/wnc_control.cpp	Sun Jul 24 19:33:23 2016 +0000
@@ -15,7 +15,8 @@
 enum WNC_ERR_e {
     WNC_OK =0,
     WNC_CMD_ERR = -1,
-    WNC_NO_RESPONSE = -2
+    WNC_NO_RESPONSE = -2,
+    WNC_CELL_LINK_DOWN = -3
 };
 
 // Contains result of last call to send_wnc_cmd(..)
@@ -64,7 +65,7 @@
 //     wait_ms(31000);
 //     at_send_wnc_cmd("AT+CFUN=1,0", &pRespStr, WNC_TIMEOUT_MS);
 //     wait_ms(31000);
-          WNC_MDM_ERR = WNC_NO_RESPONSE;
+          WNC_MDM_ERR = WNC_CELL_LINK_DOWN;
       }
   } while (WNC_MDM_ERR != WNC_OK);
 }
@@ -82,8 +83,6 @@
       else if (WNC_MDM_ERR == WNC_CMD_ERR)
       {
         pc.puts("Bad URL!!!!!!\r\n");
-        MyServerIpAddress = "192.168.0.1";
-        WNC_MDM_ERR = WNC_OK;
       }
     } while (WNC_MDM_ERR != WNC_OK);
     
@@ -224,13 +223,14 @@
     string * pRespStr;
     size_t pos;
     int regSts;
+    int cmdRes1, cmdRes2;
 
     pc.puts("<-------- Begin Cell Status ------------\r\n");
-  
-    at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS);       // Check RSSI,BER
-    at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS);      // Check if SIM locked
-  
-    if (WNC_MDM_ERR == WNC_NO_RESPONSE)
+
+    cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS);       // Check RSSI,BER
+    cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS);      // Check if SIM locked
+    
+    if ((cmdRes1 != 0) && (cmdRes2 != 0))
     {
         pc.puts("------------ WNC No Response! --------->\r\n");
         return (-2);      
@@ -244,7 +244,7 @@
     }
   
     // SIM card OK, now check for signal and cellular network registration
-    at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);      // Check if registered on network
+    cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);      // Check if registered on network
     pos = pRespStr->find("CREG: ");
     if (pos != string::npos)
     {
@@ -267,6 +267,8 @@
 // Sets a global with failure or success, assumes 1 thread all the time
 int send_wnc_cmd(const char * s, string ** r, int ms_timeout)
 {
+  int cmdRes;
+  
   if (check_wnc_ready() < 0)
   {
      static string noRespStr;
@@ -274,14 +276,25 @@
      pc.puts("FAIL send cmd: ");
      pc.puts(truncStr.c_str());
      pc.puts("\r\n");
-     WNC_MDM_ERR = WNC_NO_RESPONSE;
+     WNC_MDM_ERR = WNC_CELL_LINK_DOWN;
      noRespStr.erase();
      *r = &noRespStr;
-     return (-2);
+     return (-3);
   }
   
   // If WNC ready, send user command
-  return (at_send_wnc_cmd(s, r, ms_timeout));
+  cmdRes = at_send_wnc_cmd(s, r, ms_timeout);
+  
+  if (cmdRes == -1)
+     WNC_MDM_ERR = WNC_CMD_ERR;
+  
+  if (cmdRes == -2)
+     WNC_MDM_ERR = WNC_NO_RESPONSE;
+
+  if (cmdRes == 0)
+     WNC_MDM_ERR = WNC_OK;
+  
+  return (cmdRes);
 }
 
 int at_send_wnc_cmd(const char * s, string ** r, int ms_timeout)
@@ -313,17 +326,12 @@
       pc.puts("]\n\r");
 
       if (res > 0)
-      {
-          if (WNC_MDM_ERR != WNC_NO_RESPONSE)
-            WNC_MDM_ERR = WNC_CMD_ERR;
           return -1;
-      }
       else
           return 0;
   }
   else
   {
-      WNC_MDM_ERR = WNC_NO_RESPONSE;
       pc.puts("No response from WNC!\n\r");
       return -2;
   }
@@ -342,6 +350,7 @@
   static bool intSet = false;
   static bool sockDialSet = false;  
   string * pRespStr;
+  int cmdRes;
   
   if (hardReset == true)
   {
@@ -351,22 +360,27 @@
       sockDialSet = false;
   }
   
-  WNC_MDM_ERR = WNC_OK;  // Below commands will re-establish error state
   pc.puts("Start AT init of WNC:\r\n");
   // Quick commands below do not need to check cellular connectivity
-  at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
-  at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS);           // Echo Off
-  at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS);      // 2 - verbose error, 1 - numeric error, 0 - just ERROR
+  cmdRes = at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
+  cmdRes += at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS);           // Echo Off
+  cmdRes += at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS);      // 2 - verbose error, 1 - numeric error, 0 - just ERROR
   
   // If the simple commands are not working no chance of more complex.
   //  I have seen re-trying commands make it worse.
-  if (WNC_MDM_ERR != WNC_OK)
+  if (cmdRes < 0)
+  {
+      // Since I used the at_send_wnc_cmd I am setting the error state based upon
+      //  the responses.  And since these are simple commands, even if the WNC
+      //  is saying ERROR, treat it like a no response.
+      WNC_MDM_ERR = WNC_NO_RESPONSE;
       return ;
+  }
   
-  if ((WNC_MDM_ERR == WNC_OK) && (intSet == false))
-    send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);
+  if (intSet == false)
+    cmdRes = send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);
   
-  if (WNC_MDM_ERR == WNC_OK)
+  if (cmdRes == 0)
     intSet = true;
   else
     return ;
@@ -376,18 +390,18 @@
     string cmd_str("AT%PDNSET=1,");
     cmd_str += MY_APN_STR;
     cmd_str += ",IP";
-    send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
+    cmdRes = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
   }
   
-  if (WNC_MDM_ERR == WNC_OK)
+  if (cmdRes == 0)
     pdnSet = true;
   else
     return ;
   
   if (sockDialSet == false)
-    send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
+    cmdRes = send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
   
-  if (WNC_MDM_ERR == WNC_OK)
+  if (cmdRes == 0)
     sockDialSet = true;
   else
     return ;
@@ -418,7 +432,7 @@
   string * pRespStr;
   string str(s);
   str = "AT@DNSRESVDON=\"" + str + "\"";
-  if (send_wnc_cmd(str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0)
+  if (send_wnc_cmd(str.c_str(), &pRespStr, 15000) == 0)
   {
     size_t pos_start = pRespStr->find(":\"") + 2;
     size_t pos_end = pRespStr->rfind("\"") - 1;