From ebfcefa74d0dcd5732e58d58f19b2fdbd1bafa1f Mon Sep 17 00:00:00 2001 From: cormoran Date: Sun, 25 Jan 2026 19:32:07 +0900 Subject: [PATCH] fix(studio): Ensure putting framing byte when sending rpc If tx buffer became full when sending PRC response, framing byte can be skipped and communication gets stuck. This patch adds wait for putting EOF failure and adds error handling for putting SOF failure. --- app/src/studio/rpc.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/studio/rpc.c b/app/src/studio/rpc.c index 8dd711e1e..c353b1adf 100644 --- a/app/src/studio/rpc.c +++ b/app/src/studio/rpc.c @@ -188,7 +188,11 @@ static int send_response(const zmk_studio_Response *resp) { pb_ostream_t stream = pb_ostream_for_tx_buf(user_data); uint8_t framing_byte = FRAMING_SOF; - ring_buf_put(&rpc_tx_buf, &framing_byte, 1); + if (ring_buf_put(&rpc_tx_buf, &framing_byte, 1) != 1) { + LOG_ERR("RPC TX buffer full"); + k_mutex_unlock(&rpc_transport_mutex); + return -ENOMEM; + } selected_transport->tx_notify(&rpc_tx_buf, 1, false, user_data); @@ -199,11 +203,15 @@ static int send_response(const zmk_studio_Response *resp) { #if !IS_ENABLED(CONFIG_NANOPB_NO_ERRMSG) LOG_ERR("Failed to encode the message %s", stream.errmsg); #endif // !IS_ENABLED(CONFIG_NANOPB_NO_ERRMSG) + k_mutex_unlock(&rpc_transport_mutex); return -EINVAL; } framing_byte = FRAMING_EOF; - ring_buf_put(&rpc_tx_buf, &framing_byte, 1); + // If response is large, tx buffer can be full for async transport. + while (ring_buf_put(&rpc_tx_buf, &framing_byte, 1) == 0) { + k_sleep(K_MSEC(1)); + } selected_transport->tx_notify(&rpc_tx_buf, 1, true, user_data);