mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-08 12:37:01 +03:00
httpd implementation - wip
This commit is contained in:
@@ -19,56 +19,107 @@
|
||||
*/
|
||||
|
||||
const static char tag[] = "messaging";
|
||||
|
||||
typedef struct {
|
||||
struct messaging_list_t * next;
|
||||
char * subscriber_name;
|
||||
size_t max_count;
|
||||
RingbufHandle_t buf_handle;
|
||||
} messaging_list_t;
|
||||
static messaging_list_t top;
|
||||
|
||||
|
||||
messaging_list_t * get_struct_ptr(messaging_handle_t handle){
|
||||
return (messaging_list_t *)handle;
|
||||
}
|
||||
messaging_handle_t get_handle_ptr(messaging_list_t * handle){
|
||||
return (messaging_handle_t )handle;
|
||||
}
|
||||
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);
|
||||
StaticRingbuffer_t *buffer_struct = malloc(sizeof(StaticRingbuffer_t));
|
||||
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);
|
||||
uint8_t *buffer_storage = (uint8_t *)heap_caps_malloc(buf_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if (buffer_storage== NULL) {
|
||||
ESP_LOGE(tag,"Failed to allocate memory for messaging ring buffer !");
|
||||
ESP_LOGE(tag,"buff alloc failed");
|
||||
}
|
||||
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!");
|
||||
ESP_LOGE(tag,"ringbuf alloc failed");
|
||||
}
|
||||
return buf_handle;
|
||||
}
|
||||
void messaging_fill_messages(messaging_list_t * target_subscriber){
|
||||
single_message_t * message=NULL;
|
||||
UBaseType_t uxItemsWaiting;
|
||||
|
||||
RingbufHandle_t messaging_register_subscriber(uint8_t max_count, char * name){
|
||||
vRingbufferGetInfo(top.buf_handle, NULL, NULL, NULL, NULL, &uxItemsWaiting);
|
||||
for(size_t i=0;i<uxItemsWaiting;i++){
|
||||
message= messaging_retrieve_message(top.buf_handle);
|
||||
if(message){
|
||||
//re-post to original queue so it is available to future subscribers
|
||||
messaging_post_to_queue(get_handle_ptr(&top), message, sizeof(single_message_t));
|
||||
// post to new subscriber
|
||||
messaging_post_to_queue(get_handle_ptr(target_subscriber) , message, sizeof(single_message_t));
|
||||
FREE_AND_NULL(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
messaging_handle_t messaging_register_subscriber(uint8_t max_count, char * name){
|
||||
messaging_list_t * cur=⊤
|
||||
while(cur->next){
|
||||
cur = cur->next;
|
||||
cur = get_struct_ptr(cur->next);
|
||||
}
|
||||
cur->next=heap_caps_malloc(sizeof(messaging_list_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
cur->next=heap_caps_malloc(sizeof(messaging_list_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if(!cur->next){
|
||||
ESP_LOGE(tag,"Failed to allocate messaging subscriber entry!");
|
||||
ESP_LOGE(tag,"subscriber alloc failed");
|
||||
return NULL;
|
||||
}
|
||||
memset(cur->next,0x00,sizeof(messaging_list_t));
|
||||
cur = cur->next;
|
||||
cur = get_struct_ptr(cur->next);
|
||||
cur->max_count=max_count;
|
||||
cur->subscriber_name=strdup(name);
|
||||
cur->buf_handle = messaging_create_ring_buffer(max_count);
|
||||
strncpy(cur->subscriber_name,name,sizeof(cur->subscriber_name));
|
||||
|
||||
if(cur->buf_handle){
|
||||
messaging_fill_messages(cur);
|
||||
}
|
||||
return cur->buf_handle;
|
||||
}
|
||||
esp_err_t messaging_service_init(){
|
||||
void messaging_service_init(){
|
||||
size_t max_count=15;
|
||||
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);
|
||||
else {
|
||||
top.max_count = max_count;
|
||||
top.subscriber_name = strdup("messaging");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const char * messaging_get_type_desc(messaging_types msg_type){
|
||||
switch (msg_type) {
|
||||
CASE_TO_STR(MESSAGING_INFO);
|
||||
CASE_TO_STR(MESSAGING_WARNING);
|
||||
CASE_TO_STR(MESSAGING_ERROR);
|
||||
default:
|
||||
return "Unknown";
|
||||
break;
|
||||
}
|
||||
}
|
||||
const char * messaging_get_class_desc(messaging_classes msg_class){
|
||||
switch (msg_class) {
|
||||
CASE_TO_STR(MESSAGING_CLASS_OTA);
|
||||
CASE_TO_STR(MESSAGING_CLASS_SYSTEM);
|
||||
default:
|
||||
return "Unknown";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON * messaging_retrieve_messages(RingbufHandle_t buf_handle){
|
||||
single_message_t * message=NULL;
|
||||
@@ -81,12 +132,13 @@ cJSON * messaging_retrieve_messages(RingbufHandle_t buf_handle){
|
||||
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!");
|
||||
ESP_LOGE(tag,"received null ptr");
|
||||
}
|
||||
else {
|
||||
json_message = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(json_message, "message", message->message);
|
||||
cJSON_AddStringToObject(json_message, "type", message->message);
|
||||
cJSON_AddStringToObject(json_message, "type", messaging_get_type_desc(message->type));
|
||||
cJSON_AddStringToObject(json_message, "class", messaging_get_class_desc(message->msg_class));
|
||||
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);
|
||||
@@ -95,19 +147,55 @@ cJSON * messaging_retrieve_messages(RingbufHandle_t buf_handle){
|
||||
}
|
||||
return json_messages;
|
||||
}
|
||||
|
||||
void messaging_release_message(RingbufHandle_t buf_handle, single_message_t * message){
|
||||
|
||||
single_message_t * messaging_retrieve_message(RingbufHandle_t buf_handle){
|
||||
single_message_t * message=NULL;
|
||||
single_message_t * message_copy=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));
|
||||
if(item_size!=sizeof(single_message_t)){
|
||||
ESP_LOGE(tag,"Invalid message length!");
|
||||
}
|
||||
else {
|
||||
message_copy = heap_caps_malloc(item_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if(message_copy){
|
||||
memcpy(message_copy,message,item_size);
|
||||
}
|
||||
}
|
||||
vRingbufferReturnItem(buf_handle, (void *)message);
|
||||
}
|
||||
return message_copy;
|
||||
}
|
||||
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));
|
||||
|
||||
esp_err_t messaging_post_to_queue(messaging_handle_t subscriber_handle, single_message_t * message, size_t message_size){
|
||||
UBaseType_t uxItemsWaiting=0;
|
||||
size_t item_size=0;
|
||||
messaging_list_t * subscriber=get_struct_ptr(subscriber_handle);
|
||||
if(!subscriber->buf_handle){
|
||||
ESP_LOGE(tag,"post failed: null buffer for %s", str_or_unknown(subscriber->subscriber_name));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
vRingbufferGetInfo(subscriber->buf_handle,NULL,NULL,NULL,NULL,&uxItemsWaiting);
|
||||
if(uxItemsWaiting>=subscriber->max_count){
|
||||
ESP_LOGW(tag,"messaged dropped for %s",str_or_unknown(subscriber->subscriber_name));
|
||||
single_message_t * dummy = (single_message_t *)xRingbufferReceive(subscriber->buf_handle, &item_size, pdMS_TO_TICKS(50));
|
||||
if (dummy== NULL) {
|
||||
ESP_LOGE(tag,"receive from buffer failed");
|
||||
}
|
||||
else {
|
||||
vRingbufferReturnItem(subscriber->buf_handle, (void *)dummy);
|
||||
}
|
||||
}
|
||||
UBaseType_t res = xRingbufferSend(subscriber->buf_handle, message, message_size, pdMS_TO_TICKS(1000));
|
||||
if (res != pdTRUE) {
|
||||
ESP_LOGE(tag,"Failed to post message to subscriber %s",subscriber->subscriber_name);
|
||||
ESP_LOGE(tag,"post to %s failed",str_or_unknown(subscriber->subscriber_name));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
void messaging_post_message(messaging_types type, char *fmt, ...){
|
||||
void messaging_post_message(messaging_types type,messaging_classes msg_class, char *fmt, ...){
|
||||
single_message_t message={};
|
||||
messaging_list_t * cur=⊤
|
||||
va_list va;
|
||||
@@ -115,10 +203,11 @@ void messaging_post_message(messaging_types type, char *fmt, ...){
|
||||
vsnprintf(message.message, sizeof(message.message), fmt, va);
|
||||
va_end(va);
|
||||
message.type = type;
|
||||
message.msg_class = msg_class;
|
||||
message.sent_time = esp_timer_get_time() / 1000;
|
||||
while(cur->next){
|
||||
messageing_post_to_queue(cur, &message);
|
||||
cur = cur->next;
|
||||
while(cur){
|
||||
messaging_post_to_queue(get_handle_ptr(cur), &message, sizeof(single_message_t));
|
||||
cur = get_struct_ptr(cur->next);
|
||||
}
|
||||
return;
|
||||
|
||||
|
||||
@@ -7,19 +7,24 @@ typedef enum {
|
||||
MESSAGING_WARNING,
|
||||
MESSAGING_ERROR
|
||||
} messaging_types;
|
||||
typedef struct {
|
||||
void * next;
|
||||
char subscriber_name[21];
|
||||
RingbufHandle_t buf_handle;
|
||||
} messaging_list_t;
|
||||
typedef enum {
|
||||
MESSAGING_CLASS_OTA,
|
||||
MESSAGING_CLASS_SYSTEM
|
||||
} messaging_classes;
|
||||
|
||||
typedef struct messaging_list_t *messaging_handle_t;
|
||||
|
||||
typedef struct {
|
||||
time_t sent_time;
|
||||
messaging_types type;
|
||||
char message[101];
|
||||
messaging_classes msg_class;
|
||||
char message[151];
|
||||
} 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, ...);
|
||||
messaging_handle_t messaging_register_subscriber(uint8_t max_count, char * name);
|
||||
esp_err_t messaging_post_to_queue(messaging_handle_t subscriber_handle, single_message_t * message, size_t message_size);
|
||||
void messaging_post_message(messaging_types type,messaging_classes msg_class, char * fmt, ...);
|
||||
cJSON * messaging_retrieve_messages(RingbufHandle_t buf_handle);
|
||||
single_message_t * messaging_retrieve_message(RingbufHandle_t buf_handle);
|
||||
void messaging_service_init();
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "monitor.h"
|
||||
#include "globdefs.h"
|
||||
#include "accessors.h"
|
||||
#include "messaging.h"
|
||||
|
||||
extern void battery_svc_init(void);
|
||||
extern void monitor_svc_init(void);
|
||||
@@ -52,6 +53,7 @@ void set_power_gpio(int gpio, char *value) {
|
||||
*/
|
||||
void services_init(void) {
|
||||
messaging_service_init();
|
||||
messaging_post_message(MESSAGING_INFO,MESSAGING_CLASS_SYSTEM, "Initializing services");
|
||||
gpio_install_isr_service(0);
|
||||
|
||||
#ifdef CONFIG_I2C_LOCKED
|
||||
|
||||
Reference in New Issue
Block a user