#!/bin/bash #set -e #set -o pipefail # 24.05.2020 - v1.5 - DL1MTG - show user and group, some cleanups # 19.05.2020 - v1.4 - DL1MTG - you can select a single rigctld to start, stop and debug now # 17.05.2020 - v1.3 - DL1MTG - error checking, help and debug, simplified some things # 03.05.2020 - v1.1 - DL1MTG - check for HamLib 4.x # 13.12.2019 - v1.0 - DL1MTG - first internal release # # FIRST: You can get some help with: # rigcontrol.sh help # or try to pipe to more # rigcontrol.sh help | more # use 'space" for next page, 'q' to quit # declare -A RIG_DEV declare -A RIG_TYPE declare -A RIG_PORT declare -A RIG_OPT declare -i HAMLIB # use device string "NONE" to disable device # Example: # RIG_DEV[2]="NONE" # # Hint: use "/dev/serial/by-id/" instead of "/dev/ttyUSBx" - the id is unique! RIG_DEV=( [0]="/dev/serial/by-id/usb-FTDI_K3S_SN00000_-_FT232R_USB_UART_AK1Q82IY-if00-port0" [1]="/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_IC-7100_00000000_A-if00-port0" [2]="/dev/serial/by-id/usb-Silicon_Labs_CP2102N_USB_to_UART_Bridge_Controller_IC-9700_00000000_A-if00-port0" [3]="NONE" [4]="/dev/ttyS0" #[5], [6] and so on - extend to your needs! You can delete not needed entries! ) # enter model and parameters # check which HamLib version you have! # the model number was changed from version 3 to 4! RIG_TYPE=( [0]="2029" # Elecraft K3/K3S/KX3 [1]="3070" # Icom 7100, CI-V address 0x88 [2]="3081" # Icom 9700, CI-V 0xa2, [not needed anymore: set CI-V USB to 19200 Baud (pse no AUTO!) and set CI-V USB Echo Back to ON] [3]="1" # [4]="1" # #[5], [6] and so on - extend to your needs! You can delete not needed entries! ) # default is 4532. Be careful, port 4533 is used by rotctld! RIG_PORT=( [0]="4532" # Network Port for Elecraft K3/K3S/KX3 [1]="4531" # Network Port for Icom 7100 [2]="4530" # Network Port for Icom 9700 [3]="1" # [4]="2" # #[5], [6] and so on - extend to your needs! You can delete not needed entries! ) # Here you can add special options for rigctld RIG_OPT=( [0]="" # Optional parameters. Example: "-s 19200" to set rig static on 19200 baud [1]="" # Optional parameters [2]="" # Optional parameters [3]="" # Optional parameters [4]="" # Optional parameters #[5], [6] and so on - extend to your needs! You can delete not needed entries! ) # only change this when needed! You might need it for remote operation? LISTEN="localhost" # for security reason, listen only on localhost ##################################### # nothing to change below this line # ##################################### # horizontal line makeline () { seq -s# 80|tr -d '[:digit:]' } # check netcat or nc if [ ! -x "`which nc`" ]; then echo "Missing command: netcat or nc. Please install to get script running!" exit 1 fi # check HamLib version if [ -x "`which rigctld`" ]; then HAMLIB_RAW="`rigctld -V|head -1`" HAMLIB="`echo $HAMLIB_RAW|cut -d"," -f2|cut -d" " -f3|cut -d"." -f1`" else echo "HamLib version seems not to be installed!. Please check your setup!" exit 1 fi # HamLib version 3 or 4 is needed! if [ $HAMLIB -le 3 -a $HAMLIB -gt 4 ]; then echo "HamLib version is not sufficient. You need version 3 or 4!!" echo "You are using: $HAMLIB_RAW" exit 1 fi # check HamLib version. If using version 4, nothing to do. # Otherwise: relinking "by-id" to "/dev/ttyXXXX" if [ $HAMLIB -ne 4 ]; then # check length of USB-Device by ID # https://sourceforge.net/p/hamlib/mailman/message/35646816/ # old hamlib issue: only 100 chars allowed! Use "/dev/ttyUSBx" instead "by-id"! for RIGS in ${!RIG_DEV[*]}; do if [ ${#RIG_DEV[$RIGS]} -gt 99 ]; then RIG_DEV_RAW[$RIGS]="`ls -l "${RIG_DEV[$RIGS]}"|cut -d">" -f2|cut -d"/" -f3`" RIG_DEV[$RIGS]="/dev/${RIG_DEV_RAW[$RIGS]}" fi done fi # check for second parameter if [ ! -z "$2" ]; then if [ ${#RIG_DEV[$2]} = 0 ]; then echo "Cannot find \"RIG_DEV[$2]\"! Check configuration!" exit 1 fi fi # check device, port number and rigtype number if [ "$1" != "help" ]; then # get process id for RIGS in ${!RIG_DEV[*]}; do RIG_PID[$RIGS]=`ps aux|grep "rigctld -r ${RIG_DEV[$RIGS]} -m ${RIG_TYPE[$RIGS]}"|grep -v grep|awk '{print $2}'` if [ -z ${RIG_PID[$RIGS]} ]; then RIG_PID[$RIGS]="NONE"; fi if [ ${RIG_DEV[$RIGS]} != "NONE" ]; then # check device for character special if [ ! -c ${RIG_DEV[$RIGS]} ]; then echo "Device not useable / not found: ${RIG_DEV[$RIGS]} !!" echo "Check Device RIG_DEV[$RIGS]=\"${RIG_DEV[$RIGS]}\"" exit 1 fi # rigport: simple check for numbers if ! [[ ${RIG_PORT[$RIGS]} =~ ^[0-9]+$ ]]; then echo "RIG_PORT[$RIGS] not useable!" echo "RIG_PORT[$RIGS]=\"${RIG_PORT[$RIGS]}\"" exit 1 fi # rigtype: simple check for numbers if ! [[ ${RIG_TYPE[$RIGS]} =~ ^[0-9]+$ ]]; then echo "RIG_TYPE[$RIGS] not useable!" echo "RIG_TYPE[$RIGS]=\"${RIG_TYPE[$RIGS]}\"" exit 1 fi fi done fi # main part case $1 in start) # start all rigctlds if [ -z "$2" ]; then for RIGS in ${!RIG_DEV[*]}; do if [ ${RIG_DEV[$RIGS]} != "NONE" ]; then # start all rigctlds if [ ${RIG_PID[$RIGS]} = "NONE" ]; then rigctld -r ${RIG_DEV[$RIGS]} -m ${RIG_TYPE[$RIGS]} -T $LISTEN -t ${RIG_PORT[$RIGS]} ${RIG_OPT[$RIGS]} 2>&1 >/dev/null & else echo "rigctld on device \"${RIG_DEV[$RIGS]}\" already running!" fi fi done # start selected rigctld elif [ ${RIG_DEV[$2]} ]; then if [ ${RIG_DEV[$2]} != "NONE" ]; then # start only one rigctld if [ ${RIG_PID[$2]} = "NONE" ]; then rigctld -r ${RIG_DEV[$2]} -m ${RIG_TYPE[$2]} -T $LISTEN -t ${RIG_PORT[$2]} ${RIG_OPT[$2]} 2>&1 >/dev/null & else echo "rigctld on device \"${RIG_DEV[$2]}\" already running!" fi fi fi ;; stop) if [ -z "$2" ]; then for RIGS in ${!RIG_DEV[*]}; do if [ ${RIG_PID[$RIGS]} != "NONE" ]; then kill ${RIG_PID[$RIGS]}; fi done # stop selected rigctld elif [ ${RIG_DEV[$2]} ]; then if [ ${RIG_PID[$2]} != "NONE" ]; then kill ${RIG_PID[$2]}; fi fi ;; stop-all) echo "Stopping all rigctld proccesses!" ps -ef|grep rigctld|grep -v grep killall rigctld ;; restart) if [ -z "$2" ]; then $0 stop && sleep 1 && $0 start elif [ ${RIG_DEV[$2]} ]; then $0 stop $2 && sleep 1 && $0 start $2 fi ;; status) if [ -z "$2" ]; then echo "NETWORK ADDRESS: $LISTEN" makeline for RIGS in ${!RIG_DEV[*]}; do if [ ${RIG_DEV[$RIGS]} != "NONE" ]; then echo -e "RIG_PID[$RIGS]: ${RIG_PID[$RIGS]}\tPORT[$RIGS]: ${RIG_PORT[$RIGS]}\tDEVICE[$RIGS}: ${RIG_DEV[$RIGS]}" fi done elif [ ${RIG_DEV[$2]} ]; then echo -e "RIG_PID[$2]: ${RIG_PID[$2]}\tPORT[$2]: ${RIG_PORT[$2]}\tDEVICE[$2}: ${RIG_DEV[$2]}" fi ;; setup) echo -n "You are using hamlib version: " echo $HAMLIB_RAW makeline echo "Try to get 'USB' devices" if [ `ls -la /dev/ttyUSB* 2>1 >/dev/null;echo $?` -eq 0 ]; then ls -la /dev/ttyUSB* else echo "No 'USB' devices at '/dev/ttyUSB*' found!" fi makeline echo "Try to get 'serial' devices" if [ `ls -la /dev/ttyS* 2>1 >/dev/null;echo $?` -eq 0 ]; then ls -la /dev/ttyS* else echo "No 'serial' devices at '/dev/ttyS*' found!" fi makeline echo "Try to get 'by-id' devices" if [ -d "/dev/serial/by-id" ]; then ls -la /dev/serial/by-id/* else echo "No 'by-id' devices found!" fi makeline echo "Checking membership of groups to access serial devices:" if [ `ls -la /dev/ttyS* 2>1 >/dev/null;echo $?` -eq 0 ]; then TTY_GRP=`ls -la /dev/ttyS*|head -1|awk {'print $4'}` if [ -z $TTY_GRP ]; then TTY_GRP="UNKNOWN"; fi # check '/dev/ttyS* - hopefully this exists on any linux system to check serial device permissions? if [ `id|grep -c $TTY_GRP` -ge 1 ]; then echo "It seems you're groupmember of \"$TTY_GRP\" and you should have access to serial devices! Good!" fi else echo "Cannot find group permissions for serial devices! Check membership of groups on your system!" fi id ;; debug|debug-hamlib) if [ -z "$2" ]; then $0 status makeline echo $HAMLIB_RAW for RIGS in ${!RIG_DEV[*]}; do if [ ${RIG_DEV[$RIGS]} != "NONE" ]; then makeline echo "RIG_DEV[$RIGS]=\"${RIG_DEV[$RIGS]}\"" echo "RIG_TYPE[$RIGS]=\"${RIG_TYPE[$RIGS]}\"" echo "RIG_PORT[$RIGS]=\"${RIG_PORT[$RIGS]}\"" echo "RIG_OPT[$RIGS]=\"${RIG_OPT[$RIGS]}\"" echo "RIG_PID[$RIGS]=\"${RIG_PID[$RIGS]}\"" echo "CMDLINE=\"rigctld -r ${RIG_DEV[$RIGS]} -m ${RIG_TYPE[$RIGS]} -T $LISTEN -t ${RIG_PORT[$RIGS]} ${RIG_OPT[$RIGS]}\"" echo -e "\nHamLib output:" makeline # check communication with rig nc -w 3 -z $LISTEN ${RIG_PORT[$RIGS]}; ERROR=$? if [ $ERROR -ne 0 ]; then echo "HamLib cannot reach rig! Check model, device, serial settings, port and options" fi # gather information from rig using hamlib if [ $ERROR -eq 0 -a $1 = "debug" ]; then echo "\dump_caps" | nc -w 1 $LISTEN ${RIG_PORT[$RIGS]}|head -4 elif [ $ERROR -eq 0 -a $1 = "debug-hamlib" ]; then echo "\dump_caps" | nc -w 1 $LISTEN ${RIG_PORT[$RIGS]} fi fi done else if [ ${RIG_DEV[$2]} != "NONE" ]; then $0 status $2 makeline echo $HAMLIB_RAW makeline echo "RIG_DEV[$2]=\"${RIG_DEV[$2]}\"" echo "RIG_TYPE[$2]=\"${RIG_TYPE[$2]}\"" echo "RIG_PORT[$2]=\"${RIG_PORT[$2]}\"" echo "RIG_OPT[$2]=\"${RIG_OPT[$2]}\"" echo "RIG_PID[$2]=\"${RIG_PID[$2]}\"" echo "CMDLINE=\"rigctld -r ${RIG_DEV[$2]} -m ${RIG_TYPE[$2]} -T $LISTEN -t ${RIG_PORT[$2]} ${RIG_OPT[$2]}\"" echo -e "\nHamLib output:" makeline # check communication with rig nc -w 3 -z $LISTEN ${RIG_PORT[$2]}; ERROR=$? if [ $ERROR -ne 0 ]; then echo "HamLib cannot reach rig! Check model, device, serial settings, port and options" fi # gather information from rig using hamlib if [ $ERROR -eq 0 -a $1 = "debug" ]; then echo "\dump_caps" | nc -w 1 $LISTEN ${RIG_PORT[$2]}|head -4 elif [ $ERROR -eq 0 -a $1 = "debug-hamlib" ]; then echo "\dump_caps" | nc -w 1 $LISTEN ${RIG_PORT[$2]} fi makeline else echo "Device \"RIG_DEV[$2]\" has type \"NONE\"!" fi fi ;; help) makeline echo "First: Use rigcontrol to identify your serial devices" echo "Second: check, which hamlib version you have" echo "Please run this script like this to gather the informations:" echo "`basename $0` setup" echo echo "Now enter the serial device string of your rig" echo "Example:" echo "RIG_DEV[1]=\"/dev/ttyUSB0\"" echo echo "Choose your network port, default is 4532" echo "You have to use unique ports for each rig!" echo "Example:" echo "RIG_PORT[1]=\"4532\"" echo echo "When you have more rigs, enter these values for rig2 up to rigN" echo "using RIG_DEV[2], RIG_DEV[3], and so on. There is no limit!" echo echo "Enter model id and parameters for your rig" echo "You can get your model id from the hamlib documentation" echo "https://github.com/Hamlib/Hamlib/wiki/Supported-Radios" echo "Example:" echo "RIG_TYPE[1}=\"370\"" echo "this one is for Icom IC-7100" echo echo "A bit more advanced setting for a rig to set 19200 baud static:" echo "RIG_OPT[2]=\"-s 19200\"" echo echo "You can disable a rig by setting RIG_DEV to \"NONE\"." echo "Example:" echo "RIG_DEV[2]=\"NONE\"" echo echo "This value is default. Check carefully if you really need to change this!" echo "LISTEN=\"localhost\"" echo echo "Save the configuration and try the first start:" echo "`basename $0` start" echo echo "Now it is up to you to check the rigctl functions of your software" echo "and hopefully your rig is working now!" echo echo "How to check the running rigctld process:" echo "`basename $0` status" echo echo "To stop all rigctld you can use:" echo "`basename $0` stop" echo echo "To restart all rigctld you can use:" echo "`basename $0` restart" echo echo "When you have some problems to get rigctld working with your rig try this," echo "you will get some debugging parameters, rig-information and so on:" echo "`basename $0` debug" echo makeline echo "Some commands for setup und debuging issues with rigctld:" echo echo "Stop all running rigctld proccesses - needed only for setup issues:" echo `basename $0` stop-all echo echo "When in big trouble, you might need to get everything, try this, lengthy output!" echo "`basename $0` debug-hamlib|more" echo echo "To start only one rigctld (eg. RIG_DEV[1] you can use the number of the device you declared:" echo "`basename $0` start 1" echo echo "To stop only one rigctld you can use the number of the device you declared:" echo "`basename $0` stop 1" echo echo "To restart only one rigctld you can use the number of the device you declared:" echo "`basename $0` restart 1" echo echo "To debug only one rigctld you can use the number of the device you declared:" echo "`basename $0` debug 1" echo echo "To debug only one rigctld you can use the number of the device you declared:" echo "`basename $0` debug-hamlib 1" makeline ;; *) $0 help echo "Usage: `basename $0` {start|stop|stop-all|restart|status|setup|debug|debug-hamlib|help}" exit 2 ;; esac