![](/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@97:9f2de96941c4, 2015-04-27 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 27 13:30:21 2015 +0000
- Revision:
- 97:9f2de96941c4
- Parent:
- 95:5dfdc8568e9f
- Child:
- 100:dbcd3bc51758
Get rid of useless DeviceMemory class wrapper
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 | 97:9f2de96941c4 | 6 | #include "DeviceMemory.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 | 95:5dfdc8568e9f | 15 | DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, |
xinlei | 97:9f2de96941c4 | 16 | DeviceInfo& deviceInfo) : |
Cumulocity | 41:804f6a0bda26 | 17 | _client(client), |
xinlei | 97:9f2de96941c4 | 18 | _deviceInfo(deviceInfo) |
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 | { |
xinlei | 97:9f2de96941c4 | 47 | return 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 | 95:5dfdc8568e9f | 63 | LCDDisplay::inst().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 | 95:5dfdc8568e9f | 92 | LCDDisplay::inst().setLines("Bootstrap Success", _username, _password); |
Cumulocity | 41:804f6a0bda26 | 93 | return true; |
vwochnik | 52:8f1370084268 | 94 | } while (--tries > 0); |
vwochnik | 52:8f1370084268 | 95 | |
xinlei | 95:5dfdc8568e9f | 96 | LCDDisplay::inst().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 | { |
xinlei | 97:9f2de96941c4 | 102 | return 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 | } |