Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SmartRestConf.cpp Source File

SmartRestConf.cpp

00001 #include <string.h>
00002 #include <stdio.h>
00003 #include "b64.h"
00004 #include "SmartRestConf.h"
00005 
00006 char srTenant[CREDENTIAL_LENGTH] = {0};
00007 char srUsername[CREDENTIAL_LENGTH] = {0};
00008 char srPassword[CREDENTIAL_LENGTH] = {0};
00009 char srAuthStr[100] = {0};
00010 //const char *srX_ID = "com_cumulocity_MbedAgent_1.5.2";
00011 const char *srX_ID = NULL;
00012 const char *srHost = "developer.cumulocity.com";
00013 //const char *srHost = "iot.etisalat.eg";
00014 //const char *srHost = "management.teleena-iot.com";
00015 //const char *srHost = "post-iot.lu";
00016 //const char *srHost = "dev-a.cumulocity.com";
00017 //const char *srHost = "dev-c.cumulocity.com";
00018 //const char *srHost = "management.m2m-devicecloud.com";
00019 long deviceID = 0;
00020 char fmtSmartRest[200] = {0};
00021 
00022 static void setAuthStr(const char* p1, const char* p2)
00023 {
00024         memset(srAuthStr, 0, sizeof(srAuthStr));
00025         size_t ul = strlen(p1);
00026         size_t pl = strlen(p2);
00027         unsigned char input[3], output[5];
00028         int inputOffset = 0;
00029 
00030         for (size_t i = 0; i < (ul+1+pl); i++) {
00031                 if (i < ul)
00032                         input[inputOffset++] = p1[i];
00033                 else if (i == ul)
00034                         input[inputOffset++] = ':';
00035                 else
00036                         input[inputOffset++] = p2[i-(ul+1)];
00037 
00038                 if ((inputOffset == 3) || (i == ul+pl)) {
00039                         b64_encode(input, inputOffset, output, 4);
00040                         output[4] = '\0';
00041                         strcat(srAuthStr, (char*)output);
00042                         inputOffset = 0;
00043                 }
00044         }
00045 }
00046 
00047 static void setTenant(const char *tenant)
00048 {
00049         snprintf(srTenant, sizeof(srTenant), "%s", tenant);
00050 }
00051 
00052 static void setUsername(const char *username)
00053 {
00054         snprintf(srUsername, sizeof(srUsername), "%s", username);
00055 }
00056 
00057 static void setPassword(const char *password)
00058 {
00059         snprintf(srPassword, sizeof(srPassword), "%s", password);
00060 }
00061 
00062 static void setSmartRestFmt()
00063 {
00064         if (srX_ID) {
00065                 const char fmt[] = "POST %%s HTTP/1.0\r\nHost: %s\r\nAuthorization: Basic %s\r\nX-Id: %s\r\nContent-Length: %%d\r\n\r\n%%s";
00066                 snprintf(fmtSmartRest, sizeof(fmtSmartRest), fmt, srHost, srAuthStr, srX_ID);
00067         } else {
00068                 const char fmt[] = "POST %%s HTTP/1.0\r\nHost: %s\r\nAuthorization: Basic %s\r\nContent-Length: %%d\r\n\r\n%%s";
00069                 snprintf(fmtSmartRest, sizeof(fmtSmartRest), fmt, srHost, srAuthStr);
00070         }
00071 }
00072 
00073 void setX_ID(const char* id)
00074 {
00075         if (id) {
00076             srX_ID = id;
00077             setSmartRestFmt();
00078         }
00079 }
00080 
00081 void setDeviceID(long id)
00082 {
00083         deviceID = id;
00084 }
00085 
00086 void setAuth(const char *tenant, const char *username, const char *password)
00087 {
00088         if (tenant)
00089             setTenant(tenant);
00090         if (username)
00091             setUsername(username);
00092         if (password)
00093             setPassword(password);
00094         if (tenant || username || password) {
00095             char s[CREDENTIAL_LENGTH*2];
00096             snprintf(s, sizeof(s), "%s/%s", tenant, username);
00097             setAuthStr(s, password);
00098             setSmartRestFmt();
00099         }
00100 }