this is using the mbed os version 5-13-1
Diff: source/WiFiManager.cpp
- Revision:
- 84:7c7add00f4bf
- Parent:
- 82:10072c1794d3
- Child:
- 85:9f896e1e041c
--- a/source/WiFiManager.cpp Tue Mar 26 23:33:51 2019 +0000
+++ b/source/WiFiManager.cpp Thu Mar 28 22:13:06 2019 +0000
@@ -31,6 +31,7 @@
wifiCmd = WIFI_CMD_NONE;
internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
is_connected = false;
+ chunkNum = 0;
}
WiFiManager::~WiFiManager()
@@ -97,6 +98,8 @@
queueATresponse(AT_INTERNET_CONFIG_RESP);
break;
case WIFI_CMD_SEND_HTTPS_REQ:
+ createHttpsRequest();
+ wifiCmd = WIFI_CMD_NONE;
break;
case WIFI_CMD_SEND_HTTP_REQ:
break;
@@ -243,12 +246,12 @@
{
internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer;
internet_config.peer_id = internet_cfg->peer_id;
- internet_config.url = internet_cfg->url;
+ strncpy(internet_config.url,internet_cfg->url, strlen(internet_cfg->url)+1);
internet_config.connectionScheme = internet_cfg->connectionScheme;
free_DataMsg();
printf("[WIFI MAN] Internet configuration setup completed\n");
printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
- internet_config.url.c_str(),
+ internet_config.url,
internet_config.connectionScheme);
}
@@ -372,19 +375,97 @@
*/
+void WiFiManager::return_response(HttpResponse* res) {
+
+ //queueWiFiDataResponse(at_data_msg_t at_data_resp);
+ printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+ printf("Headers:\n");
+ at_data_msg_t at_data_resp;
+ //at_data_resp = &at_data;
+ at_data_resp.dataLen = sizeof(res); // start with minimum size of response
+ for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
+ printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+ at_data_resp.dataLen+= res->get_headers_fields()[ix]->size();
+ at_data_resp.dataLen+= res->get_headers_values()[ix]->size();
+ }
+ at_data_resp.dataLen+= res->get_body_length();
+ printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+ //printf("WIFI MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
+ // 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);
+ queueWiFiDataResponse(at_data_resp);
-void WiFiManager::createHttpsRequest(http_method method,
- const char* url,
- Callback<void(const char *at, uint32_t length)> body_callback)
+}
+
+
+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)
+ 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");
+ }
+ printf("\n\n");
+ chunkNum++;
+ //device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+}
+
+void WiFiManager::createHttpsRequest()
{
- https_request = new HttpsRequest(network, SSL_CA_PEM, method, url, body_callback);
+
+ //http_method method,
+ //Callback<void(const char *at, uint32_t length)> body_callback = 0
+ printf("\n[WIFI MAN] Http Request received:");
+ http_req_cfg = (http_request_t *) data_msg->buffer;
+ strncat(internet_config.url, http_req_cfg->request_URI.c_str(), strlen(http_req_cfg->request_URI.c_str()+1));
+ printf("\n[WIFI MAN] server url = %s\n", internet_config.url);
+ //printf("\n[WIFI MAN] server url = %s\n", internet_config.url.c_str());
+ //const char* url = internet_config.url;
+
+ //https_request = new HttpsRequest(network,
+ http_request = new HttpRequest(network,
+ //SSL_CA_PEM,
+ http_req_cfg->method,
+ internet_config.url,
+ callback(this, &WiFiManager::body_callback));
+ setHttpHeader("Host", http_req_cfg->hostName);
+ setHttpHeader("Accept", http_req_cfg->AcceptVal);
+ setHttpHeader("Content-Type", http_req_cfg->contentType);
+ setHttpHeader("Content-Length", http_req_cfg->contentLen);
+ int bodyLen;
+ sscanf(http_req_cfg->contentLen.c_str(), "%d", &bodyLen);
+ http_response = https_request->send(http_req_cfg->body, bodyLen);
+ free_DataMsg();
+ if (!http_response) {
+ printf("HttpRequest failed (error code %d)\n", https_request->get_error());
+ return;
+ }
+
+ printf("\n----- HTTPS POST response -----\n");
+ return_response(http_response);
}
void WiFiManager::createHttpRequest(http_method method,
const char* url,
Callback<void(const char *at, uint32_t length)> body_callback)
{
- http_request = new HttpRequest(network, method, url, body_callback);;
+ http_request = new HttpRequest(network,
+ method, url, body_callback);;
}
void WiFiManager::setHttpHeader(string key, string value)