Multi-Hackers / Axeda_IoT_Connection_Example

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Vanger
Date:
Mon Mar 23 19:13:52 2015 +0000
Parent:
0:562ad81d9dd4
Child:
2:cdfc629d20fa
Commit message:
updated example to be fully functional.; Made program easier to read, added more explanation of some functions, updated libraries to be current release ones.

Changed in this revision

certs.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show annotated file Show diff for this revision Revisions of this file
mtsas-dev.lib Show diff for this revision Revisions of this file
mtsas.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/certs.h	Mon Mar 23 19:13:52 2015 +0000
@@ -0,0 +1,90 @@
+/** Any non-root certificates will have no effect on the SSL client, but will
+ * use up extra space for their allocation into the certificate list.
+ * Methods for obtaining the Root CA certificate(s) for a website:
+ * 1. Open a browser to the target website using HTTPS. 
+ *      (These instructions are for Chrome, but should be similar for other browsers)
+ *      Click on the lock icon at the beginning of the url, select the "connection" tab,
+ *      and click the "certificate information" hyperlink. The browser will open a new window
+ *      labeled "Certificate". Switch to the "Certification Path" tab, click the certificate 
+ *      closest to the top of the window, then click "View Certificate". This will open a new
+ *      window labeled "Certificate" again. This time, click on the "Details" tab, then click 
+ *      "Copy to File". This will open a "Certificate Export Wizard". Click next, and choose the 
+ *      "Base-64 encoded X.509 (.CER)" option, then click next. Choose the name and location where
+ *      you want to save the PEM formatted certificate, then click next, then finish. Open the file
+ *      that was created and copy-paste the certificate into this certs.h file. Format the certificate
+ *      data so that each line begins with " and is appended with \r\n"
+ *
+ * 2. Go to https://www.bluessl.com/en/ssltest and enter the website for which you wish to get the root CA
+ *      certificates. Click the [Click here to download the public part of this certificate] button located
+ *      underneath the certificates that label themselves as Root, or Root CA. The root CA certificate should
+ *      self-signed, otherwise it is not the root CA certificate. Take the downloaded file, copy the data
+ *      into this certs.h file, and format the data so that each line begins with " and is appended with \r\n"
+ *      
+ * 3. Using openssl on linux: (Make sure you have openssl installed.) Run the command: openssl s_client
+ *      -showcerts -connect <host>:<port> -CApath /etc/ssl/certs
+ *      Where <host> is the host name (www.google.com for example), and port is the port on which the connection
+ *      will be made (usually 443). When the connection is made with the remote server, the last certificate(s)
+ *      listed will be the Root CA certificate(s). Merely copy-paste those certificates into this certs.h file
+ *      and format them with each line beginning with " and ending with \r\n"
+ */
+
+//Root Certificates
+const char CERTIFICATE1[1237] = 
+    
+    //Nucleus-connect.axeda.com    
+    //GeoTrustGlobalCA from GeoTrust website
+    "-----BEGIN CERTIFICATE-----\r\n"
+    "MIIDVDCCAjygAwIBAgIDAjRWMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT\r\n"
+    "MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i\r\n"
+    "YWwgQ0EwHhcNMDIwNTIxMDQwMDAwWhcNMjIwNTIxMDQwMDAwWjBCMQswCQYDVQQG\r\n"
+    "EwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UEAxMSR2VvVHJ1c3Qg\r\n"
+    "R2xvYmFsIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2swYYzD9\r\n"
+    "9BcjGlZ+W988bDjkcbd4kdS8odhM+KhDtgPpTSEHCIjaWC9mOSm9BXiLnTjoBbdq\r\n"
+    "fnGk5sRgprDvgOSJKA+eJdbtg/OtppHHmMlCGDUUna2YRpIuT8rxh0PBFpVXLVDv\r\n"
+    "iS2Aelet8u5fa9IAjbkU+BQVNdnARqN7csiRv8lVK83Qlz6cJmTM386DGXHKTubU\r\n"
+    "1XupGc1V3sjs0l44U+VcT4wt/lAjNvxm5suOpDkZALeVAjmRCw7+OC7RHQWa9k0+\r\n"
+    "bw8HHa8sHo9gOeL6NlMTOdReJivbPagUvTLrGAMoUgRx5aszPeE4uwc2hGKceeoW\r\n"
+    "MPRfwCvocWvk+QIDAQABo1MwUTAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTA\r\n"
+    "ephojYn7qwVkDBF9qn1luMrMTjAfBgNVHSMEGDAWgBTAephojYn7qwVkDBF9qn1l\r\n"
+    "uMrMTjANBgkqhkiG9w0BAQUFAAOCAQEANeMpauUvXVSOKVCUn5kaFOSPeCpilKIn\r\n"
+    "Z57QzxpeR+nBsqTP3UEaBU6bS+5Kb1VSsyShNwrrZHYqLizz/Tt1kL/6cdjHPTfS\r\n"
+    "tQWVYrmm3ok9Nns4d0iXrKYgjy6myQzCsplFAMfOEVEiIuCl6rYVSAlk6l5PdPcF\r\n"
+    "PseKUgzbFbS9bZvlxrFUaKnjaZC2mqUPuLk/IH2uSrW4nOQdtqvmlKXBx4Ot2/Un\r\n"
+    "hw4EbNX/3aBd7YdStysVAq45pmp06drE57xNNB6pXE0zX5IJL4hmXXeXxx12E6nV\r\n"
+    "5fEWCRE11azbJHFwLJhWC9kXtNHjUStedejV0NxPNO3CBWaAocvmMw==\r\n"
+    "-----END CERTIFICATE-----\r\n";
+const char CERTIFICATE2[849] =
+    //VeriSignClass3PublicPrimaryCA
+    "-----BEGIN CERTIFICATE-----\r\n"
+    "MIICPDCCAaUCEDyRMcsf9tAbDpq40ES/Er4wDQYJKoZIhvcNAQEFBQAwXzELMAkG\r\n"
+    "A1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMTcwNQYDVQQLEy5DbGFz\r\n"
+    "cyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTk2\r\n"
+    "MDEyOTAwMDAwMFoXDTI4MDgwMjIzNTk1OVowXzELMAkGA1UEBhMCVVMxFzAVBgNV\r\n"
+    "BAoTDlZlcmlTaWduLCBJbmMuMTcwNQYDVQQLEy5DbGFzcyAzIFB1YmxpYyBQcmlt\r\n"
+    "YXJ5IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MIGfMA0GCSqGSIb3DQEBAQUAA4GN\r\n"
+    "ADCBiQKBgQDJXFme8huKARS0EN8EQNvjV69qRUCPhAwL0TPZ2RHP7gJYHyX3KqhE\r\n"
+    "BarsAx94f56TuZoAqiN91qyFomNFx3InzPRMxnVx0jnvT0Lwdd8KkMaOIG+YD/is\r\n"
+    "I19wKTakyYbnsZogy1Olhec9vn2a/iRFM9x2Fe0PonFkTGUugWhFpwIDAQABMA0G\r\n"
+    "CSqGSIb3DQEBBQUAA4GBABByUqkFFBkyCEHwxWsKzH4PIRnN5GfcX6kb5sroc50i\r\n"
+    "2JhucwNhkcV8sEVAbkSdjbCxlnRhLQ2pRdKkkirWmnWXbj9T/UWZYB2oK0z5XqcJ\r\n"
+    "2HUw19JlYD1n1khVdWk/kfVIC0dpImmClr7JyDiGSnoscxlIaU5rfGW/D/xwzoiQ\r\n"
+    "-----END CERTIFICATE-----\r\n";
+const char CERTIFICATE3[977] = 
+    //Axeda Root CA Certificate
+    "-----BEGIN CERTIFICATE-----\r\n"
+    "MIICmTCCAgICAQAwDQYJKoZIhvcNAQEEBQAwgZQxCzAJBgNVBAYTAlVTMRYwFAYD\r\n"
+    "VQQIEw1NYXNzYWNodXNldHRzMRIwEAYDVQQHEwlNYW5zZmllbGQxHDAaBgNVBAoT\r\n"
+    "E0F4ZWRhIFN5c3RlbXMsIEluYy4xGTAXBgNVBAMTEEF4ZWRhIFN5c3RlbXMgQ0Ex\r\n"
+    "IDAeBgkqhkiG9w0BCQEWEXN1cHBvcnRAYXhlZGEuY29tMB4XDTAzMDExMDE3MzUy\r\n"
+    "N1oXDTEzMDEwNzE3MzUyN1owgZQxCzAJBgNVBAYTAlVTMRYwFAYDVQQIEw1NYXNz\r\n"
+    "YWNodXNldHRzMRIwEAYDVQQHEwlNYW5zZmllbGQxHDAaBgNVBAoTE0F4ZWRhIFN5\r\n"
+    "c3RlbXMsIEluYy4xGTAXBgNVBAMTEEF4ZWRhIFN5c3RlbXMgQ0ExIDAeBgkqhkiG\r\n"
+    "9w0BCQEWEXN1cHBvcnRAYXhlZGEuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\r\n"
+    "iQKBgQD0VtQ82SdSI4QTwIWIXTya91GJ4IFZMwY3eXVkg3jpBwGGQFAk2yOAhITZ\r\n"
+    "nQkZn5/JEifRJTvLhqq7AtFqkKG0bKza3jLFhMDh4q7nn5en1wWvMWQM8hSA7cBV\r\n"
+    "DYbtsRObM8b7TiC8ZlxhN/6fZFiLyzX431Ppx2nSjyfpHK3oSQIDAQABMA0GCSqG\r\n"
+    "SIb3DQEBBAUAA4GBAGav/orW9wQ7TvUiJV5IcpckJKQJrTd0M2XBu+iPwJ52+4pP\r\n"
+    "SzJJ7zMdhUTEkxuWegz1L25DewZdnMBddtSK9/AcB6l7Ezqwfblr6cuLNduO9+MU\r\n"
+    "29I/wb5gbC2vSppa/clLB7Cw/b7ypS+bTTIU9RbbOrtuKtyGGN3YCvXGKUgB\r\n"
+    "-----END CERTIFICATE-----\r\n"
+    ;
\ No newline at end of file
--- a/main.cpp	Mon Jan 19 22:44:46 2015 +0000
+++ b/main.cpp	Mon Mar 23 19:13:52 2015 +0000
@@ -1,29 +1,41 @@
 #include "mbed.h"
 #include "mtsas.h"
