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 Feb 16 13:15:52 2015 +0000
Revision:
72:c5709ae7b193
Parent:
71:063c45e99578
Child:
77:f6717e4eccc4
logging: incorporation of dedicated logging module.

Who changed what in which revision?

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