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-04-20
Revision:
94:61d44636f020
Parent:
93:0acd11870c6a
Child:
95:010b0f7a0a1a

File content as of revision 94:61d44636f020:

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

const char* _username = NULL;
const char* _password = NULL;
char _authStr[100] = {0};
const char *_identifier = "com_cumulocity_MbedAgent_1.5.2";
const char _host[] = "developer.cumulocity.com";
// const char _host[] = "management.m2m-devicecloud.com"
const int _port = 80;
char fmtSmartRest[200] = {0};

const char* getAuthStr()
{
        return _authStr;
}

void setAuthStr(const char* p1, const char* p2)
{
        memset(_authStr, 0, sizeof(_authStr));
        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(_authStr, (char*)output);
                        inputOffset = 0;
                }
        }
}

const char* getUsername()
{
        return _username;
}

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

const char* getPassword()
{
        return _password;
}

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

const char* getHost()
{
        return _host;
}

int getPort()
{
        return _port;
}

const char* getSmartRestFmt()
{
        return fmtSmartRest;
}

const char* getIdentifier()
{
        return _identifier;
}

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, getHost(), getAuthStr(), getIdentifier());
}

void setIdentifier(const char* id)
{
        if (id) {
            _identifier = id;
            setSmartRestFmt();
        }
}

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