+#include "certs.h"
 
 //Simple function that converts the HTTP result to a string
 //Ex: Result is 0, the string result will be "HTTP_OK"
 char * httpResToStr(HTTPResult res);
 
+/**Write the model for your device here. 
+ * Can be located on the axeda developer toolbox device info page.
+ */
+const string Modelstr = "";
+
+/**Write the serial for your device here
+ * Can be located on the axeda developer toolbox device info page.
+ */
+const string Serialstr = "";
+
+const string base_url = "https://nucleus-connect.axeda.com/ammp/data/1/";
+
 int main(){
     
     //Sets the log level to INFO, higher log levels produce more log output.
     //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
     MTSLog::setLogLevel(MTSLog::INFO_LEVEL);
     
-    /**Root Certificate(s) of the remote server you want to connect to.
-     * Make sure the certificates are in PEM format, contain \r\n to end each line,
-     * and if using multiple root CA certificates, just make multiple calls to the addRootCACertificate
-     * function for each root certificate you wish to add.
-     */
-    const char CERTIFICATES[] = "";
-    /**Example certificate formatted correctly (length not to scale):
-     -----BEGIN CERTIFICATE-----\r\n
-     aosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfija\r\n
-     afjklewijafliefhiszelifhlsfhilasihflihsalifhalhifliahlfihaslihfl\r\n
-     fawefaewf==\r\n
-     -----END CERTIFICATE-----\r\n
-     */
+    /**Root Certificate(s) of the remote server you want to connect to are listed in "certs.h"
+    * Make sure the certificates are in PEM format, contain \r\n to end each line,
+    * and if using multiple root CA certificates, just make multiple calls to the addRootCACertificate
+    * function for each root certificate you wish to add, or add them all as one concatenated string.
+    *
+    * Example certificate formatted correctly (length not to scale):
+    * -----BEGIN CERTIFICATE-----\r\n
+    * aosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfija\r\n
+    * afjklewijafliefhiszelifhlsfhilasihflihsalifhalhifliahlfihaslihfl\r\n
+    * fawefaewf==\r\n
+    * -----END CERTIFICATE-----\r\n
+    */
     
     //Modify to match your apn if you are using an HSPA radio with a SIM card
     const char APN[] = "";
@@ -59,7 +71,7 @@
         logFatal("Radio initialization failed");
         return 1;
     }
