portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
Cumulocity
Date:
Thu Jul 24 14:38:48 2014 +0000
Revision:
43:eff77697d88c
Parent:
42:104746744af8
Child:
46:f6976fd64387
fix bootstrap

Who changed what in which revision?

UserRevisionLine numberNew 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
Cumulocity 41:804f6a0bda26 11 DeviceBootstrap::DeviceBootstrap(SmartRest& client, MDMSerial& mdm, DeviceInfo& deviceInfo) :
Cumulocity 41:804f6a0bda26 12 _client(client),
Cumulocity 41:804f6a0bda26 13 _mdm(mdm),
Cumulocity 41:804f6a0bda26 14 _deviceInfo(deviceInfo)
Cumulocity 41:804f6a0bda26 15 {
Cumulocity 41:804f6a0bda26 16 *_username = *_password = '\0';
Cumulocity 41:804f6a0bda26 17 }
Cumulocity 41:804f6a0bda26 18
Cumulocity 41:804f6a0bda26 19 bool DeviceBootstrap::setUpCredentials()
Cumulocity 41:804f6a0bda26 20 {
Cumulocity 41:804f6a0bda26 21 if (((*_username == '\0') || (*_password == '\0')) &&
Cumulocity 41:804f6a0bda26 22 (!obtainFromStorage())) {
Cumulocity 41:804f6a0bda26 23 if (!obtainFromPlatform())
Cumulocity 41:804f6a0bda26 24 return false;
Cumulocity 41:804f6a0bda26 25 if (!writeToStorage())
Cumulocity 41:804f6a0bda26 26 puts("Warning: Could not write credentials to file!");
Cumulocity 41:804f6a0bda26 27 }
Cumulocity 41:804f6a0bda26 28
Cumulocity 41:804f6a0bda26 29 printf("Credentials: %s : %s\n", _username, _password);
Cumulocity 41:804f6a0bda26 30 if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS)
Cumulocity 41:804f6a0bda26 31 return false;
Cumulocity 41:804f6a0bda26 32 return true;
Cumulocity 41:804f6a0bda26 33 }
Cumulocity 41:804f6a0bda26 34
Cumulocity 41:804f6a0bda26 35 bool DeviceBootstrap::obtainFromStorage()
Cumulocity 41:804f6a0bda26 36 {
Cumulocity 41:804f6a0bda26 37 char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr;
Cumulocity 42:104746744af8 38
Cumulocity 41:804f6a0bda26 39 int res = _mdm.readFile(CREDENTIALS_FILE, buf, sizeof(buf));
Cumulocity 41:804f6a0bda26 40
Cumulocity 41:804f6a0bda26 41 if (res < 0)
Cumulocity 41:804f6a0bda26 42 return false;
Cumulocity 41:804f6a0bda26 43
Cumulocity 41:804f6a0bda26 44 buf[res] = '\0';
Cumulocity 41:804f6a0bda26 45 if ((ptr = strchr(buf, '\n')) == NULL)
Cumulocity 41:804f6a0bda26 46 return false;
Cumulocity 41:804f6a0bda26 47 *ptr = '\0';
Cumulocity 41:804f6a0bda26 48
Cumulocity 41:804f6a0bda26 49 ptr = buf;
Cumulocity 41:804f6a0bda26 50 strcpy(_username, ptr);
Cumulocity 41:804f6a0bda26 51 ptr += strlen(ptr)+1;
Cumulocity 41:804f6a0bda26 52 strcpy(_password, ptr);
Cumulocity 41:804f6a0bda26 53 return true;
Cumulocity 41:804f6a0bda26 54 }
Cumulocity 41:804f6a0bda26 55
Cumulocity 41:804f6a0bda26 56 bool DeviceBootstrap::obtainFromPlatform()
Cumulocity 41:804f6a0bda26 57 {
Cumulocity 41:804f6a0bda26 58 uint8_t ret;
Cumulocity 42:104746744af8 59 const char *id;
Cumulocity 42:104746744af8 60
Cumulocity 43:eff77697d88c 61 id = _deviceInfo.imei();
Cumulocity 42:104746744af8 62 printf("Starting device bootstrap with '%s'\n", id);
Cumulocity 41:804f6a0bda26 63
Cumulocity 41:804f6a0bda26 64 ComposedRecord record;
Cumulocity 41:804f6a0bda26 65 ParsedRecord recvdRecord;
Cumulocity 41:804f6a0bda26 66
Cumulocity 41:804f6a0bda26 67 IntegerValue msgId(61);
Cumulocity 42:104746744af8 68 CharValue identifier(id);
Cumulocity 41:804f6a0bda26 69 if ((!record.add(msgId)) || (!record.add(identifier)))
Cumulocity 41:804f6a0bda26 70 return false;
Cumulocity 41:804f6a0bda26 71
Cumulocity 41:804f6a0bda26 72 // set authorization for bootstrap
Cumulocity 41:804f6a0bda26 73 if (_client.setAuthorization(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD) != SMARTREST_SUCCESS)
Cumulocity 41:804f6a0bda26 74 return false;
Cumulocity 41:804f6a0bda26 75
Cumulocity 41:804f6a0bda26 76 while (true) {
Cumulocity 41:804f6a0bda26 77 if (_client.send(record, "") != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 78 puts("Connection unsuccessful. Retrying.");
Cumulocity 41:804f6a0bda26 79 _client.stop();
Cumulocity 41:804f6a0bda26 80 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 81 continue;
Cumulocity 41:804f6a0bda26 82 }
Cumulocity 41:804f6a0bda26 83
Cumulocity 41:804f6a0bda26 84 if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 85 puts("Receiving failure.");
Cumulocity 41:804f6a0bda26 86 _client.stop();
Cumulocity 41:804f6a0bda26 87 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 88 continue;
Cumulocity 41:804f6a0bda26 89 }
Cumulocity 42:104746744af8 90 _client.stop();
Cumulocity 41:804f6a0bda26 91
Cumulocity 42:104746744af8 92 for (size_t q = 0; q < recvdRecord.values(); q++)
Cumulocity 42:104746744af8 93 puts(recvdRecord.rawValue(q));
Cumulocity 42:104746744af8 94
Cumulocity 41:804f6a0bda26 95 if ((recvdRecord.values() < 1) ||
Cumulocity 41:804f6a0bda26 96 (recvdRecord.value(0).integerValue() == 50)) {
Cumulocity 41:804f6a0bda26 97 puts("No credentials available yet. Retrying.");
Cumulocity 41:804f6a0bda26 98 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 99 continue;
Cumulocity 41:804f6a0bda26 100 }
Cumulocity 41:804f6a0bda26 101
Cumulocity 41:804f6a0bda26 102 if ((recvdRecord.value(0).integerValue() != 70) ||
Cumulocity 41:804f6a0bda26 103 (recvdRecord.values() != 6)) {
Cumulocity 41:804f6a0bda26 104 puts("Bad credentials received.");
Cumulocity 41:804f6a0bda26 105 return false;
Cumulocity 41:804f6a0bda26 106 }
Cumulocity 41:804f6a0bda26 107
Cumulocity 41:804f6a0bda26 108 setCredentials(recvdRecord.value(3).characterValue(),
Cumulocity 41:804f6a0bda26 109 recvdRecord.value(4).characterValue(),
Cumulocity 41:804f6a0bda26 110 recvdRecord.value(5).characterValue());
Cumulocity 41:804f6a0bda26 111
Cumulocity 41:804f6a0bda26 112 return true;
Cumulocity 41:804f6a0bda26 113 }
Cumulocity 41:804f6a0bda26 114 return false;
Cumulocity 41:804f6a0bda26 115 }
Cumulocity 41:804f6a0bda26 116
Cumulocity 41:804f6a0bda26 117 bool DeviceBootstrap::writeToStorage()
Cumulocity 41:804f6a0bda26 118 {
Cumulocity 41:804f6a0bda26 119 char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr;
Cumulocity 41:804f6a0bda26 120 size_t len;
Cumulocity 41:804f6a0bda26 121
Cumulocity 41:804f6a0bda26 122 ptr = buf;
Cumulocity 41:804f6a0bda26 123 len = strlen(_username);
Cumulocity 41:804f6a0bda26 124 strcpy(ptr, _username);
Cumulocity 41:804f6a0bda26 125 ptr += len;
Cumulocity 41:804f6a0bda26 126
Cumulocity 41:804f6a0bda26 127 *ptr++ = '\n';
Cumulocity 41:804f6a0bda26 128 len++;
Cumulocity 41:804f6a0bda26 129
Cumulocity 41:804f6a0bda26 130 len += strlen(_password);
Cumulocity 41:804f6a0bda26 131 strcpy(ptr, _password);
Cumulocity 41:804f6a0bda26 132
Cumulocity 41:804f6a0bda26 133 _mdm.delFile(CREDENTIALS_FILE);
Cumulocity 41:804f6a0bda26 134 _mdm.writeFile(CREDENTIALS_FILE, buf, len);
Cumulocity 41:804f6a0bda26 135 return true;
Cumulocity 41:804f6a0bda26 136 }
Cumulocity 41:804f6a0bda26 137
Cumulocity 41:804f6a0bda26 138 void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
Cumulocity 41:804f6a0bda26 139 {
Cumulocity 41:804f6a0bda26 140 *_username = '\0';
Cumulocity 41:804f6a0bda26 141 if (tenant != NULL) {
Cumulocity 41:804f6a0bda26 142 strncpy(_username, tenant, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
Cumulocity 41:804f6a0bda26 143 _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 144 if (strlen(_username)+1 < DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH)
Cumulocity 41:804f6a0bda26 145 strcat(_username, "/");
Cumulocity 41:804f6a0bda26 146 }
Cumulocity 41:804f6a0bda26 147 strncat(_username, username, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-strlen(_username));
Cumulocity 41:804f6a0bda26 148 _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 149
Cumulocity 41:804f6a0bda26 150 strncpy(_password, password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
Cumulocity 41:804f6a0bda26 151 _password[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
Cumulocity 41:804f6a0bda26 152 }