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
Diff: DeviceBootstrap.cpp
- Revision:
- 93:0acd11870c6a
- Parent:
- 92:48069375dffa
- Child:
- 94:61d44636f020
--- a/DeviceBootstrap.cpp Fri Mar 20 14:27:10 2015 +0000 +++ b/DeviceBootstrap.cpp Mon Apr 13 14:24:58 2015 +0000 @@ -1,35 +1,36 @@ -#include "DeviceBootstrap.h" #include <stdlib.h> #include <stdio.h> #include <string.h> +#include "DeviceBootstrap.h" #include "rtos.h" #include "ComposedRecord.h" #include "CharValue.h" #include "IntegerValue.h" #include "ParsedRecord.h" +#include "SmartRestConf.h" #include "logging.h" -DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, DeviceIO& io, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) : +DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, LCDDisplay& lcdDisplay, + DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) : _client(client), _deviceInfo(deviceInfo), _deviceMemory(deviceMemory), - _io(io) + _lcdDisplay(lcdDisplay) { *_username = *_password = '\0'; } bool DeviceBootstrap::setUpCredentials() { - if (((*_username == '\0') || (*_password == '\0')) && + if ((*_username == '\0' || *_password == '\0') && (!obtainFromStorage())) { if (!obtainFromPlatform()) return false; if (!writeToStorage()) - aWarning("Could not write credentials to file!"); + aWarning("Can not write credentials!\n"); } - if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS) - return false; + setAuth(_username, _password); return true; } @@ -49,24 +50,21 @@ } bool DeviceBootstrap::obtainFromPlatform() -{ - uint8_t tries; - +{ ComposedRecord record; ParsedRecord recvdRecord; IntegerValue msgId(61); CharValue identifier(_deviceInfo.imei()); - if ((!record.add(msgId)) || (!record.add(identifier))) + 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; + setAuth(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD); - _io.lcdPrint("Bootstrap", _deviceInfo.imei()); - - tries = 255; + _lcdDisplay.setLines("Bootstrap", _deviceInfo.imei()); + + uint8_t tries = 255; do { if (_client.send(record, "") != SMARTREST_SUCCESS) { _client.stop(); @@ -79,11 +77,7 @@ 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); @@ -97,11 +91,11 @@ setCredentials(recvdRecord.value(3).characterValue(), recvdRecord.value(4).characterValue(), recvdRecord.value(5).characterValue()); - _io.lcdPrint("Bootstrap Success", _username, _password); + _lcdDisplay.setLines("Bootstrap Success", _username, _password); return true; } while (--tries > 0); - _io.lcdPrint("Bootstrap Failure"); + _lcdDisplay.setLines("Bootstrap Failure"); return false; }