V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Revision:
29:f71a0be59b99
Parent:
28:54d9a550adf1
--- a/WNCInterface/WncControllerK64F/WncController/WncController.cpp	Mon Oct 09 21:13:49 2017 +0000
+++ b/WNCInterface/WncControllerK64F/WncController/WncController.cpp	Fri Nov 03 20:28:02 2017 +0000
@@ -60,11 +60,12 @@
 bool WncController::m_sReadyForSMS = false;
 
 string WncController::m_sChannelID;
-char WncController::sOutput[4000];
+char WncController::sOutput[100];
 char WncController::sCommand[256];
-unsigned char WncController::ucObject[2500];
-
-
+//unsigned char WncController::ucObject[2500];
+string sObject;
+string sBlockObject, sByte;
+unsigned char sSubjectName[100];
 
 
 /**
@@ -204,13 +205,39 @@
     if (initWncModem(powerUpTimeoutSecs) == true) 
     {
         //read certificates and keys
+        
         dbgPuts("Reading Object: Device Certificate");
-        getObject("00", (unsigned char *)AWS_IOT_CERTIFICATE, &AWS_IOT_CERTIFICATE_LENGTH);
+        if (getObject("00", (unsigned char *)AWS_IOT_CERTIFICATE, &AWS_IOT_CERTIFICATE_LENGTH) == false)
+        {
+            dbgPuts("Device Certificate Read Failed");
+            m_sState = WNC_OFF;
+            return false;
+        }
         //getObject("00", ucObject);
         dbgPuts("Reading Object: Device Private Key");
-        getObject("01", (unsigned char *)AWS_IOT_PRIVATE_KEY, &AWS_IOT_PRIVATE_KEY_LENGTH);
+        if (getObject("01", (unsigned char *)AWS_IOT_PRIVATE_KEY, &AWS_IOT_PRIVATE_KEY_LENGTH) == false)
+        {
+            dbgPuts("Device Private Key Read Failed");
+            m_sState = WNC_OFF;
+            return false;
+        }
         dbgPuts("Reading Object: Root CA Certificate");
-        getObject("02", (unsigned char *)AWS_IOT_ROOT_CA, &AWS_IOT_ROOT_CA_LENGTH);
+        if (getObject("02", (unsigned char *)AWS_IOT_ROOT_CA, &AWS_IOT_ROOT_CA_LENGTH) == false)
+        {
+            dbgPuts("Root CA Certificate Read Failed");
+            m_sState = WNC_OFF;
+            return false;
+        }
+        
+        /*
+        dbgPuts("Reading Subject Name");
+        int iSubjectLength;
+        getSubjectName("03", sSubjectName, &iSubjectLength);
+        
+        dbgPuts("Reading Update Status");
+        unsigned char cStatus;
+        getUpdateStatus (&cStatus);
+        */
         
         // Set the Apn
         setApnName(apn);
@@ -220,6 +247,14 @@
             m_sState = WNC_OFF;
         }
         
+        dbgPuts("Reading Signal Strength");
+        int16_t dbm = getDbmRssi();
+        int16_t ber = get3gBer();
+        sprintf (sOutput, "dbm = %d  Ber = %d", dbm, ber);
+        dbgPuts(sOutput);
+        
+        
+        
     }
     else {
         dbgPuts("Power up failed!");
@@ -229,6 +264,33 @@
     return ((m_sState == WNC_ON) || (m_sState == WNC_ON_NO_CELL_LINK));
 }
 
