From b497292ef88bf25638abff522dc9729cec623059 Mon Sep 17 00:00:00 2001 From: d15521469561 <1731678236@qq.com> Date: Fri, 22 May 2026 18:47:14 +0800 Subject: [PATCH] =?utf8?q?1.=E8=93=9D=E7=89=99=E8=BF=9E=E6=8E=A5=E7=81=AF?= =?utf8?q?=E6=95=88=202.=E6=8C=89=E9=94=AE=E6=81=A2=E5=A4=8D=E5=87=BA?= =?utf8?q?=E5=8E=82=E8=AE=BE=E7=BD=AE=203.=E6=A8=A1=E7=BB=84=E5=8D=87?= =?utf8?q?=E7=BA=A7=E4=B8=BB=E6=8E=A7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .gitignore | 24 +- App/inc/iap.h | 80 +++++- App/inc/io.h | 6 +- App/inc/iot.h | 8 +- App/inc/led.h | 7 +- App/inc/uart6.h | 2 +- App/src/base.c | 3 +- App/src/flash.c | 22 +- App/src/iap.c | 227 +++++++++++++++- App/src/io.c | 21 +- App/src/iot.c | 449 ++++++++++++++++++++++++------- App/src/led.c | 86 ++++++ App/src/uart6.c | 17 +- RVMDK/RVMDKN32G45X/project_cfg.h | 6 +- 14 files changed, 834 insertions(+), 124 deletions(-) diff --git a/.gitignore b/.gitignore index a72520c..44fbfa1 100644 --- a/.gitignore +++ b/.gitignore @@ -69,18 +69,18 @@ Thumbs.db ehthumbs.db Desktop.ini -.ini -.lst -.iex -.axf -.hex -.htm -.lnp -.dep -.jscope -..d -.crf -.sct +*.ini +*.lst +*.iex +*.axf +*.hex +*.htm +*.lnp +*.dep +*.jscope +*.d +*.crf +*.sct # User-specific configuration files *.user diff --git a/App/inc/iap.h b/App/inc/iap.h index e57f699..45b7d60 100644 --- a/App/inc/iap.h +++ b/App/inc/iap.h @@ -19,6 +19,7 @@ #define CMD1_IAP_CHIP5 0xB6 #define CMD1_IAP_IOT 0xB7 + #define CMD2_IAP_FETCH 0x00 #define CMD2_IAP_CERTIFICATE 0x01 #define CMD2_IAP_ERASE 0x02 @@ -226,9 +227,9 @@ typedef union _IAP_UART_CMD typedef struct { uint8_t move_enable; - uint8_t type; // 1:App0µ½App1 1:App2µ½App0 1:App1µ½App0 + uint8_t type; // 1:App0µ½App1 2:App2µ½App0 3:App1µ½App0 uint32_t crc32; - uint8_t move_time; + uint8_t move_time; // ÀۼưáÔË´ÎÊý£¬ÓÃÓÚʧ°ÜÖØÊÔ }IAP_MOVE_T; typedef struct @@ -239,6 +240,79 @@ typedef struct uint16_t build_version; }IAP_APP_PARTITION_INFO_T; +/***************************************** IAP LITE *****************************************/ +typedef enum +{ + IAP_LITE_CMD_READY = 0, + IAP_LITE_CMD_ERASE, + IAP_LITE_CMD_WRITE, + IAP_LITE_CMD_WRITE_DONE, + IAP_LITE_CMD_CRC, + IAP_LITE_CMD_MOVE, + IAP_LITE_CMD_END, +}IAP_LITE_SUB_CMD_E; + +typedef union +{ + struct + { + uint8_t protocol_version; + uint8_t reply; + uint8_t sub_cmd; + }payload; + + struct + { + uint8_t protocol_version; + uint8_t reply; + uint8_t sub_cmd; + }ready; + + struct + { + uint8_t protocol_version; + uint8_t reply; + uint8_t sub_cmd; + }erase; + + struct + { + uint8_t protocol_version; + uint8_t reply; + uint8_t sub_cmd; + + uint32_t offset; + uint16_t size; + uint8_t content; + }write; + + struct + { + uint8_t protocol_version; + uint8_t reply; + uint8_t sub_cmd; + }write_done; + + struct + { + uint8_t protocol_version; + uint8_t reply; + uint8_t sub_cmd; + + uint32_t size; + uint32_t crc32; + }crc; + + struct + { + uint8_t protocol_version; + uint8_t reply; + uint8_t sub_cmd; + }end; + +}IAP_LITE_CMD_T; + + #pragma pack(pop) extern IAP_CMD_REPLY_T iap_cmd; @@ -256,5 +330,5 @@ uint32_t Iap_Check(uint32_t start_addr, uint32_t size); uint8_t Iap_Verify(uint32_t addr, uint8_t packet_size, uint8_t* data); void Iap_Process(uint8_t* data, uint8_t len); - +void Iap_Lite_Process(uint8_t* data, uint16_t len, uint8_t host_type); #endif diff --git a/App/inc/io.h b/App/inc/io.h index 90f5363..dc72fa5 100644 --- a/App/inc/io.h +++ b/App/inc/io.h @@ -589,6 +589,7 @@ typedef struct uint32_t restore; uint32_t led_switch; uint32_t adc_calibration; + uint32_t iot_factory; }; uint8_t data[4]; }count; @@ -600,7 +601,8 @@ typedef struct uint8_t restore:1; uint8_t led_switch:1; uint8_t adc_calibration:1; - uint8_t reserved:5; + uint8_t iot_factory:1; + uint8_t reserved:4; }; uint8_t data[1]; }trigger; @@ -951,6 +953,7 @@ typedef enum #define HOST_TYPE_USB 0 #define HOST_TYPE_UART 1 +#define HOST_TYPE_UART_IOT 2 #define USB_COMM_READ 0 #define USB_COMM_WRITE 1 @@ -1225,6 +1228,7 @@ typedef union extern uint8_t platform_mode, platform_mode_last; +extern uint8_t is_connect_usb; #if (ABS_ENC_NUM) extern ENCODER_BCD_T encoder_bcd[ABS_ENC_NUM]; #endif diff --git a/App/inc/iot.h b/App/inc/iot.h index 6d43bc2..303e83d 100644 --- a/App/inc/iot.h +++ b/App/inc/iot.h @@ -18,16 +18,18 @@ typedef struct }IOT_UART_TX_QUERY_T; #define IOT_UART_QUERY_MAX 20 +#define IOT_UART_IAP_CMD 0xE0 typedef struct { + uint16_t vid; uint16_t pid; uint16_t fw_version; uint16_t build_version; uint8_t mac[6]; uint8_t heartbeat_time; - char device_id[32]; + char device_id[2]; }IOT_INFO_T; #pragma pack(pop) @@ -56,8 +58,10 @@ void Iot_Uart_Init(void); void Iot_Status_Check(void); void Iot_Init(void); -uint8_t Iot_Read_Object(uint16_t dp_id, void* buf, uint8_t* len); +uint8_t Iot_Read_Object(uint16_t dp_id, void* buf,uint8_t *is_string, uint8_t* len); uint8_t Iot_Write_Object(uint16_t dp_id, void* buf, uint8_t len); void Iot_OneTimerSecond(void); +void Iot_Uart_Tx_Query_Add(uint8_t cmd, uint8_t *pbuf, uint8_t len); +void Iot_Key_Factory_Reset(void); #endif diff --git a/App/inc/led.h b/App/inc/led.h index 7f3ab9f..fbb2b88 100644 --- a/App/inc/led.h +++ b/App/inc/led.h @@ -347,6 +347,7 @@ typedef enum KEY_TURN_OFF_FOR_A_WHILE, KEY_TURN_OFF, KEY_COLOR_PREVIEW, + KEY_BLE_CONNECT_BLINK, KEY_EXHIBITION, KEY_POWER_ON, }KEY_LED_STATUS_ENUM; @@ -364,10 +365,12 @@ typedef struct uint16_t turn_off_for_a_while:1; uint16_t turn_off:1; uint16_t color_preview:1; + uint16_t ble_state:1; uint16_t exhibition:1; + uint16_t power_on:1; - uint16_t reserved1:7; + uint16_t reserved1:6; }; uint16_t status; }; @@ -434,6 +437,8 @@ void Led_Key_Turn_Off(void); void Led_Key_Preview(void); // KEY_POWER_ON void Led_Key_Power_On(void); +// KEY_BLE_CONN_BLINK +void Led_Key_Ble_Connected_Blink(void); /*********** ENC LED ***********/ #if (ENC_LED_TOTAL_NUM) diff --git a/App/inc/uart6.h b/App/inc/uart6.h index 4f962c9..fe3a2a2 100644 --- a/App/inc/uart6.h +++ b/App/inc/uart6.h @@ -19,7 +19,7 @@ void dma_uart6_rx_deal(void); void dma_uart6_rx_init(void); - +uint8_t uart_is_send_finish(void); extern volatile bool uart6_rx_flag; extern uint8_t uart6_rx_data[UART6_MAX_LEN]; diff --git a/App/src/base.c b/App/src/base.c index 64c52f6..65bb96d 100644 --- a/App/src/base.c +++ b/App/src/base.c @@ -165,6 +165,7 @@ void Base_Uart_Rx_Handle(uint8_t *data ,uint8_t len) } base_connection.uart_conn = 1; + is_connect_usb=1; base_connection.uart_disconn_cnt = 0; } } @@ -177,7 +178,7 @@ void Base_Disconnect_Count(void) { base_connection.uart_conn = 0; base_connection.uart_conn_success = 0; - + is_connect_usb=0; base_connection.vid = 0x0000; base_connection.pid = 0x0000; base_connection.fw_version = 0x0000; diff --git a/App/src/flash.c b/App/src/flash.c index 1f2c113..26f692c 100644 --- a/App/src/flash.c +++ b/App/src/flash.c @@ -4,7 +4,7 @@ #include "crc.h" #include "hw_config.h" #include "base.h" - +#include "iot.h" uint8_t flash_wrote_finish_system_reset = 0; uint32_t flash_data_write[FLASH_PAGE_SIZE/4] = {0xFFFFFFFF}; @@ -1032,6 +1032,16 @@ void Flash_Move_Check(void) } DUG_PRINTF("\n"); } + else if(flash_move_query[0].reply_type == HOST_TYPE_UART_IOT) + { + Iot_Uart_Tx_Query_Add(flash_move_query[0].uart_cmd, flash_move_query[0].reply_fail, flash_move_query[0].size_fail); + DUG_PRINTF("HOST_TYPE_UART_IOT 1 ok %02x: ", flash_move_query[0].uart_cmd); + for(uint8_t i=0;i IAP_UART_FW_LEN) + { + return REPLY_STATUS_FAIL; + } + uint32_t wdata; + uint32_t waddr = addr; + packet_size = (packet_size/4 + ((packet_size % 4) != 0)) * 4; + uint8_t i, j; +// DUG_PRINTF("packet_size:%08x %d\n",addr,packet_size); + for (i=0,j=0;i<(packet_size/4);i++) + { + wdata = data[j]; + wdata |= data[j+1]<<8; + wdata |= data[j+2]<<16; + wdata |= data[j+3]<<24; +// DUG_PRINTF("addr:%08x wdata:%08x\n",waddr+4*i,wdata); + FLASH_ProgramWord(waddr+4*i, wdata); + j+=4; + } +// DUG_PRINTF(":end\n"); + return REPLY_STATUS_OK; +} + +uint8_t Iap_Lite_Read(uint32_t addr, uint8_t packet_size, uint8_t* data) +{ + Flash_Read(addr, data, packet_size); + + return REPLY_STATUS_OK; +} + +uint32_t Iap_Lite_Check(uint32_t start_addr, uint32_t size) +{ + CRC32_CTX_T ctx = {0}; + CRC32_Init(&ctx); + + uint8_t temp[FLASH_PAGE_SIZE]; + uint32_t read_num = size/FLASH_PAGE_SIZE + ((size%FLASH_PAGE_SIZE) != 0); + uint32_t read_cnt = 0; + uint32_t remain = 0; + + for(read_cnt = 0;read_cnt= read_num) + { + remain = size - (read_num-1)*FLASH_PAGE_SIZE; + } + else + { + remain = FLASH_PAGE_SIZE; + } + + Flash_Read(start_addr+read_cnt*FLASH_PAGE_SIZE, temp, remain); + + CRC32_Update(&ctx, temp, remain); +//#if (IWDG_ENABLE) +// IWDG_ReloadKey(); +//#endif +// DUG_PRINTF("CRC32_Update:%x\n", CRC32_Final(&ctx)); + } + + uint32_t crc_result = CRC32_Final(&ctx); + DUG_PRINTF("iap check_crc32:%08X \n", crc_result); + return crc_result; +} + +uint8_t Iap_Lite_Verify(uint32_t addr, uint8_t packet_size, uint8_t* data) +{ + uint8_t temp[64]; + Flash_Read(addr, temp, packet_size); + + if(memcmp(temp, data, packet_size) == 0) + { + return REPLY_STATUS_OK; + } + else + { + return REPLY_STATUS_FAIL; + } +} + +void Iap_Lite_Process(uint8_t* data, uint16_t len, uint8_t host_type) +{ + IAP_LITE_CMD_T* iap_lite_cmd = (IAP_LITE_CMD_T*)data; + IAP_LITE_CMD_T iap_lite_reply; + iap_lite_reply.payload.protocol_version = iap_lite_cmd->payload.protocol_version; + iap_lite_reply.payload.reply = REPLY_STATUS_NONE; + iap_lite_reply.payload.sub_cmd = iap_lite_cmd->payload.sub_cmd; + + // ¼ì²éЭÒé°æ±¾ + if(iap_lite_cmd->payload.protocol_version == 0x01) + { + // ·ÖÎöÃüÁîÂë + switch(iap_lite_cmd->payload.sub_cmd) + { + // ×¼±¸Éý¼¶ + case IAP_LITE_CMD_READY: + { + // ½«APP0°áÔ˵½APP1±¸·Ý + uint32_t crc32 = Iap_Check(FLASH_APP_PARTITION, FLASH_APP_PARTITION_SIZE); + + IAP_LITE_CMD_T reply_ok = {.payload.protocol_version = 0x01, .payload.reply = REPLY_STATUS_OK, .payload.sub_cmd = IAP_LITE_CMD_READY}; + IAP_LITE_CMD_T reply_fail = {.payload.protocol_version = 0x01, .payload.reply = REPLY_STATUS_FAIL, .payload.sub_cmd = IAP_LITE_CMD_READY}; + Flash_Move_Query_Add(FLASH_APP_PARTITION, FLASH_APP_BACKUP_PARTITION, FLASH_APP_PARTITION_SIZE, crc32, HOST_TYPE_UART_IOT, IOT_UART_IAP_CMD, &reply_ok, sizeof(reply_ok.payload), &reply_fail, sizeof(reply_fail.payload)); + return; + } + break; + + // ²Á³ý + case IAP_LITE_CMD_ERASE: + { + iap_lite_reply.payload.reply = Iap_Lite_Erase(FLASH_APP_DOWNLOAD_PARTITION, FLASH_APP_PARTITION_SIZE); + } + break; + + // дÈë + case IAP_LITE_CMD_WRITE: + { + if(iap_lite_cmd->write.offset > FLASH_APP_PARTITION_SIZE) + { + break; + } +// DUG_PRINTF("Iap_Lite_Write:start:%d\n",systicks); + iap_lite_reply.payload.reply = Iap_Lite_Write(FLASH_APP_DOWNLOAD_PARTITION + iap_lite_cmd->write.offset, iap_lite_cmd->write.size, &iap_lite_cmd->write.content); +// DUG_PRINTF("Iap_Lite_Write:ens:%d\n",systicks); + } + break; + + // дÈëÍê±Ï + case IAP_LITE_CMD_WRITE_DONE: + { + iap_lite_reply.payload.reply = REPLY_STATUS_OK; + } + break; + + // УÑé + case IAP_LITE_CMD_CRC: + { + DUG_PRINTF("IAP_LITE_CMD_CRC :%08x %08x %08x\n",FLASH_APP_DOWNLOAD_PARTITION,iap_lite_cmd->crc.size,iap_lite_cmd->crc.crc32); + if( iap_lite_cmd->crc.size > FLASH_APP_PARTITION_SIZE) + { + iap_lite_reply.payload.reply = REPLY_STATUS_FAIL; + break; + } + if(Iap_Lite_Check(FLASH_APP_DOWNLOAD_PARTITION, iap_lite_cmd->crc.size) == iap_lite_cmd->crc.crc32) + { + iap_lite_reply.payload.reply = REPLY_STATUS_OK; + } + else + { + iap_lite_reply.payload.reply = REPLY_STATUS_FAIL; + } + } + break; + + // °áÔË + case IAP_LITE_CMD_MOVE: + { + + iap_move.move_enable = 1; + iap_move.type = MOVE_APP2_TO_APP0; + iap_move.crc32 = Iap_Lite_Check(FLASH_APP_DOWNLOAD_PARTITION, FLASH_APP_PARTITION_SIZE); + iap_move.move_time = 0; + Flash_Write_With_Flag(FLASH_MOVE_PARTITION_ADDR, FLASH_MOVE_PARTITION_FLAG, &iap_move, sizeof(IAP_MOVE_T)); + + iap_lite_reply.payload.reply = REPLY_STATUS_OK; + } + break; + //½áÊø¸´Î» + case IAP_LITE_CMD_END : + { + iap_end_flag = 1; + iap_lite_reply.payload.reply = REPLY_STATUS_OK; + } + break; + + default: + { + iap_lite_reply.payload.reply = REPLY_STATUS_NO_CMD; + } + break; + } + } + + // »Ø¸´ + if(host_type == HOST_TYPE_UART_IOT) + { + Iot_Uart_Tx_Query_Add(IOT_UART_IAP_CMD, (uint8_t*)&iap_lite_reply, sizeof(iap_lite_reply.payload)); + } +} diff --git a/App/src/io.c b/App/src/io.c index f31b91a..73ae933 100644 --- a/App/src/io.c +++ b/App/src/io.c @@ -12,7 +12,7 @@ #include "simhub.h" uint8_t platform_mode = DINPUT_MODE, platform_mode_last = DINPUT_MODE; - +uint8_t is_connect_usb = 0; /************************** ADC **************************/ #if (ADC_TOTAL_NUM) __IO uint16_t adc_value[ADC_TOTAL_NUM]; // HALL_L2 HALL_R2 @@ -487,6 +487,7 @@ void Io_Init(void) // Led_Key_Switch_Profile(0); // Led_Tele_Switch_Profile(0); // Led_Enc_Switch_Profile(0); + Led_Key_Ble_Connected_Blink(); Base_Uart_Init(); #if (IOT_ENABLE) @@ -1503,7 +1504,23 @@ void Combine_Key_Handle(void) combine_key.trigger.led_switch = 0; } #endif - + //IOTÖØÖà + if(physic_key_value == COMBINE_KEY_IOT_FACTORY) + { + if(combine_key.trigger.iot_factory == 0 && ++combine_key.count.iot_factory > 5000) + { + combine_key.trigger.iot_factory = 1; + + DUG_PRINTF("iot_factory\n"); + Iot_Key_Factory_Reset(); + + } + } + else + { + combine_key.count.iot_factory = 0; + combine_key.trigger.iot_factory = 0; + } // ÖØÖà if(physic_key_value == COMBINE_KEY_RESTORE) { diff --git a/App/src/iot.c b/App/src/iot.c index 22123e3..0b5fead 100644 --- a/App/src/iot.c +++ b/App/src/iot.c @@ -31,11 +31,11 @@ void Iot_Send_Mcu_Info(void) Iot_Uart_Tx_Query_Add(0x01, product_reply, sizeof(product_reply)); } -// ´®¿Ú½âÎö´¦ÀíÍêÒ»Ö¡Êý¾Ý È¥µôheader crc +// ´®¿Ú½âÎöÊôÐÔÉ豸ÉèÖà ȥµôheader crc // cmd: ÃüÁîÂë // data: ´¿Êý¾Ý¶ÎÖ¸Õë // len: Êý¾Ý¶Î×ܳ¤¶È -void uart_parse_dpid_frame(uint8_t cmd, uint8_t *data, uint16_t len) +void uart_parse_set_dpid_frame(uint8_t cmd, uint8_t *data, uint16_t len) { // ±ØÐëÖÁÉÙ1¸ödpidµÄ×îС³¤¶È 2+1+2 =5 if (len < 5 || data == NULL) { @@ -46,7 +46,7 @@ void uart_parse_dpid_frame(uint8_t cmd, uint8_t *data, uint16_t len) // Ñ­»·½âÎöËùÓеÄDPID£¨×Ô¶¯´¦Àí¿É±äµÄ³¤¶È£© // DF 02 01 01 00 64 E0 02 01 01 00 64 E1 02 01 01 00 64 00 - DUG_PRINTF("uart_parse_dpid_frame len=%d\n",len); + DUG_PRINTF("uart_parse_set_dpid_frame len=%d\n",len); while (1) { if(offset+5 > len) @@ -57,50 +57,117 @@ void uart_parse_dpid_frame(uint8_t cmd, uint8_t *data, uint16_t len) // 1. È¡ DPID (2¸ö×Ö½Ú) uint16_t dpid = data[offset + 0] | (data[offset + 1] << 8); - // 2. È¡ÀàÐÍ(1¸ö×Ö½Ú) - uint8_t type = data[offset + 2]; - // 3. È¡Êý¾Ý³¤¶È(2¸ö×Ö½Ú) + // 2. È¡Êý¾Ý³¤¶È(2¸ö×Ö½Ú) uint16_t data_len = data[offset + 3] | (data[offset + 4] << 8); - // 4. ¼ì²éÊ£ÓàµÄ³¤¶È¹»²»¹» - if (offset + 5 + data_len > len || data_len > 32 || data_len == 0) { + // 3. ¼ì²éÊ£ÓàµÄ³¤¶È¹»²»¹» + if (offset + 5 + data_len > len || data_len > 63 || data_len == 0) { DUG_PRINTF("offset + 5 + data_len=%d, len=%d\n",offset + 5 + data_len,data_len); break; } - // 5.Êý¾ÝÖ¸Õë - uint8_t *p_value = &data[offset + 5]; + // 4.Êý¾ÝÖ¸Õë +// uint8_t *p_value = &data[offset + 5]; // ============================== // ¸ù¾ÝÀàÐͽâÎöÊý¾Ý // ============================== - uint32_t value = 0; - char str_value[64] = {0}; - - if (type == 1) { // u8 - value = p_value[0]; - } - else if (type == 2) { // u16 - value = (p_value[0]) | (p_value[1] << 8); - } - else if (type == 4) { // u32 - value = (p_value[0]) | (p_value[1] << 8) | (p_value[2] << 16) | (p_value[3] << 24); - } - else if (type == 5) { // string - uint8_t copy_len = (data_len < 63) ? data_len : 63; - memcpy(str_value, p_value, copy_len); - str_value[copy_len] = 0; - } // DUG_PRINTF("DPID=%d, type=%d, data_len=%d\n", dpid, type, data_len); - Iot_Write_Object(dpid,&data[offset + 5],data_len); - offset += 5 + data_len; + if(Iot_Write_Object(dpid,&data[offset + 5],data_len)) + { + offset += 5 + data_len; + } + + } +} + + +// ´®¿Ú½âÎöÊôÐÔÉϱ¨dpidÊý¾Ýµã È¥µôheader crc +// cmd: ÃüÁîÂë +// data: ´¿Êý¾Ý¶ÎÖ¸Õë +// len: Êý¾Ý¶Î×ܳ¤¶È +// +//IOT: F1 09 08 01 4F 01 50 01 50 01 51 C1 +void uart_parse_get_dpid_frame(uint8_t cmd, uint8_t *data, uint8_t len) +{ + // ±ØÐëÖÁÉÙ1¸ödpidµÄ×îС³¤¶È 2+1+2 =5 + if (len <= 0 || data == NULL || (len %2) != 0) { + return; } + DUG_PRINTF("uart_parse_get_dpid_frame len=%d\n",len); + uint8_t total_len = 0; + uint16_t dpid_num = len/sizeof(uint16_t); //×ܹ²ÊǶà¸öÐèÒªÉϱ¨dpidÊôÐÔÊý¾Ý + uint8_t send_dpid_data[IOT_UART_BUFFER_MAX-8]={0}; + uint8_t tmp_buff[64]={0}; + for(uint16_t i = 0;i < dpid_num;i++) + { + if(((i*sizeof(uint16_t))+1) > len) //·ÀÖ¹Òç³ö + { + break; + } + uint16_t dpid = (data[i*sizeof(uint16_t) + 0] << 8) | data[i*sizeof(uint16_t) + 1];//ԭʼÊý¾ÝÊÇС¶ËÐò + DUG_PRINTF("dpid =%d\n",dpid); + // ============================== + // ¸ù¾ÝdpidÉϱ¨ÊôÐÔÊý¾Ý + // ============================== + memset(tmp_buff,0,sizeof(tmp_buff)); + + uint8_t data_len = 0; + uint8_t string_type = 0; + //send_dpid_dataΪ¶Á³öµÄÄÚÈÝ£¬¸ñʽΪ£º dpid ³¤¶È ÄÚÈÝ dpid ³¤¶È ÄÚÈÝ...... + if(Iot_Read_Object(dpid,tmp_buff,&string_type,&data_len)) + { + //22*5 = 110 dpid + // 108 + uint16_t item_len = 5+data_len; +// DUG_PRINTF("total_len =%d %d\n",total_len,item_len); + if(total_len + item_len >= sizeof(send_dpid_data)) + { + //Âú255ÐèÒª·¢ËÍ +// DUG_PRINTF("total_len =%d %d %d %d %d\n",total_len,total_len + item_len,item_len,data_len,sizeof(send_dpid_data)); + Iot_Uart_Tx_Query_Add(0x07, send_dpid_data,total_len); + total_len=0; + memset(send_dpid_data,0,sizeof(send_dpid_data)); + } + send_dpid_data[total_len+0] = dpid & 0xff; + send_dpid_data[total_len+1] = (dpid >> 8)&0xff; + send_dpid_data[total_len+2] = string_type; + send_dpid_data[total_len+3] = data_len & 0xff; + send_dpid_data[total_len+4] = (data_len >> 8)&0xff; + + memcpy(&send_dpid_data[total_len+5],tmp_buff,data_len); + total_len+=item_len; + + } + } + + if(total_len > 0) + { + //×îºóÒ»°ü»òÕß²»Âú255ÐèÒª·¢ËÍ + DUG_PRINTF("total_len end =%d\n",total_len); + Iot_Uart_Tx_Query_Add(0x07, send_dpid_data,total_len); + total_len=0; + } +} + +static uint8_t iot_factroy_flag = 0; +static uint8_t factory_retry_count=0; +//°´¼üʹÄÜiot»Ö¸´³ö³§ÉèÖà +void Iot_Key_Factory_Reset(void) +{ + factory_retry_count=0; + //0x13Ö¸Áî + iot_factroy_flag = 1; + Iot_Uart_Tx_Query_Add(0x13, NULL,0); } + + uint8_t got_heartbeat_flag = 0; + void Iot_Uart_Rx_Handle(uint8_t *data ,uint8_t len) { - if(data[1] != 0xb7) + if(data[1] != 0xE0) { DUG_PRINTF("IOT: "); for(uint8_t i=0;i= 4 && len <= IOT_UART_BUFFER_MAX && data[0] == FRAMEHEAD_IOT_TO_PANEL && Crc8_ROHC_Cal(&data[1], len-2) == data[len-1]) { switch (data[1]) @@ -125,11 +195,12 @@ void Iot_Uart_Rx_Handle(uint8_t *data ,uint8_t len) real_update_data[0] = iot_info.heartbeat_time; real_update_data[1] = platform_mode;//Á¬½Óƽ̨ - Iot_Uart_Tx_Query_Add(0x00, real_update_data, 2); + real_update_data[2] = base_connection.uart_conn;//ÊÇ·ñÁ¬½Ó»ù×ù»¹ÊǵçÄÔ + Iot_Uart_Tx_Query_Add(0x00, real_update_data, 3); } if(data[1] == 0xff) { - DUG_PRINTF("wifi restart:%d\n", got_heartbeat_flag); +// DUG_PRINTF("wifi restart:%d\n", got_heartbeat_flag); if(got_heartbeat_flag >= 3) { got_heartbeat_flag=0; @@ -144,27 +215,10 @@ void Iot_Uart_Rx_Handle(uint8_t *data ,uint8_t len) Iot_Uart_Tx_Query_Add(0x0C, NULL, 0); } - DUG_PRINTF("data[2]:%02x\n\n", data[2]); +// DUG_PRINTF("data[2]:%02x\n\n", data[2]); switch (data[3]) { case 0x00: - if(data[4] == 1) - { - -// iot_send_struct_t *s = &iot_send_type[IOT_SEND_KEY_COLOR]; -// DUG_PRINTF("KEY LED: %d, %d\n",s-> is_sending_flag,s-> is_send_finish_flag); -// if(s-> is_sending_flag == 0 && s-> is_send_finish_flag == 0) -// { - -// iot_start_send(IOT_SEND_KEY_COLOR,0x0d,KEY_LED_NUM,(uint8_t *)&led_key_profile[0],BUTTON_COLOR_DPID_START,3,5); -// // Iot_Start_Send_Key_Color(); - -// } -// } -// else -// { -// - } break; @@ -189,7 +243,7 @@ void Iot_Uart_Rx_Handle(uint8_t *data ,uint8_t len) uint16_t data_len = data[2]; // ͨÓÃdpid½âÎöº¯Êý Òª·ûºÏdpidÊý¾ÝЭÒé - uart_parse_dpid_frame(cmd, p_data, data_len); + uart_parse_set_dpid_frame(cmd, p_data, data_len); } // break; @@ -198,7 +252,25 @@ void Iot_Uart_Rx_Handle(uint8_t *data ,uint8_t len) case 0x08: break; - case 0x09: break; + case 0x09: //7.Ö¸¶¨ÊôÐÔÅúÁ¿²éѯ(0x09) + { + if(len == 0) + { + + break; + } + //data[0] //Ö¡Í· + //data[1] cmd + //data[2] len 1 + //data[3+n] dpid 2¸ö×Ö½Ú + uint8_t cmd = 0x07; + uint8_t *p_data = &data[3]; + uint8_t data_len = data[2]; + + // ͨÓÃdpid½âÎöº¯Êý Òª·ûºÏdpidÊý¾ÝЭÒé + uart_parse_get_dpid_frame(cmd, p_data, data_len); + } + break; case 0x0A: { @@ -232,12 +304,32 @@ void Iot_Uart_Rx_Handle(uint8_t *data ,uint8_t len) } break; + + case 0x13: + { + iot_factroy_flag=0; + } + break; + + //À¶ÑÀÒÑÁ¬½Ó + case 0x14: + { + //°´¼üµÆÉÁ˸ + key_led_status.ble_state = 1; + } + break; + case 0xB1: { Iap_Process(&data[3], data[2]); } break; + case IOT_UART_IAP_CMD: + { + Iap_Lite_Process(&data[3], data[2],HOST_TYPE_UART_IOT); + } + break; case 0xB7: { @@ -279,18 +371,18 @@ void Iot_OneTimerSecond(void) { static uint32_t timerSecond=0; static uint32_t powerOn_count=0; + if(timerSecond == 0) { timerSecond= systicks; } - if(timerSecond -systicks >= ONE_TIMER_SECOND) + if(systicks - timerSecond >= ONE_TIMER_SECOND) { timerSecond= systicks; - if(powerOn_count <= 10) { powerOn_count++; - DUG_PRINTF("iot got_heartbeat_flag:%d",got_heartbeat_flag); +// DUG_PRINTF("iot got_heartbeat_flag:%d",got_heartbeat_flag); if(powerOn_count == 10 && got_heartbeat_flag >= 3) //¿ª»ú10¼ì²éÒ»´ÎÊÇ·ñÐèÒª·¢ËÍmcuÐÅÏ¢ { Iot_Send_Mcu_Info(); @@ -298,6 +390,14 @@ void Iot_OneTimerSecond(void) Iot_Uart_Tx_Query_Add(0x0C, NULL, 0); } } + //°´¼üʹÄÜIOT»Ö¸´³ö³§ÉèÖÃ ÖØ·¢´ÎÊý3 + if(iot_factroy_flag){ + if(factory_retry_count < 3) + { + factory_retry_count++; + Iot_Uart_Tx_Query_Add(0x13, NULL, 0); + } + } } } @@ -324,38 +424,57 @@ void Iot_Uart_Tx_Query_Add(uint8_t cmd, uint8_t *pbuf, uint8_t len) iot_uart_tx_query_cnt++; } -//¶ÓÁж¨Ê±´¦Àí void Iot_Uart_Tx_Query_Process(void) { - static uint32_t iot_send_query_time = 0; + + static uint32_t systicks_iot_uart = 0; + static uint32_t iot_uart_tick = 0; if(iot_uart_tx_query_cnt == 0) return; - if(systicks - iot_send_query_time >= 20) - { - iot_send_query_time = systicks; + if(systicks - iot_uart_tick >= 50) // +// if(systicks - iot_uart_tick >= 100) + { + iot_uart_tick = systicks; + +// if(uart_is_send_finish()) +// { +// systicks_iot_uart++; +// } +// - iot_uart_send(iot_uart_tx_query[0].buffer, iot_uart_tx_query[0].size); +// if(systicks_iot_uart - iot_send_query_time >= 30) + { + +// iot_send_query_time = systicks_iot_uart; + + iot_uart_send(iot_uart_tx_query[0].buffer, iot_uart_tx_query[0].size); +// DUG_PRINTF("iot_uart_tx_query[0].size %d\n",iot_uart_tx_query[0].size); #if (1) - if(iot_uart_tx_query[0].buffer[0] != 0xb7){ - DUG_PRINTF("iot send:"); - for(uint8_t i=0;i= 0xA) && (*src <= 0xF)) desc = *src + 0x37; // ͳһ´óд - + return desc; } -uint8_t Iot_Read_Object(uint16_t dp_id, void* buf, uint8_t* len) +uint8_t Iot_Read_Object(uint16_t dp_id, void* buf,uint8_t *is_string, uint8_t* len) { uint8_t err = 0; + if(dp_id >= DP_OBJECT_MAX_NUM) + { + DUG_PRINTF("read dpid >= DP_OBJECT_MAX_NUM %d %d\n",dp_id,DP_OBJECT_MAX_NUM); + return 0; // + } + if(dp_object[dp_id].object != NULL) { + if(dp_object[dp_id].rw_prop == IOT_WRITE_ONLY) + { + return err; + } err = 1; // ³É¹¦ if(dp_object[dp_id].string_type > 0) // ÊÇ·ñΪ×Ö·û´® { *len = 0; - if(dp_object[dp_id].string_type == 2) + if(dp_object[dp_id].string_type == 2) //´ø#×Ö·û´®´« { // ¿ªÍ·¼ÓÉÏ#ºÅ (((char*)buf)[*len]) = '#'; *len += 1; + }else if(dp_object[dp_id].string_type == 1) //ÆÕͨÊý¾ÝÐèҪת³É×Ö·û´® + { + + if(dp_object[dp_id].data_len == 1) + { + uint8_t value = *(uint8_t *)dp_object[dp_id].object; + char hex_str[4] = {0}; + snprintf(hex_str, sizeof(hex_str), "%02X",value); + // ¿½±´µ½Êä³öbuf + // ¹Ì¶¨¿½±´ 2 ¸ö×Ö·û£¬ + ((char*)buf)[*len + 0] = hex_str[0]; + ((char*)buf)[*len + 1] = hex_str[1]; + + *len += dp_object[dp_id].data_len; // 1 ×Ö½Ú ¡ú 2 ¸öÊ®Áù½øÖÆ×Ö·û + *is_string = 5; + return err; + } + else if(dp_object[dp_id].data_len == 2) //uint16_t 0XFFAF ÐèҪת»»³É "FFAF" + { + // È¡³ö uint16_t + + uint16_t value = *(uint16_t *)dp_object[dp_id].object; + // ¸ß8λ¡¢µÍ8λ + uint8_t h = (value >> 8) & 0xFF; + uint8_t l = value & 0xFF; + + // ת³É 4 λ´óдʮÁù½øÖÆ×Ö·û´® + char hex_str[6] = {0}; + snprintf(hex_str, sizeof(hex_str), "%02X%02X", h, l); + // ¿½±´µ½Êä³öbuf + // ¹Ì¶¨¿½±´ 4 ¸ö×Ö·û£¬²»ÊÜ 00 Ó°Ïì ? + ((char*)buf)[*len + 0] = hex_str[0]; + ((char*)buf)[*len + 1] = hex_str[1]; + ((char*)buf)[*len + 2] = hex_str[2]; + ((char*)buf)[*len + 3] = hex_str[3]; + + *len += dp_object[dp_id].data_len*2; // 2 ×Ö½Ú ¡ú 4 ¸öÊ®Áù½øÖÆ×Ö·û + *is_string = 5; + return err; + + }else if(dp_object[dp_id].data_len == 4) //uint16_t 0XFFAFAEEF ÐèҪת»»³É "FFAFAEEF" + { + // È¡³ö uint32_t + + uint32_t value = *(uint32_t *)dp_object[dp_id].object; + // ¸ß8λ¡¢µÍ8λ + uint8_t H1 = (value >> 24) & 0xFF; + uint8_t H2 = (value >> 16) & 0xFF; + uint8_t L1 = (value >> 8) & 0xFF; + uint8_t L2 = value & 0xFF; + + // ת³É 4 λ´óдʮÁù½øÖÆ×Ö·û´® + char hex_str[10] = {0}; + snprintf(hex_str, sizeof(hex_str), "%02X%02X%02X%02X",H1,H2,L1,L2); + // ¿½±´µ½Êä³öbuf + // ¹Ì¶¨¿½±´ 6 ¸ö×Ö·û + ((char*)buf)[*len + 0] = hex_str[0]; + ((char*)buf)[*len + 1] = hex_str[1]; + ((char*)buf)[*len + 2] = hex_str[2]; + ((char*)buf)[*len + 3] = hex_str[3]; + ((char*)buf)[*len + 4] = hex_str[4]; + ((char*)buf)[*len + 5] = hex_str[5]; + ((char*)buf)[*len + 6] = hex_str[6]; + ((char*)buf)[*len + 7] = hex_str[7]; + + *len += dp_object[dp_id].data_len*2; // 4 ×Ö½Ú ¡ú 8 ¸öÊ®Áù½øÖÆ×Ö·û + *is_string = 5; + return err; + + } } - for(uint8_t i=0;i 1) { - uint8_t byte_h = (((uint8_t*)dp_object[dp_id].object)[i] & 0xF0) >> 4; - uint8_t byte_l = (((uint8_t*)dp_object[dp_id].object)[i] & 0x0F); - (((char*)buf)[*len]) = hex_2_char(&byte_h); - *len += 1; - (((char*)buf)[*len]) = hex_2_char(&byte_l); - *len += 1; + for(uint8_t i=0;i= DP_OBJECT_MAX_NUM) + { + DUG_PRINTF("write dpid >= DP_OBJECT_MAX_NUM %d %d\n",dp_id,DP_OBJECT_MAX_NUM); + return 0; + } if(dp_object[dp_id].object != NULL) { + if(dp_object[dp_id].rw_prop == IOT_READ_ONLY) + { + return err; + } if(dp_object[dp_id].string_type == 0) // ²»ÊÇ×Ö·û´® { // if(len <= dp_object[dp_id].data_len) @@ -2181,7 +2436,7 @@ uint8_t Iot_Write_Object(uint16_t dp_id, void* buf, uint8_t len) } else // ×Ö·û´® { - if(dp_object[dp_id].string_type == 2 && len == 7 && ((uint8_t*)buf)[0] == '#') // ×Ö·û´®ÀàÐÍ1 ²¢ÇÒµÚÒ»¸ö×Ö½ÚÊÇ#ºÅ×Ö·û + if(dp_object[dp_id].string_type == 2 && len >= 7 && ((uint8_t*)buf)[0] == '#') // ×Ö·û´®ÀàÐÍ1 ²¢ÇÒµÚÒ»¸ö×Ö½ÚÊÇ#ºÅ×Ö·û { LED_COLOR_T color = {0}; uint8_t* ptr = buf; @@ -2204,7 +2459,11 @@ uint8_t Iot_Write_Object(uint16_t dp_id, void* buf, uint8_t len) } }else{ //Ö»¸Ä±äµ¥¸öµÆÑÕÉ« DUG_PRINTF("single color\n"); - memcpy(dp_object[dp_id].object,&color,sizeof(LED_COLOR_T)); + if(dp_object[dp_id].data_len == sizeof(LED_COLOR_T)) + { + memcpy(dp_object[dp_id].object,&color,sizeof(LED_COLOR_T)); + } + } diff --git a/App/src/led.c b/App/src/led.c index 720f7d3..15f51ee 100644 --- a/App/src/led.c +++ b/App/src/led.c @@ -1997,6 +1997,83 @@ void Led_Key_Preview(void) } } +void Led_Key_Ble_Connected_Blink(void) +{ + // Ó¦Óà + for(uint8_t num=0;num