V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Revision:
28:54d9a550adf1
Parent:
15:6f2798e45099
Child:
29:f71a0be59b99
--- a/WNCInterface/WncControllerK64F/WncController/WncController.cpp	Tue Feb 07 16:18:57 2017 +0000
+++ b/WNCInterface/WncControllerK64F/WncController/WncController.cpp	Mon Oct 09 21:13:49 2017 +0000
@@ -31,6 +31,15 @@
 #include <cctype>
 #include "WncController.h"
 
+// Links to our certs from certs.cpp
+extern const unsigned char AWS_IOT_ROOT_CA[];
+extern const unsigned char AWS_IOT_CERTIFICATE[];
+extern const unsigned char AWS_IOT_PRIVATE_KEY[];
+
+extern int AWS_IOT_ROOT_CA_LENGTH;
+extern int AWS_IOT_CERTIFICATE_LENGTH;
+extern int AWS_IOT_PRIVATE_KEY_LENGTH;
+
 namespace WncController_fk {
 
 /////////////////////////////////////////////////////
@@ -50,6 +59,13 @@
 const char * const WncController::INVALID_IP_STR = "";
 bool WncController::m_sReadyForSMS = false;
 
+string WncController::m_sChannelID;
+char WncController::sOutput[4000];
+char WncController::sCommand[256];
+unsigned char WncController::ucObject[2500];
+
+
+
 
 /**
  * C++ version 0.4 char* style "itoa":
@@ -185,13 +201,25 @@
     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) {
+    if (initWncModem(powerUpTimeoutSecs) == true) 
+    {
+        //read certificates and keys
+        dbgPuts("Reading Object: Device Certificate");
+        getObject("00", (unsigned char *)AWS_IOT_CERTIFICATE, &AWS_IOT_CERTIFICATE_LENGTH);
+        //getObject("00", ucObject);
+        dbgPuts("Reading Object: Device Private Key");
+        getObject("01", (unsigned char *)AWS_IOT_PRIVATE_KEY, &AWS_IOT_PRIVATE_KEY_LENGTH);
+        dbgPuts("Reading Object: Root CA Certificate");
+        getObject("02", (unsigned char *)AWS_IOT_ROOT_CA, &AWS_IOT_ROOT_CA_LENGTH);
+        
         // Set the Apn
         setApnName(apn);
-        if (false == softwareInitMdm()) {
+        if (false == softwareInitMdm()) 
+        {
             dbgPuts("Software init failed!");
             m_sState = WNC_OFF;
         }
+        
     }
     else {
         dbgPuts("Power up failed!");
@@ -940,6 +968,235 @@
 }
 
 
+
+bool WncController::getObject(string sObjectName, unsigned char *ucObject, int *iObjectTotalLength)
+{
+    char cBlockOffset[7];
+    int iMaxBlockSize = 248;
+    bool bChannelOpen = false;
+    
+    int iObjectLength = 0, iObjectBlockLength = 0, iNumBlocks = 0, iRemainder = 0, iObjectStringLength = 0, iIndex = 0, k;
+    string sObject, sBlockObject, sByte;
+    
+    
+    dbgPuts("openChannel...");
+    if (at_openChannel_wnc(&m_sChannelID) == false) 
+    {
+        dbgPuts("openChannel error!");
+        return (false);
+    }
+    bChannelOpen = true;
+    
+    dbgPuts("selectObject...");
+    if (at_selectObject_wnc(m_sChannelID, sObjectName, &iObjectLength) == false) 
+    {
+        dbgPuts("selectObject error!");
+        if (bChannelOpen)
+        {
+            dbgPuts("closChannel...");
+            at_closeChannel_wnc(m_sChannelID);
+        }
+        return (false);
+    }
+ 
+    
+    iRemainder = iObjectLength%iMaxBlockSize;
+    iNumBlocks = iObjectLength/iMaxBlockSize;
+    if (iRemainder > 0)
+        iNumBlocks++;
+    
+    
+    for (int i = 0; i < iNumBlocks; i++)
+    {
+        if (i == (iNumBlocks - 1))
+            sprintf (cBlockOffset, "%04X%02X", i*iMaxBlockSize, iRemainder);
+        else
+            sprintf (cBlockOffset, "%04X%02X", i*iMaxBlockSize, iMaxBlockSize);
+        
+        dbgPuts(cBlockOffset);
+        
+        dbgPuts("getObjectBlock...!");
+        if (at_getObjectBlock_wnc(m_sChannelID, sObjectName, cBlockOffset, &iObjectBlockLength, &sBlockObject) == false) 
+        {
+            dbgPuts("getObjectBlock error!");
+            if (bChannelOpen)
+            {
+                dbgPuts("closChannel...");
+                at_closeChannel_wnc(m_sChannelID);
+            }
+            return (false);
+        }
+        sObject += sBlockObject;
+    }
+    //dbgPuts ("Object = ");
+    //dbgPuts (sObject.c_str());
+    
+    if (bChannelOpen)
+    {
+        for (int j =0; j < 10; j++)
+        {
+            //waitMs(2000);
+            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::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);
+
+    if (r != WNC_AT_CMD_OK || respStr->size() == 0)
+        return (false);
+
+    size_t pos = respStr->find("AT+CCHO");
+    if (pos == string::npos)
+        return (false);
+    
+    size_t posOK = respStr->rfind("OK");
+    if (posOK == string::npos)
+        return (false);
+    
+    *sChannelID = respStr->substr(31, 1);
+    
+    sprintf (sOutput, "Channel ID = %s", (*sChannelID).c_str());
+    dbgPuts(sOutput);
+        
+    return (true);
+}
+
+bool WncController::at_closeChannel_wnc(string sChannelID)
+{
+    string * respStr;
+    
+    
+    //close the channel to certificate management applet
+    sprintf (sCommand, "AT+CCHC=%s", 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+CCHC");
+    if (pos == string::npos)
+        return (false);
+    
+    size_t posOK = respStr->rfind("OK");
+    if (posOK == string::npos)
+        return (false);
+    
+    dbgPuts("Channel Closed");
+        
+    return (true);
+}
+
+bool WncController::at_selectObject_wnc(string sChannelID, string sObject, int *iObjectLength)
+{
+    string * respStr;
+    string sObjectLength;
+    
+     //open a channel to certificate management applet
+    sprintf (sCommand, "AT+CSIM=14,\"8%s57%s01024455\"", sChannelID.c_str(), sObject.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=14");
+    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);
+    
+    sObjectLength = respStr->substr(37, 4);
+    *iObjectLength = (int) strtol (sObjectLength.c_str(), NULL, 16);
+    
+    sprintf (sOutput, "Object Length = %s %d", sObjectLength.c_str(), *iObjectLength);
+    dbgPuts(sOutput);
+        
+    return (true);
+}
+
+bool WncController::at_getObjectBlock_wnc(string sChannelID, string sObject, string sBlock, int *iObjectBlockLength, string *sData)
+{
+    string * respStr;
+    string sObjectBlockLength;
+    
+    //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());
+    
+    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(41, *iObjectBlockLength);
+    
+    sprintf (sOutput, "Object Length = %s %d", sObjectBlockLength.c_str(), *iObjectBlockLength);
+    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)