feat(vd960DBN): TCP JSON协议服务 — 端口5960, 鉴权+15条命令

- net_config.h: TCP_LISTEN=0→1, TCP=2 支持 JSON 监听
- 新增 tcp_json_srv.h/c: 行分隔 JSON, pwd_verify鉴权, 命令分发
- 实现15条协议命令: dev_info/ssc_net/iot_net/iot_topic/pwd_set/factory_reset等
- loop_param_set/query 接受命令返回stub(Loop MCU中继待实现)
- net_srv.c: 集成 JSON 中断路由 + init
- peripheral_main.c: 主循环 tcp_json_poll()
This commit is contained in:
wangfq
2026-06-30 14:53:53 +08:00
parent ece82be6dd
commit af997a79fe
5 changed files with 843 additions and 23 deletions
@@ -24,9 +24,9 @@ extern "C" {
#define WCHNET_NUM_UDP 1 /* The number of UDP connections */
#define WCHNET_NUM_TCP 1 /* Number of TCP connections */
#define WCHNET_NUM_TCP 2 /* Number of TCP connections (1 SSC/MQTT + 1 JSON accepted) */
#define WCHNET_NUM_TCP_LISTEN 0 /* Number of TCP listening */
#define WCHNET_NUM_TCP_LISTEN 1 /* Number of TCP listening (JSON protocol on port 5960) */
/* The number of sockets, the maximum is 31 */
#define WCHNET_MAX_SOCKET_NUM (WCHNET_NUM_IPRAW+WCHNET_NUM_UDP+WCHNET_NUM_TCP+WCHNET_NUM_TCP_LISTEN)
@@ -0,0 +1,63 @@
/**
******************************************************************************
* @file tcp_json_srv.h
* @author wangfq
* @version V1.0
* @date 2026-06-30
* @brief TCP JSON protocol server — DLD960 TCP JSON protocol handler
* Port 5960, line-delimited JSON, auth + command dispatch
******************************************************************************
*/
#ifndef __TCP_JSON_SRV_H__
#define __TCP_JSON_SRV_H__
#include <stdint.h>
/*===========================================================================
* Protocol Constants
*===========================================================================*/
#define TCP_JSON_PORT 5960 // TCP JSON protocol default port
#define TCP_JSON_MAX_FRAME 4096 // Max JSON frame length
#define TCP_JSON_AUTH_TIMEOUT_MS 60000 // 60s auth timeout (ms)
#define TCP_JSON_RECV_BUF_LEN (TCP_JSON_MAX_FRAME * 2)
#define TCP_JSON_MAX_PWD_RETRY 3 // Max password retries before lockout
#define TCP_JSON_LOCKOUT_TIMEOUT_MS 60000 // 60s lockout after max retries
/*===========================================================================
* Auth / State
*===========================================================================*/
typedef enum {
JSON_STATE_WAIT_AUTH = 0, // Waiting for pwd_verify
JSON_STATE_AUTHED, // Authenticated, commands accepted
JSON_STATE_LOCKOUT // Locked out (max retries exceeded)
} TcpJsonAuthState;
/*===========================================================================
* Error Codes
*===========================================================================*/
#define JSON_CODE_SUCCESS 0
#define JSON_CODE_PARAM_ERR 1
#define JSON_CODE_AUTH_FAIL 2
#define JSON_CODE_BUSY 3
#define JSON_CODE_UNSUPPORTED 4
#define JSON_CODE_INTERNAL_ERR 5
#define JSON_CODE_DATA_TOO_LONG 6
#define JSON_CODE_NOT_AUTHED 7
/*===========================================================================
* Externs
*===========================================================================*/
extern uint8_t g_json_socket_listen; // listen socket ID
extern uint8_t g_json_socket_client; // accepted client socket ID (0xFF = none)
extern TcpJsonAuthState g_json_auth_state;
extern uint32_t g_json_auth_timer; // ms timer for auth timeout
extern uint8_t g_json_pwd_retry; // password retry counter
/*===========================================================================
* API
*===========================================================================*/
void tcp_json_srv_init(void); // Create listen socket on port 5960
void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat); // Socket interrupt handler
void tcp_json_poll(void); // Periodic poll (auth timeout, etc.)
#endif /* __TCP_JSON_SRV_H__ */