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:
Tue Sep 13 19:10:34 2016 +0000
Parent:
17:59b1e9341188
Child:
19:83a52353b97e
Commit message:
Enhanced SMS.; Added ICCID.; Added MSISDN.; Increased max sockets at once to 5.;

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
--- a/WncController.cpp	Sat Sep 10 02:55:56 2016 +0000
+++ b/WncController.cpp	Tue Sep 13 19:10:34 2016 +0000
@@ -207,10 +207,11 @@
     
     if (sizeRespBuf > 0) {
         AtCmdErr_e r = at_send_wnc_cmd(cmd, &respStr, ms_timeout);
-        if (respStr->size() < sizeRespBuf) {
-            strcpy(resp, respStr->c_str());
-            return (respStr->size());
-        }
+        strncpy(resp, respStr->c_str(), sizeRespBuf);
+        if (respStr->size() > sizeRespBuf)
+            dbgPuts("sendCustomCmd truncated!");
+            
+        return (respStr->size());
     }
     
     dbgPuts("sendCustomCmd: would have overrun!");
@@ -855,6 +856,52 @@
     } while (m_sSock[numSock].open == false);
 }
 
+
+bool WncController::getICCID(string * iccid)
+{
+    string * respStr;
+    
+    iccid->erase();
+    
+    AtCmdErr_e r = at_send_wnc_cmd("AT%CCID", &respStr, m_sCmdTimeoutMs);
+
+    if (r != WNC_AT_CMD_OK || respStr->size() == 0)
+        return (false);
+
+    size_t pos = respStr->find("AT%CCID");
+    if (pos == string::npos)
+        return (false);
+    
+    size_t posOK = respStr->rfind("OK");
+    if (posOK == string::npos)
+        return (false);
+
+    pos += 7; // Advanced to the number
+    *iccid = respStr->substr(pos, posOK - pos);
+    
+    return (true);
+}
+
+bool WncController::convertICCIDtoMSISDN(const string & iccid, string * msisdn)
+{
+    msisdn->erase();
+    
+    if (iccid.size() != 20 && iccid.size() != 19) {
+        dbgPuts("Invalid ICCID length!");
+        return (false);
+    }
+ 
+    *msisdn = "882350";
+    
+    if (iccid.size() == 20)
+        *msisdn += iccid.substr(10,iccid.size() - 11);
+    else
+        *msisdn += iccid.substr(10,iccid.size() - 10);
+    
+    return (true);
+}
+
+
 bool WncController::sendSMSText(const char * const phoneNum, const char * const text)
 {
     if (at_sendSMStext_wnc(phoneNum, text) == true)
@@ -865,15 +912,113 @@
     }
 }
 
