this is using the mbed os version 5-13-1
Revision 127:a21788227ca6, committed 2019-07-15
- Comitter:
- ocomeni
- Date:
- Mon Jul 15 21:37:22 2019 +0000
- Branch:
- PassingRegression
- Parent:
- 126:9bc33f8b57d5
- Child:
- 128:3a641aaad2d9
- Child:
- 129:590bdc2dcf5b
- Commit message:
- started coding of access token acquisition:; 1. make request with credentials - DONE; 2. get response - DONE; 3. extract Id and refresh tokens from response - DONE; 4. integrate with code - pending; 5. test - pending
Changed in this revision
--- a/source/ATCmdManager.cpp Sun Jul 07 20:36:41 2019 +0000
+++ b/source/ATCmdManager.cpp Mon Jul 15 21:37:22 2019 +0000
@@ -90,13 +90,14 @@
_parser.oob("AT+UFACTORY", callback(this, &ATCmdManager::_oob_factoryReset));
_parser.oob("AT+UDELCFG=", callback(this, &ATCmdManager::_oob_deleteConfiguration));
+ _parser.oob("AT+SDBGFLG", callback(this, &ATCmdManager::_oob_set_debug_flag));
//_parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage));
//sendAtConfirmation("Testing:: +UBTLE:2\r\nOK\r\n");
dbg_printf(LOG, "\n --- ATCmdManager constructor completed ---\n");
at_resp = AT_RESP_NONE;
dataMode = AT_CMD_DATA_MODE;
- debug_flag = 0;
+ debug_flags_map = 0;
wifiStateControl = AT_RESP_NONE;
//_event_queue.call_in(10, &print_heap_and_isr_stack_info);
//print_heap_and_isr_stack_info();
@@ -451,6 +452,7 @@
}
wifiBusyTimeOut = 0;
#endif
+ bool full_debug_ena = (debug_flags_map & ATCMD_MGR_FULL_DEBUG_ENA) != 0x00;
http_request_t *http_req; // = new http_request_t;
wifi_data_msg_t data_msg;
http_req = (http_request_t *)data_msg.buffer;
@@ -472,9 +474,10 @@
{
n = sscanf(nxtPtr,"%s %s %s", s1, s2, s3);
if(n!=3) return false; // error in input abort
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nmethod = %s\nuri = %s\nhttp_ver = %s",s1, s2, s3 );
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nmethod = %s\nuri = %s\nhttp_ver = %s",s1, s2, s3 );
+ }
//http_req.method = str2HttpMethod(s1.c_str());
http_req->method = str2HttpMethod(s1);
http_req->request_URI = (char *) http_req->buffer; // point 1st string to start of buffer
@@ -483,13 +486,14 @@
http_req->http_version = http_req->request_URI + len; // point 2nd string to after 1st
len = strlen(s3)+1;
strncpy(http_req->http_version, s3, len);
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nhttp_request 1st line:\n method = %d\nuri = %s\nhttp_ver = %s",http_req->method,
- http_req->request_URI,
- http_req->http_version );
- dbg_printf(LOG, "\nhttp_request str ptrs\nuri = %X\nhttp_ver = %X",http_req->request_URI,
- http_req->http_version );
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nhttp_request 1st line:\n method = %d\nuri = %s\nhttp_ver = %s",http_req->method,
+ http_req->request_URI,
+ http_req->http_version );
+ dbg_printf(LOG, "\nhttp_request str ptrs\nuri = %X\nhttp_ver = %X",http_req->request_URI,
+ http_req->http_version );
+ }
outPtr = http_req->http_version + len; // point output buffer ptr to after 2nd string
}
else{ // scan header pairs
@@ -497,9 +501,10 @@
if(n!=2) return false; // error in input abort
p = strstr(s1,":");
*p = NULL;
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nname = %s value = %s",s1, s2);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nname = %s value = %s",s1, s2);
+ }
if(strstr(s1, "Host")!= NULL){
http_req->hostName = outPtr;
#ifdef BOX_UBLOX_DEMO_TESTING
@@ -514,54 +519,74 @@
len = strlen(s2)+1;
strncpy(outPtr, s2, len);
#endif
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+ }
outPtr += len; // point output buffer ptr to after current string
}
else if(strstr(s1, "Accept")!= NULL){
http_req->AcceptVal = outPtr;
len = strlen(s2)+1;
strncpy(outPtr, s2, len);
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+ }
outPtr += len; // point output buffer ptr to after current string
}
else if(strstr(s1, "Content-Type")!= NULL){
http_req->contentType = outPtr;
len = strlen(s2)+1;
strncpy(outPtr, s2, len);
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+ }
outPtr += len; // point output buffer ptr to after current string
}
else if(strstr(s1, "Content-Length")!= NULL){
http_req->contentLen = outPtr;
len = strlen(s2)+1;
strncpy(outPtr, s2, len);
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+ }
+ outPtr += len; // point output buffer ptr to after current string
+ }
+ else if(strstr(s1, "X-Amz-Target")!= NULL){
+ http_req->X_Amz_Target = outPtr;
+ len = strlen(s2)+1;
+ strncpy(outPtr, s2, len);
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+ }
outPtr += len; // point output buffer ptr to after current string
}
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\noutPtr = %X len = %d\n", outPtr, len);
-#endif
+
+
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\noutPtr = %X len = %d\n", outPtr, len);
+ }
}
nxtPtr = strstr(nxtPtr, "\r\n")+2; // goto next line
if(nxtPtr >= p2) break;
}
// print header from http_req_struct
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nhttp request header: \n %s\n", http_req->buffer);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nhttp request header: \n %s\n", http_req->buffer);
+ }
int bodyLen = edm_hdr.payloadLen -(p2+7-strPtr);
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nLen = %d\n", bodyLen);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nLen = %d\n", bodyLen);
+ }
+
uint32_t msecsSinceLastMsg = Kernel::get_ms_count() - lastHttpRespTime;
if((lastCloudMsgType == BAR_DB_MSGTYPE || lastCloudMsgType == REC_DB_MSGTYPE) &&
lastCloudMsgType == (uint8_t) bodyPtr[8] &&
@@ -580,10 +605,11 @@
// package and send on wifi data queue
data_msg.wifi_cmd = WIFI_CMD_SEND_HTTPS_REQ;
data_msg.dataLen = (uint8_t *)outPtr - http_req->buffer;//sizeof(http_request_t);
-#ifdef FULL_DEBUG_ENABLED
- dbg_printf(LOG, "\nsizeof(http_req) on population = %d\n", sizeof(*http_req));
- dbg_printf(LOG, "\ndata_msg.dataLen = %d\n", data_msg.dataLen);
-#endif
+ if(full_debug_ena)
+ {
+ dbg_printf(LOG, "\nsizeof(http_req) on population = %d\n", sizeof(*http_req));
+ dbg_printf(LOG, "\ndata_msg.dataLen = %d\n", data_msg.dataLen);
+ }
// queue next data request
if(queueWiFiDataRequest(data_msg) == true)
{
@@ -844,6 +870,17 @@
#endif
}
+
+void ATCmdManager::_oob_set_debug_flag()
+{
+ int rdFlag;
+ if(_parser.scanf("=%3d", &rdFlag) >0)
+ {
+ debug_flags_map = rdFlag;
+ dbg_printf(LOG, "\n Changing debug_flags_map to %d %d\n", rdFlag, debug_flags_map);
+ sendAtConfirmation(OK_RESP);
+ }
+}
#define TTP_DEBUGGING
#define TTP_DEBUGGING_UART_NOCHANGE
void ATCmdManager::_oob_uart_setup(){
@@ -1329,8 +1366,6 @@
wifiCmd->wifi_cmd = cmd;
_aT2WiFiCmdQueue->put(wifiCmd);
dbg_printf(LOG, "[ATCMD MAN] queued wifi_cmd = %d\n", wifiCmd->wifi_cmd);
- if((int)cmd == 5)
- debug_flag = 3;
return true;
}
--- a/source/ATCmdManager.h Sun Jul 07 20:36:41 2019 +0000
+++ b/source/ATCmdManager.h Mon Jul 15 21:37:22 2019 +0000
@@ -93,7 +93,7 @@
edm_header_t edm_hdr;
uint8_t *rx_buf_ptr;
startup_config_t *startup_config;
- int debug_flag;
+ int debug_flags_map;
at_cmd_resp_t wifiStateControl;
#ifdef BOX_UBLOX_DEMO_TESTING
bool check_for_at_cmd;
@@ -114,6 +114,7 @@
void _oob_echo_on();
void _oob_debug_logs_on();
void _oob_debug_logs_off();
+ void _oob_set_debug_flag();
void _oob_data_mode();
void _oob_get_mac_addr();
void _oob_get_ble_role();
--- a/source/WiFiManager.cpp Sun Jul 07 20:36:41 2019 +0000
+++ b/source/WiFiManager.cpp Mon Jul 15 21:37:22 2019 +0000
@@ -51,8 +51,8 @@
//_event_queue.call_every(10000, this, &WiFiManager::callWifiWatchDog);
//keep_alive_id = _event_queue.call_every(CLOUD_KEEP_ALIVE_INTERVAL, this, &WiFiManager::callInternetKeepAlive);
keep_alive_id = 0;
+ https_request_succeeded = false;
//watchDogTick.attach(callback(this, &WiFiManager::callWifiWatchDogIsr), 10.0); // call flip function every 10 seconds
-
}
WiFiManager::~WiFiManager()
@@ -61,6 +61,8 @@
wifiWatchdogTimer.stop();
socket->close();
delete socket;
+ free(aws_id_token);
+ free(aws_refresh_token);
}
//#define DISABLE_WATCHDOG
@@ -118,6 +120,7 @@
// Send data
nsapi_size_or_error_t error;
//serr = socket->send("GET /nudgebox/v1 HTTP/1.0\r\nHost: https://dev2.dnanudge.io\r\n\r\n", 18);
+ dbg_printf(LOG, "\n[WIFI MAN] KEEP ALIVE SERVER REQUEST\r\n");
error = socket->send(HELLO_MSG, sizeof(HELLO_MSG));
if(error < 0)
@@ -133,7 +136,7 @@
// Receive data
char buf[500];
error = socket->recv(buf, 500);
- if(error >= 0)
+ if(error >= 0 && strstr(buf, "HTTP/1.1 200")!= NULL) // received HTTP 200 OK status
{
dbg_printf(LOG, "\n[WIFI MAN] KEEP ALIVE SERVER RESPONSE: \r\n %s\r\n", buf);
queueATresponse(AT_SOCKET_KEEP_ALIVE_OK);
@@ -146,6 +149,70 @@
}
}
+void WiFiManager::CreateTokenHttpsRequest(char * httpsReq)
+{
+
+ sprintf(httpsReq, "%s\r\n\r\n{\r\n\"AuthParameters\" : {\r\n"
+ "\"USERNAME\" : \"%s\",\r\n\"PASSWORD\" : \"%s\"\r\n},\r\n"
+ "\"AuthFlow\" : \"%s\",\r\n"
+ "\"ClientId\" : \"%s\"\r\n}", TOKEN_REQ_HDR, UserName, Password, AuthFlow, ClientId);
+ dbg_printf(LOG, "\n[WIFI MAN] Cloud Access Token REQUEST:\r\n%s\r\n", httpsReq);
+}
+
+void WiFiManager::GetCloudAccessToken()
+{
+ // Send data
+ nsapi_size_or_error_t error;
+ dbg_printf(LOG, "\n[WIFI MAN] Get Cloud Access Token REQUEST\r\n");
+ char *httpsReq = new char[500];
+ httpsReq[0] = NULL;
+ CreateTokenHttpsRequest(httpsReq);
+ if(strlen(httpsReq) == 0)
+ {
+ queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
+ return;
+ }
+ error = socket->send(httpsReq, strlen(httpsReq)+1);
+
+ if(error < 0)
+ {
+ queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
+ return;
+ }
+
+ // Receive data
+ // reserve large buffer from heap to hold response
+ char *respBuf = new char[TOKEN_RESPONSE_SIZE];
+ error = socket->recv(respBuf, TOKEN_RESPONSE_SIZE);
+ if(error >= 0 && strstr(respBuf, "HTTP/1.1 200")!= NULL) // received HTTP 200 OK status
+ {
+ dbg_printf(LOG, "\n[WIFI MAN] ACCESS TOKEN RESPONSE: \r\n %s\r\n", respBuf);
+ // reserve memomry on the heap for tokens
+ aws_id_token = new char[ID_TOKEN_SIZE];
+ aws_refresh_token = new char[REFRESH_TOKEN_SIZE];
+ // extract ID TOKEN
+ char *sp1 = strstr(respBuf,ID_TOKEN_STR_START);
+ char *sp2 = strstr(respBuf,ACCESS_TOKEN_STR_END);
+ int tokenLen = sp2-sp1;
+ strncpy(aws_id_token, sp1, tokenLen);
+ dbg_printf(LOG, "\n[WIFI MAN] IdToken: \r\n %s\r\n", aws_id_token);
+ // extract Refresh TOKEN
+ sp1 = strstr(respBuf,REFRESH_TOKEN_STR_START);
+ sp2 = strstr(respBuf,ACCESS_TOKEN_STR_END);
+ tokenLen = sp2-sp1;
+ strncpy(aws_refresh_token, sp1, tokenLen);
+ dbg_printf(LOG, "\n[WIFI MAN] RefreshToken: \r\n %s\r\n", aws_refresh_token);
+ queueATresponse(AT_SOCKET_KEEP_ALIVE_OK);
+ https_connection_active = true;
+ }
+ else
+ {
+ queueATresponse(AT_SOCKET_KEEP_ALIVE_FAILED);
+ https_connection_active = false;
+ }
+ delete respBuf;
+}
+
void WiFiManager::sendThreadATresponseString(const char * buf, at_cmd_resp_t at_cmd)
{
@@ -446,6 +513,11 @@
}
sendATresponseString(AT_COMMAND_FAILED);
}
+ if(https_request_succeeded == false)
+ {
+ https_request_succeeded = result;
+ }
+
dbg_printf(LOG, "after call to send http request \n");
print_memory_info();
wifiCmd = WIFI_CMD_NONE;
--- a/source/WiFiManager.h Sun Jul 07 20:36:41 2019 +0000
+++ b/source/WiFiManager.h Mon Jul 15 21:37:22 2019 +0000
@@ -16,6 +16,21 @@
#include "https_certificates.h"
#include "common_types.h"
//const char HELLO_MSG[] =
+#define TOKEN_RESPONSE_SIZE 5*1024
+#define ID_TOKEN_SIZE 1024
+#define REFRESH_TOKEN_SIZE 2*1024
+#define ID_TOKEN_STR_START "\"IdToken\":\""
+#define REFRESH_TOKEN_STR_START "\"RefreshToken\":\""
+#define ACCESS_TOKEN_STR_END "\",\""
+
+const char TOKEN_REQ_HDR[] = "POST / HTTP1.1\r\n"
+ "Host: cognito-idp.eu-west-2.amazonaws.com\r\n"
+ "X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth\r\n"
+ "Content-Type: application/x-amz-json-1.1";
+const char UserName[] = "box1";
+const char Password[] = "DnaN2dg3B4x!";
+const char AuthFlow[] = "USER_PASSWORD_AUTH";
+const char ClientId[] = "3el1h42i52p0e1gkb0l86ougdv";
const uint8_t HELLO_MSG[] = {0x50,0x4f
,0x53,0x54,0x20,0x2f,0x6e,0x75,0x64,0x67
@@ -106,6 +121,9 @@
http_result_t http_result;
bool use_full_hostname;
int keep_alive_id;
+ bool https_request_succeeded;
+ char * aws_id_token;
+ char * aws_refresh_token;
#ifdef DNANUDGE_DEBUG
rtos::Semaphore callback_semaphore;
@@ -163,6 +181,8 @@
bool outputBuffersAvailable();
void callInternetKeepAlive();
void keepSocketAlive();
+ void CreateTokenHttpsRequest(char * httpsReq);
+ void GetCloudAccessToken();
--- a/source/common_config.h Sun Jul 07 20:36:41 2019 +0000 +++ b/source/common_config.h Mon Jul 15 21:37:22 2019 +0000 @@ -57,6 +57,9 @@ #define WIFI_IF_ID 2 #define LOCAL_ADDRESS_RESP "+UMLA:" #define BLE_DEVICE_NAME_RESP "+UBTLN:" +#define ATCMD_MGR_FULL_DEBUG_ENA 0x80 +#define WIFI_MGR_FULL_DEBUG_ENA 0x8000 +#define BTLE_MGR_FULL_DEBUG_ENA 0x800000 //#define ENABLE_MEMORY_CHECKS //#define SEND_DEBUG_MESSAGES
--- a/source/common_types.h Sun Jul 07 20:36:41 2019 +0000
+++ b/source/common_types.h Mon Jul 15 21:37:22 2019 +0000
@@ -228,6 +228,7 @@
char * AcceptVal; /* types accepted */
char * contentType; /* content type */
char * contentLen; /* content length */
+ char * X_Amz_Target; /* X-Amz-Target */
uint8_t * body; /* body */
uint8_t buffer[TX_BUFFER_LEN - 8*sizeof(char *)
- sizeof(http_method)]; // buffer