fix: 回调注册前置到 socket 操作之前 + 入口诊断日志

- lup_set_sensor_callback 移到 SocketCreat/SocketListen 之前
  避免 socket 失败 return 导致回调漏注册
- json_sensor_callback 入口打印 socket/auth/report 三状态
  各检查点分别打印 skip 原因
This commit is contained in:
wangfq
2026-07-02 14:35:56 +08:00
parent c4a2b50ca5
commit ff17bbbc88

View File

@@ -752,10 +752,13 @@ static int format_loop_param_json(char *buf, uint16_t buf_size, const LUP_ParamG
static void json_sensor_callback(const uint8_t *pkg, uint16_t len) static void json_sensor_callback(const uint8_t *pkg, uint16_t len)
{ {
PRINT("JSON: sensor_cb enter socket=%d auth=%d report=%d\n",
g_json_socket_listen, g_json_auth_state, g_report_active);
// Check: socket active, authed, report enabled // Check: socket active, authed, report enabled
if (g_json_socket_listen == 0xFF) return; if (g_json_socket_listen == 0xFF) { PRINT("JSON: sensor_cb skip: no socket\n"); return; }
if (g_json_auth_state != JSON_STATE_AUTHED) return; if (g_json_auth_state != JSON_STATE_AUTHED) { PRINT("JSON: sensor_cb skip: not authed\n"); return; }
if (!g_report_active) return; if (!g_report_active) { PRINT("JSON: sensor_cb skip: report disabled\n"); return; }
LUP_SensorReport sr; LUP_SensorReport sr;
memset(&sr, 0, sizeof(sr)); memset(&sr, 0, sizeof(sr));
@@ -792,6 +795,10 @@ void tcp_json_srv_init(void) {
if (_init_done) return; // 防止 net_srv_init 反复调用 if (_init_done) return; // 防止 net_srv_init 反复调用
_init_done = 1; _init_done = 1;
// 注册传感器回调 — 必须在 socket 操作之前,
// 避免 socket 失败导致 return 而漏掉注册
lup_set_sensor_callback(json_sensor_callback);
memset(&sock_inf, 0, sizeof(SOCK_INF)); memset(&sock_inf, 0, sizeof(SOCK_INF));
sock_inf.SourPort = TCP_JSON_PORT; sock_inf.SourPort = TCP_JSON_PORT;
sock_inf.ProtoType = PROTO_TYPE_TCP; sock_inf.ProtoType = PROTO_TYPE_TCP;
@@ -809,9 +816,6 @@ void tcp_json_srv_init(void) {
return; return;
} }
// Register sensor callback: 0xC0 frames from Loop MCU → TCP push
lup_set_sensor_callback(json_sensor_callback);
PRINT("JSON: TCP listen on port %d (socket %d)\n", TCP_JSON_PORT, g_json_socket_listen); PRINT("JSON: TCP listen on port %d (socket %d)\n", TCP_JSON_PORT, g_json_socket_listen);
} }