Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 13 15:00:24 2015 +0000
Revision:
109:b7a403dbceb6
Parent:
104:fd3571349e5d
Child:
112:3872569be2af
small fix for when there are no pending operations

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"
xinlei 104:fd3571349e5d 6 #include "Storage.h"
xinlei 95:5dfdc8568e9f 7 #include "LCDDisplay.h"
Cumulocity 41:804f6a0bda26 8 #include "ComposedRecord.h"
Cumulocity 41:804f6a0bda26 9 #include "CharValue.h"
Cumulocity 41:804f6a0bda26 10 #include "IntegerValue.h"
Cumulocity 41:804f6a0bda26 11 #include "ParsedRecord.h"
xinlei 92:0acd11870c6a 12 #include "SmartRestConf.h"
xinlei 72:c5709ae7b193 13 #include "logging.h"
Cumulocity 41:804f6a0bda26 14
xinlei 100:dbcd3bc51758 15 /** The username used for device bootstrapping. */
xinlei 100:dbcd3bc51758 16 #define BOOTSTRAP_USERNAME "management/devicebootstrap"
xinlei 100:dbcd3bc51758 17
xinlei 100:dbcd3bc51758 18 /** The password used for device bootstrapping. */
xinlei 100:dbcd3bc51758 19 #define BOOTSTRAP_PASSWORD "Fhdt1bb1f"
xinlei 100:dbcd3bc51758 20
xinlei 95:5dfdc8568e9f 21 DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
xinlei 97:9f2de96941c4 22 DeviceInfo& deviceInfo) :
Cumulocity 41:804f6a0bda26 23 _client(client),
xinlei 97:9f2de96941c4 24 _deviceInfo(deviceInfo)
Cumulocity 41:804f6a0bda26 25 {
Cumulocity 41:804f6a0bda26 26 *_username = *_password = '\0';
Cumulocity 41:804f6a0bda26 27 }
Cumulocity 41:804f6a0bda26 28
Cumulocity 41:804f6a0bda26 29 bool DeviceBootstrap::setUpCredentials()
Cumulocity 41:804f6a0bda26 30 {
xinlei 100:dbcd3bc51758 31 if (loadCredential(_username, _password, CREDENTIAL_LENGTH)) {
xinlei 100:dbcd3bc51758 32 return true;
xinlei 100:dbcd3bc51758 33 } else if (obtainFromPlatform()) {
xinlei 100:dbcd3bc51758 34 if (!saveCredential(_username, _password, CREDENTIAL_LENGTH))
xinlei 100:dbcd3bc51758 35 aError("Write credentials!\n");
xinlei 100:dbcd3bc51758 36 return true;
xinlei 100:dbcd3bc51758 37 } else {
xinlei 100:dbcd3bc51758 38 return false;
Cumulocity 41:804f6a0bda26 39 }
Cumulocity 41:804f6a0bda26 40 }
Cumulocity 41:804f6a0bda26 41
Cumulocity 41:804f6a0bda26 42 bool DeviceBootstrap::obtainFromPlatform()
xinlei 100:dbcd3bc51758 43 {
Cumulocity 41:804f6a0bda26 44 ComposedRecord record;
Cumulocity 41:804f6a0bda26 45 ParsedRecord recvdRecord;
Cumulocity 41:804f6a0bda26 46
Cumulocity 41:804f6a0bda26 47 IntegerValue msgId(61);
vwochnik 52:8f1370084268 48 CharValue identifier(_deviceInfo.imei());
xinlei 92:0acd11870c6a 49 if (!record.add(msgId) || !record.add(identifier))
Cumulocity 41:804f6a0bda26 50 return false;
Cumulocity 41:804f6a0bda26 51
Cumulocity 41:804f6a0bda26 52 // set authorization for bootstrap
xinlei 100:dbcd3bc51758 53 setAuth(BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
Cumulocity 41:804f6a0bda26 54
xinlei 95:5dfdc8568e9f 55 LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei());
xinlei 92:0acd11870c6a 56
xinlei 92:0acd11870c6a 57 uint8_t tries = 255;
vwochnik 52:8f1370084268 58 do {
Cumulocity 41:804f6a0bda26 59 if (_client.send(record, "") != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 60 _client.stop();
Cumulocity 41:804f6a0bda26 61 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 62 continue;
xinlei 89:0525121f307e 63 }
Cumulocity 41:804f6a0bda26 64 if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 65 _client.stop();
Cumulocity 41:804f6a0bda26 66 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 67 continue;
Cumulocity 41:804f6a0bda26 68 }
Cumulocity 42:104746744af8 69 _client.stop();
xinlei 92:0acd11870c6a 70
Cumulocity 41:804f6a0bda26 71 if ((recvdRecord.values() < 1) ||
Cumulocity 41:804f6a0bda26 72 (recvdRecord.value(0).integerValue() == 50)) {
Cumulocity 41:804f6a0bda26 73 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 74 continue;
Cumulocity 41:804f6a0bda26 75 }
Cumulocity 41:804f6a0bda26 76 if ((recvdRecord.value(0).integerValue() != 70) ||
Cumulocity 41:804f6a0bda26 77 (recvdRecord.values() != 6)) {
Cumulocity 41:804f6a0bda26 78 return false;
Cumulocity 41:804f6a0bda26 79 }
Cumulocity 41:804f6a0bda26 80
Cumulocity 41:804f6a0bda26 81 setCredentials(recvdRecord.value(3).characterValue(),
Cumulocity 41:804f6a0bda26 82 recvdRecord.value(4).characterValue(),
xinlei 77:f6717e4eccc4 83 recvdRecord.value(5).characterValue());
xinlei 95:5dfdc8568e9f 84 LCDDisplay::inst().setLines("Bootstrap Success", _username, _password);
Cumulocity 41:804f6a0bda26 85 return true;
vwochnik 52:8f1370084268 86 } while (--tries > 0);
vwochnik 52:8f1370084268 87
xinlei 95:5dfdc8568e9f 88 LCDDisplay::inst().setLines("Bootstrap Failure");
Cumulocity 41:804f6a0bda26 89 return false;
Cumulocity 41:804f6a0bda26 90 }
Cumulocity 41:804f6a0bda26 91
Cumulocity 41:804f6a0bda26 92 void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
Cumulocity 41:804f6a0bda26 93 {
Cumulocity 41:804f6a0bda26 94 *_username = '\0';
Cumulocity 41:804f6a0bda26 95 if (tenant != NULL) {
xinlei 100:dbcd3bc51758 96 strncpy(_username, tenant, CREDENTIAL_LENGTH);
xinlei 100:dbcd3bc51758 97 _username[CREDENTIAL_LENGTH-1] = '\0';
xinlei 100:dbcd3bc51758 98 if (strlen(_username)+1 < CREDENTIAL_LENGTH)
Cumulocity 41:804f6a0bda26 99 strcat(_username, "/");
Cumulocity 41:804f6a0bda26 100 }
xinlei 100:dbcd3bc51758 101 strncat(_username, username, CREDENTIAL_LENGTH-strlen(_username));
xinlei 100:dbcd3bc51758 102 _username[CREDENTIAL_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 103
xinlei 100:dbcd3bc51758 104 strncpy(_password, password, CREDENTIAL_LENGTH);
xinlei 100:dbcd3bc51758 105 _password[CREDENTIAL_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 106 }