Skip to content

Commit 315427b

Browse files
committed
app: Allow CMUX to be set up separately from PPP
Add new scripts to manage CMUX * sm2_start_cmux.sh * sm2_stop_cmux.sh Add new parameter to PPP script '-C' that allow attaching to already running CMUX session. Then connection is teared down, the CMUX session is left running. This combination can be used with NTN as the CMUX does not need to be teared down when swithing between normal and NTN networks. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent a72923d commit 315427b

4 files changed

Lines changed: 407 additions & 52 deletions

File tree

app/scripts/sm2_start_cmux.sh

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#!/bin/bash -eu
2+
#
3+
# Copyright (c) 2026 Nordic Semiconductor ASA
4+
#
5+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
6+
7+
#
8+
# Script to set up CMUX multiplexing on Serial Modem application 2.0.0 or
9+
# later using standard 3GPP AT commands (AT+CMUX=0).
10+
#
11+
# Use this script to initialize CMUX independently from PPP. Once CMUX is
12+
# running, start and stop PPP using sm2_start_ppp.sh -C and sm_stop_ppp.sh
13+
# without tearing down CMUX each time.
14+
#
15+
# Use sm2_stop_cmux.sh to fully close CMUX and release the serial port.
16+
#
17+
# Uses the following DLC channels in CMUX:
18+
# - 1: PPP data channel
19+
# - 2: AT commands channel
20+
# - 3: Modem trace collection (optional)
21+
#
22+
23+
#
24+
# Default parameters
25+
#
26+
MODEM=/dev/ttyACM0
27+
BAUD=115200
28+
IPR_BAUD=0
29+
VERBOSE=0
30+
CHATOPT=""
31+
MODEM_TRACE_FILE="/var/log/nrf91-modem-trace.bin"
32+
TRACE_PID_FILE="/var/run/nrf91-modem-trace.pid"
33+
TRACE=0
34+
35+
usage() {
36+
echo "Usage: $0 [-s serial_port] [-b baud_rate] [-B new_speed] [-T] [-v] [-h]"
37+
echo ""
38+
echo " -s serial_port : Serial port where the modem is connected (default: $MODEM)"
39+
echo " -b baud_rate : Current baud rate of Serial Modem (default: $BAUD)"
40+
echo " -B new_speed : Use AT+IPR to change baud rate to <new_speed>"
41+
echo " Start with current baud rate and switch to new_speed after modem"
42+
echo " is responsive. If not set, baud rate will not be changed."
43+
echo " -T : Enable modem trace collection (file: $MODEM_TRACE_FILE)"
44+
echo " -v : Enable verbose output"
45+
echo " -h : Show this help message"
46+
echo ""
47+
exit 0
48+
}
49+
50+
# Parse command line parameters
51+
while getopts s:b:B:Tvh flag
52+
do
53+
case "${flag}" in
54+
s) MODEM=${OPTARG};;
55+
b) BAUD=${OPTARG};;
56+
B) IPR_BAUD=${OPTARG};;
57+
T) TRACE=1;;
58+
v) VERBOSE=1; CHATOPT="-v";;
59+
h|?) usage;;
60+
esac
61+
done
62+
63+
log_dbg() {
64+
if [ $VERBOSE -eq 1 ]; then
65+
logger --id=$$ -s "$@"
66+
fi
67+
}
68+
69+
if [[ ! -c $MODEM ]]; then
70+
echo "Serial port not found: $MODEM"
71+
exit 1
72+
fi
73+
74+
# Remove stale trace PID file if process is not running
75+
if [ -f "$TRACE_PID_FILE" ]; then
76+
if ! kill -0 $(cat "$TRACE_PID_FILE" 2>/dev/null) 2>/dev/null; then
77+
log_dbg "Removing stale trace PID file: $TRACE_PID_FILE"
78+
rm -f "$TRACE_PID_FILE"
79+
fi
80+
fi
81+
82+
if find /dev -type c -name 'gsmtty*' | grep -q . ; then
83+
echo "Error: existing CMUX devices found (/dev/gsmtty*)"
84+
exit 1
85+
fi
86+
87+
if pgrep ldattach >/dev/null; then
88+
echo "Error: existing ldattach process found"
89+
exit 1
90+
fi
91+
92+
cmux_close() {
93+
printf "\xF9\xF9\xF9\xF9\xF9\xF9\xF9\xF9" > $MODEM
94+
printf "\xF9\x03\xEF\x05\xC3\x01\xF2\xF9" > $MODEM
95+
sleep 2
96+
}
97+
98+
cleanup() {
99+
set +eu
100+
start-stop-daemon --stop --pidfile $TRACE_PID_FILE --remove-pidfile --oknodo
101+
pkill ldattach
102+
cmux_close
103+
echo "Failed to start CMUX..."
104+
exit 1
105+
}
106+
107+
trap cleanup ERR
108+
109+
# Configure serial port
110+
stty -F $MODEM $BAUD pass8 raw crtscts clocal
111+
112+
log_dbg "Wait modem to boot"
113+
if chat -t1 "Ready--" "AT" "OK" <$MODEM >$MODEM; then
114+
log_dbg "Modem is in AT mode"
115+
else
116+
log_dbg "Modem not responding, try CMUX close down..."
117+
cmux_close
118+
if ! chat -t1 "" "AT" "OK" <$MODEM >$MODEM; then
119+
echo "Error: Modem not responding"
120+
exit 1
121+
fi
122+
fi
123+
124+
if [ $IPR_BAUD -ne 0 ]; then
125+
log_dbg "Set baud rate on modem to $IPR_BAUD"
126+
chat $CHATOPT -t1 '' "AT+IPR=$IPR_BAUD" "OK" >$MODEM <$MODEM
127+
# Reconfigure serial port
128+
stty -F $MODEM $IPR_BAUD pass8 raw crtscts clocal
129+
fi
130+
131+
log_dbg "Attach CMUX channel to modem..."
132+
chat $CHATOPT -t1 '' "AT+CMUX=0" "OK" >$MODEM <$MODEM
133+
ldattach GSM0710 $MODEM
134+
sleep 3
135+
136+
# AT_CMUX is the channel where setup commands will be sent and converted into PPP
137+
AT_CMUX=$(ls /dev/gsmtty* | sort -V | head -n 1)
138+
# AT_CMUX_USER is the AT channel that host can use after PPP is set up
139+
AT_CMUX_USER=$(ls /dev/gsmtty* | sort -V | head -n 2 | tail -n 1)
140+
echo "AT CMUX: $AT_CMUX_USER"
141+
echo "PPP CMUX: $AT_CMUX"
142+
143+
MT_CMUX=""
144+
if [ $TRACE -gt 0 ]; then
145+
MT_CMUX=$(ls /dev/gsmtty* | sort -V | head -n 3 | tail -n 1)
146+
log_dbg "Trace CMUX: $MT_CMUX"
147+
log_dbg "Trace file: $MODEM_TRACE_FILE"
148+
stty -F $MT_CMUX raw clocal -icrnl -ixon -opost
149+
fi
150+
151+
stty -F $AT_CMUX clocal
152+
test -c $AT_CMUX
153+
154+
if [ $TRACE -gt 0 ]; then
155+
log_dbg "Starting trace collection..."
156+
chat $CHATOPT -t1 '' 'AT#XCMUXTRACE=3' 'OK' >$AT_CMUX <$AT_CMUX
157+
158+
# Prefer to use socat, if installed.
159+
if command -v socat >/dev/null 2>&1; then
160+
start-stop-daemon --start --pidfile $TRACE_PID_FILE --make-pidfile \
161+
--background --exec $(command -v socat) -- -u $MT_CMUX,cfmakeraw,clocal=1 \
162+
CREATE:$MODEM_TRACE_FILE
163+
else
164+
start-stop-daemon --start --pidfile $TRACE_PID_FILE --make-pidfile \
165+
--background --exec /bin/dd -- if=$MT_CMUX of=$MODEM_TRACE_FILE bs=1024
166+
fi
167+
fi
168+
169+
log_dbg "CMUX started"

0 commit comments

Comments
 (0)