mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-16 16:37:16 +03:00
Merge remote-tracking branch 'origin/httpd' into master-cmake
Conflicts: .cproject .gitmodules .project .pydevproject .settings/language.settings.xml .settings/org.eclipse.cdt.core.prefs components/cmd_i2c/CMakeLists.txt components/cmd_i2c/cmd_i2ctools.c components/cmd_i2c/component.mk components/cmd_nvs/cmd_nvs.c components/cmd_nvs/component.mk components/cmd_system/cmd_system.c components/cmd_system/component.mk components/config/config.c components/config/config.h components/config/nvs_utilities.c components/display/CMakeLists.txt components/driver_bt/CMakeLists.txt components/driver_bt/component.mk components/raop/raop.c components/services/CMakeLists.txt components/squeezelite-ota/cmd_ota.c components/squeezelite-ota/squeezelite-ota.c components/squeezelite-ota/squeezelite-ota.h components/squeezelite/component.mk components/telnet/CMakeLists.txt components/wifi-manager/CMakeLists.txt components/wifi-manager/dns_server.c components/wifi-manager/http_server.c components/wifi-manager/http_server.h components/wifi-manager/wifi_manager.c components/wifi-manager/wifi_manager.h main/CMakeLists.txt main/console.c main/esp_app_main.c main/platform_esp32.h
This commit is contained in:
@@ -10,6 +10,4 @@
|
||||
COMPONENT_SRCDIRS := .
|
||||
COMPONENT_SRCDIRS += ./libtelnet
|
||||
COMPONENT_ADD_INCLUDEDIRS := .
|
||||
COMPONENT_ADD_INCLUDEDIRS += ./libtelnet
|
||||
COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/
|
||||
|
||||
COMPONENT_PRIV_INCLUDEDIRS += ./libtelnet
|
||||
@@ -38,6 +38,8 @@
|
||||
#include "config.h"
|
||||
#include "nvs_utilities.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "messaging.h"
|
||||
#include "trace.h"
|
||||
|
||||
|
||||
/************************************
|
||||
@@ -47,15 +49,16 @@
|
||||
#define TELNET_STACK_SIZE 8048
|
||||
#define TELNET_RX_BUF 1024
|
||||
|
||||
const static char tag[] = "telnet";
|
||||
const static char TAG[] = "telnet";
|
||||
static int uart_fd=0;
|
||||
RingbufHandle_t buf_handle;
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
//static SemaphoreHandle_t xSemaphore = NULL;
|
||||
static size_t send_chunk=300;
|
||||
static size_t log_buf_size=2000; //32-bit aligned size
|
||||
static bool bIsEnabled=false;
|
||||
static int partnerSocket=0;
|
||||
static telnet_t *tnHandle;
|
||||
extern bool bypass_wifi_manager;
|
||||
|
||||
/************************************
|
||||
* Forward declarations
|
||||
@@ -68,22 +71,34 @@ static int stdout_fstat(int fd, struct stat * st);
|
||||
static ssize_t stdout_write(int fd, const void * data, size_t size);
|
||||
static char *eventToString(telnet_event_type_t type);
|
||||
static void handle_telnet_conn();
|
||||
static void process_logs( UBaseType_t bytes);
|
||||
|
||||
static void process_logs( UBaseType_t bytes, bool is_write_op);
|
||||
static bool bMirrorToUART=false;
|
||||
struct telnetUserData {
|
||||
int sockfd;
|
||||
telnet_t *tnHandle;
|
||||
char * rxbuf;
|
||||
};
|
||||
|
||||
|
||||
bool is_serial_suppressed(){
|
||||
return bIsEnabled?!bMirrorToUART:false ;
|
||||
}
|
||||
void init_telnet(){
|
||||
char *val= get_nvs_value_alloc(NVS_TYPE_STR, "telnet_enable");
|
||||
if (!val || strlen(val) == 0 || !strcasestr("YX",val) ) {
|
||||
ESP_LOGI(tag,"Telnet support disabled");
|
||||
if (!val || strlen(val) == 0 || !strcasestr("YXD",val) ) {
|
||||
ESP_LOGI(TAG,"Telnet support disabled");
|
||||
if(val) free(val);
|
||||
return;
|
||||
}
|
||||
// if wifi manager is bypassed, there will possibly be no wifi available
|
||||
//
|
||||
bMirrorToUART = (strcasestr("D",val)!=NULL);
|
||||
if(!bMirrorToUART && bypass_wifi_manager){
|
||||
// This isn't supposed to happen, as telnet won't start if wifi manager isn't
|
||||
// started. So this is a safeguard only.
|
||||
ESP_LOGW(TAG,"Wifi manager is not active. Forcing console on Serial output.");
|
||||
}
|
||||
|
||||
FREE_AND_NULL(val);
|
||||
val=get_nvs_value_alloc(NVS_TYPE_STR, "telnet_block");
|
||||
if(val){
|
||||
send_chunk=atol(val);
|
||||
@@ -97,18 +112,21 @@ void init_telnet(){
|
||||
log_buf_size=log_buf_size>0?log_buf_size:4000;
|
||||
}
|
||||
// Create the semaphore to guard a shared resource.
|
||||
vSemaphoreCreateBinary( xSemaphore );
|
||||
//vSemaphoreCreateBinary( xSemaphore );
|
||||
|
||||
// Redirect the output to our telnet handler as soon as possible
|
||||
StaticRingbuffer_t *buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
|
||||
uint8_t *buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t)*log_buf_size, MALLOC_CAP_SPIRAM);
|
||||
StaticRingbuffer_t *buffer_struct = (StaticRingbuffer_t *)malloc(sizeof(StaticRingbuffer_t) );
|
||||
// All non-split ring buffer must have their memory alignment set to 32 bits.
|
||||
uint8_t *buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t)*log_buf_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT );
|
||||
buf_handle = xRingbufferCreateStatic(log_buf_size, RINGBUF_TYPE_BYTEBUF, buffer_storage, buffer_struct);
|
||||
if (buf_handle == NULL) {
|
||||
ESP_LOGE(tag,"Failed to create ring buffer for telnet!");
|
||||
ESP_LOGE(TAG,"Failed to create ring buffer for telnet!");
|
||||
messaging_post_message(MESSAGING_ERROR,MESSAGING_CLASS_SYSTEM,"Failed to allocate memory for telnet buffer");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(tag, "***Redirecting log output to telnet");
|
||||
ESP_LOGI(TAG, "***Redirecting log output to telnet");
|
||||
const esp_vfs_t vfs = {
|
||||
.flags = ESP_VFS_FLAG_DEFAULT,
|
||||
.write = &stdout_write,
|
||||
@@ -116,8 +134,12 @@ void init_telnet(){
|
||||
.fstat = &stdout_fstat,
|
||||
.close = &stdout_close,
|
||||
.read = &stdout_read,
|
||||
|
||||
};
|
||||
uart_fd=open("/dev/uart/0", O_RDWR);
|
||||
|
||||
if(bMirrorToUART){
|
||||
uart_fd=open("/dev/uart/0", O_RDWR);
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_vfs_register("/dev/pkspstdout", &vfs, NULL));
|
||||
freopen("/dev/pkspstdout", "w", stdout);
|
||||
freopen("/dev/pkspstdout", "w", stderr);
|
||||
@@ -125,11 +147,11 @@ void init_telnet(){
|
||||
}
|
||||
void start_telnet(void * pvParameter){
|
||||
static bool isStarted=false;
|
||||
StaticTask_t *xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
StackType_t *xStack = malloc(TELNET_STACK_SIZE);
|
||||
StaticTask_t *xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
|
||||
StackType_t *xStack = heap_caps_malloc(TELNET_STACK_SIZE,(MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT));
|
||||
|
||||
if(!isStarted && bIsEnabled) {
|
||||
xTaskCreateStatic( (TaskFunction_t) &telnet_task, "telnet", TELNET_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, xTaskBuffer);
|
||||
xTaskCreateStatic( (TaskFunction_t) &telnet_task, "telnet", TELNET_STACK_SIZE, NULL, ESP_TASK_MAIN_PRIO , xStack, xTaskBuffer);
|
||||
isStarted=true;
|
||||
}
|
||||
}
|
||||
@@ -142,13 +164,15 @@ static void telnet_task(void *data) {
|
||||
|
||||
int rc = bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
|
||||
if (rc < 0) {
|
||||
ESP_LOGE(tag, "bind: %d (%s)", errno, strerror(errno));
|
||||
ESP_LOGE(TAG, "bind: %d (%s)", errno, strerror(errno));
|
||||
close(serverSocket);
|
||||
return;
|
||||
}
|
||||
|
||||
rc = listen(serverSocket, 5);
|
||||
if (rc < 0) {
|
||||
ESP_LOGE(tag, "listen: %d (%s)", errno, strerror(errno));
|
||||
ESP_LOGE(TAG, "listen: %d (%s)", errno, strerror(errno));
|
||||
close(serverSocket);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -156,16 +180,17 @@ static void telnet_task(void *data) {
|
||||
socklen_t len = sizeof(serverAddr);
|
||||
rc = accept(serverSocket, (struct sockaddr *)&serverAddr, &len);
|
||||
if (rc < 0 ){
|
||||
ESP_LOGE(tag, "accept: %d (%s)", errno, strerror(errno));
|
||||
ESP_LOGE(TAG, "accept: %d (%s)", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
else {
|
||||
partnerSocket = rc;
|
||||
ESP_LOGD(tag, "We have a new client connection!");
|
||||
ESP_LOGD(TAG, "We have a new client connection!");
|
||||
handle_telnet_conn();
|
||||
ESP_LOGD(tag, "Telnet connection terminated");
|
||||
ESP_LOGD(TAG, "Telnet connection terminated");
|
||||
}
|
||||
}
|
||||
close(serverSocket);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
@@ -228,7 +253,9 @@ void process_received_data(const char * buffer, size_t size){
|
||||
command[size]='\0';
|
||||
if(command[0]!='\r' && command[0]!='\n'){
|
||||
// echo the command buffer out to uart and run
|
||||
write(uart_fd, command, size);
|
||||
if(bMirrorToUART){
|
||||
write(uart_fd, command, size);
|
||||
}
|
||||
run_command((char *)command);
|
||||
}
|
||||
free(command);
|
||||
@@ -265,23 +292,23 @@ static void handle_telnet_events(
|
||||
} // myhandle_telnet_events
|
||||
|
||||
|
||||
static void process_logs(UBaseType_t count){
|
||||
static void process_logs( UBaseType_t bytes, bool is_write_op){
|
||||
//Receive an item from no-split ring buffer
|
||||
size_t item_size;
|
||||
UBaseType_t uxItemsWaiting;
|
||||
UBaseType_t uxBytesToSend=count;
|
||||
UBaseType_t uxItemsWaiting;
|
||||
UBaseType_t uxBytesToSend=bytes;
|
||||
|
||||
vRingbufferGetInfo(buf_handle, NULL, NULL, NULL, NULL, &uxItemsWaiting);
|
||||
if(count == 0){
|
||||
// this sends the entire buffer to the remote client
|
||||
uxBytesToSend = uxItemsWaiting;
|
||||
}
|
||||
if( partnerSocket ==0 && (uxItemsWaiting*100 / log_buf_size) <75){
|
||||
// We still have some room in the ringbuffer and there's no telnet
|
||||
// connection yet, so bail out for now.
|
||||
//printf("%s() Log buffer used %u of %u bytes used\n", __FUNCTION__, uxItemsWaiting, log_buf_size);
|
||||
vRingbufferGetInfo(buf_handle, NULL, NULL, NULL, NULL, &uxItemsWaiting);
|
||||
bool is_space_available = ((log_buf_size-uxItemsWaiting)>=bytes && log_buf_size>uxItemsWaiting);
|
||||
if( is_space_available && (is_write_op || partnerSocket == 0) ){
|
||||
// there's still some room left in the buffer, and we're either
|
||||
// processing a write operation or telnet isn't connected yet.
|
||||
return;
|
||||
}
|
||||
if(is_write_op && !is_space_available && uxBytesToSend==0){
|
||||
// flush at least the size of a full chunk
|
||||
uxBytesToSend = send_chunk;
|
||||
}
|
||||
|
||||
while(uxBytesToSend>0){
|
||||
char *item = (char *)xRingbufferReceiveUpTo(buf_handle, &item_size, pdMS_TO_TICKS(50), uxBytesToSend);
|
||||
@@ -322,7 +349,7 @@ static void handle_telnet_conn() {
|
||||
pTelnetUserData->sockfd = partnerSocket;
|
||||
|
||||
// flush all the log buffer on connect
|
||||
process_logs(0);
|
||||
process_logs(log_buf_size, false);
|
||||
|
||||
while(1) {
|
||||
//ESP_LOGD(tag, "waiting for data");
|
||||
@@ -339,7 +366,7 @@ static void handle_telnet_conn() {
|
||||
partnerSocket = 0;
|
||||
return;
|
||||
}
|
||||
process_logs(send_chunk);
|
||||
process_logs(send_chunk, false);
|
||||
|
||||
taskYIELD();
|
||||
}
|
||||
@@ -348,37 +375,24 @@ static void handle_telnet_conn() {
|
||||
|
||||
// ******************* stdout/stderr Redirection to ringbuffer
|
||||
static ssize_t stdout_write(int fd, const void * data, size_t size) {
|
||||
if (xSemaphoreTake(xSemaphore, (TickType_t) 10) == pdTRUE) {
|
||||
// #1 Write to ringbuffer
|
||||
if (buf_handle == NULL) {
|
||||
printf("%s() ABORT. file handle _log_remote_fp is NULL\n",
|
||||
__FUNCTION__);
|
||||
} else {
|
||||
//Send an item
|
||||
UBaseType_t res = xRingbufferSend(buf_handle, data, size,
|
||||
pdMS_TO_TICKS(100));
|
||||
if (res != pdTRUE) {
|
||||
// flush some entries
|
||||
process_logs(size);
|
||||
res = xRingbufferSend(buf_handle, data, size,
|
||||
pdMS_TO_TICKS(100));
|
||||
if (res != pdTRUE) {
|
||||
|
||||
printf("%s() ABORT. Unable to store log entry in buffer\n",
|
||||
__FUNCTION__);
|
||||
}
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(xSemaphore);
|
||||
// #1 Write to ringbuffer
|
||||
if (buf_handle == NULL) {
|
||||
printf("%s() ABORT. file handle _log_remote_fp is NULL\n",
|
||||
__FUNCTION__);
|
||||
} else {
|
||||
// We could not obtain the semaphore and can therefore not access
|
||||
// the shared resource safely.
|
||||
// flush the buffer if needed
|
||||
process_logs(size, true);
|
||||
//Send an item
|
||||
UBaseType_t res = xRingbufferSend(buf_handle, data, size, pdMS_TO_TICKS(10));
|
||||
assert(res == pdTRUE);
|
||||
|
||||
}
|
||||
return write(uart_fd, data, size);
|
||||
return bMirrorToUART?write(uart_fd, data, size):size;
|
||||
}
|
||||
|
||||
static ssize_t stdout_read(int fd, void* data, size_t size) {
|
||||
return read(fd, data, size);
|
||||
//return read(fd, data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int stdout_open(const char * path, int flags, int mode) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
void init_telnet();
|
||||
void start_telnet(void * pvParameter);
|
||||
extern bool is_serial_suppressed();
|
||||
|
||||
Reference in New Issue
Block a user