-    radio->configureSignals(D4,D7,RESET);
+    
     Transport::setTransport(radio);
     
     //Set radio APN
@@ -88,35 +100,53 @@
         }
     }
     
-    //Receive buffer
+    //Create receive interface and buffer
     char rbuf[2000];
     HTTPText* receive = new HTTPText(rbuf, sizeof(rbuf));
     
-    //Json output object, data must be in Json format
+    //Json output object, data must be in Json format, example data is input already:
+    //HTTPJson type merely sets the HTTP header to JSON type, nothing else is different from the HTTPText type
     char sbuf[2000] = "{\"data\":[{\"dataItems\":{\"mental_trauma\":1,\"physical_trauma\":2,\"emotional_trauma\":3}}]}\0";
     HTTPJson* send = new HTTPJson(sbuf);
     
     //Create HTTP Client Instance
     HTTPClient* http = new HTTPClient();
     if( !http || !receive || !send) {
-        logFatal("Failed to instantiate client or send buffer or receive buffer");
+        logFatal("Failed to instantiate client, send, or receive");
         return 1;
     }
     
+    /**Certificates can all be loaded concurrently as one string with the certificates
+     * concatenated after one another if so desired. Otherwise, the example here shows
+     * loading the certificates one by one.
+     */
     logTrace("Loading certificate(s)");
