this is using the mbed os version 5-13-1
Diff: source/WiFiManager.cpp
- Revision:
- 98:65c2333a38b6
- Parent:
- 96:f5ed273881af
- Child:
- 99:05398b3184f8
--- a/source/WiFiManager.cpp Fri Apr 19 10:26:58 2019 +0000
+++ b/source/WiFiManager.cpp Sun Apr 21 13:06:41 2019 +0000
@@ -3,6 +3,7 @@
WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi,
+ events::EventQueue &event_queue,
MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool,
Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue,
MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool,
@@ -14,6 +15,7 @@
:
wifi_config(wifi_config),
network(wifi),
+ _event_queue(event_queue),
_aT2WiFimPool(aT2WiFimPool),
_aT2WiFiCmdQueue(aT2WiFiCmdQueue),
@@ -68,6 +70,7 @@
void WiFiManager::runMain(){
nsapi_error_t error;
+ bool httpReqResult;
printf("\r\n [WIFI MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
while(true){
dequeueWiFiCommands();
@@ -82,12 +85,15 @@
queueATresponse(AT_SCAN_RESP);
break;
case WIFI_CMD_DETAILED_SCAN:
+ {
nsapi_size_or_error_t cnt_err;
cnt_err = getAvailableAPs(lastScanCount);
wifiCmd = WIFI_CMD_NONE;
queueATresponse(AT_DETAILED_SCAN_RESP);
break;
+ }
case WIFI_CMD_CONNECT:
+ {
error = connect();
int secCount = 0;
while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS || is_connected==false){
@@ -105,6 +111,7 @@
sendATresponseString(AT_CONNECT_RESP);
}
break;
+ }
case WIFI_CMD_DISCONNECT:
error = disconnect();
wifiCmd = WIFI_CMD_NONE;
@@ -133,7 +140,11 @@
case WIFI_CMD_SEND_HTTPS_REQ:
printf("before call to send http request \n");
print_memory_info();
- createHttpsRequest();
+ httpReqResult = createHttpsRequest();
+ if(httpReqResult == false)
+ {
+ sendATresponseString(AT_COMMAND_FAILED);
+ }
printf("after call to send http request \n");
print_memory_info();
wifiCmd = WIFI_CMD_NONE;
@@ -258,7 +269,7 @@
//_smutex.lock();
printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
//_smutex.unlock();
- return;
+ return 0;
}
/* Limit number of network arbitrary to 15 */
count = count < 15 ? count : 15;
@@ -266,7 +277,7 @@
count = network->scan(ap, count);
if (count <= 0) {
printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
- return;
+ return 0;
}
for (int i = 0; i < count; i++) {
@@ -459,6 +470,12 @@
void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
{
+ printf("[WIFI-MAN] about call callback... \r\n");
+ _event_queue.call_in(50, this, &WiFiManager::status_callback_event, status, param);
+ //status_callback_event(status, param);
+}
+void WiFiManager::status_callback_event(nsapi_event_t status, intptr_t param)
+{
//if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
//}
switch(param) {
@@ -526,6 +543,7 @@
error = network->connect(wifi_config.ssid,
wifi_config.pass,
wifi_config.security);
+ printf("[WIFI-MAN] network->connect called. error = %d\r\n", error);
return error;
}
@@ -537,14 +555,15 @@
return error;
}
+#define MIX_HDR_AND_BODY
void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen)
{
- printf("before call to new at_data_msg_t \n");
- print_memory_info();
+ //printf("before call to new at_data_msg_t \n");
+ //print_memory_info();
at_data_resp = new at_data_msg_t;
- printf("after call to new at_data_msg_t \n");
- print_memory_info();
+ //printf("after call to new at_data_msg_t \n");
+ //print_memory_info();
at_data_resp->at_resp = at_cmd;
size_t bufSize = sizeof(at_data_resp->buffer);
int pos = 0;
@@ -559,11 +578,16 @@
wait_ms(10);
}
else {
- if(http_response_hdr_sent == false){
- copyResponseHdr2Queue();
- printf("[WIFI-MAN] Http Response header copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
- hdrLen = at_data_resp->dataLen;
- http_response_hdr_sent = true;
+ if(http_response_hdr_sent == false && chunkNum==1){ // only do this for first chunk
+ bool status = copyResponseHdr2Queue();
+ if(status == true){
+ printf("[WIFI-MAN] Http Response header copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
+ hdrLen = at_data_resp->dataLen;
+ http_response_hdr_sent = true;
+ }
+ else {
+ printf("[WIFI-MAN] Http Response header copy failed\r\n");
+ }
}
int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos) ;
printf("[WIFI-MAN] Http Response body [bytes = %d] \r\n",cpyLen);
@@ -582,48 +606,37 @@
delete at_data_resp;
}
-void WiFiManager::copyResponseHdr2Queue()
+bool WiFiManager::copyResponseHdr2Queue()
{
int numChars = 0;
// create message pointer for response header generation
char * msgPtr = (char *)at_data_resp->buffer;
// do status line
+ printf("before getting HTTP status line\n");
numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(),
http_response->get_status_message().c_str());
- printf("after getting HTTP status line \n");
- print_memory_info();
msgPtr += numChars;
- //vector<string*> hdrFields(http_response->get_headers_fields());
- //hdrFields.reserve(http_response->get_headers_length());
- //printf("after defining HTTP header fields vector \r\n");
- //hdrFields = http_response->get_headers_fields();
- //printf("after getting HTTP header fields \n");
- //print_memory_info();
- //vector<string*> hdrValues(http_response->get_headers_values());
- //printf("after getting HTTP header values \n");
- //print_memory_info();
+ printf("before getting HTTP headers length\n");
+
int hdrsLen = http_response->get_headers_length();
printf("after getting HTTP headers length = %d\n", hdrsLen);
- print_memory_info();
+ //print_memory_info();
+ if(hdrsLen <= 1)
+ {
+ printf("copy failed: Header Line = [%s]", msgPtr);
+ return false;
+ }
+ char * hdrField;
+ char * hdrValue;
for (size_t ix = 0; ix < hdrsLen; ix++) {
int sLen = http_response->get_headers_fields()[ix]->size()+1;
- printf("before creating allocation HTTP headers field [size = %d] \n", sLen);
- char * hdrField = (char *) malloc(sLen);
- printf("after creating allocation HTTP headers field \n");
- print_memory_info();
+ hdrField = new char [sLen];
std::strcpy (hdrField, http_response->get_headers_fields()[ix]->c_str());
- printf("after getting HTTP headers field copy \n");
- print_memory_info();
- char * hdrValue = new char [http_response->get_headers_values()[ix]->size()+1];
+ hdrValue = new char [sLen];
std::strcpy (hdrValue, http_response->get_headers_values()[ix]->c_str());
numChars = sprintf(msgPtr, "%s: %s\r\n", hdrField, hdrValue);
- //numChars = sprintf(msgPtr, "%s: %s\r\n",
- // http_response->get_headers_fields()[ix]->c_str(),
- // http_response->get_headers_values()[ix]->c_str());
- printf("after call #%d to get http header field:values \r\n", ix);
- print_memory_info();
- free(hdrField);
delete hdrField;
+ delete hdrValue;
msgPtr += numChars;
}
numChars = sprintf(msgPtr, "\r\n");
@@ -633,6 +646,7 @@
printf("%s\r\n", (char *)at_data_resp->buffer);
// calculate header length
at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
+ return true;
}
void WiFiManager::return_response(HttpResponse* res) {
@@ -712,6 +726,7 @@
r = socket->set_root_ca_cert(SSL_CA_PEM);
if(r != NSAPI_ERROR_OK)
{
+ socket->close();
printf("TLS set_root_ca_cert failed!!\n");
return false;
}
@@ -729,8 +744,9 @@
return true;
}
#define TESTING_HTTPS
+
//#define DONT_USE_TLS_SOCKET
-void WiFiManager::createHttpsRequest()
+bool WiFiManager::createHttpsRequest()
{
// reset chunk #;
chunkNum = 0;
@@ -789,7 +805,7 @@
printf("HttpRequest failed (error code %s)\n", buf);
//printf("HttpsRequest failed (error code %d)\n", https_request->get_error());
delete http_request; // free the memory
- return;
+ return false;
}
delete http_request; // free the memory
printf("\n----- HTTP POST response -----\n");
@@ -805,12 +821,14 @@
#endif
if(tlsResult == false){
delete socket;
+ printf("TLS Socket connection failed - deleting data msg\r\n");
free_DataMsg();
- return;
+ printf("data msg deleted \r\n");
+ return false;
}
//printf("[create https] TLS connection successful for https site : %s\n", host);
- printf("after call to createTLSconnection \n");
- print_memory_info();
+ //printf("after call to createTLSconnection \n");
+ //print_memory_info();
}
// Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument
//HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
@@ -843,6 +861,18 @@
setHttpsHeader("Content-Type", http_req_cfg->contentType);
setHttpsHeader("Content-Length", http_req_cfg->contentLen);
printf("https headers setup - about to send request\r\n");
+ mbedtls_ssl_context* tlsContext ;
+ tlsContext = socket->get_ssl_context();
+ mbedtls_ssl_config * tlsConfig;
+ tlsConfig = socket->get_ssl_config();
+ if(tlsContext != NULL)
+ {
+ printf("current TLS tlsContext is not null [%d] \r\n", tlsContext->state);
+ }
+ else
+ {
+ printf("TLS Context is NULL \r\n");
+ }
//_wmutex.lock();
http_response = https_request->send(http_req_cfg->body, bodyLen);
//_wmutex.unlock();
@@ -864,10 +894,11 @@
delete https_request; // free the memory
https_request = NULL;
https_connection_active = false; // reset true whenever connection fails
+ socket->close();
delete socket;
socket = NULL;
free_DataMsg();
- return;
+ return false;
}
https_connection_active = true; // set true whenever connection succeeds
printf("\n----- HTTPS POST response -----\r\n");
@@ -876,6 +907,10 @@
#ifndef MIX_HDR_AND_BODY
return_response(http_response);
+#else
+ if(http_response_hdr_sent == false){ // if it failed to add to first chunk send separately
+ return_response(http_response);
+ }
#endif
//delete http_response; // free the response memory
//http_response = NULL;
@@ -885,6 +920,7 @@
delete https_request; // free the request & response memory
printf("deleted https_request\r\n");
https_request = NULL;
+ return true;
}
void WiFiManager::createHttpRequest(http_method method,