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/RtosSmartRest.cpp

Committer:
vwochnik
Date:
2014-10-23
Revision:
55:a0f7295ed6b6
Child:
67:c360a2b2c948

File content as of revision 55:a0f7295ed6b6:

#include "RtosSmartRest.h"

RtosSmartRest::RtosSmartRest(const char *host, uint16_t port, const char *identifier, uint8_t tries) :
    _host(host),
    _port(port),
    _identifier(identifier),
    _tries(tries),
    _username(NULL),
    _password(NULL),
    _count(0)
{
}

RtosSmartRest::~RtosSmartRest()
{
    size_t i;
    
    for (i = 0; i < _count; i++)
        delete _slots[i].inst;
}

uint8_t RtosSmartRest::setAuthorization(const char *username, const char *password)
{
    uint8_t ret;
    MbedSmartRest *inst;
    
    if ((inst = instance()) == NULL)
        return SMARTREST_INTERNAL_ERROR;
    
    if ((ret = inst->setAuthorization(username, password)) == SMARTREST_SUCCESS) {
        _username = username;
        _password = password;
    }
    
    return ret;
}

uint8_t RtosSmartRest::bootstrap(const DataGenerator& generator)
{
    uint8_t ret;
    MbedSmartRest *inst;
    
    if ((inst = instance()) == NULL)
        return SMARTREST_INTERNAL_ERROR;
    
    if ((ret = inst->bootstrap(generator)) == SMARTREST_SUCCESS)
        _identifier = inst->getIdentifier();
    
    return ret;
}
    
uint8_t RtosSmartRest::send(const DataGenerator& generator, const char *overrideIdentifier)
{
    MbedSmartRest *inst;
    
    if ((inst = instance()) == NULL)
        return SMARTREST_INTERNAL_ERROR;

    return inst->send(generator, overrideIdentifier);
}

uint8_t RtosSmartRest::receive(ParsedRecord& record)
{
    MbedSmartRest *inst;
    
    if ((inst = instance()) == NULL)
        return SMARTREST_INTERNAL_ERROR;

    return inst->receive(record);
}

void RtosSmartRest::stop()
{
    MbedSmartRest *inst;
    
    if ((inst = instance()) == NULL)
        return;

    inst->stop();
}

const char * RtosSmartRest::getIdentifier()
{
    return _identifier;
}

MbedSmartRest * RtosSmartRest::instance()
{
    size_t i;
    osThreadId tid;
    MbedSmartRest *inst;
    RtosSmartRest::Slot slot;
    
    tid = Thread::gettid();
    
    for (i = 0; i < _count; i++) {
        if (tid == _slots[i].tid)
            return _slots[i].inst;
    }
    
    if (_count == RTOS_SMARTREST_SLOTS)
        return NULL;
    
    inst = new MbedSmartRest(_host, _port, _identifier, _tries);
    inst->setAuthorization(_username, _password);

    slot.tid = tid;
    slot.inst = inst;
    
    _slots[_count++] = slot;
    return inst;
}