diff --git a/include/Helpers.h b/include/Helpers.h index c00f06e..335a9b6 100644 --- a/include/Helpers.h +++ b/include/Helpers.h @@ -35,5 +35,6 @@ void bin_to_hex_str(const uint8_t *buf, int len, char *hex); void UnencodeURL(char* URL); esp_err_t SHA256Hash(unsigned char *data, int datalen, unsigned char *res); +void vTaskGetRunTimeStatsCustom( char *pcWriteBuffer ); #endif /* MAIN_INCLUDE_HELPERS_H_ */ diff --git a/include/NetTransport.h b/include/NetTransport.h index b64f56f..2550e3a 100644 --- a/include/NetTransport.h +++ b/include/NetTransport.h @@ -65,6 +65,7 @@ void WiFiStop(); void WiFiStopAP(); void WiFiStartAP(); void WiFiStartAPTemp(int seconds); +int GetAPClientsNumber(); void EthStart(void); void WiFiTransportTask(void *prm); diff --git a/src/HTTPPostSystem.c b/src/HTTPPostSystem.c index 614cd53..6cf6fb2 100644 --- a/src/HTTPPostSystem.c +++ b/src/HTTPPostSystem.c @@ -544,7 +544,7 @@ static HTTP_IO_RESULT HTTPPostSystemSettings(httpd_req_t *req, char *PostData) { if (!strcmp(tmp, (const char*) "prs")) { - vTaskGetRunTimeStats(rtstat); + vTaskGetRunTimeStatsCustom(rtstat); httpd_resp_sendstr(req, rtstat); return HTTP_IO_DONE_API; } diff --git a/src/Helpers.c b/src/Helpers.c index ba262e1..b02d985 100644 --- a/src/Helpers.c +++ b/src/Helpers.c @@ -22,6 +22,8 @@ #include "esp_mac.h" #include "esp_rom_crc.h" #include "mbedtls/md.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" uint32_t crc32(uint32_t crc, uint8_t const *buf, uint32_t len) { @@ -189,4 +191,69 @@ esp_err_t SHA256Hash(unsigned char *data, int datalen, return ESP_OK; } +void vTaskGetRunTimeStatsCustom( char *pcWriteBuffer ) +{ +TaskStatus_t *pxTaskStatusArray; +volatile UBaseType_t uxArraySize, x; +unsigned long ulTotalRunTime, ulStatsAsPercentage; + /* Make sure the write buffer does not contain a string. */ + *pcWriteBuffer = 0x00; + + /* Take a snapshot of the number of tasks in case it changes while this + function is executing. */ + uxArraySize = uxTaskGetNumberOfTasks(); + + /* Allocate a TaskStatus_t structure for each task. An array could be + allocated statically at compile time. */ + pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) ); + + if( pxTaskStatusArray != NULL ) + { + /* Generate raw status information about each task. */ + uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, + uxArraySize, + &ulTotalRunTime ); + + /* For percentage calculations. */ + ulTotalRunTime /= 100UL; + + /* Avoid divide by zero errors. */ + if( ulTotalRunTime > 0 ) + { + /* For each populated position in the pxTaskStatusArray array, + format the raw data as human readable ASCII data. */ + for( x = 0; x < uxArraySize; x++ ) + { + /* What percentage of the total run time has the task used? + This will always be rounded down to the nearest integer. + ulTotalRunTimeDiv100 has already been divided by 100. */ + ulStatsAsPercentage = + pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalRunTime; + + if( ulStatsAsPercentage > 0UL ) + { + sprintf( pcWriteBuffer, "%s----%llu----%lu%%----%d\r\n", + pxTaskStatusArray[ x ].pcTaskName, + (uint64_t)pxTaskStatusArray[ x ].ulRunTimeCounter, + ulStatsAsPercentage, + (int)pxTaskStatusArray[ x ].usStackHighWaterMark); + } + else + { + /* If the percentage is zero here then the task has + consumed less than 1% of the total run time. */ + sprintf( pcWriteBuffer, "%s----%llu----<1%%----%d\r\n", + pxTaskStatusArray[ x ].pcTaskName, + (uint64_t)pxTaskStatusArray[ x ].ulRunTimeCounter, + (int)pxTaskStatusArray[ x ].usStackHighWaterMark); + } + + pcWriteBuffer += strlen( ( char * ) pcWriteBuffer ); + } + } + + /* The array is no longer needed, free the memory it consumes. */ + vPortFree( pxTaskStatusArray ); + } +} diff --git a/src/WiFiTransport.c b/src/WiFiTransport.c index ad80336..7a0fc5c 100644 --- a/src/WiFiTransport.c +++ b/src/WiFiTransport.c @@ -537,8 +537,13 @@ static void WiFiControlTask(void *arg) { if (--TempAPCounter <= 0) { - WiFiStopAP(); - ESP_LOGI(TAG, "WiFi AP stopped after temporarily activity"); + if (GetAPClientsNumber() > 0) + TempAPCounter = WIFI_AP_ONBOOT_TIME; + else + { + WiFiStopAP(); + ESP_LOGI(TAG, "WiFi AP stopped after temporarily activity"); + } } } @@ -610,3 +615,18 @@ void WiFiScan(void) isScanExecuting = true; xTaskCreate(wifi_scan, "ScanWiFiTask", 1024 * 4, (void*) 0, 3, NULL); } + +int GetAPClientsNumber() +{ + wifi_sta_list_t clients; + wifi_mode_t mode; + esp_wifi_get_mode(&mode); + if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_STA) + return -1; + if (esp_wifi_ap_get_sta_list(&clients) == ESP_OK) + { + return clients.num; + } + return -1; +} +