+bool WncController::getAllObjects()
+{
+    //read certificates and keys
+        
+    dbgPuts("Reading Object: Device Certificate");
+    if (getObject("00", (unsigned char *)AWS_IOT_CERTIFICATE, &AWS_IOT_CERTIFICATE_LENGTH) == false)
+    {
+        dbgPuts("Device Certificate Read Failed");
+        return false;
+    }
+    
+    dbgPuts("Reading Object: Device Private Key");
+    if (getObject("01", (unsigned char *)AWS_IOT_PRIVATE_KEY, &AWS_IOT_PRIVATE_KEY_LENGTH) == false)
+    {
+        dbgPuts("Device Private Key Read Failed");
+        return false;
+    }
+    dbgPuts("Reading Object: Root CA Certificate");
+    if (getObject("02", (unsigned char *)AWS_IOT_ROOT_CA, &AWS_IOT_ROOT_CA_LENGTH) == false)
+    {
+        dbgPuts("Root CA Certificate Read Failed");
+        return false;
+    }
+        
+    return true;
+}
+
 size_t WncController::sendCustomCmd(const char * cmd, char * resp, size_t sizeRespBuf, int ms_timeout)
 {
     string * respStr;
@@ -976,7 +1038,7 @@
     bool bChannelOpen = false;
     
     int iObjectLength = 0, iObjectBlockLength = 0, iNumBlocks = 0, iRemainder = 0, iObjectStringLength = 0, iIndex = 0, k;
-    string sObject, sBlockObject, sByte;
+    //string sBlockObject, sByte;
     
     
     dbgPuts("openChannel...");
@@ -1005,6 +1067,7 @@
     if (iRemainder > 0)
         iNumBlocks++;
     
+    sObject.erase();
     
     for (int i = 0; i < iNumBlocks; i++)
     {
@@ -1072,13 +1135,140 @@
     return (true);
 }
 
+bool WncController::getSubjectName(string sObjectName, unsigned char *ucObject, int *iObjectTotalLength)
+{
+    char cBlockOffset[7];
+    bool bChannelOpen = false;
+    
+    int iObjectLength = 0, iObjectStringLength = 0, iIndex = 0, k;
+    //string sBlockObject, sByte;
+    
+    
+    dbgPuts("openChannel...");
+    if (at_openChannel_wnc(&m_sChannelID) == false) 
+    {
+        dbgPuts("openChannel error!");
+        return (false);
+    }
+    bChannelOpen = true;
+    
+    
+    sObject.erase();
+    
+    dbgPuts("getSubjectName...!");
+    if (at_getSubjectName_wnc(m_sChannelID, &iObjectLength, &sObject) == false) 
+    {
+        dbgPuts("getSubjectName error!");
+        if (bChannelOpen)
+        {
+            dbgPuts("closChannel...");
+            at_closeChannel_wnc(m_sChannelID);
+        }
+        return (false);
+    }
+    
+    if (bChannelOpen)
+    {
+        for (int j =0; j < 10; j++)
+        {
+            dbgPuts("closeChannel...");
+            if (at_closeChannel_wnc(m_sChannelID) == true)
+                break;
+        }
+    }
+    
+    //convert the Object in string to unsigned char array
+    iObjectStringLength = strlen(sObject.c_str());
+    sprintf (sOutput, "Object Length = %d", iObjectStringLength);
+    dbgPuts(sOutput);
+
+    iIndex = 0;
+    k = 0;
+    while (k < iObjectStringLength)
+    {
+        sByte = sObject.substr(k,2);
+        ucObject[iIndex] = (unsigned char) strtoul (sByte.c_str(), NULL, 16);
+        k +=2;
+        iIndex++;
+    }
+    
+    sprintf (sOutput, "Buf Length = %d  Object = ", iIndex);
+    dbgPuts(sOutput);
+    sObject.erase();
+    for (int i = 0; i < iIndex; i++)
+    {
+        sprintf (cBlockOffset, "%02X", ucObject[i]);
+        sObject += string (cBlockOffset);
+        
+    }
+    dbgPuts(sObject.c_str());
+    
+    *iObjectTotalLength = iIndex; 
+    
+    return (true);
+}
+
+bool WncController::getUpdateStatus(unsigned char *cStatus)
+{
+    char cBlockOffset[7];
+    bool bChannelOpen = false;
+    
+    int iObjectLength = 0, iObjectStringLength = 0, iIndex = 0, k;
+    //string sBlockObject, sByte;
+    
+    
+    dbgPuts("openChannel...");
+    if (at_openChannel_wnc(&m_sChannelID) == false) 
+    {
+        dbgPuts("openChannel error!");
+        return (false);
+    }
+    bChannelOpen = true;
+    
+    
+    sObject.erase();
+    
+    dbgPuts("getUpdateStatus...!");
+    if (at_getUpdateStatus_wnc(m_sChannelID, &iObjectLength, &sObject) == false) 
+    {
+        dbgPuts("getUpdateStatus error!");
+        if (bChannelOpen)
+        {
+            dbgPuts("closChannel...");
+            at_closeChannel_wnc(m_sChannelID);
+        }
+        return (false);
+    }
+    
+    if (bChannelOpen)
+    {
+        for (int j =0; j < 10; j++)
+        {
+            dbgPuts("closeChannel...");
+            if (at_closeChannel_wnc(m_sChannelID) == true)
+                break;
+        }
+    }
+    
+    //convert the Object in string to unsigned char array
+    iObjectStringLength = strlen(sObject.c_str());
+    sprintf (sOutput, "Object Length = %d", iObjectStringLength);
+    dbgPuts(sOutput);
+
+    sByte = sObject.substr(0,2);
+    *cStatus = (unsigned char) strtoul (sByte.c_str(), NULL, 16);
+    
+    return (true);
+}
+
 bool WncController::at_openChannel_wnc(string * sChannelID)
 {
     string * respStr;
     
     
     //open a channel to certificate management applet
-    AtCmdErr_e r = at_send_wnc_cmd("AT+CCHO=\"f0504b43533135\"", &respStr, m_sCmdTimeoutMs);
+    //AtCmdErr_e r = at_send_wnc_cmd("AT+CCHO=\"f0504b43533135\"", &respStr, m_sCmdTimeoutMs);
+    AtCmdErr_e r = at_send_wnc_cmd("AT+CCHO=\"D2760001180002FFF1003D89\"", &respStr, m_sCmdTimeoutMs);
 
     if (r != WNC_AT_CMD_OK || respStr->size() == 0)
         return (false);
@@ -1091,7 +1281,7 @@
     if (posOK == string::npos)
         return (false);
     
-    *sChannelID = respStr->substr(31, 1);
+    *sChannelID = respStr->substr(41, 1);
     
     sprintf (sOutput, "Channel ID = %s", (*sChannelID).c_str());
     dbgPuts(sOutput);
@@ -1130,7 +1320,7 @@
     string sObjectLength;
     
      //open a channel to certificate management applet
-    sprintf (sCommand, "AT+CSIM=14,\"8%s57%s01024455\"", sChannelID.c_str(), sObject.c_str()); 
+    sprintf (sCommand, "AT+CSIM=14,\"8%s58%s01024455\"", sChannelID.c_str(), sObject.c_str()); 
      
     AtCmdErr_e r = at_send_wnc_cmd(sCommand, &respStr, m_sCmdTimeoutMs);
 
@@ -1165,7 +1355,7 @@
     
     //read block from object specified in the sBlock String. The for mat of string is offset = XXXX, length ZZ. So the offset at 0000 and length of F8 witll be 0000F8. The string must be in ascii hex format
     
-    sprintf (sCommand, "AT+CSIM=16,\"8%s57%s0203%s\"", sChannelID.c_str(), sObject.c_str(), sBlock.c_str());
+    sprintf (sCommand, "AT+CSIM=16,\"8%s58%s0203%s\"", sChannelID.c_str(), sObject.c_str(), sBlock.c_str());
     
     AtCmdErr_e r = at_send_wnc_cmd(sCommand, &respStr, m_sCmdTimeoutMs);
 
@@ -1197,6 +1387,84 @@
     return (true);
 }
 
+bool WncController::at_getSubjectName_wnc(string sChannelID, int *iObjectBlockLength, string *sData)
+{
+    string * respStr;
+    string sObjectBlockLength;
+    
+        
+    sprintf (sCommand, "AT+CSIM=16,\"8%s58030003010101\"", sChannelID.c_str());
+    
+    AtCmdErr_e r = at_send_wnc_cmd(sCommand, &respStr, m_sCmdTimeoutMs);
+
+    if (r != WNC_AT_CMD_OK || respStr->size() == 0)
+        return (false);
+
+    size_t pos = respStr->find("AT+CSIM=16");
+    if (pos == string::npos)
+        return (false);
+    
+    size_t posOK = respStr->rfind("OK");
+    if (posOK == string::npos)
+        return (false);
+    
+    size_t pos9000 = respStr->rfind("9000");
+    if (pos9000 == string::npos)
+        return (false);
+    
+    sObjectBlockLength = respStr->substr(36, 3);
+    *iObjectBlockLength = (int) strtol (sObjectBlockLength.c_str(), NULL, 0);
+    //without the status characters
+    *iObjectBlockLength = *iObjectBlockLength - 4;
+    *sData = respStr->substr(40, *iObjectBlockLength);
+    
+    sprintf (sOutput, "Object Length = %s %d", sObjectBlockLength.c_str(), *iObjectBlockLength);
+    dbgPuts(sOutput);
+    dbgPuts((*sData).c_str());
+        
+    return (true);
+}
+
+
+bool WncController::at_getUpdateStatus_wnc(string sChannelID, int *iLength, string *sData)
+{
+    string * respStr;
+    string sObjectBlockLength;
+    
+        
+    sprintf (sCommand, "AT+CSIM=10,\"8%s5A000001\"", sChannelID.c_str());
+    
+    AtCmdErr_e r = at_send_wnc_cmd(sCommand, &respStr, m_sCmdTimeoutMs);
+
+    if (r != WNC_AT_CMD_OK || respStr->size() == 0)
+        return (false);
+
+    size_t pos = respStr->find("AT+CSIM=10");
+    if (pos == string::npos)
+        return (false);
+    
+    size_t posOK = respStr->rfind("OK");
+    if (posOK == string::npos)
+        return (false);
+    
+    size_t pos9000 = respStr->rfind("9000");
+    if (pos9000 == string::npos)
+        return (false);
+    
+    sObjectBlockLength = respStr->substr(29, 2);
+    *iLength = (int) strtol (sObjectBlockLength.c_str(), NULL, 0);
+    //without the status characters
+    *iLength = *iLength - 4;
+    *sData = respStr->substr(33, *iLength);
+    
+    sprintf (sOutput, "Object Length = %s %d", sObjectBlockLength.c_str(), *iLength);
+    dbgPuts(sOutput);
+    dbgPuts((*sData).c_str());
+        
+    return (true);
+}
+
+
 bool WncController::sendSMSText(const char * const phoneNum, const char * const text)
 {
     if (at_sendSMStext_wnc(phoneNum, text) == true)