Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
DeviceBootstrap.cpp
00001 #include <stdlib.h> 00002 #include <stdio.h> 00003 #include <string.h> 00004 #include "DeviceBootstrap.h" 00005 #include "Storage.h" 00006 #include "LCDDisplay.h" 00007 #include "SmartRestConf.h" 00008 #include "SmartRestSocket.h" 00009 #include "lex.h" 00010 #include "logging.h" 00011 00012 // Device bootstrap tenant, username and password 00013 #define BOOTSTRAP_TENANT "management" 00014 #define BOOTSTRAP_USERNAME "devicebootstrap" 00015 #define BOOTSTRAP_PASSWORD "Fhdt1bb1f" 00016 00017 bool DeviceBootstrap::bootstrap() 00018 { 00019 char tenant[CREDENTIAL_LENGTH]; 00020 char username[CREDENTIAL_LENGTH]; 00021 char password[CREDENTIAL_LENGTH]; 00022 if (loadCredential(tenant, username, password, CREDENTIAL_LENGTH)) { 00023 setAuth(tenant, username, password); 00024 aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr); 00025 return true; 00026 } else if (obtainFromPlatform()) { 00027 if (!saveCredential(srTenant, srUsername, srPassword, CREDENTIAL_LENGTH)) 00028 aError("Save credentials!\n"); 00029 return true; 00030 } else { 00031 return false; 00032 } 00033 } 00034 00035 bool DeviceBootstrap::obtainFromPlatform() 00036 { 00037 char buf[SMARTREST_SIZE]; 00038 char buf2[SMARTREST_BODY_SIZE]; 00039 SmartRestSocket sock; 00040 int l2 = snprintf(buf2, sizeof(buf2), "61,%s\r\n", deviceInfo.imei()); 00041 00042 // set authorization for bootstrap 00043 setAuth(BOOTSTRAP_TENANT, BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD); 00044 LCDDisplay::inst().setLines("Bootstrap", deviceInfo.imei()); 00045 for (unsigned short i = 0; i < 255; ++i) { 00046 int l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l2, buf2); 00047 l = sock.sendAndReceive(buf, l, sizeof(buf)); 00048 if (l <= 0) continue; 00049 00050 const char *p = skipHTTPHeader(buf); 00051 if (p) { 00052 Token tok; 00053 p = lex(p, tok); 00054 if (tok.len==2 && strncmp(tok.p, "70", tok.len)==0) { 00055 for (unsigned short j = 0; *p && j < 3; ++j) { 00056 p = lex(p, tok); 00057 } 00058 if (tok.type == Token::STRING) { 00059 char tenant[CREDENTIAL_LENGTH] = {0}; 00060 char username[CREDENTIAL_LENGTH] = {0}; 00061 char password[CREDENTIAL_LENGTH] = {0}; 00062 strncpy(tenant, tok.p, tok.len); 00063 p = lex(p, tok); 00064 if (tok.type == Token::STRING) 00065 strncpy(username, tok.p, tok.len); 00066 else 00067 return false; 00068 p = lex(p, tok); 00069 if (tok.type == Token::STRING) 00070 strncpy(password, tok.p, tok.len); 00071 else 00072 return false; 00073 setAuth(tenant, username, password); 00074 LCDDisplay::inst().setLines("Bootstrap Success", srTenant, srUsername); 00075 aInfo("%s/%s:%s\n", srTenant, srUsername, srPassword); 00076 return true; 00077 } else 00078 return false; 00079 } 00080 } 00081 } 00082 return false; 00083 }
Generated on Sat Jul 16 2022 07:10:59 by 1.7.2