diff --git a/DBNetClient/tcp_json_client.py b/DBNetClient/tcp_json_client.py index 49b45bd..40f48bc 100644 --- a/DBNetClient/tcp_json_client.py +++ b/DBNetClient/tcp_json_client.py @@ -92,7 +92,8 @@ class TcpJsonClient: try: self._sock.sendall(raw.encode("utf-8")) - self._log(f">>> {cmd} (msg_id={msg_id})") + self._log(f">>> {cmd} (msg_id={msg_id}) [{len(raw)} bytes]") + self._log(f">>> RAW: {raw.strip()}") except OSError as e: with self._lock: self._pending.pop(msg_id, None) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c index acb01a4..dc6c42d 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c @@ -487,7 +487,7 @@ void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) return; } // Also catch newly accepted TCP connections that might be JSON clients - if (intstat & SINT_STAT_CONNECT && g_json_socket_client == 0xFF && socketid != g_json_socket_listen) { + if ((intstat & SINT_STAT_CONNECT) && socketid != SocketId_TCP && socketid != SocketId_UDP) { tcp_json_handle_sock_int(socketid, intstat); return; } diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c index fd380bb..420082b 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c @@ -614,30 +614,9 @@ void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) { // === Listen socket events === if (socketid == g_json_socket_listen) { if (intstat & SINT_STAT_CONNECT) { - // A client connected — find the accepted socket - // The accepted connection gets a different socket ID - // We find it by scanning TCP sockets in ESTABLISHED state - uint8_t i; - for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) { - if (i == g_json_socket_listen) continue; - // Check if this socket is in ESTABLISHED state and not our SSC/MQTT socket - SOCK_INF *si = NULL; - // We check by trying to recv — if it works, it's the accepted socket - uint32_t recv_len = WCHNET_SocketRecvLen(i, NULL); - (void)recv_len; - // Simpler approach: just assign the first non-listen TCP socket - // that's not the existing SSC/MQTT client (SocketId_TCP) - if (i != SocketId_TCP && g_json_socket_client == 0xFF) { - g_json_socket_client = i; - g_json_auth_state = JSON_STATE_WAIT_AUTH; - g_json_pwd_retry = 0; - g_json_auth_timer = mstick(); - g_json_recv_len = 0; - memset(g_json_recv_buf, 0, sizeof(g_json_recv_buf)); - PRINT("JSON: Client connected on socket %d\n", i); - break; - } - } + // A client connected — the accepted socket gets its own CONNECT event + // We handle the accept in the "newly accepted" path below + PRINT("JSON: Listen socket got CONNECT\n"); } if (intstat & SINT_STAT_DISCONNECT) { PRINT("JSON: Listen socket disconnect (unexpected)\n"); @@ -645,6 +624,25 @@ void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) { return; } + // === Newly accepted client (CONNECT on unknown socket) === + if ((intstat & SINT_STAT_CONNECT) && socketid != g_json_socket_client + && socketid != SocketId_TCP && socketid != SocketId_UDP) { + // This is the newly accepted JSON client connection + g_json_socket_client = socketid; + g_json_auth_state = JSON_STATE_WAIT_AUTH; + g_json_pwd_retry = 0; + g_json_auth_timer = mstick(); + g_json_recv_len = 0; + memset(g_json_recv_buf, 0, sizeof(g_json_recv_buf)); + + // CRITICAL: Set up receive buffer for the accepted TCP socket + // Without this, WCHNET won't buffer incoming data on this socket + WCHNET_ModifyRecvBuf(socketid, (uint32_t)g_json_recv_buf, TCP_JSON_RECV_BUF_LEN); + + PRINT("JSON: Client accepted on socket %d, recv buf configured\n", socketid); + return; + } + // === Client socket events === if (socketid == g_json_socket_client) { if (intstat & SINT_STAT_RECV) {