this is using the mbed os version 5-13-1
Diff: source/WiFiManager.cpp
- Revision:
- 104:11e9605093c9
- Parent:
- 103:7b566b522427
- Child:
- 105:e5ce023eee93
--- a/source/WiFiManager.cpp Thu May 02 21:50:17 2019 +0000
+++ b/source/WiFiManager.cpp Sun May 05 08:24:46 2019 +0000
@@ -31,19 +31,51 @@
_wiFi2ATDataQueue(wiFi2ATDataQueue)
{
- lastScanCount = 0;
- wifiCmd = WIFI_CMD_NONE;
- internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
- is_connected = false;
- http_response = NULL;
- chunkNum = 0;
- socket = NULL;
- https_connection_active = false;
+ lastScanCount = 0;
+ wifiCmd = WIFI_CMD_NONE;
+ internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
+ is_connected = false;
+ http_response = NULL;
+ chunkNum = 0;
+ socket = NULL;
+ https_connection_active = false;
+ wifiBusy = 0;
+ wifiWatchdogTimer.start();
+ watchdogCnt = 0;
+ _event_queue.call_every(10000, this, &WiFiManager::callWifiWatchDog);
}
WiFiManager::~WiFiManager()
{
}
+
+void WiFiManager::callWifiWatchDog()
+{
+ static int inactivity_monitor = 0;
+ watchdogCnt++;
+ if(watchdogCnt >= 6 && responseString==NULL) // every minute
+ {
+ responseString = (char *) malloc(100);
+ sprintf(responseString, "\r\n[WiFi-MAN] WiFi Manager Alive\r\n");
+ sendATresponseString(WIFI_WATCH_DOG);
+ watchdogCnt = 0;
+ }
+ else if(wifiWatchdogTimer.read() > 30.0 && responseString==NULL)
+ {
+ if(wifiCmd == WIFI_CMD_NONE)
+ inactivity_monitor++;
+ responseString = (char *) malloc(100);
+ sprintf(responseString, "\r\n[WiFi-MAN] Main Loop InActive : state = %d busy = %d httpsConnActive = %d\r\n",
+ wifiCmd, wifiBusy, https_connection_active);
+ sendATresponseString(WIFI_WATCH_DOG);
+ if(inactivity_monitor >= 3)
+ {
+ free_DataMsg();
+ inactivity_monitor = 0;
+ }
+ }
+
+}
bool WiFiManager::queueATresponse(at_cmd_resp_t resp){
#ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
@@ -78,25 +110,31 @@
while(true){
dequeueWiFiCommands();
dequeueATdataResponse();
+ wifiWatchdogTimer.reset();
switch(wifiCmd){
case WIFI_CMD_NONE:
// IDLE STATE
break;
case WIFI_CMD_SCAN:
+ wifiBusy = 1;
error = scanNetworks();
wifiCmd = WIFI_CMD_NONE;
queueATresponse(AT_SCAN_RESP);
+ wifiBusy = 0;
break;
case WIFI_CMD_DETAILED_SCAN:
{
+ wifiBusy = 1;
nsapi_size_or_error_t cnt_err;
cnt_err = getAvailableAPs(lastScanCount);
wifiCmd = WIFI_CMD_NONE;
queueATresponse(AT_DETAILED_SCAN_RESP);
+ wifiBusy = 0;
break;
}
case WIFI_CMD_CONNECT:
{
+ wifiBusy = 1;
error = connect();
int secCount = 0;
while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS || is_connected==false){
@@ -113,44 +151,72 @@
else {
sendATresponseString(AT_CONNECT_RESP);
}
+ wifiBusy = 0;
break;
}
case WIFI_CMD_DISCONNECT:
+ wifiBusy = 1;
error = disconnect();
wifiCmd = WIFI_CMD_NONE;
queueATresponse(AT_DISCONNECT_RESP);
+ wifiBusy = 0;
break;
case WIFI_CMD_CONFIG:
+ wifiBusy = 1;
set_WIFI_CONFIG();
wifiCmd = WIFI_CMD_NONE;
queueATresponse(AT_CONFIG_RESP);
+ wifiBusy = 0;
break;
case WIFI_CMD_INTERNET_CONFIG:
+ wifiBusy = 1;
set_internet_config();
queueATresponse(AT_INTERNET_CONFIG_RESP);
wifiCmd = WIFI_CMD_NONE;
break;
case WIFI_CMD_NETWORK_STATUS:
+ wifiBusy = 1;
getNetworkStatus();
sendATresponseString(AT_NETWORK_STATUS_RESP);
wifiCmd = WIFI_CMD_NONE;
+ wifiBusy = 0;
break;
case WIFI_CMD_WIFI_STATUS:
+ wifiBusy = 1;
getWiFiStatus();
sendATresponseString(AT_WIFI_STATUS_RESP);
wifiCmd = WIFI_CMD_NONE;
+ wifiBusy = 0;
break;
case WIFI_CMD_SEND_HTTPS_REQ:
+ wifiBusy = 1;
+ if(responseString == NULL)
+ {
+ responseString = (char *) malloc(100);
+ sprintf(responseString, "\r\nHTTP REQUEST RECEIVED\r\n");
+ sendATresponseString(AT_EVENT);
+ }
dbg_printf(LOG, "before call to send http request \n");
+ dbg_printf(LOG, "\r\n[WIFI-MAN] Received HTTPS request...\r\n");
print_memory_info();
result = createHttpsRequest();
if(result == false)
{
+ responseString = (char *) malloc(100);
+ if(http_result==TLS_CONNECTION_FAILED)
+ {
+ sprintf(responseString, "\r\nTLS CONNECTION FAILURE\r\n");
+ }
+ else
+ {
+ sprintf(responseString, "\r\nHTTP REQUEST FAILED\r\n");
+ }
sendATresponseString(AT_COMMAND_FAILED);
}
dbg_printf(LOG, "after call to send http request \n");
print_memory_info();
wifiCmd = WIFI_CMD_NONE;
+ wifiBusy = 0;
break;
case WIFI_CMD_SEND_HTTP_REQ:
break;
@@ -219,7 +285,7 @@
bool WiFiManager::dequeueWiFiCommands(){
- if(wifiCmd != WIFI_CMD_NONE) return false; // busy
+ if(wifiCmd != WIFI_CMD_NONE || wifiBusy!=0) return false; // busy
osEvent evt = _aT2WiFiCmdQueue->get(0);
if(evt.status == osEventMessage){
wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p;
@@ -237,7 +303,7 @@
bool WiFiManager::dequeueATdataResponse(){
- if(wifiCmd != WIFI_CMD_NONE) return false; // busy
+ if(wifiCmd != WIFI_CMD_NONE || wifiBusy!=0) return false; // busy
osEvent evt = _aT2WiFiDataQueue->get(0);
if(evt.status == osEventMessage){
data_msg = (wifi_data_msg_t*)evt.value.p;
@@ -531,7 +597,7 @@
void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
{
- dbg_printf(LOG, "[WIFI-MAN] about call callback... \r\n");
+ dbg_printf(LOG, "[WIFI-MAN] about to call callback... \r\n");
_event_queue.call_in(50, this, &WiFiManager::status_callback_event, status, param);
//status_callback_event(status, param);
}
@@ -555,6 +621,7 @@
DEFAULT_WIFI_CHANNEL);
is_connected = true;
+ wifiBusy = 0;
break;
case NSAPI_STATUS_DISCONNECTED:
dbg_printf(LOG, "No connection to network!\r\n");
@@ -564,6 +631,7 @@
// attempt reconnection if always connected scheme is set
if(internet_config.connectionScheme == ALWAYS_CONNECTED)
{
+ wifiBusy = 1;
nsapi_error_t error;
error = connect();
queueATresponse(WIFI_RECONNECT_INFO);
@@ -616,15 +684,15 @@
int i = 0;
responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
- memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
- strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
- i +=4;
- uint16_t port = address->get_port();
- internet_config.remote_port = port;
- memcpy(&responseBytes[i], &port, 2); // remote IPv4 port #
- i +=2;
- if(is_connected)
+ if(is_connected && result>0)
{
+ memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
+ strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
+ i +=4;
+ uint16_t port = address->get_port();
+ internet_config.remote_port = port;
+ memcpy(&responseBytes[i], &port, 2); // remote IPv4 port #
+ i +=2;
// local IPv4 address
int ipAddr[4];
strcpy(internet_config.local_IPv4Address, network->get_ip_address());
@@ -646,6 +714,8 @@
}
_event_queue.call_in(2, this, &WiFiManager::sendATresponseBytes,
CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
+ wifiBusy = 0;
+
}
void WiFiManager::sendSocketConnectionEvent()
@@ -801,13 +871,14 @@
}
-void WiFiManager::printBufferInHex(uint8_t *buf, int pLen)
+void WiFiManager::printBufferInHex(const uint8_t *buf, int pLen)
{
- for(int i =0;i<pLen;i++){
- if(i%8==0) dbg_printf(LOG, "\n[%3d]",i);
- dbg_printf(LOG, "%02x ", buf[i]);
- }
- dbg_printf(LOG, "\n");
+ //for(int i =0;i<pLen;i++){
+ // if(i%8==0) dbg_printf(LOG, "\n[%3d]",i);
+ // dbg_printf(LOG, "%02x ", buf[i]);
+ //}
+ //dbg_printf(LOG, "\n");
+ print_debug_hex(buf, pLen);
}
//#define TRY_PRINTF
@@ -823,6 +894,13 @@
{
dbg_printf(LOG, "[WIFI-MAN] response pointer NULL not!!\r\n");
}
+ if(responseString == NULL && chunkNum==1)
+ {
+ responseString = (char *) malloc(100);
+ sprintf(responseString, "\r\nHTTPS BODY CALLBACK RECEIVED\r\n");
+ sendATresponseString(AT_EVENT);
+ }
+
sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length);
}
@@ -871,10 +949,18 @@
dbg_printf(LOG, "Updating internet_config... \r\n");
nsapi_error_t error;
SocketAddress * address = new SocketAddress;
- error = socket->getpeername(address);
- strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
- uint16_t port = address->get_port();
- internet_config.remote_port = port;
+ error = socket->getpeername(address);
+ if(error>=0)
+ {
+ strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
+ uint16_t port = address->get_port();
+ internet_config.remote_port = port;
+ }
+ else
+ {
+ strcpy(internet_config.remote_IPv4Address, "");
+ internet_config.remote_port = 0;
+ }
delete address;
}
@@ -959,14 +1045,17 @@
dbg_printf(LOG, "TLS Socket connection failed - deleting data msg\r\n");
free_DataMsg();
dbg_printf(LOG, "data msg deleted \r\n");
+ http_result = TLS_CONNECTION_FAILED;
return false;
}
// update remote peer details after socket connection
updateRemotePeerDetails();
// send socket connection event before proceeding to send https request
// give about 2 ms
- _event_queue.call_in(2, this, &WiFiManager::sendSocketConnectionEvent);
- return true;
+ //_event_queue.call(this, &WiFiManager::sendSocketConnectionEvent);
+ sendSocketConnectionEvent();
+ //https_connection_active = true; // set true whenever connection succeeds
+ //return true;
//dbg_printf(LOG, "[create https] TLS connection successful for https site : %s\n", host);
//dbg_printf(LOG, "after call to createTLSconnection \n");
//print_memory_info();
@@ -1002,22 +1091,7 @@
setHttpsHeader("Content-Type", http_req_cfg->contentType);
setHttpsHeader("Content-Length", http_req_cfg->contentLen);
dbg_printf(LOG, "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)
- {
- dbg_printf(LOG, "current TLS tlsContext is not null [%d] \r\n", tlsContext->state);
- }
- else
- {
- dbg_printf(LOG, "TLS Context is NULL \r\n");
- }
- //_wmutex.lock();
http_response = https_request->send(http_req_cfg->body, bodyLen);
- //https_request->send(http_req_cfg->body, bodyLen);
- //_wmutex.unlock();
}
#else
setHttpsHeader("Host", http_req_cfg->hostName);
@@ -1033,6 +1107,7 @@
char buf[100];
mbedtls_strerror(https_request->get_error(), buf, 100);
dbg_printf(LOG, "HttpsRequest failed (error code %s)\n", buf);
+ http_result = HTTP_REQUEST_FAILED;
delete https_request; // free the memory
https_request = NULL;
https_connection_active = false; // reset true whenever connection fails
@@ -1063,6 +1138,7 @@
dbg_printf(LOG, "deleted https_request\r\n");
https_request = NULL;
http_response = NULL;
+ http_result = RESPONSE_OK;
return true;
}