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:
Thu Sep 01 02:28:10 2016 +0000
Parent:
0:affdbb35faa4
Child:
2:30d78cda6779
Commit message:
Had to revert back to MODSERIAL and cleaned up a bunch of minor things.

Changed in this revision

WncController.cpp Show annotated file Show diff for this revision Revisions of this file
WncController.h Show annotated file Show diff for this revision Revisions of this file
WncControllerK64F.cpp Show annotated file Show diff for this revision Revisions of this file
WncControllerK64F.h Show annotated file Show diff for this revision Revisions of this file
--- 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);
 };
--- a/WncController.h	Wed Aug 31 02:06:26 2016 +0000
+++ b/WncController.h	Thu Sep 01 02:28:10 2016 +0000
@@ -100,7 +100,7 @@
      *  constructor below.  Assumes UART is enabled, setup and ready to go. This
      *  class will read and write to this UART.
      */
-    WncController(const char * const apnStr);
+    WncController(void);
     
     // WncController( const char * const apnStr, MODSERIAL * wnc_uart, MODSERIAL * debug_uart = NULL);
     
@@ -140,7 +140,7 @@
      *  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 powerWncOn(uint8_t powerUpTimeoutSecs = MAX_POWERUP_TIMEOUT);
+    bool powerWncOn(const char * const apn, uint8_t powerUpTimeoutSecs = MAX_POWERUP_TIMEOUT);
 
     /**
      *  \brief  Query the WNC modem for its Internet attributes
@@ -295,6 +295,7 @@
     int dbgPutsNoTime(const char * s, bool crlf = true);
     int dbgPuts(const char * s, bool crlf = true);
     const char * _to_string(int64_t value);
+    const char * _to_hex_string(uint8_t value);    
 
     // Sends commands to WNC via
     enum AtCmdErr_e {
@@ -312,21 +313,21 @@
     virtual int putc(char c)              = 0;
     virtual int puts(const char * s)      = 0;
     virtual char getc(void)               = 0;
-    virtual int byteReady(void)           = 0;
-    virtual int dbgWriteByte(char b)      = 0;
-    virtual int dbgWriteBytes(const char *b) = 0;
+    virtual int charReady(void)           = 0;
+    virtual int dbgWriteChar(char b)      = 0;
+    virtual int dbgWriteChars(const char *b) = 0;
     virtual void waitMs(int t)            = 0;
     virtual void waitUs(int t)            = 0;
     virtual bool initWncModem(uint8_t powerUpTimeoutSecs) = 0;
     
     // Isolate OS timers
-    virtual int  getLogTimerTicks(void) = 0;
-    virtual void startTimerA(void)      = 0;
-    virtual void stopTimerA(void)       = 0;
-    virtual int  getUsTimerTicksA(void) = 0;
-    virtual void startTimerB(void)      = 0;
-    virtual void stopTimerB(void)       = 0;
-    virtual int  getUsTimerTicksB(void) = 0;
+    virtual int  getLogTimerTicks(void)  = 0;
+    virtual void startTimerA(void)       = 0;
+    virtual void stopTimerA(void)        = 0;
+    virtual int  getTimerTicksA_mS(void) = 0;
+    virtual void startTimerB(void)       = 0;
+    virtual void stopTimerB(void)        = 0;
+    virtual int  getTimerTicksB_mS(void) = 0;
         
     bool waitForPowerOnModemToRespond(uint8_t powerUpTimeoutSecs);    
 
@@ -335,11 +336,11 @@
     bool softwareInitMdm(void);
     bool checkCellLink(void);
     AtCmdErr_e mdmSendAtCmdRsp(const char * cmd, int timeout_ms, string * rsp, bool crLf = true);
-    size_t mdmGetline(string & buff, int timeout_ms);
+    size_t mdmGetline(string * buff, int timeout_ms);
     AtCmdErr_e sendWncCmd(const char * const s, string ** r, int ms_timeout);
     bool at_at_wnc(void);
     bool at_init_wnc(bool hardReset = false);
-    bool at_sockopen_wnc(const string & ipStr, uint16_t port, uint16_t numSock, bool tcp, uint16_t timeOutSec);
+    bool at_sockopen_wnc(const char * const ip, uint16_t port, uint16_t numSock, bool tcp, uint16_t timeOutSec);
     bool at_sockclose_wnc(uint16_t numSock);
     bool at_dnsresolve_wnc(const char * s, string * ipStr);
     AtCmdErr_e at_sockwrite_wnc(const char * s, uint32_t n, uint16_t numSock, bool isTcp);
@@ -353,7 +354,7 @@
     size_t at_readSMStext_wnc(const char ** log);
     bool at_getrssiber_wnc(int16_t * dBm, int16_t * ber3g);
     void closeOpenSocket(uint16_t numSock);
-    bool sockWrite(const char * s, uint32_t n, uint16_t numSock, bool isTcp);
+    bool sockWrite(const char * const s, uint32_t n, uint16_t numSock, bool isTcp);
     bool at_sendSMStextMem_wnc(char n);
     bool at_deleteSMSTextFromMem_wnc(char n);
     bool at_saveSMStext_wnc(const char * const phoneNum, const char * const text, char * msgIdx);
--- a/WncControllerK64F.cpp	Wed Aug 31 02:06:26 2016 +0000
+++ b/WncControllerK64F.cpp	Thu Sep 01 02:28:10 2016 +0000
@@ -30,8 +30,7 @@
 
 using namespace WncControllerK64F_fk;
 
-WncControllerK64F::WncControllerK64F(const char * const apnStr, struct WncGpioPinListK64F * pPins, Serial * wnc_uart, Serial * debug_uart)
-: WncController(apnStr)
+WncControllerK64F::WncControllerK64F(struct WncGpioPinListK64F * pPins, MODSERIAL * wnc_uart, MODSERIAL * debug_uart)
 {
     m_logTimer.start(); // Start the log timer now!    
     m_pDbgUart = debug_uart;
@@ -54,12 +53,12 @@
     return (m_pWncUart->getc());
 }
 
-int WncControllerK64F::byteReady(void)
+int WncControllerK64F::charReady(void)
 {
     return (m_pWncUart->readable());
 }
 
-int WncControllerK64F::dbgWriteByte(char b)
+int WncControllerK64F::dbgWriteChar(char b)
 {
     if (m_pDbgUart != NULL)
         return (m_pDbgUart->putc(b));
@@ -67,7 +66,7 @@
         return (0);
 }
 
-int WncControllerK64F::dbgWriteBytes(const char * b)
+int WncControllerK64F::dbgWriteChars(const char * b)
 {
     if (m_pDbgUart != NULL)
         return (m_pDbgUart->puts(b));
@@ -138,9 +137,9 @@
     m_timerA.stop();
 }
 
-int  WncControllerK64F::getUsTimerTicksA(void)
+int  WncControllerK64F::getTimerTicksA_mS(void)
 {
-    return (m_timerA.read_us());
+    return (m_timerA.read_ms());
 }
 
 void WncControllerK64F::startTimerB(void)
@@ -154,7 +153,7 @@
     m_timerB.stop();
 }
 
-int  WncControllerK64F::getUsTimerTicksB(void)
+int  WncControllerK64F::getTimerTicksB_mS(void)
 {
-    return (m_timerB.read_us());
+    return (m_timerB.read_ms());
 }
--- a/WncControllerK64F.h	Wed Aug 31 02:06:26 2016 +0000
+++ b/WncControllerK64F.h	Thu Sep 01 02:28:10 2016 +0000
@@ -69,7 +69,7 @@
      *  constructor below.  Assumes UART is enabled, setup and ready to go. This
      *  class will read and write to this UART.
      */
