1#!/usr/local/bin/bash
2# tnc1_high_snr.sh
3#
4#  HF TNC use case test 1
5#  + Send unidirectional frames at high SNR over an alsa loopback virtual sound card
6#  + Using the sound card can take some time, so implemented as a service to run automatically in background
7
8NAME=tnc1
9CODEC2_PATH=${HOME}/codec2
10PIDFILE_TX=/tmp/${NAME}_tx.pid
11PIDFILE_RX=/tmp/${NAME}_rx.pid
12LOGFILE=${NAME}.log
13PATH=${CODEC2_PATH}/build_linux/src:${PATH}
14DELAY="${DELAY:-500}"
15MAX_RUN_TIME=3600
16# we use single frame bursts, so BURSTS==FRAME
17BURSTS=$2
18
19function run_tx {
20    bursts=$1
21    delay=$2
22    freedv_data_raw_tx DATAC0 /dev/zero - --testframes ${bursts} --bursts ${bursts} --delay ${delay} | aplay --device="plughw:CARD=CHAT2,DEV=1" -f S16_LE
23}
24
25function start_rx_background {
26    arecord --device="plughw:CARD=CHAT2,DEV=0" -f S16_LE -d $MAX_RUN_TIME | freedv_data_raw_rx DATAC0 - /dev/null --framesperburst 1 --vv --testframes &
27    echo $!>${PIDFILE_RX}
28}
29
30function stop_service {
31    echo "service stopping - bye!" 1>&2
32    if [ -e ${PIDFILE_RX} ]; then
33        pid_rx=$(cat ${PIDFILE_RX})
34        rm ${PIDFILE_RX}
35        kill ${pid_rx}
36    fi
37
38    if [ -e ${PIDFILE_TX} ]; then
39        pid_tx=$(cat ${PIDFILE_TX})
40        rm ${PIDFILE_TX}
41        kill ${pid_tx}
42    fi
43}
44
45function check_running {
46    if [ -e ${PIDFILE_TX} ]; then
47        echo "Tx already running... pid: ${PIDFILE_TX}"
48        exit 1
49    fi
50    if [ -e ${PIDFILE_RX} ]; then
51        echo "Rx already running... pid: ${PIDFILE_RX}"
52        exit 1
53    fi
54}
55
56function check_alsa_loopback {
57    lsmod | grep snd_aloop >> /dev/null
58    if [ $? -eq 1 ]; then
59      echo "ALSA loopback device not present.  Please install with:"
60      echo
61      echo "  sudo modprobe snd-aloop index=1,2 enable=1,1 pcm_substreams=1,1 id=CHAT1,CHAT2"
62      exit 1
63    fi
64}
65
66case "$1" in
67    start)
68        check_running
69        check_alsa_loopback
70        ( start_rx_background && sleep 1 && run_tx ${BURSTS} ${DELAY} && stop_service ) 2>${LOGFILE} &
71        echo $!>${PIDFILE_TX}
72        echo "Results in ${LOGFILE}"
73        ;;
74    start_verbose)
75        set -x
76        check_running
77        check_alsa_loopback
78        # Show all outputs and log output to stderr rather than logfile
79        verbose=1
80        start_rx_background && sleep 1 && run_tx ${BURSTS} ${DELAY} && stop_service
81        ;;
82    stop)
83        stop_service
84        ;;
85    restart)
86        $0 stop
87        $0 start
88        ;;
89    status)
90        if [ -e ${PIDFILE_TX} ]; then
91            echo ${NAME} is running, pid=`cat ${PIDFILE_TX}`
92        else
93            echo ${NAME} is NOT running
94            exit 1
95        fi
96        ;;
97    *)
98    echo "Usage: $0 {start|start_verbose|stop|restart|status} NumFrames"
99    echo "   $0 start_verbose 1       - 1 frame packet verbose, logs to stderr"
100    echo "   $0 start 5               - 5 frames, run as service in background, logs sent to ${LOGFILE}"
101esac
102
103exit 0
104