Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

DeviceBootstrap.cpp

Committer:
Cumulocity
Date:
2014-07-24
Revision:
42:104746744af8
Parent:
41:804f6a0bda26
Child:
43:eff77697d88c

File content as of revision 42:104746744af8:

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

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

bool DeviceBootstrap::setUpCredentials()
{
    if (((*_username == '\0') || (*_password == '\0')) &&
        (!obtainFromStorage())) {
        if (!obtainFromPlatform())
            return false;
        if (!writeToStorage())
            puts("Warning: Could not write credentials to file!");
    }

    printf("Credentials: %s : %s\n", _username, _password);
    if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS)
        return false;
    return true;
}

bool DeviceBootstrap::obtainFromStorage()
{
    char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr;
    
    int res = _mdm.readFile(CREDENTIALS_FILE, buf, sizeof(buf));

    if (res < 0)
        return false;
        
    buf[res] = '\0';
    if ((ptr = strchr(buf, '\n')) == NULL)
        return false;
    *ptr = '\0';
    
    ptr = buf;
    strcpy(_username, ptr);
    ptr += strlen(ptr)+1;
    strcpy(_password, ptr);
    return true;
}

bool DeviceBootstrap::obtainFromPlatform()
{
    uint8_t ret;
    const char *id;
    
    id = _deviceInfo.imsi();
    printf("Starting device bootstrap with '%s'\n", id);
    
    ComposedRecord record;
    ParsedRecord recvdRecord;

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

    // set authorization for bootstrap
    if (_client.setAuthorization(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD) != SMARTREST_SUCCESS)
        return false;

    while (true) {
        if (_client.send(record, "") != SMARTREST_SUCCESS) {
            puts("Connection unsuccessful. Retrying.");
            _client.stop();
            Thread::wait(2000);
            continue;
        }
        
        if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) {
            puts("Receiving failure.");
            _client.stop();
            Thread::wait(2000);
            continue;
        }
        _client.stop();
        
        for (size_t q = 0; q < recvdRecord.values(); q++)
            puts(recvdRecord.rawValue(q));
            
        if ((recvdRecord.values() < 1) ||
            (recvdRecord.value(0).integerValue() == 50)) {
            puts("No credentials available yet. Retrying.");
            Thread::wait(2000);
            continue;
        }
        
        if ((recvdRecord.value(0).integerValue() != 70) ||
            (recvdRecord.values() != 6)) {
            puts("Bad credentials received.");
            return false;
        }
        
        setCredentials(recvdRecord.value(3).characterValue(),
                       recvdRecord.value(4).characterValue(),
                       recvdRecord.value(5).characterValue());
        
        return true;
    }
    return false;
}

bool DeviceBootstrap::writeToStorage()
{
    char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr;
    size_t len;

    ptr = buf;
    len = strlen(_username);
    strcpy(ptr, _username);
    ptr += len;
    
    *ptr++ = '\n';
    len++;
    
    len += strlen(_password);
    strcpy(ptr, _password);
    
    _mdm.delFile(CREDENTIALS_FILE);
    _mdm.writeFile(CREDENTIALS_FILE, buf, len);
    return true;
}

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

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