![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
DeviceBootstrap.cpp
- Committer:
- xinlei
- Date:
- 2015-03-16
- Revision:
- 88:8ab476939897
- Parent:
- 77:f6717e4eccc4
- Child:
- 89:0525121f307e
File content as of revision 88:8ab476939897:
#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" #include "logging.h" DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, DeviceIO& io, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) : _client(client), _io(io), _deviceInfo(deviceInfo), _deviceMemory(deviceMemory) { *_username = *_password = '\0'; } bool DeviceBootstrap::setUpCredentials() { if (((*_username == '\0') || (*_password == '\0')) && (!obtainFromStorage())) { if (!obtainFromPlatform()) return false; if (!writeToStorage()) aWarning("Could not write credentials to file!"); } if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS) return false; return true; } const char * DeviceBootstrap::username() { return _username; } const char * DeviceBootstrap::password() { return _password; } bool DeviceBootstrap::obtainFromStorage() { return _deviceMemory.loadPlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH); } bool DeviceBootstrap::obtainFromPlatform() { uint8_t tries; ComposedRecord record; ParsedRecord recvdRecord; IntegerValue msgId(61); CharValue identifier(_deviceInfo.imei()); 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; _io.lcdPrint("Bootstrap", _deviceInfo.imei()); 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 (getLevel() == A_DEBUG) { for (size_t q = 0; q < recvdRecord.values(); q++) aDebug("Bootstrap, received: %s\r\n", recvdRecord.rawValue(q)); } 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()); _io.lcdPrint("Bootstrap Success", _username, _password); return true; } while (--tries > 0); _io.lcdPrint("Bootstrap Failure"); return false; } bool DeviceBootstrap::writeToStorage() { return _deviceMemory.savePlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH); } 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'; }