238 lines
6.9 KiB
Bash
Raw Normal View History

2016-12-13 17:48:44 -05:00
#! /bin/bash
2016-12-14 10:27:34 -05:00
2016-07-12 13:27:54 -04:00
# Exit if we're debugging and haven't yet built the gateway
2016-04-22 14:25:20 -04:00
if [ ! -f "mp_pkt_fwd" ]; then
2016-12-14 10:27:34 -05:00
echo "ERROR: gateway executable not yet built"
exit 1
2016-04-22 14:25:20 -04:00
fi
2016-03-21 12:36:55 -07:00
2016-06-17 10:18:30 -04:00
if [[ $HALT != "" ]]; then
echo "*** HALT asserted - exiting ***"
2016-04-22 14:25:20 -04:00
exit 1
fi
# Show info about the machine we're running on
echo "*** Resin Machine Info:"
2016-12-13 17:38:17 -05:00
echo "*** Type: '$RESIN_MACHINE_NAME'"
echo "*** Arch: '$RESIN_ARCH'"
2016-04-22 14:25:20 -04:00
##### Raspberry Pi 3 hacks necessary for LinkLabs boards
##
2016-04-22 14:25:20 -04:00
## Because of backward incompatibility between RPi2 and RPi3,
## a hack is necessary to get serial working so that we can
## access the GPS on the LinkLabs board.
2016-12-14 10:27:34 -05:00
##
## Option #1: Leave RPi serial on /dev/ttyS0 and bluetooth on /dev/ttyAMA0
2016-04-22 14:25:20 -04:00
## This requires adding this Fleet Configuration variable, which
## fixes RPi3 serial port speed issues:
## RESIN_HOST_CONFIG_core_freq = 250
##
## Option #2: Disable bluetooth and put RPi serial back onto /dev/ttyAMA0
2016-04-22 14:25:20 -04:00
## This requires adding this Fleet Configuration variable, which
## swaps the bluetooth and serial uarts:
## RESIN_HOST_CONFIG_dtoverlay=pi3-miniuart-bt
##
#####
2016-03-29 18:27:49 -04:00
2016-04-22 14:25:20 -04:00
if [[ $GW_TYPE == "" ]]; then
2016-12-14 10:27:34 -05:00
echo "ERROR: GW_TYPE required"
echo "See https://github.com/rayozzie/ttn-resin-gateway-rpi/blob/master/README.md"
exit 1
2016-03-29 18:27:49 -04:00
fi
2016-03-28 19:48:33 -04:00
# We need to be online, wait if needed.
2016-04-10 15:23:51 -04:00
until $(curl --output /dev/null --silent --head --fail http://www.google.com); do
2016-12-14 10:27:34 -05:00
echo "[TTN Gateway]: Waiting for internet connection..."
sleep 30
done
2016-03-28 19:48:33 -04:00
2016-03-25 14:21:04 -04:00
# Ensure that we've got the required env vars
2016-03-21 14:20:50 -07:00
2016-03-22 16:35:24 -07:00
echo "*******************"
2016-03-21 21:10:53 -07:00
echo "*** Configuration:"
2016-03-22 16:35:24 -07:00
echo "*******************"
2016-03-21 14:20:50 -07:00
2016-03-22 13:03:58 -07:00
if [[ $GW_REGION == "" ]]; then
2016-03-22 13:23:35 -07:00
echo "ERROR: GW_REGION required"
2016-12-14 10:27:34 -05:00
exit 1
2016-03-22 10:45:40 -07:00
fi
GW_REGION=${GW_REGION^^}
2016-03-22 13:03:58 -07:00
echo GW_REGION: $GW_REGION
2016-03-22 10:45:40 -07:00
2016-03-22 13:03:58 -07:00
if [[ $GW_DESCRIPTION == "" ]]; then
2016-03-22 13:23:35 -07:00
echo "ERROR: GW_DESCRIPTION required"
2016-12-14 10:27:34 -05:00
echo "See https://github.com/rayozzie/ttn-resin-gateway-rpi/blob/master/README.md"
exit 1
2016-03-21 14:20:50 -07:00
fi
2016-03-22 13:03:58 -07:00
if [[ $GW_CONTACT_EMAIL == "" ]]; then
2016-03-22 13:23:35 -07:00
echo "ERROR: GW_CONTACT_EMAIL required"
2016-12-14 10:27:34 -05:00
echo "See https://github.com/rayozzie/ttn-resin-gateway-rpi/blob/master/README.md"
exit 1
2016-04-16 15:05:52 -07:00
fi
if [[ $GW_ID == "" ]]; then
echo "ERROR: GW_ID required"
echo "See https://www.thethingsnetwork.org/docs/gateways/registration.html#via-gateway-connector"
exit 1
fi
if [[ $GW_KEY == "" ]]; then
echo "ERROR: GW_KEY required"
echo "See https://www.thethingsnetwork.org/docs/gateways/registration.html#via-gateway-connector"
exit 1
fi
2016-03-21 21:10:53 -07:00
echo "******************"
2016-03-21 14:20:50 -07:00
echo ""
2016-03-25 14:22:57 -04:00
# Load the region-appropriate global config
2016-03-22 10:45:40 -07:00
2016-03-25 13:23:18 -04:00
if curl -sS --fail https://raw.githubusercontent.com/TheThingsNetwork/gateway-conf/master/$GW_REGION-global_conf.json --output ./global_conf.json
2016-03-22 10:45:40 -07:00
then
2016-12-14 10:27:34 -05:00
echo Successfully loaded $GW_REGION-global_conf.json from TTN repo
2016-03-22 11:09:32 -07:00
else
2016-12-14 10:27:34 -05:00
echo "******************"
2016-03-22 13:03:58 -07:00
echo "ERROR: GW_REGION not found"
2016-12-14 10:27:34 -05:00
echo "******************"
2016-03-22 10:45:40 -07:00
exit 1
fi
2016-03-25 14:21:04 -04:00
# Fetch location info, which we'll use as a hint to the gateway software
2016-03-22 14:54:00 -07:00
if curl -sS --fail ipinfo.io --output ./ipinfo.json
then
2016-12-14 10:27:34 -05:00
echo "Actual gateway location:"
IPINFO=$(cat ./ipinfo.json)
echo $IPINFO
2016-03-22 14:54:00 -07:00
else
2016-12-14 10:27:34 -05:00
echo "Unable to determine gateway location"
IPINFO="\"\""
2016-03-22 14:54:00 -07:00
fi
2016-03-25 14:21:04 -04:00
# Set up environmental defaults for local.conf
2016-03-22 13:03:58 -07:00
2016-04-10 15:46:57 -04:00
if [[ $GW_REF_LATITUDE == "" ]]; then GW_REF_LATITUDE="52.376364"; fi
if [[ $GW_REF_LONGITUDE == "" ]]; then GW_REF_LONGITUDE="4.884232"; fi
if [[ $GW_REF_ALTITUDE == "" ]]; then GW_REF_ALTITUDE="3"; fi
2016-03-22 13:58:07 -07:00
if [[ $GW_GPS == "" ]]; then GW_GPS="true"; fi
2016-03-22 13:03:58 -07:00
2016-04-22 14:25:20 -04:00
if [[ $GW_GPS_TTY_PATH == "" ]]
2016-12-14 10:27:34 -05:00
then
# Default to AMA0 unless this is an RPi3 with core frequency set in fleet config vars
GW_GPS_TTY_PATH="/dev/ttyAMA0"
if [[ "$RESIN_MACHINE_NAME" == "raspberrypi3" && "$RESIN_HOST_CONFIG_core_freq" != "" ]]
then
GW_GPS_TTY_PATH="/dev/ttyS0"
fi
2016-04-22 14:25:20 -04:00
fi
2016-04-10 15:32:09 -04:00
if [[ $GW_FAKE_GPS == "" ]]; then GW_FAKE_GPS="false"; fi
2016-03-22 13:03:58 -07:00
2016-03-25 14:21:04 -04:00
# Synthesize local.conf JSON from env vars
2016-03-22 10:45:40 -07:00
2016-03-22 12:09:41 -07:00
echo -e "{\n\
\t\"gateway_conf\": {\n\
2016-03-22 13:03:58 -07:00
\t\t\"ref_latitude\": $GW_REF_LATITUDE,\n\
\t\t\"ref_longitude\": $GW_REF_LONGITUDE,\n\
\t\t\"ref_altitude\": $GW_REF_ALTITUDE,\n\
\t\t\"contact_email\": \"$GW_CONTACT_EMAIL\",\n\
2016-03-22 13:58:07 -07:00
\t\t\"description\": \"$GW_DESCRIPTION\", \n\
2016-03-22 13:40:41 -07:00
\t\t\"gps\": $GW_GPS,\n\
2016-04-22 10:25:48 -04:00
\t\t\"gps_tty_path\": \"$GW_GPS_TTY_PATH\",\n\
2016-03-22 13:40:41 -07:00
\t\t\"fake_gps\": $GW_FAKE_GPS,\n\
\t\t\"gateway_ID\": \"0000000000000000\",\n\
\t\t\"servers\": [\n\
\t\t{\
\t\t\t\"serv_type\": \"ttn\",\n\
\t\t\t\"serv_address\": \"router.$GW_REGION.thethings.network\",\n\
\t\t\t\"serv_gw_id\": \"$GW_ID\",\n\
\t\t\t\"serv_gw_key\": \"$GW_KEY\",\n\
\t\t\t\"serv_enabled\": true\n\
\t\t}]\n\
2016-03-22 11:45:20 -07:00
\t}\n\
}" >./local_conf.json
2016-03-21 14:20:50 -07:00
echo "local_conf.json"
echo "==============="
cat local_conf.json
echo "==============="
2016-03-21 15:58:37 -07:00
echo "******************"
# get gateway ID from its MAC address
GWID_PREFIX="FFFE"
GWID=$(ip link show eth0 | awk '/ether/ {print $2}' | awk -F\: '{print $1$2$3$4$5$6}')
# replace last 8 digits of default gateway ID by actual GWID, in given JSON configuration file
sed -i 's/\(^\s*"gateway_ID":\s*"\).\{16\}"\s*\(,\?\).*$/\1'${GWID_PREFIX}${GWID}'"\2/' local_conf.json
echo "Gateway_ID set to "$GWID_PREFIX$GWID" "
echo "******************"
echo ""
2016-03-21 15:58:37 -07:00
2016-03-26 15:18:15 -04:00
2016-12-14 10:27:34 -05:00
# Fire up the forwarder.
2016-03-25 14:21:04 -04:00
2016-03-21 12:36:55 -07:00
while true
2016-12-14 10:27:34 -05:00
do
# Reset the board to a known state prior to launching the forwarder
if [[ $GW_TYPE == "imst-ic880a-spi" ]]; then
echo "[TTN Gateway]: Toggling reset pin on IMST iC880A-SPI Board"
gpio -1 mode 22 out
gpio -1 write 22 0
sleep 0.1
gpio -1 write 22 1
sleep 0.1
gpio -1 write 22 0
sleep 0.1
elif [[ $GW_TYPE == "linklabs-dev" ]]; then
echo "[TTN Gateway]: Toggling reset pin on LinkLabs Raspberry Pi Development Board"
gpio -1 mode 29 out
gpio -1 write 29 0
sleep 0.1
gpio -1 write 29 1
sleep 0.1
gpio -1 write 29 0
sleep 0.1
elif [[ $GW_TYPE == "risinghf" ]]; then
## found this info via gwrst.sh in the risinghf loriot concentrator install package
## that info toggled pin 2, which I must assume to be Wiring's GPIO02 and thus
## pin BCM27/RPI13 on Raspberry Pi. It couldn't be RPi pin 2 because that's 5VDC.
echo "[TTN Gateway]: Toggling reset pin on Rising HF Board"
2016-12-15 20:03:35 -05:00
gpio -1 mode 26 out
gpio -1 write 26 0
2016-12-14 10:27:34 -05:00
sleep 0.1
2016-12-15 20:03:35 -05:00
gpio -1 write 26 1
2016-12-14 10:27:34 -05:00
sleep 0.1
2016-12-15 20:03:35 -05:00
gpio -1 write 26 0
2016-12-14 10:27:34 -05:00
sleep 0.1
elif [[ $GW_TYPE == "custom" ]]; then
echo "[TTN Gateway]: Toggling custom reset pin $CUSTOM_RESET_PIN"
gpio -1 mode $CUSTOM_RESET_PIN out
gpio -1 write $CUSTOM_RESET_PIN 0
sleep 0.1
gpio -1 write $CUSTOM_RESET_PIN 1
sleep 0.1
gpio -1 write $CUSTOM_RESET_PIN 0
sleep 0.1
else
echo "ERROR: unrecognized GW_TYPE=$GW_TYPE"
echo "See https://github.com/rayozzie/ttn-resin-gateway-rpi/blob/master/README.md"
exit 1
fi
2016-03-21 12:36:55 -07:00
echo "[TTN Gateway]: Starting packet forwarder..."
./mp_pkt_fwd
2016-12-14 10:33:27 -05:00
echo "****************** $CUSTOM_RESET_PIN"
2016-03-21 21:21:11 -07:00
echo "*** [TTN Gateway]: EXIT (retrying in 15s)"
2016-12-14 10:27:34 -05:00
echo "******************"
2016-03-21 12:36:55 -07:00
sleep 15
2016-12-14 10:27:34 -05:00
done