This commit is contained in:
2026-03-14 21:47:25 +08:00
parent 9230e7985d
commit 30cbf31276
2 changed files with 116 additions and 26 deletions
@@ -69,7 +69,7 @@ static void tcp_stream_init(TcpStreamCtx_t *ctx, const char *label) {
}
static void tcp_stream_disconnect(TcpStreamCtx_t *ctx) {
DBG_LOGIC("[%s] disconnecting\r\n", ctx->label);
DBG_LOGIC("!! [%s] Disconnected\r\n", ctx->label);
ctx->is_connected = 0;
if (ctx->sock) {
qdx_port_tcp_close(ctx->sock);
@@ -90,7 +90,7 @@ static int8_t tcp_stream_connect(TcpStreamCtx_t *ctx, const char *ip,
ctx->last_activity_ms = qdx_port_get_tick_ms();
ctx->last_heartbeat_ms = ctx->last_activity_ms;
ctx->recv_len = 0;
DBG_LOGIC("[%s] connected OK\r\n", ctx->label);
DBG_LOGIC(">> [%s] Connected to %s:%d\r\n", ctx->label, ip, port);
return 0;
}
@@ -121,7 +121,7 @@ static int32_t tcp_send_frame(TcpStreamCtx_t *ctx, uint8_t msg_class,
}
static void tcp_send_handshake(TcpStreamCtx_t *ctx) {
DBG_LOGIC("[%s] sending handshake\r\n", ctx->label);
DBG_LOGIC(">> [%s] Handshake (DevID=%d)\r\n", ctx->label, (int)g_TcpLogic.dev_id);
uint8_t payload[54];
memset(payload, 0, sizeof(payload));
qdx_write_u16_le(payload + 0, 0x0200);
@@ -271,6 +271,8 @@ static void parse_and_dispatch_tlv(TcpStreamCtx_t *ctx, const uint8_t *packet,
case TYPE_DEVID_ASSIGN: {
if (len >= sizeof(DevIDAssignment_t)) {
uint16_t new_id = qdx_read_u16_le(value);
DBG_LOGIC("<< [%s] DevID assigned: %d -> %d\r\n",
ctx->label, (int)g_TcpLogic.dev_id, (int)new_id);
g_TcpLogic.pending_new_dev_id = new_id;
tcp_send_ack(ctx, hdr_seq, 0, 0);
}
@@ -283,19 +285,40 @@ static void parse_and_dispatch_tlv(TcpStreamCtx_t *ctx, const uint8_t *packet,
g_TcpLogic.has_valid_config = 1;
cfg_updated = 1;
qdx_port_mutex_unlock(g_TcpLogic.config_mutex);
DBG_LOGIC("<< [%s] ConfigCommon: Mode=%d Tag=%d Strict=%d\r\n",
ctx->label,
(int)g_TcpLogic.cached_common.WorkMode,
(int)g_TcpLogic.cached_common.ConfigTag,
(int)g_TcpLogic.cached_common.StrictnessLevel);
}
break;
}
case TYPE_CONFIG_2D: {
if (len >= sizeof(Config2D_t)) {
DBG_LOGIC("[%s] Config2D parsed OK\r\n", ctx->label);
qdx_port_mutex_lock(g_TcpLogic.config_mutex);
qdx_deserialize_config2d(&g_TcpLogic.cached_cfg2d, value);
g_TcpLogic.has_valid_config = 1; /* 任意配置到达即标记有效 */
g_TcpLogic.has_valid_config = 1;
cfg_updated = 1;
qdx_port_mutex_unlock(g_TcpLogic.config_mutex);
DBG_LOGIC("<< [%s] Config2D: En=%d %dx%d Fps=%d "
"Trig=%d Burst=%d Intv=%d Thresh=%d "
"ROI(%d,%d,%d,%d) NGio=%d\r\n",
ctx->label,
(int)g_TcpLogic.cached_cfg2d.Enabled,
(int)g_TcpLogic.cached_cfg2d.Width,
(int)g_TcpLogic.cached_cfg2d.Height,
(int)g_TcpLogic.cached_cfg2d.Fps,
(int)g_TcpLogic.cached_cfg2d.TriggerMode,
(int)g_TcpLogic.cached_cfg2d.TriggerBurstCount,
(int)g_TcpLogic.cached_cfg2d.TriggerInternalIntervalMs,
(int)g_TcpLogic.cached_cfg2d.TriggerTemperatureThreshold,
(int)g_TcpLogic.cached_cfg2d.TriggerRoiX,
(int)g_TcpLogic.cached_cfg2d.TriggerRoiY,
(int)g_TcpLogic.cached_cfg2d.TriggerRoiW,
(int)g_TcpLogic.cached_cfg2d.TriggerRoiH,
(int)g_TcpLogic.cached_cfg2d.NGioDelay);
} else {
DBG_LOGIC("[%s] ! Config2D bad len=%d (need %d)\r\n",
DBG_LOGIC("<< [%s] Config2D bad len=%d (need %d)\r\n",
ctx->label, len, (int)sizeof(Config2D_t));
}
break;
@@ -304,34 +327,47 @@ static void parse_and_dispatch_tlv(TcpStreamCtx_t *ctx, const uint8_t *packet,
if (len >= sizeof(Config1D_t)) {
qdx_port_mutex_lock(g_TcpLogic.config_mutex);
qdx_deserialize_config1d(&g_TcpLogic.cached_cfg1d, value);
g_TcpLogic.has_valid_config = 1; /* 任意配置到达即标记有效 */
g_TcpLogic.has_valid_config = 1;
cfg_updated = 1;
qdx_port_mutex_unlock(g_TcpLogic.config_mutex);
DBG_LOGIC("<< [%s] Config1D: En=%d RunMode=%d TrigType=%d "
"BufSz=%d TempLim=%d NGio=%d\r\n",
ctx->label,
(int)g_TcpLogic.cached_cfg1d.Enabled,
(int)g_TcpLogic.cached_cfg1d.RunMode,
(int)g_TcpLogic.cached_cfg1d.TriggerType,
(int)g_TcpLogic.cached_cfg1d.BufferSize,
(int)g_TcpLogic.cached_cfg1d.TriggerTempLimit,
(int)g_TcpLogic.cached_cfg1d.NGioDelay);
}
break;
}
case TYPE_TEMP_FRAME: {
DBG_LOGIC("<< [%s] TempFrame request (len=%d)\r\n", ctx->label, (int)len);
if (g_TcpLogic.temp_req_cb) {
/* If payload length is >= 18 (TemperatureFrameHeader_t), we can peek
at Is2D. Otherwise we pass 0 or a default value. For now let's pass
an indicator if Is2D is set. */
uint8_t is2d = 0;
if (len >= 18) {
is2d = value[18]; /* index 18 in TemperatureFrameHeader_t is Is2D */
is2d = value[18];
}
DBG_LOGIC(" -> callback: is2D=%d\r\n", (int)is2d);
g_TcpLogic.temp_req_cb(is2d);
}
break;
}
case TYPE_DETECTION_RESULT: {
if (len >= sizeof(DetectionResult_t) && g_TcpLogic.detect_cb) {
if (len >= sizeof(DetectionResult_t)) {
uint32_t frame_num = qdx_read_u32_le(value);
uint8_t result_status = value[4];
g_TcpLogic.detect_cb(frame_num, result_status);
DBG_LOGIC("<< [%s] DetectionResult: frm=%d result=%d\r\n",
ctx->label, (int)frame_num, (int)result_status);
if (g_TcpLogic.detect_cb)
g_TcpLogic.detect_cb(frame_num, result_status);
}
break;
}
default:
DBG_LOGIC("<< [%s] Unknown TLV type=0x%02x len=%d\r\n",
ctx->label, (int)type, (int)len);
break;
}
@@ -340,9 +376,7 @@ static void parse_and_dispatch_tlv(TcpStreamCtx_t *ctx, const uint8_t *packet,
}
if (cfg_updated && g_TcpLogic.config_cb && g_TcpLogic.has_valid_config) {
/* Safely trigger callback. Passing pointers to cached config is ok
during the context of this thread, but user must copy if they
dispatch to another task. */
DBG_LOGIC("<< [%s] Config updated -> notify app\r\n", ctx->label);
qdx_port_mutex_lock(g_TcpLogic.config_mutex);
g_TcpLogic.config_cb(&g_TcpLogic.cached_common, &g_TcpLogic.cached_cfg2d,
&g_TcpLogic.cached_cfg1d);
@@ -604,6 +638,11 @@ TcpLogic_BuildAndSendTemperatureFrame(TcpTxBuffer_t *io_buffer,
g_TcpLogic.frame_count++;
DBG_LOGIC(">> [Data] TempFrame #%d: %dx%d type=%d is2D=%d payload=%d\r\n",
(int)processMeta->FrameNumber,
(int)processMeta->ValidWidth, (int)processMeta->ValidHeight,
(int)frameType, (int)is2D, (int)io_buffer->ValidPayloadLen);
/* We need to prepend: TLV Header (3) + TemperatureFrameHeader_t (18) */
uint32_t tlv_wrapper_len = TLV_HEADER_SIZE + sizeof(TemperatureFrameHeader_t);
@@ -666,6 +705,9 @@ TcpLogic_BuildAndSendTemperatureFrame(TcpTxBuffer_t *io_buffer,
uint32_t frag_count =
(total_tlv_len + MAX_FRAGMENT_PAYLOAD - 1) / MAX_FRAGMENT_PAYLOAD;
DBG_LOGIC(">> [Data] Fragmented: %d frags, total=%d\r\n",
(int)frag_count, (int)total_tlv_len);
for (uint32_t i = 0; i < frag_count; i++) {
uint32_t chunk_len = total_tlv_len - offset;
if (chunk_len > MAX_FRAGMENT_PAYLOAD)