summaryrefslogtreecommitdiff
path: root/recipes-connectivity/lora/lora-basic-station/lora-basic-station.init
blob: 80582d944820549f434359ad24c857754a7a19cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash


NAME="lora-basic-station"
ENABLED="yes"


[ -f /etc/default/$NAME ] && source /etc/default/$NAME

run_dir=/var/run/lora
opt_conf_dir=/opt/lora
conf_dir=/var/config/lora
conf_file=$conf_dir/station.conf
tc_uri_file=$conf_dir/tc.uri
tc_key_file=$conf_dir/tc.key
tc_trust_file=$conf_dir/tc.trust

pkt_fwd=$run_dir/1/station
pkt_fwd_pidfile=$run_dir/station.pid

port1=/sys/devices/platform/mts-io/ap1
port2=/sys/devices/platform/mts-io/ap2

lora_mtac_id="MTAC-LORA"
lora_1_0_hw="MTAC-LORA-1.0"
lora_1_5_h_hw="MTAC-LORA-1.5"
lora_2_1_hw="MTAC-LORA-2.1"

lora_mtac_id="MTAC-LORA"
lora_mtac_id915="MTAC-LORA-915"
lora_mtac_id868="MTAC-LORA-868"
lora_mtac_h_id915="MTAC-LORA-H-915"
lora_mtac_h_id868="MTAC-LORA-H-868"

lora_mtcap_id="MTCAP-LORA"
lora_mtcap_id868="MTCAP-LORA-868"
lora_mtcap_id915="MTCAP-LORA-915"

lora_mtac_g_id="MTAC-LORA-G"
lora_mtac_g16_id868="MTAC-LORA-G16-868"
lora_mtac_g16_id915="MTAC-LORA-G16-915"
lora_mtac_g64_id868="MTAC-LORA-G64-868"
lora_mtac_g64_id915="MTAC-LORA-G64-915"

pkt_fwd_options=""

gps_path="/dev/gps0"

read_lora_hw_info() {
    lora_id=$(mts-io-sysfs show lora/product-id 2> /dev/null)
    lora_hw=$(mts-io-sysfs show lora/hw-version 2> /dev/null)
    lora_eui=$(mts-io-sysfs show lora/eui 2> /dev/null)
    lora_eui_raw=${lora_eui//:/}
}

hardware_found() {
    if [[ "$lora_id" =~ "$lora_mtac_g_id" ]]; then
        setup_mtcdt_2_1
    elif [[ "$lora_id" =~ "$lora_mtcap_id" ]]; then
        setup_mtcap
    elif [[ "$lora_id" =~ "$lora_mtac_id" ]]; then
        setup_mtcdt
    else
        return 1
    fi
    return 0
}

setup_mtcdt() {
    if [ -d $port1 ] && [[ $(cat $port1/hw-version) = $lora_hw ]]; then
        ln -sf /dev/spidev32766.2 /dev/spidev0.0
    elif [ -d $port2 ] && [[ $(cat $port2/hw-version) = $lora_hw ]]; then
        ln -sf /dev/spidev32765.2 /dev/spidev0.0
    fi

    ln -sf $opt_conf_dir/station $pkt_fwd
}

setup_mtcap() {

    ln -sf $opt_conf_dir/station $pkt_fwd

    if [ "$lora_id" = "$lora_mtcap_id868" ]; then
        if [ "$lora_hw" = "MTCAP-0.3" ]; then
            cp $opt_conf_dir/temp_lut.json.MTCAP2.EU868 $run_dir/1/temp_lut.json
        fi
    elif [ "$lora_id" = "$lora_mtcap_id915" ]; then
        if [ "$lora_hw" = "MTCAP-0.3" ]; then
            cp $opt_conf_dir/temp_lut.json.MTCAP2.US915 $run_dir/1/temp_lut.json
        fi
    fi
}

setup_mtcdt_2_1() {
    echo LORA-2.1 not supported
    return 1
}

do_start() {

    mkdir -p $run_dir/1
    rm -rf $run_dir/1/*

    read_lora_hw_info

    if hardware_found; then
        echo "Found $lora_id with $lora_hw hardware. Configuring"
    else
        echo "$0: LORA card not detected"
        exit 1
    fi

    if ! [ -f $conf_file ]; then
        echo "$0: $conf_file missing"
        exit 1
    fi

    #
    # copy conf files to the run directory
    #
    cp $conf_file $run_dir/1/
    cp $tc_uri_file $run_dir/1/
    cp $tc_key_file $run_dir/1/
    cp $tc_trust_file $run_dir/1/

    sed -i.bak "s/\(.*routerid\"\s*\:\s*\"\)<.*>[^\"]*\(.*\)/\1${lora_eui_raw}\2/g" $run_dir/1/station.conf

    #
    # start packet forwarder
    #
    echo -n "Starting $NAME: "

    /usr/sbin/start-stop-daemon --chdir $run_dir/1 --background --start --make-pidfile \
        --pidfile $pkt_fwd_pidfile --startas /bin/bash -- -c "exec $pkt_fwd $pkt_fwd_options"

    renice -n -20 -p $(pgrep $(basename $pkt_fwd))

    echo "OK"
}


do_stop() {
    echo -n "Stopping $NAME: "
    start-stop-daemon --stop --quiet --oknodo --pidfile $pkt_fwd_pidfile --retry 5
    rm -f $pkt_fwd_pidfile
    echo "OK"
}


if [ "$ENABLED" != "yes" ]; then
    echo "$NAME: disabled in /etc/default"
    exit
fi


case "$1" in
    "start")
    do_start
        ;;
    "stop")
    do_stop
        ;;
    "restart")
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        do_stop
        do_start
        ;;
    *)
        ## If no parameters are given, print which are avaiable.
        echo "Usage: $0 {start|stop|restart}"
        exit 1
    ;;
esac