-size_t WncController::readSMSLog(const char ** log)
+bool WncController::readSMSLog(struct WncSmsList * log)
 {
-    size_t n;
+    string * logStr;
+    uint16_t i;
+    
+    if (at_readSMSlog_wnc(&logStr) == false) {
+        dbgPuts("readSMSLog: Failed!");
+        return (false);
+    }
+    
+    dbgPuts(logStr->c_str());
+
+    // Clean slate    
+    log->msgCount = 0;
+
+    // Pick out the stuff from the string and convert to struct
+    string s;
+    size_t pos2;
+    size_t pos = logStr->find("+CMGL:");
 
-    n = at_readSMSlog_wnc(log);
-    if (n == 0)
-        dbgPuts("readSMSLog: Failed!");
+    for(i=0; i<MAX_WNC_SMS_MSG_SLOTS; i++) {
+        if (pos == string::npos)
+            return (false);
+        pos = logStr->find(",\"");
+        if (pos == string::npos)
+            return (false);
+        pos += 3;  // Advance to the text we want
+        pos2 = logStr->find("\",", pos);
+        if ((pos2 == string::npos) || (pos >= pos2))
+            return (false);
+            
+        // Setup attributes
+        log->e[i].unread = false;
+        log->e[i].incoming = false;
+        log->e[i].unsent = false;
+        
+        s = logStr->substr(pos, pos2 - pos);
+        if (s.find("READ") != string::npos)
+            log->e[i].incoming = true;
+        if (s.find("REC UNREAD") != string::npos)
+            log->e[i].unread = true;
+        if (s.find("STO UNSENT") != string::npos)
+            log->e[i].unsent = true;
+            
+        // Tele number
+        pos2 += 3;  // Advance to next field
+        pos = logStr->find("\",", pos2);
+        if ((pos == string::npos) || (pos2 > pos))
+            return (false);
+        if (pos == pos2)
+            log->e[i].number.erase();
+        else    
+            log->e[i].number = logStr->substr(pos2, pos - pos2);
+        
+        // Timestamp
+        pos = pos2 + 4; // Beginning of timestamp field
+        pos2 = logStr->find("\",", pos); // End of timestamp field
+        if ((pos2 == string::npos) || (pos > pos2))
+            return (false);
+        if (pos == pos2)
+            log->e[i].timestamp.erase();
+        else
+            log->e[i].timestamp = logStr->substr(pos, pos2 - pos);
         
-    return (n);
+        // Message field
+        
+        // We don't know how many messages we have so the next search
+        // could end with +CMGL or OK.
+        pos2 += 2;  // Advanced to message text
+        pos = logStr->find("+CMGL", pos2);
+        if (pos == string::npos) {
+            pos = logStr->find("OK", pos2);
+            if (pos == string::npos) {
+                dbgPuts("Strange SMS Log Ending!");
+                return (false);
+            }
+            i = MAX_WNC_SMS_MSG_SLOTS; // break
+        }
+        if (pos2 > pos)
+            return (false);
+        if (pos2 == pos)
+            log->e[log->msgCount].msg.erase();
+        else
+            log->e[log->msgCount].msg = logStr->substr(pos2, pos - pos2);
+
+        log->msgCount++;  // Message complete
+    }    
+    
+    return (true);
+}
+
+bool WncController::readUnreadSMSText(struct WncSmsList * w)
+{
+    struct WncController::WncSmsList tmp;
+    
+    if (readSMSLog(&tmp) == false)
+        return (false);
+    
+    w->msgCount = 0;
+    for(uint16_t i = 0; i < tmp.msgCount; i++) {
+        if (tmp.e[i].unread == true) {
+            w->e[w->msgCount] = tmp.e[i];
+            w->msgCount++;
+        }
+    }
+    
+    return (true);
 }
 
 size_t WncController::getSignalQuality(const char ** log)
@@ -998,17 +1143,6 @@
     return (false);
 }
 
