Skip to content

Instantly share code, notes, and snippets.

@RipperFox
Last active June 17, 2019 18:15
Show Gist options
  • Save RipperFox/79a0bfa16ff49c5a3c84fbdf6f1fc6dd to your computer and use it in GitHub Desktop.
Save RipperFox/79a0bfa16ff49c5a3c84fbdf6f1fc6dd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# The MIT License (MIT)
#
# ant-switch-backend-ms-s4a-web - MS-S4A-WEB backend script
#
# This is part of KiwiSDR antenna switch extension
#
# Copyright (c) 2019 Kari Karvonen, DK9SAS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
IPADDRESS=192.168.1.234
VERSION=1.2
CURLEXEC=/usr/bin/curl
CURLOPTS=" --silent --connect-timeout 1"
ANTENNAS=4 # minimal configurable number at hardware controller level = 2
CONFFILE=/etc/default/ms-s4a-web
[ -r $CONFFILE ] && . $CONFFILE
AntSW_ShowBackend() {
echo "ant-switch-backend-ms-s4a-web version $VERSION"
echo ""
}
AntSW_Get () {
if [ -z "$1" ]
then
# zero legth
sleep 1
else
REPLY=`$CURLEXEC $CURLOPTS $1`
if [ "x$REPLY" == "x" ]; then
# failed. Let's try again.
REPLY=`$CURLEXEC $CURLOPTS $1`
fi
if [ "x$REPLY" == "x" ]; then
# failed again. One more shot.
REPLY=`$CURLEXEC $CURLOPTS $1`
fi
fi
}
AntSW_GroundAll() {
AntSW_SelectAntenna 0
}
AntSW_Initialize() {
# Test connectivity to MS-S4A-WEB otherwise long timeouts
AntSW_Get "http://$IPADDRESS/"
if [ "x$REPLY" == "x" ]; then
echo "Unable to connect MS-S4A-WEB at $IPADDRESS"
echo "Please check network connectivity."
exit 1
fi
}
AntSW_SelectAntenna() {
ANT=$1
if [[ "$ANT" =~ ^([0-$ANTENNAS])$ ]]; then
getselected
steps=$(($selectedantenna-$ANT))
if (( $steps > $(($ANTENNAS/2)) )); then
steps=$(( $steps-$ANTENNAS-1 ))
fi
if (( $steps < $((-1*$ANTENNAS/2)) )); then
steps=$(( $steps+$ANTENNAS+1 ))
fi
if (( $steps < 0 )); then
for (( 0; $steps<0; steps++)) do
AntSW_Get "http://$IPADDRESS/5/on"
done
fi
if (( $steps > 0 )); then
for (( 0; $steps>0; steps--)) do
AntSW_Get "http://$IPADDRESS/4/on"
done
fi
else
:
#echo "$ANT is not a valid selection (0-$ANTENNAS)"
#exit 1
fi
}
AntSW_DeSelectAntenna() {
:
}
AntSW_ToggleAntenna() {
:
}
AntSW_ShowSelected() {
getselected
echo -n "Selected antennas: "
echo $selectedantenna
}
AntSW_Configure() {
echo "Current IP address is $IPADDRESS"
echo "Enter new IP address or CTRL-C to abort"
read IPADDRESS
if valid_ip $IPADDRESS; then
AntSW_VerifyConnectivity
echo "# KiwiSDR MS-S4A-WEB ant_switch extension" > $CONFFILE
echo "IPADDRESS=$IPADDRESS" >> $CONFFILE
if [ -r $CONFFILE ]; then
echo "Created configuration file $CONFFILE"
else
echo "Configuration file $CONFFILE creation failed. Installation aborted."
exit 1
fi
else
echo "Invalid IP address. Configuration not saved."
exit 1
fi
}
function getselected() {
# Read antenna status
AntSW_Get "http://$IPADDRESS/"
selectedantenna=`echo $REPLY | grep -Po '[1-7](?=<p>)|GROUND' | sed s/GROUND/0/g `
}
function valid_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment