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

Committer:
xinlei
Date:
Mon Apr 27 10:50:21 2015 +0000
Revision:
96:5dfdc8568e9f
Parent:
94:61d44636f020
Child:
98:9f2de96941c4
LCDDisplay now is singleton

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 41:804f6a0bda26 1 #include <stdlib.h>
Cumulocity 41:804f6a0bda26 2 #include <stdio.h>
Cumulocity 41:804f6a0bda26 3 #include <string.h>
xinlei 93:0acd11870c6a 4 #include "DeviceBootstrap.h"
Cumulocity 41:804f6a0bda26 5 #include "rtos.h"
xinlei 96:5dfdc8568e9f 6 #include "LCDDisplay.h"
Cumulocity 41:804f6a0bda26 7 #include "ComposedRecord.h"
Cumulocity 41:804f6a0bda26 8 #include "CharValue.h"
Cumulocity 41:804f6a0bda26 9 #include "IntegerValue.h"
Cumulocity 41:804f6a0bda26 10 #include "ParsedRecord.h"
xinlei 93:0acd11870c6a 11 #include "SmartRestConf.h"
xinlei 72:c5709ae7b193 12 #include "logging.h"
Cumulocity 41:804f6a0bda26 13
xinlei 96:5dfdc8568e9f 14 DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
xinlei 93:0acd11870c6a 15 DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
Cumulocity 41:804f6a0bda26 16 _client(client),
vwochnik 67:c360a2b2c948 17 _deviceInfo(deviceInfo),
xinlei 96:5dfdc8568e9f 18 _deviceMemory(deviceMemory)
Cumulocity 41:804f6a0bda26 19 {
Cumulocity 41:804f6a0bda26 20 *_username = *_password = '\0';
Cumulocity 41:804f6a0bda26 21 }
Cumulocity 41:804f6a0bda26 22
Cumulocity 41:804f6a0bda26 23 bool DeviceBootstrap::setUpCredentials()
Cumulocity 41:804f6a0bda26 24 {
xinlei 93:0acd11870c6a 25 if ((*_username == '\0' || *_password == '\0') &&
Cumulocity 41:804f6a0bda26 26 (!obtainFromStorage())) {
Cumulocity 41:804f6a0bda26 27 if (!obtainFromPlatform())
Cumulocity 41:804f6a0bda26 28 return false;
Cumulocity 41:804f6a0bda26 29 if (!writeToStorage())
xinlei 93:0acd11870c6a 30 aWarning("Can not write credentials!\n");
Cumulocity 41:804f6a0bda26 31 }
Cumulocity 41:804f6a0bda26 32 return true;
Cumulocity 41:804f6a0bda26 33 }
Cumulocity 41:804f6a0bda26 34
Cumulocity 46:f6976fd64387 35 const char * DeviceBootstrap::username()
Cumulocity 46:f6976fd64387 36 {
Cumulocity 46:f6976fd64387 37 return _username;
Cumulocity 46:f6976fd64387 38 }
Cumulocity 46:f6976fd64387 39
Cumulocity 46:f6976fd64387 40 const char * DeviceBootstrap::password()
Cumulocity 46:f6976fd64387 41 {
Cumulocity 46:f6976fd64387 42 return _password;
Cumulocity 46:f6976fd64387 43 }
Cumulocity 46:f6976fd64387 44
Cumulocity 41:804f6a0bda26 45 bool DeviceBootstrap::obtainFromStorage()
Cumulocity 41:804f6a0bda26 46 {
vwochnik 67:c360a2b2c948 47 return _deviceMemory.loadPlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
Cumulocity 41:804f6a0bda26 48 }
Cumulocity 41:804f6a0bda26 49
Cumulocity 41:804f6a0bda26 50 bool DeviceBootstrap::obtainFromPlatform()
xinlei 93:0acd11870c6a 51 {
Cumulocity 41:804f6a0bda26 52 ComposedRecord record;
Cumulocity 41:804f6a0bda26 53 ParsedRecord recvdRecord;
Cumulocity 41:804f6a0bda26 54
Cumulocity 41:804f6a0bda26 55 IntegerValue msgId(61);
vwochnik 52:8f1370084268 56 CharValue identifier(_deviceInfo.imei());
xinlei 93:0acd11870c6a 57 if (!record.add(msgId) || !record.add(identifier))
Cumulocity 41:804f6a0bda26 58 return false;
Cumulocity 41:804f6a0bda26 59
Cumulocity 41:804f6a0bda26 60 // set authorization for bootstrap
xinlei 93:0acd11870c6a 61 setAuth(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD);
Cumulocity 41:804f6a0bda26 62
xinlei 96:5dfdc8568e9f 63 LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei());
xinlei 93:0acd11870c6a 64
xinlei 93:0acd11870c6a 65 uint8_t tries = 255;
vwochnik 52:8f1370084268 66 do {
Cumulocity 41:804f6a0bda26 67 if (_client.send(record, "") != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 68 _client.stop();
Cumulocity 41:804f6a0bda26 69 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 70 continue;
xinlei 90:0525121f307e 71 }
Cumulocity 41:804f6a0bda26 72 if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 73 _client.stop();
Cumulocity 41:804f6a0bda26 74 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 75 continue;
Cumulocity 41:804f6a0bda26 76 }
Cumulocity 42:104746744af8 77 _client.stop();
xinlei 93:0acd11870c6a 78
Cumulocity 41:804f6a0bda26 79 if ((recvdRecord.values() < 1) ||
Cumulocity 41:804f6a0bda26 80 (recvdRecord.value(0).integerValue() == 50)) {
Cumulocity 41:804f6a0bda26 81 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 82 continue;
Cumulocity 41:804f6a0bda26 83 }
Cumulocity 41:804f6a0bda26 84 if ((recvdRecord.value(0).integerValue() != 70) ||
Cumulocity 41:804f6a0bda26 85 (recvdRecord.values() != 6)) {
Cumulocity 41:804f6a0bda26 86 return false;
Cumulocity 41:804f6a0bda26 87 }
Cumulocity 41:804f6a0bda26 88
Cumulocity 41:804f6a0bda26 89 setCredentials(recvdRecord.value(3).characterValue(),
Cumulocity 41:804f6a0bda26 90 recvdRecord.value(4).characterValue(),
xinlei 77:f6717e4eccc4 91 recvdRecord.value(5).characterValue());
xinlei 96:5dfdc8568e9f 92 LCDDisplay::inst().setLines("Bootstrap Success", _username, _password);
Cumulocity 41:804f6a0bda26 93 return true;
vwochnik 52:8f1370084268 94 } while (--tries > 0);
vwochnik 52:8f1370084268 95
xinlei 96:5dfdc8568e9f 96 LCDDisplay::inst().setLines("Bootstrap Failure");
Cumulocity 41:804f6a0bda26 97 return false;
Cumulocity 41:804f6a0bda26 98 }
Cumulocity 41:804f6a0bda26 99
Cumulocity 41:804f6a0bda26 100 bool DeviceBootstrap::writeToStorage()
Cumulocity 41:804f6a0bda26 101 {
vwochnik 67:c360a2b2c948 102 return _deviceMemory.savePlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
Cumulocity 41:804f6a0bda26 103 }
Cumulocity 41:804f6a0bda26 104
Cumulocity 41:804f6a0bda26 105 void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
Cumulocity 41:804f6a0bda26 106 {
Cumulocity 41:804f6a0bda26 107 *_username = '\0';
Cumulocity 41:804f6a0bda26 108 if (tenant != NULL) {
Cumulocity 41:804f6a0bda26 109 strncpy(_username, tenant, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
Cumulocity 41:804f6a0bda26 110 _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 111 if (strlen(_username)+1 < DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH)
Cumulocity 41:804f6a0bda26 112 strcat(_username, "/");
Cumulocity 41:804f6a0bda26 113 }
Cumulocity 41:804f6a0bda26 114 strncat(_username, username, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-strlen(_username));
Cumulocity 41:804f6a0bda26 115 _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 116
Cumulocity 41:804f6a0bda26 117 strncpy(_password, password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
Cumulocity 41:804f6a0bda26 118 _password[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 119 }