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

Fork of WncControllerLibrary by Fred Kellerman

Revision:
1:ac2de545b981
Parent:
0:affdbb35faa4
Child:
4:c5720f4d13ff
--- a/WncController.cpp	Wed Aug 31 02:06:26 2016 +0000
+++ b/WncController.cpp	Thu Sep 01 02:28:10 2016 +0000
@@ -97,6 +97,12 @@
     return (str);
 }
 
+const char * WncController::_to_hex_string(uint8_t value)
+{
+    static char str[3];   // room for 8-bit + null
+    itoa(value, str, 16);
+    return (str);
+}
 
 /**
  *  \brief Constructor for UART controlled WNC
@@ -110,9 +116,8 @@
  *  a constructor should be added for each type just like the SerialBuffered
  *  constructor below.
  */
-WncController::WncController(const char * const apnStr)
+WncController::WncController(void)
 {
-    m_sApnStr = apnStr;
 }
 
 void WncController::enableDebug(bool on, bool moreDebugOn)
@@ -174,14 +179,14 @@
  *  and bring it to life.  It will also initialize the WNC enough to get it to be able to open sockets
  *  (with AT commands)
  */
-bool WncController::powerWncOn(uint8_t powerUpTimeoutSecs)
+bool WncController::powerWncOn(const char * const apn, uint8_t powerUpTimeoutSecs)
 {
     dbgPuts("Waiting for WNC to Initialize...");
     m_sPowerUpTimeoutSecs = powerUpTimeoutSecs;
     m_sState = WNC_ON_NO_CELL_LINK;  // Turn soft on to allow "AT" for init to be sent!
     if (initWncModem(powerUpTimeoutSecs) == true) {
         // Set the Apn
-        setApnName(m_sApnStr.c_str());
+        setApnName(apn);
         if (false == softwareInitMdm()) {
             dbgPuts("Software init failed!");
             m_sState = WNC_OFF;
@@ -192,7 +197,7 @@
         m_sState = WNC_OFF;
     }
     
-    return (m_sState == WNC_ON_NO_CELL_LINK);
+    return (m_sState != WNC_ON_NO_CELL_LINK);
 }
 
 size_t WncController::sendCustomCmd(const char * cmd, char * resp, size_t sizeRespBuf, int ms_timeout)
@@ -345,7 +350,7 @@
         m_sSock[numSock].myPort = port;
         m_sSock[numSock].isTcp = tcp;
         m_sSock[numSock].timeOutSec = timeOutSec;
-        m_sSock[numSock].open = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr, port, numSock, tcp, timeOutSec);
+        m_sSock[numSock].open = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), port, numSock, tcp, timeOutSec);
         if (m_sSock[numSock].open == false) {
             dbgPuts("Socket open fail!!!!");
             // Work-around.  If the sock open fails it needs to be told
@@ -374,7 +379,7 @@
  *  arriving at the endpoint.
  */
  
-bool WncController::sockWrite(const char * s, uint32_t n, uint16_t numSock, bool isTcp)
+bool WncController::sockWrite(const char * const s, uint32_t n, uint16_t numSock, bool isTcp)
 {
     bool result = true;;
     
@@ -382,7 +387,7 @@
     if (cmdRes != WNC_AT_CMD_OK) {
         if ((cmdRes == WNC_AT_CMD_ERREXT) || (cmdRes == WNC_AT_CMD_TIMEOUT) || (cmdRes == WNC_AT_CMD_ERRCME))
         {
-            // This may throw away any data that hasn't been wrote out of the WNC
+            // This may throw away any data that hasn't been written out of the WNC
             //  but at this point with the way the WNC currently works we have
             //  no choice.
             closeOpenSocket(numSock);
@@ -575,19 +580,19 @@
 
 // Note: If you want to make it more portable, create a
 // arecharsavailable() and readchar()
-size_t WncController::mdmGetline(string & buff, int timeout_ms)
+size_t WncController::mdmGetline(string * buff, int timeout_ms)
 {
     char chin = '\0';
     char chin_last;
     size_t len = 0;
 
     startTimerB();
-    while ((len < (MAX_LEN_WNC_CMD_RESPONSE - 1)) && (getUsTimerTicksB() < timeout_ms)) {
-        if (byteReady()) {
+    while ((len < (MAX_LEN_WNC_CMD_RESPONSE - 1)) && (getTimerTicksB_mS() < timeout_ms)) {
+        if (charReady()) {
             chin_last = chin;
-            chin = (char)getc();
+            chin = getc();
             if (isprint(chin)) {
-                buff += (char)chin;
+                *buff += chin;
                 len++;  // Bound the copy length to something reaonable just in case
                 continue;
             }
@@ -595,7 +600,6 @@
                 break;
             }
         }
-        rx_char_wait();
     }
     stopTimerB();
     
@@ -751,7 +755,7 @@
     do {
         dbgPuts("Try to close and re-open socket");
         at_sockclose_wnc(numSock);
-        m_sSock[numSock].open = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr, m_sSock[numSock].myPort, numSock, m_sSock[numSock].isTcp, m_sSock[numSock].timeOutSec);
+        m_sSock[numSock].open = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), m_sSock[numSock].myPort, numSock, m_sSock[numSock].isTcp, m_sSock[numSock].timeOutSec);
         if (m_sSock[numSock].open == false)
             dbgPuts("Failed to re-open socket!");
     } while (m_sSock[numSock].open == false);
@@ -1225,7 +1229,7 @@
 }
 
 
-bool WncController::at_sockopen_wnc(const string & ipStr, uint16_t port, uint16_t numSock, bool tcp, uint16_t timeOutSec)
+bool WncController::at_sockopen_wnc(const char * const ip, uint16_t port, uint16_t numSock, bool tcp, uint16_t timeOutSec)
 {
     string * pRespStr;
     string cmd_str("AT@SOCKCREAT=");
@@ -1241,7 +1245,7 @@
         cmd_str = "AT@SOCKCONN=";
         cmd_str += _to_string(numSock + 1);
         cmd_str += ",\"";
-        cmd_str += ipStr;
+        cmd_str += ip;
         cmd_str += "\",";
         cmd_str += _to_string(port);
         cmd_str += ",";
@@ -1304,8 +1308,9 @@
     // sending simple 'AT' commands to modem once per second.
     if (timeoutSecs > 0) {
         do {
-            dbgPuts("\rWaiting ", false); dbgPutsNoTime(_to_string(timeoutSecs), false);
             timeoutSecs--;
+            dbgPutsNoTime("\rWaiting ", false); dbgPutsNoTime(_to_string(timeoutSecs), false);
+            dbgPutsNoTime(" ", false);
             AtCmdErr_e rc = mdmSendAtCmdRsp("AT", 500, &m_sWncStr);
             if (rc == WNC_AT_CMD_OK) {
                 dbgPutsNoTime("");  // CR LF
@@ -1314,6 +1319,7 @@
             waitMs(500);
         }
         while (timeoutSecs > 0);    
+        dbgPutsNoTime(""); // CR LF
     }
     
     return (false);
@@ -1325,7 +1331,7 @@
 
     if ((n > 0) && (n <= MAX_WNC_WRITE_BYTES)) {
         string * pRespStr;
-        char num2str[10];
+        const char * num2str;
         string cmd_str;
 
         if (isTcp == true)
@@ -1335,17 +1341,14 @@
 
         cmd_str += _to_string(numSock + 1);
         cmd_str += ",";
-        _to_string(n);
+        cmd_str += _to_string(n);
         cmd_str += num2str;
         cmd_str += ",\"";
         while(*s != '\0') {
-            _to_string((int8_t)*s++);
+            num2str = _to_hex_string((uint8_t)*s++);
             // Always 2-digit ascii hex:
-            if (strlen(num2str) == 1) {
-                num2str[2] = '\0';
-                num2str[1] = num2str[0];
-                num2str[0] = '0';
-            }
+            if (strlen(num2str) == 1)
+                cmd_str += '0';
             cmd_str += num2str;
         }
         cmd_str += "\"";
@@ -1391,13 +1394,13 @@
             else
                 i = 0;
             
-            // If data convert the hex string into byte values
+            // If data, convert the hex string into byte values
             if (i > 0 && i <= (2*MAX_WNC_READ_BYTES))
             {
                 string byte;
                 while (pos_start < pos_end) {
                     byte = pRespStr->substr(pos_start, 2);
-                    *pS += (char)strtol(byte.c_str(), NULL, 16);
+                    *pS += (uint8_t)strtol(byte.c_str(), NULL, 16);
                     pos_start += 2;
                     (*numRead)++;
                 }
@@ -1471,8 +1474,8 @@
     }
 
     startTimerA();
-    while (getUsTimerTicksA() < timeout_ms) {
-        n = mdmGetline(*rsp, timeout_ms - getUsTimerTicksA());
+    while (getTimerTicksA_mS() < timeout_ms) {
+        n = mdmGetline(rsp, timeout_ms - getTimerTicksA_mS());
 
         if (n == 0)
             continue;
@@ -1659,9 +1662,9 @@
 int WncController::dbgPutsNoTime(const char * s, bool crlf)
 {
     if (m_sDebugEnabled == true) {
-        int r = dbgWriteBytes(s);
+        int r = dbgWriteChars(s);
         if (crlf == true)
-            return (dbgWriteBytes("\r\n"));
+            return (dbgWriteChars("\r\n"));
         else
             return (r);
     }
@@ -1673,11 +1676,12 @@
 {
     dbgPutsNoTime("[*] ", false);
     dbgPutsNoTime(_to_string(getLogTimerTicks()), false);
-    dbgPutsNoTime(" ");
+    dbgPutsNoTime(" ", false);
 
     int r = dbgPutsNoTime(s, false);
+    
     if (crlf == true)
-        return (dbgPutsNoTime("\r\n", false));
+        return (dbgPutsNoTime("", true));
     else
         return (r);
 };