diff --git a/src/drv_tuyaMCU.c b/src/drv_tuyaMCU.c new file mode 100644 index 000000000..6b807a059 --- /dev/null +++ b/src/drv_tuyaMCU.c @@ -0,0 +1,161 @@ +#include "new_common.h" +#include "new_pins.h" +#include "new_cfg.h" +#include "new_cmd.h" + + +#if PLATFORM_BK7231T | PLATFORM_BK7231N +#include "../../beken378/func/user_driver/BkDriverUart.h" +#endif + + + +#define TUYA_CMD_HEARTBEAT 0x00 +#define TUYA_CMD_QUERY_PRODUCT 0x01 +#define TUYA_CMD_MCU_CONF 0x02 +#define TUYA_CMD_WIFI_STATE 0x03 +#define TUYA_CMD_WIFI_RESET 0x04 +#define TUYA_CMD_WIFI_SELECT 0x05 +#define TUYA_CMD_SET_DP 0x06 +#define TUYA_CMD_STATE 0x07 +#define TUYA_CMD_QUERY_STATE 0x08 +#define TUYA_CMD_SET_TIME 0x1C + +typedef struct rtcc_s { + uint8_t second; + uint8_t minute; + uint8_t hour; + uint8_t day_of_week; // sunday is day 1 + uint8_t day_of_month; + uint8_t month; + char name_of_month[4]; + uint16_t day_of_year; + uint16_t year; + uint32_t days; + uint32_t valid; +} rtcc_t; + +#if PLATFORM_BK7231T | PLATFORM_BK7231N +void test_ty_read_uart_data_to_buffer(int port, void* param) +{ + int rc = 0; + + while((rc = uart_read_byte(port)) != -1) + { + // if(__ty_uart_read_data_size(port) < (ty_uart[port].buf_len-1)) + // { + // ty_uart[port].buf[ty_uart[port].in++] = rc; + /// if(ty_uart[port].in >= ty_uart[port].buf_len){ + /// ty_uart[port].in = 0; + /// } + // } + } + +} +#endif + +void TuyaMCU_Bridge_InitUART(int baud) { +#if PLATFORM_BK7231T | PLATFORM_BK7231N + bk_uart_config_t config; + + config.baud_rate = 9600; + config.data_width = 0x03; + config.parity = 0; //0:no parity,1:odd,2:even + config.stop_bits = 0; //0:1bit,1:2bit + config.flow_control = 0; //FLOW_CTRL_DISABLED + config.flags = 0; + + bk_uart_initialize(0, &config, NULL); + bk_uart_set_rx_callback(0, test_ty_read_uart_data_to_buffer, NULL); +#else + + +#endif +} +void TuyaMCU_Bridge_SendUARTByte(byte b) { +#if PLATFORM_BK7231T | PLATFORM_BK7231N + bk_send_byte(0, b); +#elif WINDOWS + // STUB - for testing + printf("%02X", b); +#else + + +#endif +} + +// append header, len, everything, checksum +void TuyaMCU_SendCommandWithData(byte cmdType, byte *data, int payload_len) { + int i; + + byte check_sum = (0xFF + cmdType + (payload_len >> 8) + (payload_len & 0xFF)); + TuyaMCU_Bridge_InitUART(9600); + TuyaMCU_Bridge_SendUARTByte(0x55); + TuyaMCU_Bridge_SendUARTByte(0xAA); + TuyaMCU_Bridge_SendUARTByte(0x00); // version 00 + TuyaMCU_Bridge_SendUARTByte(cmdType); // version 00 + TuyaMCU_Bridge_SendUARTByte(payload_len >> 8); // following data length (Hi) + TuyaMCU_Bridge_SendUARTByte(payload_len & 0xFF); // following data length (Lo) + for(i = 0; i < payload_len; i++) { + byte b = data[i]; + check_sum += b; + TuyaMCU_Bridge_SendUARTByte(b); + } + TuyaMCU_Bridge_SendUARTByte(check_sum); +} + +void TuyaMCU_Send_SetTime(rtcc_t *pTime) { + byte payload_buffer[8]; + byte tuya_day_of_week; + + if (pTime->day_of_week == 1) { + tuya_day_of_week = 7; + } else { + tuya_day_of_week = pTime->day_of_week-1; + } + + payload_buffer[0] = 0x01; + payload_buffer[1] = pTime->year % 100; + payload_buffer[2] = pTime->month; + payload_buffer[3] = pTime->day_of_month; + payload_buffer[4] = pTime->hour; + payload_buffer[5] = pTime->minute; + payload_buffer[6] = pTime->second; + payload_buffer[7] = tuya_day_of_week; //1 for Monday in TUYA Doc + + TuyaMCU_SendCommandWithData(TUYA_CMD_SET_TIME, payload_buffer, 8); +} +void TuyaMCU_Send_SetTime_Example() { + rtcc_t testTime; + + testTime.year = 2012; + testTime.month = 7; + testTime.day_of_month = 15; + testTime.day_of_week = 4; + testTime.hour = 6; + testTime.minute = 54; + testTime.second = 32; + + TuyaMCU_Send_SetTime(&testTime); +} +void TuyaMCU_Send(byte *data, int size) { + int i; + unsigned char check_sum; + + TuyaMCU_Bridge_InitUART(9600); + + check_sum = 0; + for(i = 0; i < size; i++) { + byte b = data[i]; + check_sum += b; + TuyaMCU_Bridge_SendUARTByte(b); + } + TuyaMCU_Bridge_SendUARTByte(check_sum); + + printf("\nWe sent %i bytes to Tuya MCU\n",size+1); +} +void TuyaMCU_Init() +{ + CMD_RegisterCommand("tuyaMcu_testSendTime","",TuyaMCU_Send_SetTime_Example); + +} diff --git a/src/drv_tuyaMCU.h b/src/drv_tuyaMCU.h new file mode 100644 index 000000000..55d40d4ca --- /dev/null +++ b/src/drv_tuyaMCU.h @@ -0,0 +1,3 @@ + + +void TuyaMCU_Init(); \ No newline at end of file diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 65a3cab9f..75f24feac 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -4,6 +4,7 @@ #include "../new_pins.h" #include "../new_cfg.h" #include "../ota/ota.h" +#include "../new_cmd.h" #ifdef WINDOWS // nothing @@ -612,75 +613,49 @@ int http_fn_flash_read_tool(http_request_t *request) { poststr(request, NULL); return 0; } -#if PLATFORM_BK7231T | PLATFORM_BK7231N -void test_ty_read_uart_data_to_buffer(int port, void* param) -{ - int rc = 0; - - while((rc = uart_read_byte(port)) != -1) - { - // if(__ty_uart_read_data_size(port) < (ty_uart[port].buf_len-1)) - // { - // ty_uart[port].buf[ty_uart[port].in++] = rc; - /// if(ty_uart[port].in >= ty_uart[port].buf_len){ - /// ty_uart[port].in = 0; - /// } - // } - } - -} -#endif -#if PLATFORM_BK7231T | PLATFORM_BK7231N -#include "../../beken378/func/user_driver/BkDriverUart.h" -#endif -void TuyaMCU_Bridge_InitUART(int baud) { -#if PLATFORM_BK7231T | PLATFORM_BK7231N - bk_uart_config_t config; - - config.baud_rate = 9600; - config.data_width = 0x03; - config.parity = 0; //0:no parity,1:odd,2:even - config.stop_bits = 0; //0:1bit,1:2bit - config.flow_control = 0; //FLOW_CTRL_DISABLED - config.flags = 0; - - bk_uart_initialize(0, &config, NULL); - bk_uart_set_rx_callback(0, test_ty_read_uart_data_to_buffer, NULL); -#else +int http_fn_cmd_tool(http_request_t *request) { + int res; + int rem; + int now; + int nowOfs; + int hex; + int i; + char tmpA[128]; + char tmpB[64]; + + http_setup(request, httpMimeTypeHTML); + poststr(request,htmlHeader); + poststr(request,g_header); + poststr(request,"

