From 4de4e07d99e77eba88569c6db3a3bbc706944530 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Wed, 19 Feb 2020 08:02:58 -0500 Subject: [PATCH] messaging subsystem wip --- .cproject | 168 ++++++++++++------ .../com.googlecode.cppcheclipse.core.prefs | 2 + .settings/language.settings.xml | 6 +- .settings/org.eclipse.cdt.core.prefs | 9 +- Makefile | 5 +- components/cmd_i2c/CMakeLists.txt | 2 +- components/cmd_i2c/component.mk | 7 +- components/cmd_nvs/component.mk | 3 - components/cmd_system/component.mk | 2 - components/display/CMakeLists.txt | 2 +- components/driver_bt/CMakeLists.txt | 2 +- components/driver_bt/component.mk | 4 - components/raop/component.mk | 4 +- components/services/CMakeLists.txt | 4 +- components/services/messaging.c | 125 +++++++++++++ components/services/messaging.h | 25 +++ components/services/services.c | 3 +- components/squeezelite-ota/component.mk | 3 - components/squeezelite-ota/squeezelite-ota.c | 37 ++-- components/squeezelite/component.mk | 28 +-- components/telnet/CMakeLists.txt | 2 +- components/telnet/component.mk | 4 +- components/wifi-manager/code.js | 15 +- components/wifi-manager/component.mk | 8 +- .../wifi-manager/http_server_handlers.c | 28 ++- .../wifi-manager/http_server_handlers.h | 2 + .../wifi-manager/wifi_manager_http_server.c | 6 +- main/component.mk | 6 +- 28 files changed, 369 insertions(+), 143 deletions(-) create mode 100644 .settings/com.googlecode.cppcheclipse.core.prefs create mode 100644 components/services/messaging.c create mode 100644 components/services/messaging.h diff --git a/.cproject b/.cproject index e4d56c6d..be379242 100644 --- a/.cproject +++ b/.cproject @@ -143,7 +143,7 @@ - + @@ -203,7 +203,7 @@ - + @@ -225,37 +225,57 @@ - + - @@ -299,7 +311,7 @@ - + @@ -321,37 +333,43 @@ - + - @@ -495,6 +505,24 @@ + + + + + + + + + + + + + + + + + + @@ -507,6 +535,12 @@ + + + + + + @@ -543,6 +577,12 @@ + + + + + + @@ -555,17 +595,35 @@ + + + + + + + + + + + + + + + + + + diff --git a/.settings/com.googlecode.cppcheclipse.core.prefs b/.settings/com.googlecode.cppcheclipse.core.prefs new file mode 100644 index 00000000..b21d0f0a --- /dev/null +++ b/.settings/com.googlecode.cppcheclipse.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +suppressions=rO0ABXNyAAxqYXZhLmlvLkZpbGUELaRFDg3k/wMAAUwABHBhdGh0ABJMamF2YS9sYW5nL1N0cmluZzt4cHQAJWNvbXBvbmVudHNcd2lmaS1tYW5hZ2VyXGh0dHBfc2VydmVyLmN3AgBceA\=\=;comparePointers;395\!rO0ABXNyAAxqYXZhLmlvLkZpbGUELaRFDg3k/wMAAUwABHBhdGh0ABJMamF2YS9sYW5nL1N0cmluZzt4cHQAE21haW5cZXNwX2FwcF9tYWluLmN3AgBceA\=\=;comparePointers;176\!rO0ABXNyAAxqYXZhLmlvLkZpbGUELaRFDg3k/wMAAUwABHBhdGh0ABJMamF2YS9sYW5nL1N0cmluZzt4cHQAJ2NvbXBvbmVudHNcdGVsbmV0XGxpYnRlbG5ldFxsaWJ0ZWxuZXQuY3cCAFx4;va_list_usedBeforeStarted;2147483647\! diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index cbee2f3c..5c545a49 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -57,9 +57,9 @@ - + - + @@ -81,7 +81,7 @@ - + diff --git a/.settings/org.eclipse.cdt.core.prefs b/.settings/org.eclipse.cdt.core.prefs index d5d56fd7..7a6fc1e3 100644 --- a/.settings/org.eclipse.cdt.core.prefs +++ b/.settings/org.eclipse.cdt.core.prefs @@ -32,7 +32,7 @@ environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.16039962 environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/EXTRA_CPPFLAGS/value=-DRECOVERY_APPLICATION\=0 environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/IDF_PATH/delimiter=\: environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/IDF_PATH/operation=replace -environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/IDF_PATH/value=C\:/msys32/opt/esp-idf +environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/IDF_PATH/value=c\:/msys32/opt/esp-idf environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/PATH/delimiter=\: environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/PATH/operation=replace environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291.395881736/PATH/value=C\:\\msys32\\usr\\bin;C\:\\msys32\\mingw32\\bin;C\:\\msys32\\opt\\xtensa-esp32-elf\\bin\\ @@ -52,10 +52,10 @@ environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.16039962 environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/EXTRA_CPPFLAGS/value=-DRECOVERY_APPLICATION\=1 environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/IDF_PATH/delimiter=\: environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/IDF_PATH/operation=replace -environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/IDF_PATH/value=C\:/msys32/opt/esp-idf +environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/IDF_PATH/value=c\:/msys32/opt/esp-idf environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/PATH/delimiter=\: environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/PATH/operation=replace -environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/PATH/value=C\:\\msys32\\opt\\openocd-esp32\\bin;c\:\\msys32\\opt\\xtensa-esp32-elf\\bin\\;c\:\\msys32\\mingw32\\bin;C\:\\msys32\\usr\\bin;c\:\\Python27;C\:\\msys32\\usr\\bin\\vendor_perl;C\:\\msys32\\usr\\bin\\core_perl +environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/PATH/value=c\:\\msys32\\opt\\openocd-esp32\\bin;c\:\\msys32\\opt\\xtensa-esp32-elf\\bin\\;c\:\\msys32\\mingw32\\bin;C\:\\msys32\\usr\\bin;c\:\\Python27;C\:\\msys32\\usr\\bin\\vendor_perl;C\:\\msys32\\usr\\bin\\core_perl environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/PROJECT_NAME/delimiter=\: environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/PROJECT_NAME/operation=replace environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348.1603996291/PROJECT_NAME/value=recovery @@ -102,6 +102,9 @@ environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/PROJECT_ environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/PROJECT_VER/delimiter=\: environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/PROJECT_VER/operation=append environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/PROJECT_VER/value=custom +environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/WIN_IDF_PATH/delimiter=; +environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/WIN_IDF_PATH/operation=append +environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/WIN_IDF_PATH/value=c\:/msys32/opt/esp-idf environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/append=true environment/project/cdt.managedbuild.toolchain.gnu.cross.base.293933348/appendContributed=true environment/project/cdt.managedbuild.toolchain.gnu.cygwin.base.58932738.859326707/BATCH_BUILD/delimiter=; diff --git a/Makefile b/Makefile index f209bfed..654bfc47 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,10 @@ #recovery: CPPFLAGS+=-DRECOVERY_APPLICATION=1 PROJECT_NAME?=squeezelite +CPPFLAGS+= -Wno-error=maybe-uninitialized \ + -I$(PROJECT_PATH)/main + include $(IDF_PATH)/make/project.mk # for future gcc version, this could be needed: CPPFLAGS+= -Wno-error=format-overflow -Wno-error=stringop-truncation -CPPFLAGS+= -Wno-error=maybe-uninitialized + diff --git a/components/cmd_i2c/CMakeLists.txt b/components/cmd_i2c/CMakeLists.txt index 421e8fa7..2282da00 100644 --- a/components/cmd_i2c/CMakeLists.txt +++ b/components/cmd_i2c/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRCS "cmd_i2ctools.c" - INCLUDE_DIRS "." "../tools/" + INCLUDE_DIRS "." "$(PROJECT_PATH)/components/tools/" REQUIRES esp_common PRIV_REQUIRES freertos esp32 spi_flash newlib log console pthread ) diff --git a/components/cmd_i2c/component.mk b/components/cmd_i2c/component.mk index 0b379962..d9c79ce7 100644 --- a/components/cmd_i2c/component.mk +++ b/components/cmd_i2c/component.mk @@ -1,9 +1,6 @@ # # Main Makefile. This is basically the same as a component makefile. # -CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO \ - -I$(COMPONENT_PATH)/../tools COMPONENT_ADD_INCLUDEDIRS := . -COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools -COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../squeezelite-ota -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/ +CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO +COMPONENT_SRCDIRS := . \ No newline at end of file diff --git a/components/cmd_nvs/component.mk b/components/cmd_nvs/component.mk index a71c86c5..e0e9f4c1 100644 --- a/components/cmd_nvs/component.mk +++ b/components/cmd_nvs/component.mk @@ -8,6 +8,3 @@ # COMPONENT_ADD_INCLUDEDIRS := . -CFLAGS += -I$(COMPONENT_PATH)/../squeezelite-ota -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/components/tools/ -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main diff --git a/components/cmd_system/component.mk b/components/cmd_system/component.mk index 93ab014b..e0e9f4c1 100644 --- a/components/cmd_system/component.mk +++ b/components/cmd_system/component.mk @@ -8,5 +8,3 @@ # COMPONENT_ADD_INCLUDEDIRS := . -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/ -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/components/tools/ \ No newline at end of file diff --git a/components/display/CMakeLists.txt b/components/display/CMakeLists.txt index 4672e0c6..0f85bb68 100644 --- a/components/display/CMakeLists.txt +++ b/components/display/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRCS "led.c" "audio_controls.c" "buttons.c" "services.c" "monitor.c"INCLUDE_DIRS - INCLUDE_DIRS . ../tools/ + INCLUDE_DIRS . $(PROJECT_PATH)/components/tools/ ) diff --git a/components/driver_bt/CMakeLists.txt b/components/driver_bt/CMakeLists.txt index 7c31465a..06d03979 100644 --- a/components/driver_bt/CMakeLists.txt +++ b/components/driver_bt/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRCS "bt_app_core.c" "bt_app_sink.c" "bt_app_source.c" - INCLUDE_DIRS "." "../tools/" + INCLUDE_DIRS "." "$($(PROJECT_PATH)/components/)/components/tools/" REQUIRES esp_common PRIV_REQUIRES freertos bt io nvs_flash esp32 spi_flash newlib log console pthread ) diff --git a/components/driver_bt/component.mk b/components/driver_bt/component.mk index 625b10be..e0e9f4c1 100644 --- a/components/driver_bt/component.mk +++ b/components/driver_bt/component.mk @@ -7,8 +7,4 @@ # please read the SDK documents if you need to do this. # -CFLAGS += -I$(COMPONENT_PATH)/../tools \ - -I$(COMPONENT_PATH)/../config COMPONENT_ADD_INCLUDEDIRS := . -COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/ diff --git a/components/raop/component.mk b/components/raop/component.mk index e06d07e7..55f53715 100644 --- a/components/raop/component.mk +++ b/components/raop/component.mk @@ -8,6 +8,6 @@ # CFLAGS += -fstack-usage\ - -I$(COMPONENT_PATH)/../tools \ - -I$(COMPONENT_PATH)/../codecs/inc/alac \ + -I$(PROJECT_PATH)/components/tools \ + -I$(PROJECT_PATH)/components/codecs/inc/alac \ -I$(PROJECT_PATH)/main/ diff --git a/components/services/CMakeLists.txt b/components/services/CMakeLists.txt index 4672e0c6..e1ffc4b9 100644 --- a/components/services/CMakeLists.txt +++ b/components/services/CMakeLists.txt @@ -1,5 +1,5 @@ -idf_component_register(SRCS "led.c" "audio_controls.c" "buttons.c" "services.c" "monitor.c"INCLUDE_DIRS - INCLUDE_DIRS . ../tools/ +idf_component_register(SRCS "led.c" "audio_controls.c" "buttons.c" "services.c" "monitor.c" "messaging.c" + INCLUDE_DIRS . $(PROJECT_PATH)/components/tools/ ) diff --git a/components/services/messaging.c b/components/services/messaging.c new file mode 100644 index 00000000..8144b1eb --- /dev/null +++ b/components/services/messaging.c @@ -0,0 +1,125 @@ + /** + * + */ +#include // Required for libtelnet.h +#include +#include "stdbool.h" +#include +#include +#include +#include +#include "esp_app_trace.h" +#include "esp_attr.h" +#include "config.h" +#include "nvs_utilities.h" +#include "platform_esp32.h" +#include "messaging.h" +/************************************ + * Globals + */ + +const static char tag[] = "messaging"; + +static messaging_list_t top; +RingbufHandle_t messaging_create_ring_buffer(uint8_t max_count){ + RingbufHandle_t buf_handle = NULL; + StaticRingbuffer_t *buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM); + if (buffer_struct != NULL) { + size_t buf_size = (size_t )(sizeof(single_message_t)+8)*(size_t )(max_count>0?max_count:5); // no-split buffer requires an additional 8 bytes + uint8_t *buffer_storage = (uint8_t *)heap_caps_malloc(buf_size, MALLOC_CAP_SPIRAM); + if (buffer_storage== NULL) { + ESP_LOGE(tag,"Failed to allocate memory for messaging ring buffer !"); + } + else { + buf_handle = xRingbufferCreateStatic(buf_size, RINGBUF_TYPE_NOSPLIT, buffer_storage, buffer_struct); + if (buf_handle == NULL) { + ESP_LOGE(tag,"Failed to create messaging ring buffer !"); + } + } + } + else { + ESP_LOGE(tag,"Failed to create ring buffer for messaging!"); + } + return buf_handle; +} + +RingbufHandle_t messaging_register_subscriber(uint8_t max_count, char * name){ + messaging_list_t * cur=⊤ + while(cur->next){ + cur = cur->next; + } + cur->next=heap_caps_malloc(sizeof(messaging_list_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + if(!cur->next){ + ESP_LOGE(tag,"Failed to allocate messaging subscriber entry!"); + return NULL; + } + memset(cur->next,0x00,sizeof(messaging_list_t)); + cur = cur->next; + cur->buf_handle = messaging_create_ring_buffer(max_count); + strncpy(cur->subscriber_name,name,sizeof(cur->subscriber_name)); + + return cur->buf_handle; +} +esp_err_t messaging_service_init(){ + top.buf_handle = messaging_create_ring_buffer(max_count); + if(!top.buf_handle){ + ESP_LOGE(tag, "messaging service init failed."); + } + strncpy(top.subscriber_name,"messaging"); + return (top.buf_handle!=NULL); +} + + +cJSON * messaging_retrieve_messages(RingbufHandle_t buf_handle){ + single_message_t * message=NULL; + cJSON * json_messages=cJSON_CreateArray(); + cJSON * json_message=NULL; + size_t item_size; + UBaseType_t uxItemsWaiting; + vRingbufferGetInfo(buf_handle, NULL, NULL, NULL, NULL, &uxItemsWaiting); + if(uxItemsWaiting>0){ + message = (single_message_t *)xRingbufferReceive(buf_handle, &item_size, pdMS_TO_TICKS(50)); + //Check received data + if (message== NULL) { + ESP_LOGE(tag,"Failed to receive message from buffer!"); + } + else { + json_message = cJSON_CreateObject(); + cJSON_AddStringToObject(json_message, "message", message->message); + cJSON_AddStringToObject(json_message, "type", message->message); + cJSON_AddNumberToObject(json_message,"sent_time",message->sent_time); + cJSON_AddNumberToObject(json_message,"current_time",esp_timer_get_time() / 1000); + cJSON_AddItemToArray(json_messages,json_message); + vRingbufferReturnItem(buf_handle, (void *)message); + } + } + return json_messages; +} + +void messaging_release_message(RingbufHandle_t buf_handle, single_message_t * message){ + +} +esp_err_t messageing_post_to_queue(messaging_list_t * subscriber, single_message_t * message){ + UBaseType_t res = xRingbufferSend(subscriber->buf_handle, message, sizeof(message), pdMS_TO_TICKS(1000)); + if (res != pdTRUE) { + ESP_LOGE(tag,"Failed to post message to subscriber %s",subscriber->subscriber_name); + return ESP_FAIL; + } + return ESP_OK; +} +void messaging_post_message(messaging_types type, char *fmt, ...){ + single_message_t message={}; + messaging_list_t * cur=⊤ + va_list va; + va_start(va, fmt); + vsnprintf(message.message, sizeof(message.message), fmt, va); + va_end(va); + message.type = type; + message.sent_time = esp_timer_get_time() / 1000; + while(cur->next){ + messageing_post_to_queue(cur, &message); + cur = cur->next; + } + return; + +} diff --git a/components/services/messaging.h b/components/services/messaging.h new file mode 100644 index 00000000..54fb5109 --- /dev/null +++ b/components/services/messaging.h @@ -0,0 +1,25 @@ +#include "sdkconfig.h" +#include "freertos/ringbuf.h" +#include "cJSON.h" +#pragma once +typedef enum { + MESSAGING_INFO, + MESSAGING_WARNING, + MESSAGING_ERROR +} messaging_types; +typedef struct { + void * next; + char subscriber_name[21]; + RingbufHandle_t buf_handle; +} messaging_list_t; + +typedef struct { + time_t sent_time; + messaging_types type; + char message[101]; +} single_message_t; + +cJSON * messaging_retrieve_messages(RingbufHandle_t buf_handle); +RingbufHandle_t messaging_register_subscriber(uint8_t max_count, char * name); + +void messaging_post_message(messaging_types type, char * fmt, ...); diff --git a/components/services/services.c b/components/services/services.c index 36e53b60..193a2b22 100644 --- a/components/services/services.c +++ b/components/services/services.c @@ -51,6 +51,7 @@ void set_power_gpio(int gpio, char *value) { * */ void services_init(void) { + messaging_service_init(); gpio_install_isr_service(0); #ifdef CONFIG_I2C_LOCKED @@ -94,4 +95,4 @@ void services_init(void) { led_svc_init(); battery_svc_init(); monitor_svc_init(); -} \ No newline at end of file +} diff --git a/components/squeezelite-ota/component.mk b/components/squeezelite-ota/component.mk index 7f4b4f39..d0d6918e 100644 --- a/components/squeezelite-ota/component.mk +++ b/components/squeezelite-ota/component.mk @@ -5,9 +5,6 @@ # todo: add support for https COMPONENT_ADD_INCLUDEDIRS := . -COMPONENT_ADD_INCLUDEDIRS += include -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/ -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/components/tools CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO -DCONFIG_OTA_ALLOW_HTTP=1 diff --git a/components/squeezelite-ota/squeezelite-ota.c b/components/squeezelite-ota/squeezelite-ota.c index 7488d355..62b91d7c 100644 --- a/components/squeezelite-ota/squeezelite-ota.c +++ b/components/squeezelite-ota/squeezelite-ota.c @@ -28,6 +28,7 @@ #include "esp_flash_encrypt.h" #include "esp_spi_flash.h" #include "sdkconfig.h" +#include "messaging.h" #include "esp_ota_ops.h" extern const char * get_certificate(); @@ -75,8 +76,6 @@ static struct { struct timeval tv; static esp_http_client_config_t ota_config; -extern void wifi_manager_refresh_ota_json(); - void _printMemStats(){ ESP_LOGD(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)", heap_caps_get_free_size(MALLOC_CAP_INTERNAL), @@ -84,22 +83,12 @@ void _printMemStats(){ heap_caps_get_free_size(MALLOC_CAP_SPIRAM), heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM)); } -void triggerStatusJsonRefresh(bool bDelay,const char * status, ...){ +void sendMessaging(messaging_types type,char * fmt, ...){ va_list args; - va_start(args, status); - vsnprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,status, args); + va_start(args, fmt); + messaging_post_message(type, fmt, args); va_end(args); _printMemStats(); - wifi_manager_refresh_ota_json(); - if(bDelay){ - ESP_LOGD(TAG,"Holding task..."); - vTaskDelay(200 / portTICK_PERIOD_MS); // wait here for a short amount of time. This will help with refreshing the UI status - ESP_LOGD(TAG,"Done holding task..."); - } - else { - ESP_LOGI(TAG,"%s",ota_status.status_text); - taskYIELD(); - } } const char * ota_get_status(){ if(!ota_status.bInitialized) @@ -151,7 +140,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) case HTTP_EVENT_ON_CONNECTED: ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); - if(ota_status.bOTAStarted) triggerStatusJsonRefresh(true,"Installing..."); + if(ota_status.bOTAStarted) sendMessaging(MESSAGING_INFO,"Installing..."); ota_status.total_image_len=0; ota_status.actual_image_len=0; ota_status.lastpct=0; @@ -193,7 +182,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) esp_err_t init_config(ota_thread_parms_t * p_ota_thread_parms){ memset(&ota_config, 0x00, sizeof(ota_config)); ota_status.bInitialized = true; - triggerStatusJsonRefresh(true,"Initializing..."); + sendMessaging(MESSAGING_INFO,"Initializing..."); ota_status.ota_type= OTA_TYPE_INVALID; if(p_ota_thread_parms->url !=NULL && strlen(p_ota_thread_parms->url)>0 ){ ota_status.ota_type= OTA_TYPE_HTTP; @@ -294,7 +283,7 @@ esp_err_t _erase_last_boot_app_partition(esp_partition_t *ota_partition) err=esp_partition_erase_range(ota_partition, i*single_pass_size, single_pass_size); if(err!=ESP_OK) return err; if(i%2) { - triggerStatusJsonRefresh(false,"Erasing flash (%u/%u)",i,num_passes); + sendMessaging(MESSAGING_INFO,"Erasing flash (%u/%u)",i,num_passes); } vTaskDelay(200/ portTICK_PERIOD_MS); // wait here for a short amount of time. This will help with reducing WDT errors } @@ -302,7 +291,7 @@ esp_err_t _erase_last_boot_app_partition(esp_partition_t *ota_partition) err=esp_partition_erase_range(ota_partition, ota_partition->size-remain_size, remain_size); if(err!=ESP_OK) return err; } - triggerStatusJsonRefresh(true,"Erasing flash complete."); + sendMessaging(MESSAGING_INFO,"Erasing flash complete."); taskYIELD(); return ESP_OK; } @@ -394,7 +383,7 @@ void ota_task_cleanup(const char * message, ...){ if(message!=NULL){ va_list args; va_start(args, message); - triggerStatusJsonRefresh(true,message, args); + sendMessaging(MESSAGING_ERROR,message, args); va_end(args); ESP_LOGE(TAG, "%s",ota_status.status_text); } @@ -438,7 +427,7 @@ void ota_task(void *pvParameter) /* Locate and erase ota application partition */ ESP_LOGW(TAG,"**************** Expecting WATCHDOG errors below during flash erase. This is OK and not to worry about **************** "); - triggerStatusJsonRefresh(true,"Erasing OTA partition"); + sendMessaging(MESSAGING_INFO,"Erasing OTA partition"); esp_partition_t *ota_partition = _get_ota_partition(ESP_PARTITION_SUBTYPE_APP_OTA_0); if(ota_partition == NULL){ ESP_LOGE(TAG,"Unable to locate OTA application partition. "); @@ -460,7 +449,7 @@ void ota_task(void *pvParameter) _printMemStats(); ota_status.bOTAStarted = true; - triggerStatusJsonRefresh(true,"Starting OTA..."); + sendMessaging(MESSAGING_INFO,"Starting OTA..."); if (ota_status.ota_type == OTA_TYPE_HTTP){ ota_http_client = esp_http_client_init(&ota_config); if (ota_http_client == NULL) { @@ -554,7 +543,7 @@ void ota_task(void *pvParameter) gettimeofday(&tv, NULL); uint32_t elapsed_ms= (tv.tv_sec-ota_status.OTA_start.tv_sec )*1000+(tv.tv_usec-ota_status.OTA_start.tv_usec)/1000; ESP_LOGI(TAG,"OTA progress : %d/%d (%d pct), %d KB/s", ota_status.actual_image_len, ota_status.total_image_len, ota_status.newpct, elapsed_ms>0?ota_status.actual_image_len*1000/elapsed_ms/1024:0); - triggerStatusJsonRefresh(true,ota_status.ota_type == OTA_TYPE_HTTP?"Downloading & writing update.":"Writing binary file."); + sendMessaging(MESSAGING_INFO,ota_status.ota_type == OTA_TYPE_HTTP?"Downloading & writing update.":"Writing binary file."); ota_status.lastpct=ota_status.newpct; } taskYIELD(); @@ -581,7 +570,7 @@ void ota_task(void *pvParameter) err = esp_ota_set_boot_partition(ota_partition); if (err == ESP_OK) { ESP_LOGI(TAG,"OTA Process completed successfully!"); - triggerStatusJsonRefresh(true,"Success!"); + sendMessaging(MESSAGING_INFO,"Success!"); vTaskDelay(1500/ portTICK_PERIOD_MS); // wait here to give the UI a chance to refresh esp_restart(); } else { diff --git a/components/squeezelite/component.mk b/components/squeezelite/component.mk index c30f0ffa..8699201b 100644 --- a/components/squeezelite/component.mk +++ b/components/squeezelite/component.mk @@ -3,21 +3,21 @@ # # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) CFLAGS += -O3 -DLINKALL -DLOOPBACK -DNO_FAAD -DRESAMPLE16 -DEMBEDDED -DTREMOR_ONLY -DBYTES_PER_FRAME=4 \ - -I$(COMPONENT_PATH)/../codecs/inc \ - -I$(COMPONENT_PATH)/../codecs/inc/mad \ - -I$(COMPONENT_PATH)/../codecs/inc/alac \ - -I$(COMPONENT_PATH)/../codecs/inc/helix-aac \ - -I$(COMPONENT_PATH)/../codecs/inc/vorbis \ - -I$(COMPONENT_PATH)/../codecs/inc/soxr \ - -I$(COMPONENT_PATH)/../codecs/inc/resample16 \ - -I$(COMPONENT_PATH)/../tools \ - -I$(COMPONENT_PATH)/../codecs/inc/opus \ - -I$(COMPONENT_PATH)/../codecs/inc/opusfile \ - -I$(COMPONENT_PATH)/../driver_bt \ - -I$(COMPONENT_PATH)/../raop \ - -I$(COMPONENT_PATH)/../services + -I$(PROJECT_PATH)/components/codecs/inc \ + -I$(PROJECT_PATH)/components/codecs/inc/mad \ + -I$(PROJECT_PATH)/components/codecs/inc/alac \ + -I$(PROJECT_PATH)/components/codecs/inc/helix-aac \ + -I$(PROJECT_PATH)/components/codecs/inc/vorbis \ + -I$(PROJECT_PATH)/components/codecs/inc/soxr \ + -I$(PROJECT_PATH)/components/codecs/inc/resample16 \ + -I$(PROJECT_PATH)/components/tools \ + -I$(PROJECT_PATH)/components/codecs/inc/opus \ + -I$(PROJECT_PATH)/components/codecs/inc/opusfile \ + -I$(PROJECT_PATH)/components/driver_bt \ + -I$(PROJECT_PATH)/components/raop \ + -I$(PROJECT_PATH)/components/services -# -I$(COMPONENT_PATH)/../codecs/inc/faad2 +# -I$(PROJECT_PATH)/components/codecs/inc/faad2 COMPONENT_SRCDIRS := . tas57xx a1s external COMPONENT_ADD_INCLUDEDIRS := . ./tas57xx ./a1s diff --git a/components/telnet/CMakeLists.txt b/components/telnet/CMakeLists.txt index 36bcf9cb..16e0f045 100644 --- a/components/telnet/CMakeLists.txt +++ b/components/telnet/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRCS "telnet.c" INCLUDE_DIRS . - INCLUDE_DIRS . ../tools/ + INCLUDE_DIRS . $(PROJECT_PATH)/components/tools/ ) diff --git a/components/telnet/component.mk b/components/telnet/component.mk index 6c466cfb..2fd9a49d 100644 --- a/components/telnet/component.mk +++ b/components/telnet/component.mk @@ -10,6 +10,4 @@ COMPONENT_SRCDIRS := . COMPONENT_SRCDIRS += ./libtelnet COMPONENT_ADD_INCLUDEDIRS := . -COMPONENT_ADD_INCLUDEDIRS += ./libtelnet -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/ - \ No newline at end of file +COMPONENT_PRIV_INCLUDEDIRS += ./libtelnet \ No newline at end of file diff --git a/components/wifi-manager/code.js b/components/wifi-manager/code.js index d3d10638..c2045cc8 100644 --- a/components/wifi-manager/code.js +++ b/components/wifi-manager/code.js @@ -646,12 +646,24 @@ function refreshAPHTML(data){ $( "#wifi-list" ).html(h) } - +function getMessages() { + $.getJSON("/messages.json", function(data) { + data.forEach(function(msg) { + }); + + }) + .fail(function(xhr, ajaxOptions, thrownError) { + console.log(xhr.status); + console.log(thrownError); + if (thrownError != '') showMessage(thrownError, 'ERROR'); + }); +} function checkStatus(){ RepeatCheckStatusInterval(); if (!enableStatusTimer) return; if (blockAjax) return; blockAjax = true; + getMessages(); $.getJSON( "/status.json", function( data ) { if (data.hasOwnProperty('ssid') && data['ssid'] != ""){ if (data["ssid"] === selectedSSID){ @@ -881,6 +893,7 @@ function getConfig() { }); } + function showMessage(message, severity) { if (severity == 'INFO') { $('#message').css('background', '#6af'); diff --git a/components/wifi-manager/component.mk b/components/wifi-manager/component.mk index 9b6972bb..a11cccff 100644 --- a/components/wifi-manager/component.mk +++ b/components/wifi-manager/component.mk @@ -7,13 +7,7 @@ # please read the SDK documents if you need to do this. # COMPONENT_EMBED_FILES := style.css code.js index.html bootstrap.min.css.gz jquery.min.js.gz popper.min.js.gz bootstrap.min.js.gz - -CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO \ - -I$(COMPONENT_PATH)/../tools COMPONENT_ADD_INCLUDEDIRS := . -COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools -COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../squeezelite-ota -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/ - +CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO diff --git a/components/wifi-manager/http_server_handlers.c b/components/wifi-manager/http_server_handlers.c index b7b305ea..5a095e7b 100644 --- a/components/wifi-manager/http_server_handlers.c +++ b/components/wifi-manager/http_server_handlers.c @@ -49,6 +49,7 @@ function to process requests, decode URLs, serve files, etc. etc. #include "sys/param.h" #include "esp_vfs.h" #include "lwip/ip_addr.h" +#include "messaging.h" #define HTTP_STACK_SIZE (5*1024) #define FREE_AND_NULL(p) if(p!=NULL){ free(p); p=NULL;} @@ -59,7 +60,7 @@ static const char TAG[] = "httpd_handlers"; /* @brief task handle for the http server */ SemaphoreHandle_t http_server_config_mutex = NULL; - +extern RingbufHandle_t messaging; #define AUTH_TOKEN_SIZE 50 typedef struct session_context { char * auth_token; @@ -1039,6 +1040,31 @@ esp_err_t redirect_processor(httpd_req_t *req, httpd_err_code_t error){ esp_err_t redirect_ev_handler(httpd_req_t *req){ return redirect_processor(req,0); } + +esp_err_t messages_get_handler(httpd_req_t *req){ + ESP_LOGD_LOC(TAG, "serving [%s]", req->uri); + if(!is_user_authenticated(req)){ + // todo: redirect to login page + // return ESP_OK; + } + esp_err_t err = set_content_type_from_req(req); + if(err != ESP_OK){ + return err; + } + + cJSON * json_messages= messaging_retrieve_messages(messaging); + if(json_messages!=NULL){ + char * json_text= cJSON_Print(json_messages); + httpd_resp_send(req, (const char *)json_text, strlen(json_text)); + cJSON_free(json_messages); + free(json_text); + } + else { + httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Unable to retrieve messages"); + } + return ESP_OK; +} + esp_err_t status_get_handler(httpd_req_t *req){ ESP_LOGD_LOC(TAG, "serving [%s]", req->uri); if(!is_user_authenticated(req)){ diff --git a/components/wifi-manager/http_server_handlers.h b/components/wifi-manager/http_server_handlers.h index d672933b..390394a1 100644 --- a/components/wifi-manager/http_server_handlers.h +++ b/components/wifi-manager/http_server_handlers.h @@ -92,6 +92,8 @@ esp_err_t recovery_post_handler(httpd_req_t *req); esp_err_t flash_post_handler(httpd_req_t *req); #endif esp_err_t status_get_handler(httpd_req_t *req); +esp_err_t messages_get_handler(httpd_req_t *req); + esp_err_t ap_scan_handler(httpd_req_t *req); esp_err_t redirect_ev_handler(httpd_req_t *req); esp_err_t redirect_200_ev_handler(httpd_req_t *req); diff --git a/components/wifi-manager/wifi_manager_http_server.c b/components/wifi-manager/wifi_manager_http_server.c index 523352eb..2e98a7b9 100644 --- a/components/wifi-manager/wifi_manager_http_server.c +++ b/components/wifi-manager/wifi_manager_http_server.c @@ -29,11 +29,12 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "config.h" - +#include "messaging.h" static const char TAG[] = "http_server"; static httpd_handle_t _server = NULL; rest_server_context_t *rest_context = NULL; +RingbufHandle_t messaging=NULL; void register_common_handlers(httpd_handle_t server){ httpd_uri_t res_get = { .uri = "/res/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context }; @@ -52,6 +53,8 @@ void register_regular_handlers(httpd_handle_t server){ httpd_register_uri_handler(server, &config_get); httpd_uri_t status_get = { .uri = "/status.json", .method = HTTP_GET, .handler = status_get_handler, .user_ctx = rest_context }; httpd_register_uri_handler(server, &status_get); + httpd_uri_t messages_get = { .uri = "/messages.json", .method = HTTP_GET, .handler = messages_get_handler, .user_ctx = rest_context }; + httpd_register_uri_handler(server, &messages_get); httpd_uri_t config_post = { .uri = "/config.json", .method = HTTP_POST, .handler = config_post_handler, .user_ctx = rest_context }; httpd_register_uri_handler(server, &config_post); @@ -112,6 +115,7 @@ void register_regular_handlers(httpd_handle_t server){ esp_err_t http_server_start() { ESP_LOGI(TAG, "Initializing HTTP Server"); + messaging = messaging_register_subscriber(10, "http_server"); rest_context = calloc(1, sizeof(rest_server_context_t)); if(rest_context==NULL){ ESP_LOGE(TAG,"No memory for http context"); diff --git a/main/component.mk b/main/component.mk index dc5314b1..8edcf630 100644 --- a/main/component.mk +++ b/main/component.mk @@ -7,8 +7,6 @@ # please read the SDK documents if you need to do this. # CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO -DMODEL_NAME=SqueezeESP32 -COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools \ - $(COMPONENT_PATH)/../config -COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/components/tools/ LDFLAGS += -s -COMPONENT_EMBED_TXTFILES := ${PROJECT_PATH}/server_certs/github.pem \ No newline at end of file +COMPONENT_EMBED_TXTFILES := ${PROJECT_PATH}/server_certs/github.pem +COMPONENT_ADD_INCLUDEDIRS := . \ No newline at end of file