blob: d3b8fd9b1221af65f8ea5c5f532d06f85efbf6da [file] [log] [blame]
Jagpal Singh Gill2168bbd2025-10-22 19:48:59 -07001#!/bin/bash
2
3set -e
4set -u
5set -o pipefail
6
7function cleanup() {
8 echo -e "\nCaught EXIT signal. Terminating background processes."
9 jobs -p | xargs -r kill 2>/dev/null
10 echo "Cleanup complete."
11}
12trap cleanup EXIT
13
14DEFAULT_COUNT=2
15MODBUS_SERVER="/usr/libexec/phosphor-modbus/mock-modbus-server"
16
17if [ "$#" -eq 0 ]; then
18 echo "No count provided. Starting with default count of $DEFAULT_COUNT servers and serial port pairs."
19 SERVER_COUNT=$DEFAULT_COUNT
20elif [ "$#" -eq 1 ]; then
21 SERVER_COUNT=$1
22 if ! [[ "$SERVER_COUNT" =~ ^[0-9]+$ ]]; then
23 echo "Error: Count must be a non-negative integer." >&2
24 exit 1
25 fi
26 echo "Starting $SERVER_COUNT $MODBUS_SERVER instances and virtual port pairs."
27else
28 echo "Error: Too many arguments." >&2
29 echo "Usage: $0 [<count>]" >&2
30 exit 1
31fi
32
33# Create the necessary directory structure for serial ports
34echo "Creating directory /dev/serial/by-path"
35mkdir -p /dev/serial/by-path
36
37# Remove old symlinks to prevent conflicts
38echo "Removing old symlinks from /dev/serial/by-path/..."
39rm -f /dev/serial/by-path/platform-1e6a1000.usb-usb-0:1:1.*-port0
40
41# Loop to create virtual serial port pairs and start modbus servers
42echo "Starting $SERVER_COUNT virtual serial port pairs and $MODBUS_SERVER instances."
43for (( i=0; i<SERVER_COUNT; i++ )); do
44 TTY_USB="/dev/ttyUSB$i"
45 TTY_V="/dev/ttyV$i"
46
47 # Start the socat process for this pair
48 echo " - Starting socat for $TTY_USB and $TTY_V."
49 socat -v -x -d -d pty,link="$TTY_USB",rawer,echo=0,b115200 pty,rawer,echo=0,link="$TTY_V",b115200 &
50
51 # Wait a moment for socat to create the devices
52 sleep 0.5
53
54 echo " - Creating symlink for $TTY_USB..."
55 ln -sf $TTY_USB "/dev/serial/by-path/platform-1e6a1000.usb-usb-0:1:1.$i-port0"
56
57 # Start the $MODBUS_SERVER instance
58 echo " - Starting $MODBUS_SERVER instance $i."
59 $MODBUS_SERVER $i &
60done
61
62echo "All background processes have been started."
63echo "Press Ctrl+C to terminate all processes gracefully."
64
65# Keep the script running to manage background jobs
66wait