Command Tool

"); + + if( http_getArg(request->url,"cmd",tmpA,sizeof(tmpA))) { + i = CMD_ExecuteCommand(tmpA); + if(i == 0) { + poststr(request,"Command not found"); + } else { + poststr(request,"Executed"); + } + poststr(request,"
"); + } + poststr(request,"
"); + + poststr(request,"
\ + ",tmpA); + poststr(request,"

\ + \ +
"); + + poststr(request,htmlReturnToCfg); + HTTP_AddBuildFooter(request); + poststr(request,htmlEnd); -#endif -} -void TuyaMCU_Bridge_SendUARTByte(byte b) { -#if PLATFORM_BK7231T | PLATFORM_BK7231N - bk_send_byte(0, b); -#elif WINDOWS - // STUB - for testing - printf("%02X", b); -#else - - -#endif + poststr(request, NULL); + return 0; } - -void TuyaMCU_Send(byte *data, int size) { - int i; - unsigned char check_sum; - - TuyaMCU_Bridge_InitUART(9600); - - check_sum = 0; - for(i = 0; i < size; i++) { - byte b = data[i]; - check_sum += b; - TuyaMCU_Bridge_SendUARTByte(b); - } - TuyaMCU_Bridge_SendUARTByte(check_sum); - - printf("\nWe sent %i bytes to Tuya MCU\n",size+1); -} int http_fn_uart_tool(http_request_t *request) { char tmpA[256]; byte results[128]; @@ -876,7 +851,7 @@ int http_fn_cfg(http_request_t *request) { poststr(request,"
"); poststr(request,"
"); poststr(request,"
"); - poststr(request,"
"); + poststr(request,"
"); poststr(request,"
"); poststr(request,"
"); diff --git a/src/httpserver/http_fns.h b/src/httpserver/http_fns.h index 6399b5d3d..de3e23b32 100644 --- a/src/httpserver/http_fns.h +++ b/src/httpserver/http_fns.h @@ -14,6 +14,7 @@ int http_fn_cfg_loglevel_set(http_request_t *request); int http_fn_cfg_wifi(http_request_t *request); int http_fn_cfg_mac(http_request_t *request); int http_fn_flash_read_tool(http_request_t *request); +int http_fn_cmd_tool(http_request_t *request); int http_fn_uart_tool(http_request_t *request); int http_fn_cfg_quick(http_request_t *request); int http_fn_cfg_ha(http_request_t *request); diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index 81e526d10..a63df0f2c 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -511,6 +511,7 @@ int HTTP_ProcessPacket(http_request_t *request) { if(http_checkUrlBase(urlStr,"flash_read_tool")) return http_fn_flash_read_tool(request); if(http_checkUrlBase(urlStr,"uart_tool")) return http_fn_uart_tool(request); + if(http_checkUrlBase(urlStr,"cmd_tool")) return http_fn_cmd_tool(request); if(http_checkUrlBase(urlStr,"config_dump_table")) return http_fn_config_dump_table(request); if(http_checkUrlBase(urlStr,"cfg_quick")) return http_fn_cfg_quick(request); diff --git a/src/new_cmd.c b/src/new_cmd.c index 5e26d864c..4bdb67b78 100644 --- a/src/new_cmd.c +++ b/src/new_cmd.c @@ -79,9 +79,11 @@ bool isWhiteSpace(char ch) { return true; return false; } -void CMD_ExecuteCommand(const char *s) { +int CMD_ExecuteCommand(const char *s) { + int r = 0; char *p; int i; + command_t *newCmd; while(isWhiteSpace(*s)) { s++; @@ -114,6 +116,14 @@ void CMD_ExecuteCommand(const char *s) { printf("Arg %i is %s\n",i,g_args[i]); } } + + newCmd = CMD_Find(g_args[0]); + if(newCmd != 0) { + r++; + newCmd->handler(); + } + + return r; } diff --git a/src/new_cmd.h b/src/new_cmd.h index b80b157cc..28080db48 100644 --- a/src/new_cmd.h +++ b/src/new_cmd.h @@ -11,7 +11,7 @@ typedef struct command_s { command_t *CMD_Find(const char *name); void CMD_RegisterCommand(const char *name, const char *args, commandHandler_t handler); -void CMD_ExecuteCommand(const char *s); +int CMD_ExecuteCommand(const char *s); diff --git a/src/new_common.h b/src/new_common.h index 1aace927f..2c815f07c 100644 --- a/src/new_common.h +++ b/src/new_common.h @@ -77,6 +77,16 @@ typedef unsigned int UINT32; typedef unsigned char byte; +#endif + +// stricmp fix +#if WINDOWS + +#else + +int wal_stricmp(const char *a, const char *b) ; +#define stricmp wal_stricmp + #endif diff --git a/src/user_main.c b/src/user_main.c index 0870ace4d..78fbdab8a 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -336,6 +336,7 @@ void user_main(void) increment_boot_count(); CFG_InitAndLoad(); + TuyaMCU_Init(); wifi_ssid = CFG_GetWiFiSSID(); wifi_pass = CFG_GetWiFiPass(); diff --git a/src/win_main.c b/src/win_main.c index 35d971fbd..879dfa52e 100644 --- a/src/win_main.c +++ b/src/win_main.c @@ -112,6 +112,8 @@ int __cdecl main(void) PIN_SetPinRoleForPinIndex(2,IOR_PWM); //init_rest(); + TuyaMCU_Init(); + // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { diff --git a/windowsTest_msvc2008.vcproj b/windowsTest_msvc2008.vcproj index f3ae4c6fb..dd1975273 100644 --- a/windowsTest_msvc2008.vcproj +++ b/windowsTest_msvc2008.vcproj @@ -163,6 +163,14 @@ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx" UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" > + + + + @@ -227,6 +235,14 @@ RelativePath=".\src\new_cfg.h" > + + + +