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 13 14:24:58 2015 +0000
Revision:
93:0acd11870c6a
Parent:
92:48069375dffa
Child:
94:61d44636f020
v2.1rc1

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