#!/bin/bash # Author: lbuque # Github: https://github.com/Xinyuan-LilyGO/LilyGo-LoRa-Series RED="\033[31m" # Error message GREEN="\033[32m" # Success message YELLOW="\033[33m" # Warning message BLUE="\033[36m" # Info message PLAIN='\033[0m' # set -euxo pipefail readonly __version=0.1.1 readonly WORK_DIR=/home/pi/.sx1302_hal readonly sx1302_hal_name=sx1302_hal readonly sx1302_hal_url=https://github.com/Lora-net/sx1302_hal/archive/refs/tags/V2.1.0.tar.gz readonly sx1302_hal_version=2.1.0 colorEcho() { echo -e "${1}${@:2}${PLAIN}" } checkSystem() { uid=$(id -u) if [[ $uid -ne 0 ]]; then colorEcho $RED " Please execute the script as root" # " 请以root身份执行该脚本" exit 1 fi res=$(command -v apt) if [[ "$res" = "" ]]; then colorEcho $RED "Unsupported Linux systems" exit 1 fi res=$(command -v systemctl) if [[ "$res" = "" ]]; then colorEcho $RED "The system version is too low, please upgrade to the latest version" exit 1 fi res=$(command -v wget) if [[ "$res" = "" ]]; then colorEcho $RED "Missing wget tool" colorEcho $YELLOW "Try to install wget tool" apt-get install wget if [ $? != 0 ]; then colorEcho $RED "Failed to install wget tool" exit 1 fi fi res=$(command -v patch) if [[ "$res" = "" ]]; then colorEcho $RED "Missing patch tool" colorEcho $YELLOW "Try to install patch tool" apt-get install patch if [ $? != 0 ]; then colorEcho $RED "Failed to install patch tool" exit 1 fi fi } prepare() { if [ ! -d "$WORK_DIR" ]; then mkdir -p $WORK_DIR fi if [ ! -d "$WORK_DIR/dist" ]; then mkdir -p $WORK_DIR/dist fi if [ ! -d "$WORK_DIR/bin" ]; then mkdir -p $WORK_DIR/bin fi } download() { sx1302_hal_archive=$WORK_DIR/dist/$sx1302_hal_name-$sx1302_hal_version.tar.gz if [ ! -f $sx1302_hal_archive ]; then wget $sx1302_hal_url -O $sx1302_hal_archive fi if [ ! -d $WORK_DIR/$sx1302_hal_name-$sx1302_hal_version ]; then tar -xvf $WORK_DIR/dist/$sx1302_hal_name-$sx1302_hal_version.tar.gz -C $WORK_DIR fi } patching() { if [ -f $WORK_DIR/$sx1302_hal_name-$sx1302_hal_version/.patch ]; then return 0 fi local readonly oragw_hal_c_patch=$(cat <> /boot/config.txt fi fi echo $ret } getEthName() { local interface=`ifconfig | grep enx | awk '{print $1}'` if [ "$interface" = "" ]; then interface=`ifconfig | grep eth0 | awk '{print $1}'` fi echo $interface } getGatewayID() { local readonly GWID_MIDFIX="FFFE" local interface=`getEthName` if [ "$interface" = "" ]; then echo "001122"$GWID_MIDFIX"334455" else interface=${interface%%:*} local readonly GWID_BEGIN=$(ip link show $interface | awk '/ether/ {print $2}' | awk -F\: '{print $1$2$3}') local readonly GWID_END=$(ip link show $interface | awk '/ether/ {print $2}' | awk -F\: '{print $4$5$6}') echo $GWID_BEGIN$GWID_MIDFIX$GWID_END fi } eu868Config() { local readonly global_conf=$(cat < $WORK_DIR/bin/global_conf.json } us915Config() { local readonly global_conf=$(cat < $WORK_DIR/bin/global_conf.json } as923Config() { local readonly global_conf=$(cat < $WORK_DIR/bin/global_conf.json } daemon() { local readonly lora_pkt_fwd_service_file=$(cat < /etc/systemd/system/lora_pkt_fwd.service systemctl daemon-reload systemctl enable lora_pkt_fwd.service systemctl restart lora_pkt_fwd.service sudo cp $WORK_DIR/$sx1302_hal_name-$sx1302_hal_version/tools/systemd/lora_pkt_fwd.conf /etc/rsyslog.d sudo systemctl restart rsyslog # reboot } help() { local readonly usage="Script to automatically deploy packet_forwarder Usage: sudo ./sx1302_hal.sh [-s server] [-p port] [-f frequency] [--eui eui] [--verbose] [-o output] sudo ./sx1302_hal.sh ./sx1302_hal.sh -v ./sx1302_hal.sh --help Option: -s --server Gateway Server address. The default value is eu1.cloud.thethings.network. -p --port Gateway Server port. The default value is 1700. -f --frequency The frequency band of lorawan supports eu868, us915, as923. The default value is eu868. -eui Gateway EUI. The default is produced by the mac address of eth0. -o --output Output gateway information. Miscellaneous options: --verbose Output extra information about the work being done. -v --version Output version info. --help Output this help. Report bugs to https://github.com/Xinyuan-LilyGO/LilyGo-LoRa-Series/issues." echo -e "$usage" } version() { local readonly version_info="sx1302_hal.sh $__version Copyright (c) 2022 Shenzhen Xin Yuan Electronic Technology Co., Ltd Written by lbuque" echo -e "$version_info" } showInfo() { echo colorEcho $GREEN "packet_forwarder installed successfully!!! " colorEcho $GREEN "===========================================" colorEcho $GREEN "Installation path: $WORK_DIR" colorEcho $GREEN "application path: $WORK_DIR/bin/lora_pkt_fwd" colorEcho $GREEN "Configuration file path: $WORK_DIR/bin/global_conf.json" colorEcho $GREEN "Systemctl file path: /etc/systemd/system/lora_pkt_fwd.service" colorEcho $GREEN "Gateway EUI: $1" colorEcho $GREEN "Gateway Server address: $2" colorEcho $GREEN "Gateway Server port: $3" if [ "$4" != "" ]; then echo "Gateway EUI: $1" > $4 echo "Gateway Server address: $2" >> $4 echo "Gateway Server port: $3" >> $4 fi } main() { TEMP=`getopt -o s:p:f:o:v --long server:,port:,frequency:,eui:,output:,verbose,version,help -n $0 -- "$@"` if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi eval set -- "$TEMP" local eui=`getGatewayID` eui=${eui^^} local server_address="eu1.cloud.thethings.network" local server_port=1700 local frequency_plan="eu868" local output_file=$WORK_DIR/.output while true ; do case "$1" in -s|--server) server_address=$2 shift 2 ;; -p|--port) server_port=$2 shift 2 ;; --eui) eui=${$2^^} shift 2 ;; -f|--frequency) frequency_plan=$2 case "$frequency_plan" in eu868|us915|as923) ;; *) colorEcho $RED "Wrong frequency plan($frequency_plan), Please choose eu868, us915, as923!" exit 1 ;; esac shift 2 ;; -o|--output) output_file=$2 shift 2 ;; --verbose) set -euxo pipefail ; shift ;; -v|--version) version; exit 0 ;; --help) help; exit 0 ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done checkSystem prepare download patching compile install ret=`raspiConfig` case "$frequency_plan" in eu868) eu868Config $eui $server_address $server_port ;; us915) us915Config $eui $server_address $server_port ;; as923) as923Config $eui $server_address $server_port ;; *) colorEcho $RED "Wrong frequency plan($frequency_plan), Please choose eu868, us915, as923!" exit 1 ;; esac daemon showInfo $eui $server_address $server_port $output_file if [ "$ret" = "0" ]; then echo colorEcho $BLUE "Take effect after restarting the system! ! !" fi } main $@