portable version of the cumulocity demo
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
DeviceBootstrap.cpp
00001 #include "DeviceBootstrap.h" 00002 #include <stdlib.h> 00003 #include <stdio.h> 00004 #include <string.h> 00005 #include "rtos.h" 00006 #include "ComposedRecord.h" 00007 #include "CharValue.h" 00008 #include "IntegerValue.h" 00009 #include "ParsedRecord.h" 00010 00011 DeviceBootstrap::DeviceBootstrap(SmartRest& client, MDMSerial& mdm, DeviceIO& io, DeviceInfo& deviceInfo) : 00012 _client(client), 00013 _mdm(mdm), 00014 _io(io), 00015 _deviceInfo(deviceInfo) 00016 { 00017 *_username = *_password = '\0'; 00018 } 00019 00020 bool DeviceBootstrap::setUpCredentials() 00021 { 00022 if (((*_username == '\0') || (*_password == '\0')) && 00023 (!obtainFromStorage())) { 00024 if (!obtainFromPlatform()) 00025 return false; 00026 if (!writeToStorage()) 00027 puts("Warning: Could not write credentials to file!"); 00028 } 00029 00030 if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS) 00031 return false; 00032 return true; 00033 } 00034 00035 const char * DeviceBootstrap::username() 00036 { 00037 return _username; 00038 } 00039 00040 const char * DeviceBootstrap::password() 00041 { 00042 return _password; 00043 } 00044 00045 bool DeviceBootstrap::obtainFromStorage() 00046 { 00047 char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr; 00048 00049 int res = _mdm.readFile(CREDENTIALS_FILE, buf, sizeof(buf)); 00050 if (res < 0) 00051 return false; 00052 00053 buf[res] = '\0'; 00054 if ((ptr = strchr(buf, '\n')) == NULL) 00055 return false; 00056 *ptr = '\0'; 00057 00058 ptr = buf; 00059 strcpy(_username, ptr); 00060 ptr += strlen(ptr)+1; 00061 strcpy(_password, ptr); 00062 return true; 00063 } 00064 00065 bool DeviceBootstrap::obtainFromPlatform() 00066 { 00067 uint8_t ret; 00068 uint8_t tries; 00069 00070 ComposedRecord record; 00071 ParsedRecord recvdRecord; 00072 00073 IntegerValue msgId(61); 00074 CharValue identifier(_deviceInfo.imei()); 00075 if ((!record.add(msgId)) || (!record.add(identifier))) 00076 return false; 00077 00078 // set authorization for bootstrap 00079 if (_client.setAuthorization(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD) != SMARTREST_SUCCESS) 00080 return false; 00081 00082 _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei()); 00083 00084 tries = 255; 00085 do { 00086 if (_client.send(record, "") != SMARTREST_SUCCESS) { 00087 _client.stop(); 00088 Thread::wait(2000); 00089 continue; 00090 } 00091 00092 if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) { 00093 _client.stop(); 00094 Thread::wait(2000); 00095 continue; 00096 } 00097 _client.stop(); 00098 00099 for (size_t q = 0; q < recvdRecord.values(); q++) 00100 puts(recvdRecord.rawValue(q)); 00101 00102 if ((recvdRecord.values() < 1) || 00103 (recvdRecord.value(0).integerValue() == 50)) { 00104 Thread::wait(2000); 00105 continue; 00106 } 00107 00108 if ((recvdRecord.value(0).integerValue() != 70) || 00109 (recvdRecord.values() != 6)) { 00110 return false; 00111 } 00112 00113 setCredentials(recvdRecord.value(3).characterValue(), 00114 recvdRecord.value(4).characterValue(), 00115 recvdRecord.value(5).characterValue()); 00116 00117 _io.lcdPrint("BOOTSTRAP SUCCESSFUL", _username, _password); 00118 00119 return true; 00120 } while (--tries > 0); 00121 00122 _io.lcdPrint("BOOTSTRAP FAILURE"); 00123 return false; 00124 } 00125 00126 bool DeviceBootstrap::writeToStorage() 00127 { 00128 char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr; 00129 size_t len; 00130 00131 ptr = buf; 00132 len = strlen(_username); 00133 strcpy(ptr, _username); 00134 ptr += len; 00135 00136 *ptr++ = '\n'; 00137 len++; 00138 00139 len += strlen(_password); 00140 strcpy(ptr, _password); 00141 00142 _mdm.delFile(CREDENTIALS_FILE); 00143 _mdm.writeFile(CREDENTIALS_FILE, buf, len); 00144 return true; 00145 } 00146 00147 void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password) 00148 { 00149 *_username = '\0'; 00150 if (tenant != NULL) { 00151 strncpy(_username, tenant, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH); 00152 _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0'; 00153 if (strlen(_username)+1 < DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH) 00154 strcat(_username, "/"); 00155 } 00156 strncat(_username, username, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-strlen(_username)); 00157 _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0'; 00158 00159 strncpy(_password, password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH); 00160 _password[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0'; 00161 }
Generated on Tue Jul 12 2022 21:10:53 by 1.7.2