diff --git a/src/cmnds/cmd_main.c b/src/cmnds/cmd_main.c index 83b6ec9d7..54dd12198 100644 --- a/src/cmnds/cmd_main.c +++ b/src/cmnds/cmd_main.c @@ -5,6 +5,8 @@ #include #include "cmd_local.h" #include "../driver/drv_ir.h" +#include "../driver/drv_uart.h" +#include "../driver/drv_public.h" #ifdef BK_LITTLEFS #include "../littlefs/our_lfs.h" @@ -240,6 +242,53 @@ static commandResult_t CMD_SetStartValue(const void* context, const char* cmd, c } +int cmd_uartInitIndex = 0; +void CMD_UART_Init() { + UART_InitUART(115200); + cmd_uartInitIndex = g_uart_init_counter; + UART_InitReceiveRingBuffer(512); +} +void CMD_UART_Run() { + char a, b; + int i; + int totalSize; + char tmp[128]; + + totalSize = UART_GetDataSize(); + while (totalSize) { + a = UART_GetNextByte(0); + if (a == '\n' || a == '\r' || a == ' ' || a == '\t') { + UART_ConsumeBytes(1); + totalSize = UART_GetDataSize(); + } + else { + break; + } + } + if (totalSize < 2) { + return 0; + } + // skip garbage data (should not happen) + for (i = 0; i < totalSize; i++) { + a = UART_GetNextByte(i); + if (i+1 < sizeof(tmp)) { + tmp[i] = a; + tmp[i + 1] = 0; + } + if (b == '\n') { + break; + } + } + UART_ConsumeBytes(i); + CMD_ExecuteCommand(tmp, 0); +} +void CMD_RunUartCmndIfRequired() { + if (CFG_HasFlag(OBK_FLAG_CMD_ACCEPT_UART_COMMANDS)) { + if (cmd_uartInitIndex && cmd_uartInitIndex == g_uart_init_counter) { + CMD_UART_Run(); + } + } +} void CMD_Init_Early() { //cmddetail:{"name":"echo","args":"[Message]", //cmddetail:"descr":"Sends given message back to console.", @@ -251,6 +300,11 @@ void CMD_Init_Early() { //cmddetail:"fn":"CMD_Restart","file":"cmnds/cmd_main.c","requires":"", //cmddetail:"examples":""} CMD_RegisterCommand("restart", "", CMD_Restart, NULL, NULL); + //cmddetail:{"name":"reboot","args":"", + //cmddetail:"descr":"Same as restart. Needed for bkWriter 1.60 which sends 'reboot' cmd before trying to get bus", + //cmddetail:"fn":"CMD_Restart","file":"cmnds/cmd_main.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("reboot", "", CMD_Restart, NULL, NULL); //cmddetail:{"name":"clearConfig","args":"", //cmddetail:"descr":"Clears all config, including WiFi data", //cmddetail:"fn":"CMD_ClearConfig","file":"cmnds/cmd_main.c","requires":"", @@ -315,6 +369,11 @@ void CMD_Init_Early() { #if (defined WINDOWS) || (defined PLATFORM_BEKEN) CMD_InitScripting(); #endif + if (!bSafeMode) { + if (CFG_HasFlag(OBK_FLAG_CMD_ACCEPT_UART_COMMANDS)) { + CMD_UART_Init(); + } + } } void CMD_Init_Delayed() { diff --git a/src/cmnds/cmd_public.h b/src/cmnds/cmd_public.h index 07ae987fb..8d4061d91 100644 --- a/src/cmnds/cmd_public.h +++ b/src/cmnds/cmd_public.h @@ -36,6 +36,7 @@ extern bool g_powersave; void CMD_Init_Early(); void CMD_Init_Delayed(); void CMD_FreeAllCommands(); +void CMD_RunUartCmndIfRequired(); void CMD_RegisterCommand(const char* name, const char* args, commandHandler_t handler, const char* userDesc, void* context); commandResult_t CMD_ExecuteCommand(const char* s, int cmdFlags); commandResult_t CMD_ExecuteCommandArgs(const char* cmd, const char* args, int cmdFlags); diff --git a/src/driver/drv_uart.c b/src/driver/drv_uart.c index 921370ecc..bacf6fc9e 100644 --- a/src/driver/drv_uart.c +++ b/src/driver/drv_uart.c @@ -97,6 +97,8 @@ static byte *g_recvBuf = 0; static int g_recvBufSize = 0; static int g_recvBufIn = 0; static int g_recvBufOut = 0; +// used to detect uart reinit +int g_uart_init_counter = 0; void UART_InitReceiveRingBuffer(int size){ if(g_recvBuf!=0) @@ -256,7 +258,8 @@ void UART_AddCommands() { //cmddetail:"examples":""} CMD_RegisterCommand("uartSendASCII", NULL, CMD_UART_Send_ASCII, NULL, NULL); } -void UART_InitUART(int baud) { +int UART_InitUART(int baud) { + g_uart_init_counter++; #if PLATFORM_BK7231T | PLATFORM_BK7231N bk_uart_config_t config; @@ -313,6 +316,7 @@ void UART_InitUART(int baud) { b_uart_commands_added = true; UART_AddCommands(); } + return g_uart_init_counter; } diff --git a/src/driver/drv_uart.h b/src/driver/drv_uart.h index 5d2fae03c..7dfd66c53 100644 --- a/src/driver/drv_uart.h +++ b/src/driver/drv_uart.h @@ -9,6 +9,9 @@ void UART_AppendByteToCircularBuffer(int rc); void UART_SendByte(byte b); void UART_InitUART(int baud); +// used to detect uart reinit/takeover by driver +extern int g_uart_init_counter; + diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 57cceb71d..e3aa5b3c9 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2213,7 +2213,7 @@ const char* g_obk_flagNames[] = { "[LED] Setting RGB white (FFFFFF) enables temperature mode", "[NETIF] Use short device name as a hostname instead of a long name", "[MQTT] Enable Tasmota TELE etc publishes (for ioBroker etc)", - "error", + "[UART] Enable UART command line", "error", "error", "error", diff --git a/src/new_cfg.c b/src/new_cfg.c index fd8e27777..b3e7d4aa6 100644 --- a/src/new_cfg.c +++ b/src/new_cfg.c @@ -189,7 +189,11 @@ void CFG_SetDefaultConfig() { // This is helpful for users CFG_SetFlag(OBK_FLAG_MQTT_BROADCASTSELFSTATEONCONNECT,true); - + // this is helpful for debuging wifi issues +#if PLATFORM_BEKEN + //CFG_SetFlag(OBK_FLAG_CMD_ACCEPT_UART_COMMANDS, true); +#endif + CFG_SetDefaultLEDCorrectionTable(); g_cfg_pendingChanges++; diff --git a/src/new_pins.h b/src/new_pins.h index f6a1f397b..b0b7da2ba 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -200,8 +200,9 @@ typedef struct pinsState_s { #define OBK_FLAG_LED_SETTING_WHITE_RGB_ENABLES_CW 28 #define OBK_FLAG_USE_SHORT_DEVICE_NAME_AS_HOSTNAME 29 #define OBK_FLAG_DO_TASMOTA_TELE_PUBLISHES 30 +#define OBK_FLAG_CMD_ACCEPT_UART_COMMANDS 31 -#define OBK_TOTAL_FLAGS 31 +#define OBK_TOTAL_FLAGS 32 #define CGF_MQTT_CLIENT_ID_SIZE 64 diff --git a/src/user_main.c b/src/user_main.c index c60299e00..8b3df3aa6 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -664,6 +664,7 @@ void QuickTick(void *param) #ifdef WINDOWS NewTuyaMCUSimulator_RunQuickTick(t_diff); #endif + CMD_RunUartCmndIfRequired(); // process recieved messages here.. MQTT_RunQuickTick();