-    HTTPResult res = http->addRootCACertificate(CERTIFICATES);
+    HTTPResult res = http->addRootCACertificate(CERTIFICATE1);
+    if(res != HTTP_OK) {
+        logError("Failed to load CERTIFICATE1");
+    }
+    
+    res = http->addRootCACertificate(CERTIFICATE2);
     if(res != HTTP_OK) {
-        logError("Failed to load CERTIFICATES");
+        logError("Failed to load CERTIFICATE2");
+    }
+    
+    res = http->addRootCACertificate(CERTIFICATE3);
+    if(res != HTTP_OK) {
+        logError("Failed to load CERTIFICATE3");
     }
     
     /**Set whether or not to verify the remote server's certificate
-     * VERIFY_NONE,VERIFY_PEER
+     * VERIFY_NONE Sets the connection to be made using SSL protocol, 
+     * but without remot peer verification using the loaded certificates.
+     * VERIFY_PEER Sets the connection to be made using SSL protocol,
+     * and to verify the peer using the loaded root certificates.
      */
     http->setPeerVerification(VERIFY_PEER);
     
-    //URL for remote host (includes path)
-    //Format: https://nucleus-connect.axeda.com/ammp/data/1/MODEL_STRING_HERE!<SERIAL_NUMBER_HERE>
-    string url = "https://nucleus-connect.axeda.com/ammp/data/1/nlr_selfcrt_arm32!nlr__EPratt_multitech_com___1159423";
+    //URL for axeda.com device connection (includes path)
+    //Format: https://nucleus-connect.axeda.com/ammp/data/1/<MODEL_STRING_HERE>!<SERIAL_NUMBER_HERE>
+    string url = base_url + Modelstr + '!' + Serialstr;
         
     logTrace("HTTPS POST Request with Certificate");
     res = http->post(url.c_str(), *send, receive);
--- a/mbed.lib	Mon Jan 19 22:44:46 2015 +0000
+++ b/mbed.lib	Mon Mar 23 19:13:52 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/#4fc01daae5a5
+http://mbed.org/users/mbed_official/code/mbed/#487b796308b0
--- a/mtsas-dev.lib	Mon Jan 19 22:44:46 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/Multi-Hackers/code/mtsas-dev/#a8a8b5603500
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mtsas.lib	Mon Mar 23 19:13:52 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Multi-Hackers/code/mtsas/#a9d7ee8ea685