diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h index 65da387..378b22a 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/iot_mqtt_srv.h @@ -5,7 +5,7 @@ * @version V1.0 * @date 2026-07-03 * @brief IoT MQTT 客户端 — DLD960 IoT 协议 (MQTT + JSON) - * 当 NET_SSC_ENABLE=0 时启用, 替代 SSC 模式 + * 当 g_sub_code_enable.iot_enable=1 时启用, 替代 SSC 模式 * 协议参考: docs/DLD960_IoT_MQTT协议.md ****************************************************************************** */ diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h index 9d38787..90841b9 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h @@ -18,39 +18,19 @@ extern "C" { #endif /********************************************************************* - * Feature toggles (0 = disabled, 1 = enabled) + * Feature toggles * - * 模式互斥: - * NET_SSC_ENABLE=1 → SSC 模式 (UDP+TCP client) + TCP JSON server - * NET_SSC_ENABLE=0 → IoT MQTT 模式 (MQTT client) - */ -#define NET_SSC_ENABLE 0 /* SSC (UDP + TCP client) protocol support */ -#define NET_JSON_ENABLE 1 /* TCP JSON protocol server (port 5960) */ -#define NET_IOT_ENABLE (!NET_SSC_ENABLE) /* IoT MQTT client (auto-derived) */ - -/********************************************************************* - * socket configuration, IPRAW + UDP + TCP + TCP_LISTEN = number of sockets + * 模式不再通过编译宏选择, 改为运行时 g_sub_code_enable.iot_enable: + * iot_enable=0 → SSC 模式 (UDP+TCP client + TCP JSON server) + * iot_enable=1 → IoT MQTT 模式 (MQTT client) + * + * Socket 数量按最坏情况 (SSC 模式) 静态分配: + * 1 UDP + 2 TCP + 1 TCP_LISTEN = 4 */ +#define WCHNET_NUM_UDP 1 /* SSC 模式需要 UDP */ +#define WCHNET_NUM_TCP 2 /* JSON(1) + SSC client(1) max */ #define WCHNET_NUM_IPRAW 0 /* Number of IPRAW connections */ -#if NET_SSC_ENABLE -#define WCHNET_NUM_UDP 1 /* SSC mode: 1 UDP */ -#else -#define WCHNET_NUM_UDP 0 /* IoT mode: 0 UDP */ -#endif - -#if NET_JSON_ENABLE && NET_SSC_ENABLE -#define WCHNET_NUM_TCP 2 /* SSC+JSON: JSON(1) + SSC client(1) */ -#elif NET_JSON_ENABLE -#define WCHNET_NUM_TCP 1 /* JSON only: listen+data(1) */ -#elif NET_SSC_ENABLE -#define WCHNET_NUM_TCP 1 /* SSC only: TCP client(1) */ -#elif NET_IOT_ENABLE -#define WCHNET_NUM_TCP 1 /* IoT MQTT client(1) */ -#else -#define WCHNET_NUM_TCP 0 -#endif - #define WCHNET_NUM_TCP_LISTEN 1 /* Number of TCP listening (required internally) */ /* The number of sockets, the maximum is 31 */ diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_srv.h b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_srv.h index 631c56e..83d5b7a 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_srv.h +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_srv.h @@ -249,10 +249,8 @@ void write_net_config(Local_Net_Cfg *local, NET_CENTER_INFO *center, IOT_NET_INF extern uint8_t g_flag_timestamp; extern uint8_t RemoteIP[4]; -#if NET_SSC_ENABLE extern uint8_t SocketId_TCP; extern uint8_t SocketId_UDP; -#endif void manage_udp_message(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len); @@ -283,10 +281,8 @@ void GetMacAddr(unsigned char *pMAC); void mStopIfError(u8 iError); void net_srv_init(void); -#if NET_SSC_ENABLE void WCHNET_CreateTcpMqttSocket(void); void WCHNET_CreateTcpSocket(void); -#endif void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat); int get_ipstr_to_array(char *src, uint8_t *dst); void WCHNET_HandleGlobalInt(void); diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c index 39b0ef8..b15bbfe 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c @@ -18,9 +18,7 @@ #include "cmcng.h" #include "simple_json.h" #include "tcp_json_srv.h" -#if NET_IOT_ENABLE #include "iot_mqtt_srv.h" -#endif uint32_t slen; @@ -58,8 +56,6 @@ uint16_t srcport = 6000; //UINT16 aport=1000; /* CH579?????????? */ uint32_t g_wdg_counter = 0; -#if NET_SSC_ENABLE - uint8_t SocketId_TCP; uint8_t SocketId_UDP ; uint8_t socket[WCHNET_MAX_SOCKET_NUM]; //Save the currently connected socket @@ -86,8 +82,6 @@ void clear_mqtt_buf(void) memset(mqttBuf, 0, MAX_MQTTBUF_LEN); } -#endif /* NET_SSC_ENABLE */ - /********************************************************************* * @fn mStopIfError * @@ -103,8 +97,6 @@ void mStopIfError(u8 iError) printf("Error: 0x%02X\r\n", (u16)iError); } -#if NET_SSC_ENABLE - void mqtt_connect(void) { int len = 0; @@ -470,8 +462,6 @@ void WCHNET_DataManage(uint8_t id) } -#endif /* NET_SSC_ENABLE */ - @@ -487,15 +477,21 @@ void WCHNET_DataManage(uint8_t id) */ void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) { -#if NET_SSC_ENABLE - uint8_t i; +uint8_t i; - // Route JSON protocol socket events (both TCP + TCP_LISTEN internal) + // Route JSON protocol socket events -- always active regardless of mode if (socketid == g_json_socket_listen || socketid == (g_json_socket_listen + 1)) { tcp_json_handle_sock_int(socketid, intstat); return; } + // IoT MQTT mode -- route to MQTT handler + if (g_sub_code_enable.iot_enable) { + iot_mqtt_handle_sock_int(socketid, intstat); + return; + } + + // === SSC mode below === g_net_state.intstat = intstat; if (intstat & SINT_STAT_RECV) //receive data @@ -513,48 +509,24 @@ void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) PRINT("TCP Connect Success\r\n"); PRINT("socket id: %d\r\n", socketid); } - - // if(g_sub_code_enable.iot_enable){ - // mqtt_connect(); - // } } if (intstat & SINT_STAT_DISCONNECT) //disconnect { { PRINT("TCP Disconnect\r\n"); } -// WCHNET_SocketClose(socketid, 0); g_net_state.flag = 1; } if (intstat & SINT_STAT_TIM_OUT) //timeout disconnect { -// for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) { //delete disconnected socket id -// if (socket[i] == socketid) { -// socket[i] = 0xff; -// break; -// } -// } -// if(g_flag_debug) { PRINT("TCP Timeout\r\n"); } -// WCHNET_SocketClose(socketid, 0); g_net_state.flag = 1; - // WCHNET_CreateTcpSocket(); } -#else /* !NET_SSC_ENABLE */ -#if NET_IOT_ENABLE - /* IoT MQTT mode — route MQTT socket, JSON server not active */ - iot_mqtt_handle_sock_int(socketid, intstat); -#else - tcp_json_handle_sock_int(socketid, intstat); -#endif -#endif } -#if NET_SSC_ENABLE - void dbn_net_ssc_srv(void) { uint8_t _flag_timestamp = 0; @@ -615,8 +587,6 @@ void poll_mqtt(void) } } -#endif /* NET_SSC_ENABLE */ - /******************************************************************************* * Function Name : GetMacAddr @@ -672,8 +642,6 @@ int get_ipstr_to_array(char *src, uint8_t *dst) { } -#if NET_SSC_ENABLE - void unpack_ssc_count_off(uint8_t socket,uint8_t count_mode, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len) { uint16_t i,j; @@ -1047,8 +1015,6 @@ void manage_mqtt_recv_message(char * msg, int length) } } -#endif /* NET_SSC_ENABLE */ - @@ -1118,9 +1084,7 @@ void net_srv_init(void) WCHNET_ConfigKeepLive(&cfg); } #endif -#if NET_SSC_ENABLE memset(socket, 0xff, WCHNET_MAX_SOCKET_NUM); -#endif } @@ -1128,17 +1092,15 @@ void net_srv_init(void) if(g_net_state.flag == 1) { -#if NET_SSC_ENABLE - WCHNET_CreateUdpSocket(); -#endif -#if NET_IOT_ENABLE - iot_mqtt_init(); // Init IoT MQTT client (connect to broker) -#else - tcp_json_srv_init(); // Start JSON protocol TCP listener on port 5960 -#endif -#if !NET_SSC_ENABLE - g_net_state.flag = 2; // Skip SSC init, advance state directly -#endif + if (g_sub_code_enable.iot_enable) { + /* IoT MQTT mode -- init MQTT client, skip UDP */ + iot_mqtt_init(); // Init IoT MQTT client (connect to broker) + g_net_state.flag = 2; // Skip SSC init, advance state directly + } else { + /* SSC mode -- create UDP socket + JSON server */ + WCHNET_CreateUdpSocket(); + tcp_json_srv_init(); // Start JSON protocol TCP listener on port 5960 + } } diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral_main.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral_main.c index 5a58480..2e85877 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral_main.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral_main.c @@ -24,9 +24,7 @@ #include "storage.h" #include "tcp_json_srv.h" #include "loop_uart_proto.h" -#if NET_IOT_ENABLE #include "iot_mqtt_srv.h" -#endif /********************************************************************* * GLOBAL TYPEDEFS @@ -239,7 +237,6 @@ void Main_Circulation(void) } if(g_net_state.flag == 2) { -#if NET_SSC_ENABLE if(g_sub_code_enable.iot_enable){ if(iot_net_info.mode == IOT_Addr_IP_Mode){ if(get_ipstr_to_array(iot_net_info.remote_addr, RemoteIP) == 0){ @@ -255,7 +252,6 @@ void Main_Circulation(void) { WCHNET_CreateTcpSocket(); } -#endif } @@ -273,13 +269,13 @@ void Main_Circulation(void) poll_dbn_ble(); -#if NET_IOT_ENABLE - iot_mqtt_publish_sensor(); // Push 0xC0 sensor data to MQTT broker - iot_mqtt_poll(); // IoT MQTT state machine + heartbeat -#else - tcp_json_push_sensor(); // Push 0xC0 sensor data to TCP JSON client - tcp_json_poll(); -#endif + if (g_sub_code_enable.iot_enable) { + iot_mqtt_publish_sensor(); // Push 0xC0 sensor data to MQTT broker + iot_mqtt_poll(); // IoT MQTT state machine + heartbeat + } else { + tcp_json_push_sensor(); // Push 0xC0 sensor data to TCP JSON client + tcp_json_poll(); + } key_event_srv(); }