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

util/SmartRestConf.cpp

Committer:
xinlei
Date:
2015-05-07
Revision:
99:e369fc75c000
Parent:
95:010b0f7a0a1a
Child:
101:dbcd3bc51758

File content as of revision 99:e369fc75c000:

#include <string.h>
#include <stdio.h>
#include "b64.h"
#include "SmartRestConf.h"

const char *srUsername = NULL;
const char *srPassword = NULL;
char srAuthStr[100] = {0};
const char *srX_ID = "com_cumulocity_MbedAgent_1.5.2";
const char *srHost = "developer.cumulocity.com";
// const char *_host = "management.m2m-devicecloud.com"
const int srPort = 80;
long deviceID = 0;
char fmtSmartRest[200] = {0};

void setAuthStr(const char* p1, const char* p2)
{
        memset(srAuthStr, 0, sizeof(srAuthStr));
        size_t ul = strlen(p1);
        size_t pl = strlen(p2);
        unsigned char input[3], output[5];
        int inputOffset = 0;

        for (size_t i = 0; i < (ul+1+pl); i++) {
                if (i < ul)
                        input[inputOffset++] = p1[i];
                else if (i == ul)
                        input[inputOffset++] = ':';
                else
                        input[inputOffset++] = p2[i-(ul+1)];

                if ((inputOffset == 3) || (i == ul+pl)) {
                        b64_encode(input, inputOffset, output, 4);
                        output[4] = '\0';
                        strcat(srAuthStr, (char*)output);
                        inputOffset = 0;
                }
        }
}

void setUsername(const char* username)
{
        srUsername = username;
}

void setPassword(const char* password)
{
        srPassword = password;
}

void setSmartRestFmt()
{
        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";
        snprintf(fmtSmartRest, sizeof(fmtSmartRest), fmt, srHost, srAuthStr, srX_ID);
}

void setX_ID(const char* id)
{
        if (id) {
            srX_ID = id;
            setSmartRestFmt();
        }
}

void setDeviceID(long id)
{
        deviceID = id;
}

void setAuth(const char* username, const char* password)
{
        if (username)
            setUsername(username);
        if (password)
            setPassword(password);
        if (username || password) {
            setAuthStr(username, password);
            setSmartRestFmt();
        }
}