27 lines
1.9 KiB
Bash
27 lines
1.9 KiB
Bash
#!/bin/sh
|
|
# B6K AT capture. 'run OUTDIR': wait for wwan0at0 (+wwan0qmi0, +2 s port-race guard), open it ONCE, enable runtime-only
|
|
# extended registration URCs (AT+CEREG=5, AT+CGREG=3; no other values tried), keep the port open and log every line with timestamps
|
|
# until told: 'close' (just close) or 'failq' (read-only FAIL queries, then close). 'gate': wait (<=30 s) until setup is done.
|
|
AT=/dev/wwan0at0; K=/tmp/k-at.d; mkdir -p $K
|
|
ts() { echo "$(cut -d' ' -f1 /proc/uptime) $(date -u +%H:%M:%S)"; }
|
|
waitfile() { i=0; while [ ! -e $1 ] && [ $i -lt $(($2*5)) ]; do sleep 0.2; i=$((i+1)); done; [ -e $1 ]; }
|
|
case "$1" in
|
|
gate) waitfile $K/ready 30 && { echo "k-at gate: ready ($(cat $K/ready))"; exit 0; }; echo "k-at gate: TIMEOUT (setup not done in 30 s)"; exit 1;;
|
|
close|failq)
|
|
[ -f $K/pid ] && kill -0 $(cat $K/pid) 2>/dev/null || { echo "k-at $1: not running"; exit 0; }
|
|
echo $1 > $K/ctl; waitfile $K/done 45 && { echo "k-at $1: done"; exit 0; }; echo "k-at $1: TIMEOUT"; exit 1;;
|
|
run) O=${2:-/tmp/k-at}; mkdir -p $O; rm -f $K/ready $K/ctl $K/done; echo $$ > $K/pid; L=$O/at.txt
|
|
echo "[$(ts)] k-at waiting for $AT" > $L
|
|
waitfile $AT 90 && waitfile /dev/wwan0qmi0 30 || { echo "[$(ts)] $AT not present" >> $L; echo fail > $K/ready; touch $K/done; exit 1; }
|
|
sleep 2; exec 3<>$AT; echo "[$(ts)] open $AT" >> $L
|
|
( while IFS= read -r l <&3; do echo "[$(ts)] < $l"; done ) >> $L 2>&1 &
|
|
RP=$!
|
|
send() { echo "[$(ts)] > $1" >> $L; printf '%s\r' "$1" >&3; sleep ${2:-2}; }
|
|
for c in AT AT+CEREG=5 AT+CGREG=3 AT+CEREG? AT+CGREG? AT+CREG?; do send $c; done
|
|
echo "[$(ts)] setup done" >> $L; echo "$(ts)" > $K/ready
|
|
while :; do c=$(cat $K/ctl 2>/dev/null)
|
|
case "$c" in
|
|
failq) echo "[$(ts)] --- FAIL queries" >> $L; for q in AT+CEREG? AT+CGREG? AT+CREG? AT+CGATT? AT+COPS? AT+CEER; do send $q 3; done; break;;
|
|
close) break;; esac; sleep 0.5; done
|
|
sleep 1; kill $RP 2>/dev/null; exec 3>&-; echo "[$(ts)] closed $AT ($c)" >> $L; tr -d '\r' < $L > $O/at.clean.txt; touch $K/done;;
|
|
esac
|