-    WncControllerK64F( const char * const apnStr, struct WncGpioPinListK64F * pPins, Serial * wnc_uart, Serial * debug_uart = NULL);
+    WncControllerK64F(struct WncGpioPinListK64F * pPins, MODSERIAL * wnc_uart, MODSERIAL * debug_uart = NULL);
     
 private:
 
@@ -80,9 +80,9 @@
     virtual int putc(char c);
     virtual int puts(const char * s);
     virtual char getc(void);
-    virtual int byteReady(void);
-    virtual int dbgWriteByte(char b);
-    virtual int dbgWriteBytes(const char *b);
+    virtual int charReady(void);
+    virtual int dbgWriteChar(char b);
+    virtual int dbgWriteChars(const char *b);
     virtual bool initWncModem(uint8_t powerUpTimeoutSecs);
     virtual void waitMs(int t);
     virtual void waitUs(int t);
@@ -90,13 +90,13 @@
     virtual int  getLogTimerTicks(void);
     virtual void startTimerA(void);
     virtual void stopTimerA(void);
-    virtual int  getUsTimerTicksA(void);
+    virtual int  getTimerTicksA_mS(void);
     virtual void startTimerB(void);
     virtual void stopTimerB(void);
-    virtual int  getUsTimerTicksB(void);
+    virtual int  getTimerTicksB_mS(void);
 
-    Serial * m_pDbgUart;
-    Serial * m_pWncUart;
+    MODSERIAL * m_pDbgUart;
+    MODSERIAL * m_pWncUart;
     WncGpioPinListK64F m_gpioPinList;
     Timer m_logTimer;
     Timer m_timerA;