From 5e9953b406e88b3668934099466fd6453345f197 Mon Sep 17 00:00:00 2001 From: Randall Hand Date: Wed, 25 Mar 2026 10:42:01 -0400 Subject: [PATCH] Improve serial bridge reliability - Add clocal and cs8 to socat serial options to prevent false carrier detect drops and ensure proper 8-bit framing for protobuf data - Add set -e for strict error handling in setup phase - Add configurable DEVICE_TIMEOUT (default 60s) to prevent indefinite hangs when device is missing; Docker restart policy handles retries - Quote variables in socat command to prevent word splitting Co-Authored-By: Claude Opus 4.6 (1M context) --- src/entrypoint.sh | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 1260229..8c40ff3 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -1,4 +1,5 @@ #!/bin/sh +set -e # Entrypoint script for socat serial bridge # Handles HUPCL disabling and starts socat with reconnect support @@ -6,6 +7,7 @@ DEVICE="${SERIAL_DEVICE:-/dev/ttyUSB0}" BAUD="${BAUD_RATE:-115200}" TCP_PORT="${TCP_PORT:-4403}" RECONNECT_DELAY="${RECONNECT_DELAY:-5}" +DEVICE_TIMEOUT="${DEVICE_TIMEOUT:-60}" VERSION=$(cat /VERSION 2>/dev/null || echo "unknown") # Validate RECONNECT_DELAY is a positive integer @@ -31,9 +33,15 @@ echo " Reconnect Delay: ${RECONNECT_DELAY}s" # Function to wait for device to be available wait_for_device() { if [ ! -e "$DEVICE" ]; then - echo "Waiting for device $DEVICE..." + echo "Waiting for device $DEVICE (timeout: ${DEVICE_TIMEOUT}s)..." + WAITED=0 while [ ! -e "$DEVICE" ]; do - sleep 1 # Poll every second for device availability + if [ "$WAITED" -ge "$DEVICE_TIMEOUT" ]; then + echo "ERROR: Device $DEVICE not found after ${DEVICE_TIMEOUT}s" + return 1 + fi + sleep 1 + WAITED=$((WAITED + 1)) done echo "Device $DEVICE found" fi @@ -70,7 +78,7 @@ except Exception as e: # Wait for device on initial startup wait_for_device -disable_hupcl +disable_hupcl || true # Register mDNS service via Avahi (if available) AVAHI_DIR="/etc/avahi/services" @@ -123,11 +131,11 @@ while true; do START_TIME=$(date +%s) + EXIT_CODE=0 socat \ - TCP-LISTEN:$TCP_PORT,fork,reuseaddr,max-children=1 \ - FILE:$DEVICE,b$BAUD,raw,echo=0 - - EXIT_CODE=$? + "TCP-LISTEN:$TCP_PORT,fork,reuseaddr,max-children=1" \ + "FILE:$DEVICE,b$BAUD,raw,echo=0,clocal,cs8" \ + || EXIT_CODE=$? END_TIME=$(date +%s) RUNTIME=$((END_TIME - START_TIME)) @@ -152,5 +160,5 @@ while true; do # Wait for device to reappear (in case it was unplugged) wait_for_device - disable_hupcl + disable_hupcl || true done