-size_t WncController::readSMSText(const char n, const char ** msg)
-{
-    size_t i;
-
-    i = at_readSMStext_wnc(n,msg);
-    if (i == 0)
-        dbgPuts("readSMSText: Failed!");
-        
-    return (n);
-}
-
 bool WncController::at_get_wnc_net_stats(WncIpStats * s)
 {
     string * pRespStr;
@@ -1243,7 +1377,7 @@
     string * pRespStr;
     size_t l = strlen(text);
     
-    if (l <= MAX_WNC_SMS_LENGTH)
+    if (l <= MAX_WNC_SMS_MSG_SLOTS)
     {
         // Check to see if the SMS service is available
         checkCellLink();
@@ -1331,20 +1465,9 @@
     return (false);
 }
 
-
-size_t WncController::at_readSMSlog_wnc(const char ** log)
+bool WncController::at_readSMSlog_wnc(string ** log)
 {
-    string * pRespStr;
-    static string smsLogStr;
-    
-    smsLogStr.erase();
-
-    if (at_send_wnc_cmd("AT+CMGL", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK)
-        smsLogStr = *pRespStr;
-        
-    *log = smsLogStr.c_str();
-    
-    return (pRespStr->size());
+    return (at_send_wnc_cmd("AT+CMGL", log, m_sCmdTimeoutMs) == WNC_AT_CMD_OK);
 }
 
 size_t WncController::at_readSMStext_wnc(const char n, const char ** log)
--- a/WncController.h	Sat Sep 10 02:55:56 2016 +0000
+++ b/WncController.h	Tue Sep 13 19:10:34 2016 +0000
@@ -46,21 +46,14 @@
  */
 
 
-static const uint8_t  MAX_LEN_IP_STR = 16;    // Length includes room for the extra NULL
-static const uint16_t WNC_MAX_SMS_MSG_SLOTS     = 3;     // How many SMS messages the WNC can store and receive at a time.
-
-struct WncSmsInfo
-{
-    char idx[WNC_MAX_SMS_MSG_SLOTS];
-    string msgText[WNC_MAX_SMS_MSG_SLOTS];
-};
-
+static const uint8_t  MAX_LEN_IP_STR = 16;         // Length includes room for the extra NULL
 
 /**
  *  \brief  Contains info fields for the WNC Internet Attributes
  */
 struct WncIpStats
 {
+    string wncMAC;
     char ip[MAX_LEN_IP_STR];
     char mask[MAX_LEN_IP_STR];
     char gateway[MAX_LEN_IP_STR];
@@ -68,21 +61,10 @@
     char dnsSecondary[MAX_LEN_IP_STR];
 };
 
-struct WncDateTime
-{
-    uint8_t  year;
-    uint8_t  month;
-    uint8_t  day;
-    uint8_t  hour;
-    uint8_t  min;
-    uint8_t  sec;
-};
-        
 class WncController
 {
 public:
-
-    static const unsigned MAX_NUM_WNC_SOCKETS = 1;  // Max number of simultaneous sockets that the WNC supports
+    static const unsigned MAX_NUM_WNC_SOCKETS = 5;  // Max number of simultaneous sockets that the WNC supports
     static const unsigned MAX_POWERUP_TIMEOUT = 60; // How long the powerUp method will try to turn on the WNC Shield
                                                     //  (this is the default if the user does not over-ride on power-up
 
@@ -274,9 +256,33 @@
     //  SMS messaging
     ///////////////////////////////////////////
 
+    static const uint16_t MAX_WNC_SMS_MSG_SLOTS = 3;   // How many SMS messages the WNC can store and receive at a time.
+    static const uint16_t MAX_WNC_SMS_LENGTH    = 160; // The maximum length of an SMS message the WNC can send and receive.
+    
+    struct WncSmsInfo
+    {
+        // Content
+        string number;
+        string timestamp;
+        string msg;
+        
+        // Attributes
+        bool incoming;
+        bool unsent;
+        bool unread;
+    };
+
+    struct WncSmsList
+    {
+        uint8_t msgCount;
+        WncSmsInfo e[MAX_WNC_SMS_MSG_SLOTS];
+    };
+
     bool sendSMSText(const char * const phoneNum, const char * const text);
 
-    size_t readSMSLog(const char ** log);
+    bool readSMSLog(struct WncSmsList * log);
+
+    bool readUnreadSMSText(struct WncSmsList * w);
     
     bool saveSMSText(const char * const phoneNum, const char * const text, char * msgIdx);
     
@@ -284,9 +290,9 @@
 
     bool deleteSMSTextFromMem(char msgIdx);
     
-    size_t readSMSText(const char n, const char ** msg);
-    
-    bool readUnreadSMSText(WncSmsInfo * w);
+    bool getICCID(string * iccid);
+
+    bool convertICCIDtoMSISDN(const string & iccid, string * msisdn);
     
     ///////////////////////////////////////////
     // Neighborhood Cell Info
@@ -294,6 +300,16 @@
     size_t getSignalQuality(const char ** log);
     
     //  Date Time
+    struct WncDateTime
+    {
+        uint8_t  year;
+        uint8_t  month;
+        uint8_t  day;
+        uint8_t  hour;
+        uint8_t  min;
+        uint8_t  sec;
+    };
+
     bool getTimeDate(struct WncDateTime * tod);
     
     // Ping
@@ -365,7 +381,7 @@
     bool at_setapn_wnc(const char * const apnStr);
     bool at_sendSMStext_wnc(const char * const phoneNum, const char * const text);
     bool at_get_wnc_net_stats(WncIpStats * s);
-    size_t at_readSMSlog_wnc(const char ** log);
+    bool at_readSMSlog_wnc(string ** log);
     size_t at_readSMStext_wnc(const char ** log);
     size_t at_readSMStext_wnc(const char n, const char ** log);
     bool at_getrssiber_wnc(int16_t * dBm, int16_t * ber3g);
@@ -393,7 +409,6 @@
     static const uint16_t WNC_CMD_TIMEOUT_MS        = 40000; // Sets default (may be overriden) time that the software waits for an AT response from the WNC.
     static const uint16_t WNC_QUICK_CMD_TIMEOUT_MS  = 2000;  // Used for simple commands that should immediately respond such as "AT", cmds that are quicker than WNC_CMD_TIMEOUT_MS.
     static const uint16_t WNC_WAIT_FOR_AT_CMD_MS    = 0;     // Wait this much between multiple in a row AT commands to the WNC.
-    static const uint16_t MAX_WNC_SMS_LENGTH        = 150;   // The maximum length of an SMS message the WNC can send and receive.
     static const uint16_t WNC_SOFT_INIT_RETRY_COUNT = 10;    // How many times the WNC will be tried to revive if it stops responding.
     static const uint16_t WNC_DNS_RESOLVE_WAIT_MS   = 60000; // How much time to wait for the WNC to respond to a DNS resolve/lookup.
     static const uint16_t WNC_TRUNC_DEBUG_LENGTH    = 80;    // Always make this an even number, how many chars for the debug output before shortening the debug ouput, this is used when moreDebug = false.