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-15
Revision:
113:3872569be2af
Parent:
110:b7a403dbceb6
Child:
117:5de54f09f754

File content as of revision 113:3872569be2af:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "rtos.h"
#include "DeviceBootstrap.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"

// Device bootstrap tenant, username and password
#define BOOTSTRAP_TENANT "management"
#define BOOTSTRAP_USERNAME "devicebootstrap"
#define BOOTSTRAP_PASSWORD "Fhdt1bb1f"

DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
    DeviceInfo& deviceInfo):
    _client(client),
    _deviceInfo(deviceInfo)
{
}

bool DeviceBootstrap::bootstrap()
{
    char tenant[CREDENTIAL_LENGTH];
    char username[CREDENTIAL_LENGTH];
    char password[CREDENTIAL_LENGTH];
    if (loadCredential(tenant, username, password, CREDENTIAL_LENGTH)) {
        setAuth(tenant, username, password);
        aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr);
        return true;
    } else if (obtainFromPlatform()) {
        if (!saveCredential(srTenant, srUsername, srPassword, CREDENTIAL_LENGTH))
            aError("Save credentials!\n");
        return true;
    } else {
        return false;
    }
}

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

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

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

    // set authorization for bootstrap
    setAuth(BOOTSTRAP_TENANT, BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
    for (uint8_t tries = 255; tries; --tries) {
        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;
        }

        setAuth(recvdRecord.value(3).characterValue(),
                recvdRecord.value(4).characterValue(),
                recvdRecord.value(5).characterValue());
        LCDDisplay::inst().setLines("Bootstrap Success", srTenant, srUsername);
        aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr);
        return true;
    }
    LCDDisplay::inst().setLines("Bootstrap Failure");
    return false;
}