Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Apr 20 15:04:23 2015 +0000
Revision:
93:61d44636f020
Parent:
92:0acd11870c6a
Child:
95:5dfdc8568e9f
Integration of new device push code base.

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 92: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 92:0acd11870c6a 10 #include "SmartRestConf.h"
xinlei 72:c5709ae7b193 11 #include "logging.h"
Cumulocity 41:804f6a0bda26 12
xinlei 92:0acd11870c6a 13 DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, LCDDisplay& lcdDisplay,
xinlei 92:0acd11870c6a 14 DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
Cumulocity 41:804f6a0bda26 15 _client(client),
vwochnik 67:c360a2b2c948 16 _deviceInfo(deviceInfo),
xinlei 91:48069375dffa 17 _deviceMemory(deviceMemory),
xinlei 92: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 92: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 92: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 92: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 92: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 92:0acd11870c6a 61 setAuth(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD);
Cumulocity 41:804f6a0bda26 62
xinlei 92:0acd11870c6a 63 _lcdDisplay.setLines("Bootstrap", _deviceInfo.imei());
xinlei 92:0acd11870c6a 64
xinlei 92: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 89: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 92: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 92:0acd11870c6a 92 _lcdDisplay.setLines("Bootstrap Success", _username, _password);
Cumulocity 41:804f6a0bda26 93 return true;
vwochnik 52:8f1370084268 94 } while (--tries > 0);
vwochnik 52:8f1370084268 95
xinlei 92:0acd11870c6a 96 _lcdDisplay.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 }