Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Aug 08 11:05:57 2016 +0000
Revision:
138:f8ab852e83e7
Parent:
116:5de54f09f754
Etisalat and Teleena APN.

Who changed what in which revision?

UserRevisionLine numberNew 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"
xinlei 104:fd3571349e5d 5 #include "Storage.h"
xinlei 95:5dfdc8568e9f 6 #include "LCDDisplay.h"
xinlei 92:0acd11870c6a 7 #include "SmartRestConf.h"
xinlei 116:5de54f09f754 8 #include "SmartRestSocket.h"
xinlei 116:5de54f09f754 9 #include "lex.h"
xinlei 72:c5709ae7b193 10 #include "logging.h"
Cumulocity 41:804f6a0bda26 11
xinlei 112:3872569be2af 12 // Device bootstrap tenant, username and password
xinlei 112:3872569be2af 13 #define BOOTSTRAP_TENANT "management"
xinlei 112:3872569be2af 14 #define BOOTSTRAP_USERNAME "devicebootstrap"
xinlei 100:dbcd3bc51758 15 #define BOOTSTRAP_PASSWORD "Fhdt1bb1f"
xinlei 100:dbcd3bc51758 16
xinlei 112:3872569be2af 17 bool DeviceBootstrap::bootstrap()
Cumulocity 41:804f6a0bda26 18 {
xinlei 112:3872569be2af 19 char tenant[CREDENTIAL_LENGTH];
xinlei 112:3872569be2af 20 char username[CREDENTIAL_LENGTH];
xinlei 112:3872569be2af 21 char password[CREDENTIAL_LENGTH];
xinlei 112:3872569be2af 22 if (loadCredential(tenant, username, password, CREDENTIAL_LENGTH)) {
xinlei 112:3872569be2af 23 setAuth(tenant, username, password);
xinlei 112:3872569be2af 24 aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr);
xinlei 100:dbcd3bc51758 25 return true;
xinlei 100:dbcd3bc51758 26 } else if (obtainFromPlatform()) {
xinlei 112:3872569be2af 27 if (!saveCredential(srTenant, srUsername, srPassword, CREDENTIAL_LENGTH))
xinlei 112:3872569be2af 28 aError("Save credentials!\n");
xinlei 100:dbcd3bc51758 29 return true;
xinlei 100:dbcd3bc51758 30 } else {
xinlei 100:dbcd3bc51758 31 return false;
Cumulocity 41:804f6a0bda26 32 }
Cumulocity 41:804f6a0bda26 33 }
Cumulocity 41:804f6a0bda26 34
Cumulocity 41:804f6a0bda26 35 bool DeviceBootstrap::obtainFromPlatform()
xinlei 100:dbcd3bc51758 36 {
xinlei 116:5de54f09f754 37 char buf[SMARTREST_SIZE];
xinlei 116:5de54f09f754 38 char buf2[SMARTREST_BODY_SIZE];
xinlei 116:5de54f09f754 39 SmartRestSocket sock;
xinlei 116:5de54f09f754 40 int l2 = snprintf(buf2, sizeof(buf2), "61,%s\r\n", deviceInfo.imei());
Cumulocity 41:804f6a0bda26 41
xinlei 116:5de54f09f754 42 // set authorization for bootstrap
xinlei 116:5de54f09f754 43 setAuth(BOOTSTRAP_TENANT, BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
xinlei 116:5de54f09f754 44 LCDDisplay::inst().setLines("Bootstrap", deviceInfo.imei());
xinlei 116:5de54f09f754 45 for (unsigned short i = 0; i < 255; ++i) {
xinlei 116:5de54f09f754 46 int l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l2, buf2);
xinlei 116:5de54f09f754 47 l = sock.sendAndReceive(buf, l, sizeof(buf));
xinlei 116:5de54f09f754 48 if (l <= 0) continue;
xinlei 92:0acd11870c6a 49
xinlei 116:5de54f09f754 50 const char *p = skipHTTPHeader(buf);
xinlei 116:5de54f09f754 51 if (p) {
xinlei 116:5de54f09f754 52 Token tok;
xinlei 116:5de54f09f754 53 p = lex(p, tok);
xinlei 116:5de54f09f754 54 if (tok.len==2 && strncmp(tok.p, "70", tok.len)==0) {
xinlei 116:5de54f09f754 55 for (unsigned short j = 0; *p && j < 3; ++j) {
xinlei 116:5de54f09f754 56 p = lex(p, tok);
xinlei 116:5de54f09f754 57 }
xinlei 116:5de54f09f754 58 if (tok.type == Token::STRING) {
xinlei 116:5de54f09f754 59 char tenant[CREDENTIAL_LENGTH] = {0};
xinlei 116:5de54f09f754 60 char username[CREDENTIAL_LENGTH] = {0};
xinlei 116:5de54f09f754 61 char password[CREDENTIAL_LENGTH] = {0};
xinlei 116:5de54f09f754 62 strncpy(tenant, tok.p, tok.len);
xinlei 116:5de54f09f754 63 p = lex(p, tok);
xinlei 116:5de54f09f754 64 if (tok.type == Token::STRING)
xinlei 116:5de54f09f754 65 strncpy(username, tok.p, tok.len);
xinlei 116:5de54f09f754 66 else
xinlei 116:5de54f09f754 67 return false;
xinlei 116:5de54f09f754 68 p = lex(p, tok);
xinlei 116:5de54f09f754 69 if (tok.type == Token::STRING)
xinlei 116:5de54f09f754 70 strncpy(password, tok.p, tok.len);
xinlei 116:5de54f09f754 71 else
xinlei 116:5de54f09f754 72 return false;
xinlei 116:5de54f09f754 73 setAuth(tenant, username, password);
xinlei 116:5de54f09f754 74 LCDDisplay::inst().setLines("Bootstrap Success", srTenant, srUsername);
xinlei 116:5de54f09f754 75 aInfo("%s/%s:%s\n", srTenant, srUsername, srPassword);
xinlei 116:5de54f09f754 76 return true;
xinlei 116:5de54f09f754 77 } else
xinlei 116:5de54f09f754 78 return false;
xinlei 116:5de54f09f754 79 }
xinlei 116:5de54f09f754 80 }
Cumulocity 41:804f6a0bda26 81 }
xinlei 116:5de54f09f754 82 return false;
xinlei 112:3872569be2af 83 }