From c425b67a5ccaaef0dd1042da08974245124c069b Mon Sep 17 00:00:00 2001 From: jk Date: Sat, 15 Aug 2020 17:43:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E9=94=AE=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E8=84=9A=E6=9C=AC=EF=BC=8C=E9=80=82=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=A0=91=E8=8E=93=E6=B4=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.sh | 10 + packet_forwarder/Makefile | 188 +++++++++--------- packet_forwarder/global_conf.json | 95 +++++++++ packet_forwarder/gwstart.sh | 6 + packet_forwarder/update_gwid.sh | 31 +++ setup/loragw.service | 15 ++ target.cfg | 2 +- ...11\350\243\205\350\257\264\346\230\216.md" | 9 + 8 files changed, 262 insertions(+), 94 deletions(-) create mode 100755 install.sh create mode 100644 packet_forwarder/global_conf.json create mode 100755 packet_forwarder/gwstart.sh create mode 100755 packet_forwarder/update_gwid.sh create mode 100644 setup/loragw.service create mode 100644 "\345\256\211\350\243\205\350\257\264\346\230\216.md" diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..b6bf019 --- /dev/null +++ b/install.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +ssh-keygen -t rsa +ssh-copy-id -i ~/.ssh/id_rsa.pub pi@localhost +make clean all +make install +make install_conf +sudo cp setup/loragw.service /usr/lib/systemd/system/loragw.service +sudo systemctl start loragw.service +sudo systemctl enable loragw.service diff --git a/packet_forwarder/Makefile b/packet_forwarder/Makefile index 8309750..eb98d2b 100644 --- a/packet_forwarder/Makefile +++ b/packet_forwarder/Makefile @@ -1,93 +1,95 @@ -### get external defined data - -include ../target.cfg - -### Application-specific constants - -APP_NAME := lora_pkt_fwd - -### Environment constants - -LGW_PATH ?= ../libloragw -LIB_PATH ?= ../libtools -ARCH ?= -CROSS_COMPILE ?= - -OBJDIR = obj -INCLUDES = $(wildcard inc/*.h) - -### External constant definitions -# must get library build option to know if mpsse must be linked or not - -include $(LGW_PATH)/library.cfg -RELEASE_VERSION := `cat ../VERSION` - -### Constant symbols - -CC := $(CROSS_COMPILE)gcc -AR := $(CROSS_COMPILE)ar - -CFLAGS := -O2 -Wall -Wextra -std=c99 -Iinc -I. -I../libtools/inc -VFLAG := -D VERSION_STRING="\"$(RELEASE_VERSION)\"" - -### Constants for Lora concentrator HAL library -# List the library sub-modules that are used by the application - -LGW_INC = -ifneq ($(wildcard $(LGW_PATH)/inc/config.h),) - # only for HAL version 1.3 and beyond - LGW_INC += $(LGW_PATH)/inc/config.h -endif -LGW_INC += $(LGW_PATH)/inc/loragw_hal.h - -### Linking options - -LIBS := -lloragw -ltinymt32 -lparson -lbase64 -lrt -lpthread -lm - -### General build targets - -all: $(APP_NAME) - -clean: - rm -f $(OBJDIR)/*.o - rm -f $(APP_NAME) - -ifneq ($(strip $(TARGET_IP)),) - ifneq ($(strip $(TARGET_DIR)),) - ifneq ($(strip $(TARGET_USR)),) -install: - @echo "---- Copying packet_forwarder files to $(TARGET_IP):$(TARGET_DIR)" - @ssh $(TARGET_USR)@$(TARGET_IP) "mkdir -p $(TARGET_DIR)" - @scp lora_pkt_fwd $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) -install_conf: - @echo "---- Copying packet_forwarder conf files to $(TARGET_IP):$(TARGET_DIR)" - @ssh $(TARGET_USR)@$(TARGET_IP) "mkdir -p $(TARGET_DIR)" - @scp global_conf.json.sx1250.* $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) - @scp global_conf.json.sx1257.* $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) - else - @echo "ERROR: TARGET_USR is not configured in target.cfg" - endif - else - @echo "ERROR: TARGET_DIR is not configured in target.cfg" - endif -else - @echo "ERROR: TARGET_IP is not configured in target.cfg" -endif - -### Sub-modules compilation - -$(OBJDIR): - mkdir -p $(OBJDIR) - -$(OBJDIR)/%.o: src/%.c $(INCLUDES) | $(OBJDIR) - $(CC) -c $(CFLAGS) -I$(LGW_PATH)/inc $< -o $@ - -### Main program compilation and assembly - -$(OBJDIR)/$(APP_NAME).o: src/$(APP_NAME).c $(LGW_INC) $(INCLUDES) | $(OBJDIR) - $(CC) -c $(CFLAGS) $(VFLAG) -I$(LGW_PATH)/inc $< -o $@ - -$(APP_NAME): $(OBJDIR)/$(APP_NAME).o $(LGW_PATH)/libloragw.a $(OBJDIR)/jitqueue.o - $(CC) -L$(LGW_PATH) -L$(LIB_PATH) $< $(OBJDIR)/jitqueue.o -o $@ $(LIBS) - -### EOF +### get external defined data + +include ../target.cfg + +### Application-specific constants + +APP_NAME := lora_pkt_fwd + +### Environment constants + +LGW_PATH ?= ../libloragw +LIB_PATH ?= ../libtools +ARCH ?= +CROSS_COMPILE ?= + +OBJDIR = obj +INCLUDES = $(wildcard inc/*.h) + +### External constant definitions +# must get library build option to know if mpsse must be linked or not + +include $(LGW_PATH)/library.cfg +RELEASE_VERSION := `cat ../VERSION` + +### Constant symbols + +CC := $(CROSS_COMPILE)gcc +AR := $(CROSS_COMPILE)ar + +CFLAGS := -O2 -Wall -Wextra -std=c99 -Iinc -I. -I../libtools/inc +VFLAG := -D VERSION_STRING="\"$(RELEASE_VERSION)\"" + +### Constants for Lora concentrator HAL library +# List the library sub-modules that are used by the application + +LGW_INC = +ifneq ($(wildcard $(LGW_PATH)/inc/config.h),) + # only for HAL version 1.3 and beyond + LGW_INC += $(LGW_PATH)/inc/config.h +endif +LGW_INC += $(LGW_PATH)/inc/loragw_hal.h + +### Linking options + +LIBS := -lloragw -ltinymt32 -lparson -lbase64 -lrt -lpthread -lm + +### General build targets + +all: $(APP_NAME) + +clean: + rm -f $(OBJDIR)/*.o + rm -f $(APP_NAME) + +ifneq ($(strip $(TARGET_IP)),) + ifneq ($(strip $(TARGET_DIR)),) + ifneq ($(strip $(TARGET_USR)),) +install: + @echo "---- Copying packet_forwarder files to $(TARGET_IP):$(TARGET_DIR)" + @ssh $(TARGET_USR)@$(TARGET_IP) "mkdir -p $(TARGET_DIR)" + @scp lora_pkt_fwd $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) +install_conf: + @echo "---- Copying packet_forwarder conf files to $(TARGET_IP):$(TARGET_DIR)" + @ssh $(TARGET_USR)@$(TARGET_IP) "mkdir -p $(TARGET_DIR)" + @scp global_conf.json.sx1250.* $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) + @scp global_conf.json.sx1257.* $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) + @scp global_conf.json $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) + @scp gwstart.sh update_gwid.sh $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR) + else + @echo "ERROR: TARGET_USR is not configured in target.cfg" + endif + else + @echo "ERROR: TARGET_DIR is not configured in target.cfg" + endif +else + @echo "ERROR: TARGET_IP is not configured in target.cfg" +endif + +### Sub-modules compilation + +$(OBJDIR): + mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: src/%.c $(INCLUDES) | $(OBJDIR) + $(CC) -c $(CFLAGS) -I$(LGW_PATH)/inc $< -o $@ + +### Main program compilation and assembly + +$(OBJDIR)/$(APP_NAME).o: src/$(APP_NAME).c $(LGW_INC) $(INCLUDES) | $(OBJDIR) + $(CC) -c $(CFLAGS) $(VFLAG) -I$(LGW_PATH)/inc $< -o $@ + +$(APP_NAME): $(OBJDIR)/$(APP_NAME).o $(LGW_PATH)/libloragw.a $(OBJDIR)/jitqueue.o + $(CC) -L$(LGW_PATH) -L$(LIB_PATH) $< $(OBJDIR)/jitqueue.o -o $@ $(LIBS) + +### EOF diff --git a/packet_forwarder/global_conf.json b/packet_forwarder/global_conf.json new file mode 100644 index 0000000..3ecb74f --- /dev/null +++ b/packet_forwarder/global_conf.json @@ -0,0 +1,95 @@ +{ + "SX130x_conf": { + "spidev_path": "/dev/spidev0.0", + "lorawan_public": true, + "clksrc": 0, + "antenna_gain": 0, /* antenna gain, in dBi */ + "full_duplex": false, + "precision_timestamp": { + "enable": false, + "max_ts_metrics": 255, + "nb_symbols": 1 + }, + "radio_0": { + "enable": true, + "type": "SX1250", + "single_input_mode": true, + "freq": 470600000, + "rssi_offset": -207.0, + "rssi_tcomp": {"coeff_a": 0, "coeff_b": 0, "coeff_c": 20.41, "coeff_d": 2162.56, "coeff_e": 0}, + "tx_enable": true, + "tx_freq_min": 470000000, + "tx_freq_max": 510000000, + "tx_gain_lut":[ + {"rf_power": -6, "pa_gain": 0, "pwr_idx": 0}, + {"rf_power": -3, "pa_gain": 0, "pwr_idx": 1}, + {"rf_power": 0, "pa_gain": 0, "pwr_idx": 2}, + {"rf_power": 3, "pa_gain": 1, "pwr_idx": 3}, + {"rf_power": 6, "pa_gain": 1, "pwr_idx": 4}, + {"rf_power": 10, "pa_gain": 1, "pwr_idx": 5}, + {"rf_power": 11, "pa_gain": 1, "pwr_idx": 6}, + {"rf_power": 12, "pa_gain": 2, "pwr_idx": 7}, + {"rf_power": 13, "pa_gain": 1, "pwr_idx": 8}, + {"rf_power": 14, "pa_gain": 2, "pwr_idx": 9}, + {"rf_power": 16, "pa_gain": 2, "pwr_idx": 10}, + {"rf_power": 20, "pa_gain": 3, "pwr_idx": 11}, + {"rf_power": 23, "pa_gain": 3, "pwr_idx": 12}, + {"rf_power": 25, "pa_gain": 3, "pwr_idx": 13}, + {"rf_power": 26, "pa_gain": 3, "pwr_idx": 14}, + {"rf_power": 27, "pa_gain": 3, "pwr_idx": 15} + ] + }, + "radio_1": { + "enable": true, + "type": "SX1250", + "single_input_mode": true, + "freq": 471400000, + "rssi_offset": -207.0, + "rssi_tcomp": {"coeff_a": 0, "coeff_b": 0, "coeff_c": 20.41, "coeff_d": 2162.56, "coeff_e": 0}, + "tx_enable": false + }, + "chan_multiSF_0": {"enable": true, "radio": 0, "if": -300000}, + "chan_multiSF_1": {"enable": true, "radio": 0, "if": -100000}, + "chan_multiSF_2": {"enable": true, "radio": 0, "if": 100000}, + "chan_multiSF_3": {"enable": true, "radio": 0, "if": 300000}, + "chan_multiSF_4": {"enable": true, "radio": 1, "if": -300000}, + "chan_multiSF_5": {"enable": true, "radio": 1, "if": -100000}, + "chan_multiSF_6": {"enable": true, "radio": 1, "if": 100000}, + "chan_multiSF_7": {"enable": true, "radio": 1, "if": 300000}, + "chan_Lora_std": {"enable": true, "radio": 1, "if": -200000, "bandwidth": 250000, "spread_factor": 7, + "implicit_hdr": false, "implicit_payload_length": 17, "implicit_crc_en": false, "implicit_coderate": 1}, + "chan_FSK": {"enable": true, "radio": 1, "if": 300000, "bandwidth": 125000, "datarate": 50000} + }, + + "gateway_conf": { + "gateway_ID": "fffedca6320e9516", + /* change with default server address/ports */ + "server_address": "loragw.things.qcloud.com", + "serv_port_up": 1700, + "serv_port_down": 1700, + /* adjust the following parameters for your network */ + "keepalive_interval": 10, + "stat_interval": 30, + "push_timeout_ms": 100, + /* forward only valid packets */ + "forward_crc_valid": true, + "forward_crc_error": false, + "forward_crc_disabled": false, + /* Beaconing parameters */ + "beacon_period": 0, + "beacon_freq_hz": 869525000, + "beacon_datarate": 9, + "beacon_bw_hz": 125000, + "beacon_power": 14, + "beacon_infodesc": 0 + }, + + "debug_conf": { + "ref_payload":[ + {"id": "0xCAFE1234"}, + {"id": "0xCAFE2345"} + ], + "log_file": "loragw_hal.log" + } +} + diff --git a/packet_forwarder/gwstart.sh b/packet_forwarder/gwstart.sh new file mode 100755 index 0000000..ce01a88 --- /dev/null +++ b/packet_forwarder/gwstart.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +cd /home/pi/gw1302s/bin +./update_gwid.sh ./global_conf.json +./lora_pkt_fwd + diff --git a/packet_forwarder/update_gwid.sh b/packet_forwarder/update_gwid.sh new file mode 100755 index 0000000..ce6938c --- /dev/null +++ b/packet_forwarder/update_gwid.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +# This script is a helper to update the Gateway_ID field of given +# JSON configuration file, as a EUI-64 address generated from the 48-bits MAC +# address of the device it is run from. +# +# Usage examples: +# ./update_gwid.sh ./local_conf.json + +iot_sk_update_gwid() { + # get gateway ID from its MAC address to generate an EUI-64 address + GWID_BEGINFIX="fffe" + GWID_MID=$(ip link show eth0 | awk '/ether/ {print $2}' | awk -F\: '{print $1$2$3}') + GWID_END=$(ip link show eth0 | awk '/ether/ {print $2}' | awk -F\: '{print $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_BEGINFIX}${GWID_MID}${GWID_END}'"\2/' $1 + + echo "Gateway_ID set to "$GWID_BEGINFIX$GWID_MID$GWID_END" in file "$1 +} + +if [ $# -ne 1 ] +then + echo "Usage: $0 [filename]" + echo " filename: Path to JSON file containing Gateway_ID for packet forwarder" + exit 1 +fi + +iot_sk_update_gwid $1 + +exit 0 diff --git a/setup/loragw.service b/setup/loragw.service new file mode 100644 index 0000000..79806bd --- /dev/null +++ b/setup/loragw.service @@ -0,0 +1,15 @@ +[Unit] +Description=lorawan gw +Wants=network.target +After=network.target + +[Service] +ExecStart=/home/pi/gw1302s/bin/gwstart.sh +ExecReload=/bin/kill -HUP $MAINPID +Restart=on-failure +KillMode=process +RestartSec=40s + +[Install] +WantedBy=multi-user.target + diff --git a/target.cfg b/target.cfg index c55e132..8db46b3 100644 --- a/target.cfg +++ b/target.cfg @@ -4,7 +4,7 @@ TARGET_IP=localhost # The directory on the gateway file system to which the files need to be installed -TARGET_DIR=/home/pi/sx1302_hal/bin +TARGET_DIR=/home/pi/gw1302s/bin # The user to be used by ssh/scp to copy the files on the gateway host TARGET_USR=pi diff --git "a/\345\256\211\350\243\205\350\257\264\346\230\216.md" "b/\345\256\211\350\243\205\350\257\264\346\230\216.md" new file mode 100644 index 0000000..aa094f9 --- /dev/null +++ "b/\345\256\211\350\243\205\350\257\264\346\230\216.md" @@ -0,0 +1,9 @@ +## 安装方法 +执行`./install.sh`,会自动进行编译、安装,并启动lorawan网关服务,而且配置成开机自启动 + +## 注意事项 +1. 在执行install.sh的过程中,因为会用到scp,并且会生成keygen,会有对话,此时回车即可 +2. 在执行install.sh的过程中,可能会询问账户密码,此时输入密码即可(树莓派的默认用户名密码为pi:rasppberry) +3. 会自动生成网关ID,其规律为"fffe+MAC地址",例如MAC地址为00:01:02:03:04:05,则生成的网关ID为fffe000102030405 +4. 默认网关的只想为腾讯云开发平台 +5. 可执行文件、配置文件都安装在$HOME/gw1302s/bin目录下,想要修改频点信息、网关指向,修改此文件夹下的global_conf.json文件 -- Gitee