![](/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@60:3c822f97fc73, 2014-10-24 (annotated)
- Committer:
- vwochnik
- Date:
- Fri Oct 24 15:00:36 2014 +0000
- Revision:
- 60:3c822f97fc73
- Parent:
- 55:a0f7295ed6b6
- Child:
- 65:a62dbef2f924
operation thread + funcs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "DeviceBootstrap.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include <stdlib.h> |
Cumulocity | 41:804f6a0bda26 | 3 | #include <stdio.h> |
Cumulocity | 41:804f6a0bda26 | 4 | #include <string.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" |
Cumulocity | 41:804f6a0bda26 | 10 | |
vwochnik | 55:a0f7295ed6b6 | 11 | DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, MDMSerial& mdm, DeviceIO& io, DeviceInfo& deviceInfo) : |
Cumulocity | 41:804f6a0bda26 | 12 | _client(client), |
Cumulocity | 41:804f6a0bda26 | 13 | _mdm(mdm), |
vwochnik | 52:8f1370084268 | 14 | _io(io), |
Cumulocity | 41:804f6a0bda26 | 15 | _deviceInfo(deviceInfo) |
Cumulocity | 41:804f6a0bda26 | 16 | { |
Cumulocity | 41:804f6a0bda26 | 17 | *_username = *_password = '\0'; |
Cumulocity | 41:804f6a0bda26 | 18 | } |
Cumulocity | 41:804f6a0bda26 | 19 | |
Cumulocity | 41:804f6a0bda26 | 20 | bool DeviceBootstrap::setUpCredentials() |
Cumulocity | 41:804f6a0bda26 | 21 | { |
vwochnik | 60:3c822f97fc73 | 22 | strcpy(_username, "vaillant/admin"); |
vwochnik | 60:3c822f97fc73 | 23 | strcpy(_password, "klanpi"); |
vwochnik | 60:3c822f97fc73 | 24 | |
Cumulocity | 41:804f6a0bda26 | 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()) |
Cumulocity | 41:804f6a0bda26 | 30 | puts("Warning: Could not write credentials to file!"); |
Cumulocity | 41:804f6a0bda26 | 31 | } |
Cumulocity | 41:804f6a0bda26 | 32 | |
Cumulocity | 41:804f6a0bda26 | 33 | if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS) |
Cumulocity | 41:804f6a0bda26 | 34 | return false; |
Cumulocity | 41:804f6a0bda26 | 35 | return true; |
Cumulocity | 41:804f6a0bda26 | 36 | } |
Cumulocity | 41:804f6a0bda26 | 37 | |
Cumulocity | 46:f6976fd64387 | 38 | const char * DeviceBootstrap::username() |
Cumulocity | 46:f6976fd64387 | 39 | { |
Cumulocity | 46:f6976fd64387 | 40 | return _username; |
Cumulocity | 46:f6976fd64387 | 41 | } |
Cumulocity | 46:f6976fd64387 | 42 | |
Cumulocity | 46:f6976fd64387 | 43 | const char * DeviceBootstrap::password() |
Cumulocity | 46:f6976fd64387 | 44 | { |
Cumulocity | 46:f6976fd64387 | 45 | return _password; |
Cumulocity | 46:f6976fd64387 | 46 | } |
Cumulocity | 46:f6976fd64387 | 47 | |
Cumulocity | 41:804f6a0bda26 | 48 | bool DeviceBootstrap::obtainFromStorage() |
Cumulocity | 41:804f6a0bda26 | 49 | { |
Cumulocity | 41:804f6a0bda26 | 50 | char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr; |
Cumulocity | 42:104746744af8 | 51 | |
Cumulocity | 41:804f6a0bda26 | 52 | int res = _mdm.readFile(CREDENTIALS_FILE, buf, sizeof(buf)); |
Cumulocity | 41:804f6a0bda26 | 53 | if (res < 0) |
Cumulocity | 41:804f6a0bda26 | 54 | return false; |
Cumulocity | 41:804f6a0bda26 | 55 | |
Cumulocity | 41:804f6a0bda26 | 56 | buf[res] = '\0'; |
Cumulocity | 41:804f6a0bda26 | 57 | if ((ptr = strchr(buf, '\n')) == NULL) |
Cumulocity | 41:804f6a0bda26 | 58 | return false; |
Cumulocity | 41:804f6a0bda26 | 59 | *ptr = '\0'; |
Cumulocity | 41:804f6a0bda26 | 60 | |
Cumulocity | 41:804f6a0bda26 | 61 | ptr = buf; |
Cumulocity | 41:804f6a0bda26 | 62 | strcpy(_username, ptr); |
Cumulocity | 41:804f6a0bda26 | 63 | ptr += strlen(ptr)+1; |
Cumulocity | 41:804f6a0bda26 | 64 | strcpy(_password, ptr); |
Cumulocity | 41:804f6a0bda26 | 65 | return true; |
Cumulocity | 41:804f6a0bda26 | 66 | } |
Cumulocity | 41:804f6a0bda26 | 67 | |
Cumulocity | 41:804f6a0bda26 | 68 | bool DeviceBootstrap::obtainFromPlatform() |
Cumulocity | 41:804f6a0bda26 | 69 | { |
Cumulocity | 41:804f6a0bda26 | 70 | uint8_t ret; |
vwochnik | 52:8f1370084268 | 71 | uint8_t tries; |
Cumulocity | 41:804f6a0bda26 | 72 | |
Cumulocity | 41:804f6a0bda26 | 73 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 74 | ParsedRecord recvdRecord; |
Cumulocity | 41:804f6a0bda26 | 75 | |
Cumulocity | 41:804f6a0bda26 | 76 | IntegerValue msgId(61); |
vwochnik | 52:8f1370084268 | 77 | CharValue identifier(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 78 | if ((!record.add(msgId)) || (!record.add(identifier))) |
Cumulocity | 41:804f6a0bda26 | 79 | return false; |
Cumulocity | 41:804f6a0bda26 | 80 | |
Cumulocity | 41:804f6a0bda26 | 81 | // set authorization for bootstrap |
Cumulocity | 41:804f6a0bda26 | 82 | if (_client.setAuthorization(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD) != SMARTREST_SUCCESS) |
Cumulocity | 41:804f6a0bda26 | 83 | return false; |
Cumulocity | 41:804f6a0bda26 | 84 | |
vwochnik | 52:8f1370084268 | 85 | _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei()); |
vwochnik | 52:8f1370084268 | 86 | |
vwochnik | 52:8f1370084268 | 87 | tries = 255; |
vwochnik | 52:8f1370084268 | 88 | do { |
Cumulocity | 41:804f6a0bda26 | 89 | if (_client.send(record, "") != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 90 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 91 | Thread::wait(2000); |
Cumulocity | 41:804f6a0bda26 | 92 | continue; |
Cumulocity | 41:804f6a0bda26 | 93 | } |
Cumulocity | 41:804f6a0bda26 | 94 | |
Cumulocity | 41:804f6a0bda26 | 95 | if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 96 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 97 | Thread::wait(2000); |
Cumulocity | 41:804f6a0bda26 | 98 | continue; |
Cumulocity | 41:804f6a0bda26 | 99 | } |
Cumulocity | 42:104746744af8 | 100 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 101 | |
Cumulocity | 42:104746744af8 | 102 | for (size_t q = 0; q < recvdRecord.values(); q++) |
Cumulocity | 42:104746744af8 | 103 | puts(recvdRecord.rawValue(q)); |
Cumulocity | 42:104746744af8 | 104 | |
Cumulocity | 41:804f6a0bda26 | 105 | if ((recvdRecord.values() < 1) || |
Cumulocity | 41:804f6a0bda26 | 106 | (recvdRecord.value(0).integerValue() == 50)) { |
Cumulocity | 41:804f6a0bda26 | 107 | Thread::wait(2000); |
Cumulocity | 41:804f6a0bda26 | 108 | continue; |
Cumulocity | 41:804f6a0bda26 | 109 | } |
Cumulocity | 41:804f6a0bda26 | 110 | |
Cumulocity | 41:804f6a0bda26 | 111 | if ((recvdRecord.value(0).integerValue() != 70) || |
Cumulocity | 41:804f6a0bda26 | 112 | (recvdRecord.values() != 6)) { |
Cumulocity | 41:804f6a0bda26 | 113 | return false; |
Cumulocity | 41:804f6a0bda26 | 114 | } |
Cumulocity | 41:804f6a0bda26 | 115 | |
Cumulocity | 41:804f6a0bda26 | 116 | setCredentials(recvdRecord.value(3).characterValue(), |
Cumulocity | 41:804f6a0bda26 | 117 | recvdRecord.value(4).characterValue(), |
Cumulocity | 41:804f6a0bda26 | 118 | recvdRecord.value(5).characterValue()); |
Cumulocity | 41:804f6a0bda26 | 119 | |
vwochnik | 52:8f1370084268 | 120 | _io.lcdPrint("BOOTSTRAP SUCCESSFUL", _username, _password); |
vwochnik | 52:8f1370084268 | 121 | |
Cumulocity | 41:804f6a0bda26 | 122 | return true; |
vwochnik | 52:8f1370084268 | 123 | } while (--tries > 0); |
vwochnik | 52:8f1370084268 | 124 | |
vwochnik | 52:8f1370084268 | 125 | _io.lcdPrint("BOOTSTRAP FAILURE"); |
Cumulocity | 41:804f6a0bda26 | 126 | return false; |
Cumulocity | 41:804f6a0bda26 | 127 | } |
Cumulocity | 41:804f6a0bda26 | 128 | |
Cumulocity | 41:804f6a0bda26 | 129 | bool DeviceBootstrap::writeToStorage() |
Cumulocity | 41:804f6a0bda26 | 130 | { |
Cumulocity | 41:804f6a0bda26 | 131 | char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr; |
Cumulocity | 41:804f6a0bda26 | 132 | size_t len; |
Cumulocity | 41:804f6a0bda26 | 133 | |
Cumulocity | 41:804f6a0bda26 | 134 | ptr = buf; |
Cumulocity | 41:804f6a0bda26 | 135 | len = strlen(_username); |
Cumulocity | 41:804f6a0bda26 | 136 | strcpy(ptr, _username); |
Cumulocity | 41:804f6a0bda26 | 137 | ptr += len; |
Cumulocity | 41:804f6a0bda26 | 138 | |
Cumulocity | 41:804f6a0bda26 | 139 | *ptr++ = '\n'; |
Cumulocity | 41:804f6a0bda26 | 140 | len++; |
Cumulocity | 41:804f6a0bda26 | 141 | |
Cumulocity | 41:804f6a0bda26 | 142 | len += strlen(_password); |
Cumulocity | 41:804f6a0bda26 | 143 | strcpy(ptr, _password); |
Cumulocity | 41:804f6a0bda26 | 144 | |
Cumulocity | 41:804f6a0bda26 | 145 | _mdm.delFile(CREDENTIALS_FILE); |
Cumulocity | 41:804f6a0bda26 | 146 | _mdm.writeFile(CREDENTIALS_FILE, buf, len); |
Cumulocity | 41:804f6a0bda26 | 147 | return true; |
Cumulocity | 41:804f6a0bda26 | 148 | } |
Cumulocity | 41:804f6a0bda26 | 149 | |
Cumulocity | 41:804f6a0bda26 | 150 | void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password) |
Cumulocity | 41:804f6a0bda26 | 151 | { |
Cumulocity | 41:804f6a0bda26 | 152 | *_username = '\0'; |
Cumulocity | 41:804f6a0bda26 | 153 | if (tenant != NULL) { |
Cumulocity | 41:804f6a0bda26 | 154 | strncpy(_username, tenant, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH); |
Cumulocity | 41:804f6a0bda26 | 155 | _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0'; |
Cumulocity | 41:804f6a0bda26 | 156 | if (strlen(_username)+1 < DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH) |
Cumulocity | 41:804f6a0bda26 | 157 | strcat(_username, "/"); |
Cumulocity | 41:804f6a0bda26 | 158 | } |
Cumulocity | 41:804f6a0bda26 | 159 | strncat(_username, username, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-strlen(_username)); |
Cumulocity | 41:804f6a0bda26 | 160 | _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0'; |
Cumulocity | 41:804f6a0bda26 | 161 | |
Cumulocity | 41:804f6a0bda26 | 162 | strncpy(_password, password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH); |
Cumulocity | 41:804f6a0bda26 | 163 | _password[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0'; |
Cumulocity | 41:804f6a0bda26 | 164 | } |