![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
DeviceBootstrap.cpp@104:fd3571349e5d, 2015-05-08 (annotated)
- Committer:
- xinlei
- Date:
- Fri May 08 14:50:43 2015 +0000
- Revision:
- 104:fd3571349e5d
- Parent:
- 100:dbcd3bc51758
- Child:
- 109:b7a403dbceb6
quick fix for file renaming.
Who changed what in which revision?
User | Revision | Line number | New 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 ((*_username == '\0' || *_password == '\0') && |
xinlei | 100:dbcd3bc51758 | 32 | // (!obtainFromStorage())) { |
xinlei | 100:dbcd3bc51758 | 33 | // if (!obtainFromPlatform()) |
xinlei | 100:dbcd3bc51758 | 34 | // return false; |
xinlei | 100:dbcd3bc51758 | 35 | // if (!writeToStorage()) |
xinlei | 100:dbcd3bc51758 | 36 | // aError("Can not write credentials!\n"); |
xinlei | 100:dbcd3bc51758 | 37 | // } |
xinlei | 100:dbcd3bc51758 | 38 | if (loadCredential(_username, _password, CREDENTIAL_LENGTH)) { |
xinlei | 100:dbcd3bc51758 | 39 | return true; |
xinlei | 100:dbcd3bc51758 | 40 | } else if (obtainFromPlatform()) { |
xinlei | 100:dbcd3bc51758 | 41 | if (!saveCredential(_username, _password, CREDENTIAL_LENGTH)) |
xinlei | 100:dbcd3bc51758 | 42 | aError("Write credentials!\n"); |
xinlei | 100:dbcd3bc51758 | 43 | return true; |
xinlei | 100:dbcd3bc51758 | 44 | } else { |
xinlei | 100:dbcd3bc51758 | 45 | return false; |
Cumulocity | 41:804f6a0bda26 | 46 | } |
Cumulocity | 41:804f6a0bda26 | 47 | } |
Cumulocity | 41:804f6a0bda26 | 48 | |
Cumulocity | 46:f6976fd64387 | 49 | const char * DeviceBootstrap::username() |
Cumulocity | 46:f6976fd64387 | 50 | { |
Cumulocity | 46:f6976fd64387 | 51 | return _username; |
Cumulocity | 46:f6976fd64387 | 52 | } |
Cumulocity | 46:f6976fd64387 | 53 | |
Cumulocity | 46:f6976fd64387 | 54 | const char * DeviceBootstrap::password() |
Cumulocity | 46:f6976fd64387 | 55 | { |
Cumulocity | 46:f6976fd64387 | 56 | return _password; |
Cumulocity | 46:f6976fd64387 | 57 | } |
Cumulocity | 46:f6976fd64387 | 58 | |
Cumulocity | 41:804f6a0bda26 | 59 | bool DeviceBootstrap::obtainFromStorage() |
Cumulocity | 41:804f6a0bda26 | 60 | { |
xinlei | 100:dbcd3bc51758 | 61 | return loadCredential(_username, _password, CREDENTIAL_LENGTH); |
Cumulocity | 41:804f6a0bda26 | 62 | } |
Cumulocity | 41:804f6a0bda26 | 63 | |
Cumulocity | 41:804f6a0bda26 | 64 | bool DeviceBootstrap::obtainFromPlatform() |
xinlei | 100:dbcd3bc51758 | 65 | { |
Cumulocity | 41:804f6a0bda26 | 66 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 67 | ParsedRecord recvdRecord; |
Cumulocity | 41:804f6a0bda26 | 68 | |
Cumulocity | 41:804f6a0bda26 | 69 | IntegerValue msgId(61); |
vwochnik | 52:8f1370084268 | 70 | CharValue identifier(_deviceInfo.imei()); |
xinlei | 92:0acd11870c6a | 71 | if (!record.add(msgId) || !record.add(identifier)) |
Cumulocity | 41:804f6a0bda26 | 72 | return false; |
Cumulocity | 41:804f6a0bda26 | 73 | |
Cumulocity | 41:804f6a0bda26 | 74 | // set authorization for bootstrap |
xinlei | 100:dbcd3bc51758 | 75 | setAuth(BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD); |
Cumulocity | 41:804f6a0bda26 | 76 | |
xinlei | 95:5dfdc8568e9f | 77 | LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei()); |
xinlei | 92:0acd11870c6a | 78 | |
xinlei | 92:0acd11870c6a | 79 | uint8_t tries = 255; |
vwochnik | 52:8f1370084268 | 80 | do { |
Cumulocity | 41:804f6a0bda26 | 81 | if (_client.send(record, "") != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 82 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 83 | Thread::wait(2000); |
Cumulocity | 41:804f6a0bda26 | 84 | continue; |
xinlei | 89:0525121f307e | 85 | } |
Cumulocity | 41:804f6a0bda26 | 86 | if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 87 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 88 | Thread::wait(2000); |
Cumulocity | 41:804f6a0bda26 | 89 | continue; |
Cumulocity | 41:804f6a0bda26 | 90 | } |
Cumulocity | 42:104746744af8 | 91 | _client.stop(); |
xinlei | 92:0acd11870c6a | 92 | |
Cumulocity | 41:804f6a0bda26 | 93 | if ((recvdRecord.values() < 1) || |
Cumulocity | 41:804f6a0bda26 | 94 | (recvdRecord.value(0).integerValue() == 50)) { |
Cumulocity | 41:804f6a0bda26 | 95 | Thread::wait(2000); |
Cumulocity | 41:804f6a0bda26 | 96 | continue; |
Cumulocity | 41:804f6a0bda26 | 97 | } |
Cumulocity | 41:804f6a0bda26 | 98 | if ((recvdRecord.value(0).integerValue() != 70) || |
Cumulocity | 41:804f6a0bda26 | 99 | (recvdRecord.values() != 6)) { |
Cumulocity | 41:804f6a0bda26 | 100 | return false; |
Cumulocity | 41:804f6a0bda26 | 101 | } |
Cumulocity | 41:804f6a0bda26 | 102 | |
Cumulocity | 41:804f6a0bda26 | 103 | setCredentials(recvdRecord.value(3).characterValue(), |
Cumulocity | 41:804f6a0bda26 | 104 | recvdRecord.value(4).characterValue(), |
xinlei | 77:f6717e4eccc4 | 105 | recvdRecord.value(5).characterValue()); |
xinlei | 95:5dfdc8568e9f | 106 | LCDDisplay::inst().setLines("Bootstrap Success", _username, _password); |
Cumulocity | 41:804f6a0bda26 | 107 | return true; |
vwochnik | 52:8f1370084268 | 108 | } while (--tries > 0); |
vwochnik | 52:8f1370084268 | 109 | |
xinlei | 95:5dfdc8568e9f | 110 | LCDDisplay::inst().setLines("Bootstrap Failure"); |
Cumulocity | 41:804f6a0bda26 | 111 | return false; |
Cumulocity | 41:804f6a0bda26 | 112 | } |
Cumulocity | 41:804f6a0bda26 | 113 | |
Cumulocity | 41:804f6a0bda26 | 114 | bool DeviceBootstrap::writeToStorage() |
Cumulocity | 41:804f6a0bda26 | 115 | { |
xinlei | 100:dbcd3bc51758 | 116 | return saveCredential(_username, _password, CREDENTIAL_LENGTH); |
Cumulocity | 41:804f6a0bda26 | 117 | } |
Cumulocity | 41:804f6a0bda26 | 118 | |
Cumulocity | 41:804f6a0bda26 | 119 | void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password) |
Cumulocity | 41:804f6a0bda26 | 120 | { |
Cumulocity | 41:804f6a0bda26 | 121 | *_username = '\0'; |
Cumulocity | 41:804f6a0bda26 | 122 | if (tenant != NULL) { |
xinlei | 100:dbcd3bc51758 | 123 | strncpy(_username, tenant, CREDENTIAL_LENGTH); |
xinlei | 100:dbcd3bc51758 | 124 | _username[CREDENTIAL_LENGTH-1] = '\0'; |
xinlei | 100:dbcd3bc51758 | 125 | if (strlen(_username)+1 < CREDENTIAL_LENGTH) |
Cumulocity | 41:804f6a0bda26 | 126 | strcat(_username, "/"); |
Cumulocity | 41:804f6a0bda26 | 127 | } |
xinlei | 100:dbcd3bc51758 | 128 | strncat(_username, username, CREDENTIAL_LENGTH-strlen(_username)); |
xinlei | 100:dbcd3bc51758 | 129 | _username[CREDENTIAL_LENGTH-1] = '\0'; |
Cumulocity | 41:804f6a0bda26 | 130 | |
xinlei | 100:dbcd3bc51758 | 131 | strncpy(_password, password, CREDENTIAL_LENGTH); |
xinlei | 100:dbcd3bc51758 | 132 | _password[CREDENTIAL_LENGTH-1] = '\0'; |
Cumulocity | 41:804f6a0bda26 | 133 | } |