this is using the mbed os version 5-13-1
Diff: source/WiFiManager.cpp
- Branch:
- PassingRegression
- Revision:
- 118:8df0e9c2ee3f
- Parent:
- 116:2296cf274661
- Child:
- 119:8d939a902333
--- a/source/WiFiManager.cpp Sun May 19 16:22:59 2019 +0000
+++ b/source/WiFiManager.cpp Sat May 25 16:25:42 2019 +0000
@@ -40,13 +40,16 @@
http_response = NULL;
chunkNum = 0;
socket = NULL;
+ responseString = NULL;
+ responseBytes = NULL;
+ at_data_resp = NULL;
https_connection_active = false;
use_full_hostname = false;
wifiBusy = 0;
wifiWatchdogTimer.start();
watchdogCnt = 0;
//_event_queue.call_every(10000, this, &WiFiManager::callWifiWatchDog);
- watchDogTick.attach(callback(this, &WiFiManager::callWifiWatchDogIsr), 10000.0); // call flip function every 10 seconds
+ watchDogTick.attach(callback(this, &WiFiManager::callWifiWatchDogIsr), 10.0); // call flip function every 10 seconds
}
@@ -58,7 +61,7 @@
void WiFiManager::callWifiWatchDogIsr()
{
- _event_queue.call(this, &WiFiManager::callWifiWatchDog);
+ _event_queue.call_in(10, this, &WiFiManager::callWifiWatchDog);
}
void WiFiManager::callWifiWatchDog()
{
@@ -67,22 +70,22 @@
#else
static int inactivity_monitor = 0;
watchdogCnt++;
- if(watchdogCnt >= 6 && responseString==NULL) // every minute
+ if(watchdogCnt >= 6 && outputBuffersAvailable()) // every minute
{
- responseString = (char *) malloc(120);
+ char * respStr = (char *) malloc(120);
sprintf(responseString, "\r\n[WiFi-MAN] WiFi Manager Alive : state = %d busy = %d httpsConnActive = %d\r\n",
wifiCmd, wifiBusy, https_connection_active);
- sendATresponseString(WIFI_WATCH_DOG);
+ sendThreadATresponseString(respStr, WIFI_WATCH_DOG);
watchdogCnt = 0;
}
else if(wifiWatchdogTimer.read() > 30 && responseString==NULL)
{
if(wifiCmd == WIFI_CMD_NONE)
inactivity_monitor++;
- responseString = (char *) malloc(120);
+ char * respStr = (char *) malloc(120);
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);
+ sendThreadATresponseString(respStr, WIFI_WATCH_DOG);
if(inactivity_monitor >= 3)
{
free_DataMsg();
@@ -91,7 +94,59 @@
}
#endif
}
-
+
+void WiFiManager::sendThreadATresponseString(const char * buf, at_cmd_resp_t at_cmd)
+{
+ if(at_data_resp != NULL) return;
+ int strLen = strlen(buf) + 1;
+ at_data_resp = new at_data_msg_t;
+ // set string length
+ at_data_resp->dataLen = strLen;
+ memcpy(at_data_resp->buffer, buf, strLen);
+ // package and send on wifi data queue
+ at_data_resp->at_resp = at_cmd;
+ bool queueResult = true;
+ int wait_count = 0;
+ do
+ {
+ if(!queueResult){
+ wait_count+=10;
+ dbg_printf(LOG, "ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
+ wait_ms(10);
+ }
+ queueResult = queueWiFiDataResponse(*at_data_resp);
+ }while(queueResult == false && wait_count<QUEUE_WAIT_TIMEOUT_MS);
+ delete at_data_resp;
+ at_data_resp = NULL;
+}
+
+
+bool WiFiManager::outputBuffersAvailable()
+{
+ int timeout = 0;
+ while(timeout < 100)
+ {
+ if(responseBytes==NULL && responseString==NULL && at_data_resp==NULL)
+ {
+ return true;
+ }
+ else
+ {
+ timeout += 10;
+ wait_ms(10);
+ }
+ }
+ if(responseBytes==NULL && responseString==NULL && at_data_resp==NULL)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+
bool WiFiManager::queueATresponse(at_cmd_resp_t resp){
#ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
at_resp_message_t *atResp = _wiFi2ATmPool->alloc();
@@ -158,11 +213,15 @@
wifiBusy = 1;
error = connect();
int secCount = 0;
- while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS || is_connected==false){
+ while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS && is_connected==false){
wait(1); // wait 1 sec
}
wifiCmd = WIFI_CMD_NONE;
if(is_connected==false){
+ if(outputBuffersAvailable() == false) // first free it
+ {
+ free(responseString);
+ }
dbg_printf(LOG, "[WIFI MAN] +++ WIFI CONNECTION TIMEOUT +++ \r\n");
//queueATresponse(AT_COMMAND_FAILED);
responseString = (char *) malloc(100);
@@ -214,14 +273,20 @@
}
case WIFI_CMD_NETWORK_STATUS:
wifiBusy = 1;
- getNetworkStatus();
+ if(outputBuffersAvailable())
+ {
+ getNetworkStatus();
+ }
sendATresponseString(AT_NETWORK_STATUS_RESP);
wifiCmd = WIFI_CMD_NONE;
wifiBusy = 0;
break;
case WIFI_CMD_WIFI_STATUS:
wifiBusy = 1;
- getWiFiStatus();
+ if(outputBuffersAvailable())
+ {
+ getWiFiStatus();
+ }
sendATresponseString(AT_WIFI_STATUS_RESP);
wifiCmd = WIFI_CMD_NONE;
wifiBusy = 0;
@@ -230,7 +295,7 @@
{
wifiBusy = 1;
#ifdef SEND_DEBUG_MESSAGES
- if(responseString == NULL)
+ if(outputBuffersAvailable())
{
responseString = (char *) malloc(100);
sprintf(responseString, "\r\nHTTP REQUEST RECEIVED\r\n");
@@ -251,7 +316,7 @@
int oldChunkNum = chunkNum;
while(!backgroundTaskCompleted && msecCount < 6000)
{
- msecCount++;
+ msecCount+=10;
wait_ms(10);
if(oldChunkNum != chunkNum) // new payload received
{
@@ -273,7 +338,7 @@
#else
result = createHttpsRequest();
#endif
- if(result == false)
+ if(result == false && outputBuffersAvailable())
{
responseString = (char *) malloc(100);
if(http_result==TLS_CONNECTION_FAILED)
@@ -551,7 +616,7 @@
//printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
//sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
}
- else // -ve number means error
+ else if(outputBuffersAvailable()) // -ve number means error
{
responseString = (char *) malloc(20);
dbg_printf(LOG, "[WIFI-MAN] hostname translation failed\r\n");
@@ -724,14 +789,17 @@
dbg_printf(LOG, "Global IP address set!\r\n");
dbg_printf(LOG, "[WIFI-MAN] IP address: %s\n", network->get_ip_address());
dbg_printf(LOG, "[WIFI-MAN] Connected to the network %s\n", wifi_config->ssid);
- responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
- sprintf(responseString, "\r\n%s%d,%s,%d\r\n", WIFI_LINK_ENABLED,
- WIFI_CHANNEL,
- network->get_mac_address(),
- DEFAULT_WIFI_CHANNEL);
-
- is_connected = true;
- wifiBusy = 0;
+ if(outputBuffersAvailable())
+ {
+ responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
+ sprintf(responseString, "\r\n%s%d,%s,%d\r\n", WIFI_LINK_ENABLED,
+ WIFI_CHANNEL,
+ network->get_mac_address(),
+ DEFAULT_WIFI_CHANNEL);
+
+ is_connected = true;
+ wifiBusy = 0;
+ }
break;
case NSAPI_STATUS_DISCONNECTED:
dbg_printf(LOG, "No connection to network!\r\n");
@@ -788,52 +856,56 @@
void WiFiManager::processGetHostByNameResult(nsapi_error_t result, SocketAddress *address)
{
+
dbg_printf(LOG, "gethostbyname_callback called... result = %d \r\n", result);
print_memory_info();
- responseBytes = new uint8_t[HOSTNAME_RESPONSE_LEN]; //malloc(HOSTNAME_RESPONSE_LEN);
- int i = 0;
- responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
- responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
- if(is_connected && result>=0)
+ if(outputBuffersAvailable())
{
- 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());
- sscanf(internet_config->local_IPv4Address, "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1],
- &ipAddr[2], &ipAddr[3]);
- responseBytes[i++] = (uint8_t) ipAddr[0];
- responseBytes[i++] = (uint8_t) ipAddr[1];
- responseBytes[i++] = (uint8_t) ipAddr[2];
- responseBytes[i++] = (uint8_t) ipAddr[3];
- // local port number
- responseBytes[i++] = 0;
- responseBytes[i] = 0;
- printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
- sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
- }
- else
- {
- // if unconnected set ip and port to zeroes
- memset(&responseBytes[i], 0x00, 6);
- delete responseBytes;
- dbg_printf(LOG, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
- if(responseString == NULL)
+ responseBytes = new uint8_t[HOSTNAME_RESPONSE_LEN]; //malloc(HOSTNAME_RESPONSE_LEN);
+ int i = 0;
+ responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
+ responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
+ if(is_connected && result>=0)
{
- responseString = (char *) malloc(100);
- sprintf(responseString, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
- sendATresponseString(AT_EVENT);
+ 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());
+ sscanf(internet_config->local_IPv4Address, "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1],
+ &ipAddr[2], &ipAddr[3]);
+ responseBytes[i++] = (uint8_t) ipAddr[0];
+ responseBytes[i++] = (uint8_t) ipAddr[1];
+ responseBytes[i++] = (uint8_t) ipAddr[2];
+ responseBytes[i++] = (uint8_t) ipAddr[3];
+ // local port number
+ responseBytes[i++] = 0;
+ responseBytes[i] = 0;
+ printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
+ sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
}
- use_full_hostname = not use_full_hostname;
+ else
+ {
+ // if unconnected set ip and port to zeroes
+ memset(&responseBytes[i], 0x00, 6);
+ delete responseBytes;
+ dbg_printf(LOG, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
+ if(responseString == NULL)
+ {
+ responseString = (char *) malloc(100);
+ sprintf(responseString, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
+ sendATresponseString(AT_EVENT);
+ }
+ use_full_hostname = not use_full_hostname;
+ }
+ wifiBusy = 0;
+ backgroundTaskCompleted = true;
}
- wifiBusy = 0;
- backgroundTaskCompleted = true;
}