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

DeviceBootstrap.cpp

Committer:
xinlei
Date:
2015-05-08
Revision:
105:fd3571349e5d
Parent:
101:dbcd3bc51758
Child:
110:b7a403dbceb6

File content as of revision 105:fd3571349e5d:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "DeviceBootstrap.h"
#include "rtos.h"
#include "Storage.h"
#include "LCDDisplay.h"
#include "ComposedRecord.h"
#include "CharValue.h"
#include "IntegerValue.h"
#include "ParsedRecord.h"
#include "SmartRestConf.h"
#include "logging.h"

/** The username used for device bootstrapping. */
#define BOOTSTRAP_USERNAME "management/devicebootstrap"

/** The password used for device bootstrapping. */
#define BOOTSTRAP_PASSWORD "Fhdt1bb1f"

DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
    DeviceInfo& deviceInfo) :
    _client(client),
    _deviceInfo(deviceInfo)
{
    *_username = *_password = '\0';
}

bool DeviceBootstrap::setUpCredentials()
{
//    if ((*_username == '\0' || *_password == '\0') &&
//        (!obtainFromStorage())) {
//        if (!obtainFromPlatform())
//            return false;
//        if (!writeToStorage())
//            aError("Can not write credentials!\n");
//    }
    if (loadCredential(_username, _password, CREDENTIAL_LENGTH)) {
        return true;
    } else if (obtainFromPlatform()) {
        if (!saveCredential(_username, _password, CREDENTIAL_LENGTH))
            aError("Write credentials!\n");
        return true;
    } else {
        return false;
    }
}

const char * DeviceBootstrap::username()
{
    return _username;
}

const char * DeviceBootstrap::password()
{
    return _password;
}

bool DeviceBootstrap::obtainFromStorage()
{
    return loadCredential(_username, _password, CREDENTIAL_LENGTH);
}

bool DeviceBootstrap::obtainFromPlatform()
{
    ComposedRecord record;
    ParsedRecord recvdRecord;

    IntegerValue msgId(61);
    CharValue identifier(_deviceInfo.imei());
    if (!record.add(msgId) || !record.add(identifier))
        return false;

    // set authorization for bootstrap
    setAuth(BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);

    LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei());

    uint8_t tries = 255;
    do {
        if (_client.send(record, "") != SMARTREST_SUCCESS) {
            _client.stop();
            Thread::wait(2000);
            continue;
        }
        if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) {
            _client.stop();
            Thread::wait(2000);
            continue;
        }
        _client.stop();

        if ((recvdRecord.values() < 1) ||
            (recvdRecord.value(0).integerValue() == 50)) {
            Thread::wait(2000);
            continue;
        }
        if ((recvdRecord.value(0).integerValue() != 70) ||
            (recvdRecord.values() != 6)) {
            return false;
        }
        
        setCredentials(recvdRecord.value(3).characterValue(),
                       recvdRecord.value(4).characterValue(),
                       recvdRecord.value(5).characterValue());        
        LCDDisplay::inst().setLines("Bootstrap Success", _username, _password);
        return true;
    } while (--tries > 0);

    LCDDisplay::inst().setLines("Bootstrap Failure");
    return false;
}

bool DeviceBootstrap::writeToStorage()
{
    return saveCredential(_username, _password, CREDENTIAL_LENGTH);
}

void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
{
    *_username = '\0';
    if (tenant != NULL) {
        strncpy(_username, tenant, CREDENTIAL_LENGTH);
        _username[CREDENTIAL_LENGTH-1] = '\0';
        if (strlen(_username)+1 < CREDENTIAL_LENGTH)
            strcat(_username, "/");
    }
    strncat(_username, username, CREDENTIAL_LENGTH-strlen(_username));
    _username[CREDENTIAL_LENGTH-1] = '\0';

    strncpy(_password, password, CREDENTIAL_LENGTH);
    _password[CREDENTIAL_LENGTH-1] = '\0';
}