this is using the mbed os version 5-13-1
Diff: source/WiFiManager.cpp
- Revision:
- 90:ed0267eca7b5
- Parent:
- 89:45f6db09a76d
- Child:
- 91:d6b6319ad681
--- a/source/WiFiManager.cpp Mon Apr 01 08:00:41 2019 +0000
+++ b/source/WiFiManager.cpp Sun Apr 07 10:52:37 2019 +0000
@@ -64,7 +64,7 @@
void WiFiManager::runMain(){
nsapi_error_t error;
- printf("\r\n [WIFI MAN] Thread Id = %d\r\n", Thread::gettid());
+ printf("\r\n [WIFI MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
while(true){
dequeueWiFiCommands();
dequeueATdataResponse();
@@ -348,29 +348,66 @@
}
void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen)
-{
- at_data_msg_t *at_data_resp;
+{
+
at_data_resp = new at_data_msg_t;
at_data_resp->at_resp = at_cmd;
size_t bufSize = sizeof(at_data_resp->buffer);
int pos = 0;
at_data_resp->dataLen = 0;
bool queueResult = true;
+ int hdrLen = 0;
do {
- if(!queueResult) wait_ms(10); // wait 10 ms to allow data to be transferred
- at_data_resp->dataLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos) ;
- memcpy(at_data_resp->buffer, &buf[pos], bufLen);
+ if(!queueResult)
+ {
+ wait_ms(10); // wait 10 ms to allow data to be transferred
+ }
+ else {
+ if(http_response_hdr_sent == false){
+ copyResponseHdr2Queue();
+ hdrLen = at_data_resp->dataLen;
+ http_response_hdr_sent = true;
+ }
+ int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos - hdrLen ) ;
+ at_data_resp->dataLen += cpyLen;
+ memcpy(&at_data_resp->buffer[hdrLen], &buf[pos], cpyLen);
+ }
queueResult = queueWiFiDataResponse(*at_data_resp);
- if(queueResult) pos+= at_data_resp->dataLen;
+ if(queueResult){
+ pos+= at_data_resp->dataLen;
+ at_data_resp->dataLen = 0;
+ hdrLen = 0;
+ }
}while(queueResult == false || pos < bufLen);
delete at_data_resp;
}
+void WiFiManager::copyResponseHdr2Queue()
+{
+ int numChars = 0;
+ // create message pointer for response header generation
+ char * msgPtr = (char *)at_data_resp->buffer;
+ // do status line
+ numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(),
+ http_response->get_status_message().c_str());
+ msgPtr += numChars;
+ for (size_t ix = 0; ix < http_response->get_headers_length(); ix++) {
+ numChars = sprintf(msgPtr, "%s: %s\r\n",
+ http_response->get_headers_fields()[ix]->c_str(),
+ http_response->get_headers_values()[ix]->c_str());
+ msgPtr += numChars;
+ }
+ numChars = sprintf(msgPtr, "\r\n");
+ msgPtr += numChars;
+ // print out generated header
+ printf("[WiFi MAN] generated response header:\n");
+ printf("%s\r\n", (char *)at_data_resp->buffer);
+ // calculate header length
+ at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
+}
+
void WiFiManager::return_response(HttpResponse* res) {
- //queueWiFiDataResponse(at_data_msg_t at_data_resp);
-
- at_data_msg_t *at_data_resp;
at_data_resp = new at_data_msg_t;
int numChars = 0;
// create message pointer for response header generation
@@ -386,18 +423,14 @@
}
numChars = sprintf(msgPtr, "\r\n\r\n");
msgPtr += numChars;
- //strncpy(msgPtr, res->get_body_as_string().c_str(), res->get_body_length()+1);
// print out generated header
- printf("generated response:\n");
+ printf("[WiFi MAN] generated response header:\n");
printf("%s\r\n", (char *)at_data_resp->buffer);
- //printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+ // calculate header length
at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
- //printf("WIFI MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
- // package and send on wifi data queue
+ // package and send on wifi data queue
at_data_resp->at_resp = AT_HTTPS_RESP;
- //at_data_resp.at_data_resp = sizeof(wifi_config_t);
- //memcpy(at_data_resp.buffer, res, at_data_resp.dataLen);
bool queueResult = true;
int wait_count = 0;
do
@@ -411,34 +444,21 @@
}while(queueResult == false);
delete at_data_resp;
}
+
+
+void WiFiManager::printBufferInHex(uint8_t *buf, int pLen)
+{
+ for(int i =0;i<pLen;i++){
+ if(i%8==0) printf("\n[%3d]",i);
+ printf("%02x ", buf[i]);
+ }
+ printf("\n");
+}
+
//#define TRY_PRINTF
-void WiFiManager::body_callback(const char *at, uint32_t length) {
-
+void WiFiManager::body_callback(const char *at, uint32_t length) {
printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length);
- //device->printf("\n Try Print Header as string:\n\n ");
- //device->printf("recv %d [%.*s]\n", length, strstr((char *)at, "\r\n")-(char *)at, (char *)at);
- //if(false)
- //if(chunkNum < 2)
- //for(int i=0; i < length; i++){
- //
- //putc((uint8_t)at[i]);
- //int resp = write( (const uint8_t *)at, (int) length, &completed, SERIAL_EVENT_TX_COMPLETE);
- //}
- //if(false)
-#ifdef DUMP_BODY_BYTES
- if(chunkNum < 2)
- for (size_t ix = 0; ix < length; ix++) {
- printf("%02X: ", (uint8_t)at[ix]);
- if((ix % 32) == 0 and ix)
- printf("\n");
- }
-#endif
-#ifdef TRY_PRINTF
- printf("%s\n", at);
-#endif
-
- printf("\n\n");
chunkNum++;
sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length);
}
@@ -446,10 +466,8 @@
bool WiFiManager::createTLSconnection(const char * hostName)
{
+ //mbed_trace_init();
socket = new TLSSocket();
- printf("\n\nafter TLS socket creation\n");
- print_memory_info();
- //printf("\n[WIFI MAN] TLS Host = %s\n", hostName);
nsapi_error_t r;
// make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
@@ -481,36 +499,36 @@
{
// reset chunk #;
chunkNum = 0;
+ http_response_hdr_sent = false;
printf("\n[WIFI MAN] Http Request received:\n");
http_req_cfg = (http_request_t *) data_msg->buffer;
- printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI.c_str());
+ printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI);
printf("\n[WIFI MAN] internet cfg url = %s\n", internet_config.url);
char full_url[100];
char host[60] ;
strncpy(full_url,internet_config.url, strlen(internet_config.url)+1);
- strncpy(host,http_req_cfg->hostName.c_str(), http_req_cfg->hostName.size());
- printf("\n[WIFI MAN] server url = %s\n", full_url);
- //strncat(internet_config.url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size());
- strncat(full_url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size());
- //printf("\n[WIFI MAN] server url = %s\n", internet_config.url);
+ strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1);
+ strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1);
printf("\n[WIFI MAN] server url+uri = %s\n", full_url);
- printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName.c_str());
- printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal.c_str());
- printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType.c_str());
- printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen.c_str());
- //printf("\n[WIFI MAN] server url = %s\n", internet_config.url.c_str());
- //const char* url = internet_config.url;
+ printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName);
+ printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal);
+ printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType);
+ printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen);
int bodyLen;
- sscanf(http_req_cfg->contentLen.c_str(), "%d", &bodyLen);
- printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen.c_str(), bodyLen);
+ sscanf(http_req_cfg->contentLen, "%d", &bodyLen);
+ printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen);
+
+ if(bodyLen > 10){
+ printf("\n Message Body:\n");
+ printBufferInHex(http_req_cfg->body, bodyLen);
+ }
if(strstr(internet_config.url, "http:")!=NULL) // http request
{
http_request = new HttpRequest(network,
- //SSL_CA_PEM,
- http_req_cfg->method,
- full_url,
- callback(this, &WiFiManager::body_callback));
+ http_req_cfg->method,
+ full_url,
+ callback(this, &WiFiManager::body_callback));
setHttpHeader("Host", http_req_cfg->hostName);
setHttpHeader("Accept", http_req_cfg->AcceptVal);
printf("http_req_cfg->method = %d\n", http_req_cfg->method);
@@ -539,6 +557,7 @@
}
else
{
+#ifndef DONT_USE_TLS_SOCKET
if(https_connection_active == false){
bool tlsResult;
tlsResult = createTLSconnection(host);
@@ -549,6 +568,8 @@
}
printf("[create https] TLS connection successful for https site : %s\n", host);
}
+ 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");
//_wmutex.lock();
@@ -556,9 +577,15 @@
http_req_cfg->method,
full_url,
callback(this, &WiFiManager::body_callback));
-
+#else
+ https_request = new HttpsRequest(network,
+ SSL_CA_PEM,
+ http_req_cfg->method,
+ full_url,
+ callback(this, &WiFiManager::body_callback));
+#endif
#ifdef TESTING_HTTPS
- printf("http_req_cfg->method = %d\n");
+ printf("http_req_cfg->method = %d\n", http_req_cfg->method);
if(http_req_cfg->method == HTTP_GET){
printf("HTTP_GET -- ignoring body\n");
setHttpsHeader("Host", http_req_cfg->hostName);
@@ -570,8 +597,8 @@
else{
setHttpsHeader("Host", http_req_cfg->hostName);
setHttpsHeader("Accept", http_req_cfg->AcceptVal);
- setHttpHeader("Content-Type", http_req_cfg->contentType);
- setHttpHeader("Content-Length", http_req_cfg->contentLen);
+ setHttpsHeader("Content-Type", http_req_cfg->contentType);
+ setHttpsHeader("Content-Length", http_req_cfg->contentLen);
http_response = https_request->send(http_req_cfg->body, bodyLen);
}
#else
@@ -583,7 +610,7 @@
#endif
//_wmutex.unlock();
- free_DataMsg();
+ //free_DataMsg();
if (!http_response) {
char buf[100];
mbedtls_strerror(https_request->get_error(), buf, 100);
@@ -591,13 +618,18 @@
delete https_request; // free the memory
https_connection_active = false; // reset true whenever connection fails
delete socket;
+ free_DataMsg();
return;
}
https_connection_active = true; // set true whenever connection succeeds
printf("\n----- HTTPS POST response -----\r\n");
}
- return_response(http_response);
- delete http_response;
+ if(http_response != NULL){
+ //return_response(http_response);
+ delete http_response; // free the response memory
+ }
+ free_DataMsg();
+ //delete https_request; // free the request memory
}
void WiFiManager::createHttpRequest(http_method method,