summaryrefslogtreecommitdiff
path: root/recipes-connectivity/lora/lora-packet-forwarder/lora-packet-forwarder.init
blob: dbc1cb73a35aefa106b5da1503d3dafea02b6ae0 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/bin/bash


NAME="lora-packet-forwarder"
LOCK="/var/lock/$NAME"
ENABLED="yes"


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

daemon_start="/usr/sbin/start-stop-daemon --start --background"
daemon_stop="/usr/sbin/start-stop-daemon --stop --quiet --oknodo"
run_dir=/var/run/lora
opt_conf_dir=/opt/lora
conf_dir=/var/config/lora
conf_file=$conf_dir/global_conf.json
conf_file_local=$conf_dir/local_conf.json
conf_file_2=$conf_dir/global_conf_2.json
conf_file_local_2=$conf_dir/local_conf_2.json

pkt_fwd=$run_dir/1/lora_pkt_fwd
pkt_fwd_log=/var/log/lora-pkt-fwd-1.log
pkt_fwd_pidfile=$run_dir/lora-pkt-fwd-1.pid

pkt_fwd_2=$run_dir/2/lora_pkt_fwd
pkt_fwd_2_log=/var/log/lora-pkt-fwd-2.log
pkt_fwd_2_pidfile=$run_dir/lora-pkt-fwd-2.pid

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

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_2g4_hw="MTAC-LORA-2G4-0.0"

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_mtac_003_id="MTAC-003"
lora_mtac_003_id868="MTAC-003E00"
lora_mtac_003_id915="MTAC-003U00"

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

lora_mtcap3_id="MTCAP3"
lora_mtcap3_id868="MTCAP3-003E00"
lora_mtcap3_id915="MTCAP3-003U00"

lora_mtac_2g4_id="MTAC-LORA-2G4"

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=""
dual_cards_installed=false
gps_path="/dev/gps0"

read_lora_hw_info() {
    set +e

    hw_id=$(mts-io-sysfs show hw-version)

    # product-id of first lora card
    lora_id=$(mts-io-sysfs show lora/product-id 2> /dev/null)
    if [ $? -ne 0 ]; then
        lora_id="NONE"
    fi
    lora_eui=$(mts-io-sysfs show lora/eui 2> /dev/null)
    if [ $? -ne 0 ]; then
        lora_eui=$(mts-io-sysfs show mac-eth)
        lora_eui=${lora_eui:0:8}":FF:FF"${lora_eui:8:16}
    fi
    # remove all colons
    lora_eui_raw=${lora_eui//:}
    lora_hw=$(mts-io-sysfs show lora/hw-version 2> /dev/null)
    if [ $? -ne 0 ]; then
        lora_hw="NONE"
    fi

    if [ -d $port1 ] && [[ $(cat $port1/hw-version) =~ "LORA-1.5" ]] && [ -d $port2 ] && [[ $(cat $port2/hw-version) =~ "LORA-1.5" ]]; then
        dual_cards_installed=true
    elif [ -d $port1 ] && [[ $(cat $port1/hw-version) =~ "MTAC-003" ]] && [ -d $port2 ] && [[ $(cat $port2/hw-version) =~ "MTAC-003" ]]; then
        dual_cards_installed=true
    elif [ -d $port1 ] && [[ $(cat $port1/hw-version) =~ "LORA-2G4" ]] && [ -d $port2 ] && [[ $(cat $port2/hw-version) =~ "LORA-2G4" ]]; then
        dual_cards_installed=true
    fi

    if [[ "$dual_cards_installed" = "true" ]] && [ -d $port2 ] && [[ $(cat $port2/hw-version) =~ "LORA-1.5" || $(cat $port2/hw-version) =~ "LORA-2G4" || $(cat $port2/hw-version) =~ "MTAC-003" ]]; then
        # product-id of first lora card
        lora_2_id=$(mts-io-sysfs show lora-2/product-id 2> /dev/null)
        lora_2_hw=$(mts-io-sysfs show lora-2/hw-version 2> /dev/null)
        lora_2_eui=$(mts-io-sysfs show lora-2/eui 2> /dev/null)
        # remove all colons
        lora_2_eui_raw=${lora_2_eui//:}
    fi

    set -e

    # Check USB id for Semtech Card
    if [[ $(lsusb | grep 05c9:5740) =~ "05c9:5740" ]] && [[ ! "$lora_id" =~ "$lora_mtac_2g4_id" ]]; then
       lora_id=$lora_mtac_2g4_id
       lora_hw=$lora_2g4_hw
       lora_eui=$(mts-io-sysfs show mac-eth)
       lora_eui=${lora_eui:0:8}":FF:FF"${lora_eui:8:16}
       LORA_CAPABLE=true
    # Check USB id for Semtech Card in Bootloader mode
    elif [[ $(lsusb | grep 0483:df11) =~ "0483:df11" ]]; then
        echo "Semtech 2g4 MTAC card detected in Bootloader Mode, remove power from Conduit and restore"
        return 1
    fi
}


hardware_found() {
    if [[ "$lora_id" =~ "$lora_mtac_g_id" ]]; then
        setup_mtcdt_2_1
    elif [[ "$lora_id" =~ "$lora_mtac_2g4_id" ]]; then
        setup_mtcdt_2g4
    elif [[ "$lora_id" =~ "$lora_mtac_003_id" ]]; then
        setup_mtcdt003
    elif [[ "$lora_id" =~ "$lora_mtac_id" ]]; then
        setup_mtcdt
    elif [[ "$lora_id" =~ "$lora_mtcap3_id" ]]; then
        setup_mtcap3
    elif [[ "$lora_id" =~ "$lora_mtcap_id" ]]; then
        setup_mtcap
    else
        return 1
    fi
}

setup_mtcdt003() {
    if [[ "$lora_id" =~ "$lora_mtac_003_id868" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_003_0_0.EU868
    elif [[ "$lora_id" =~ "$lora_mtac_003_id915" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_003_0_0.US915
    else
        return 1
    fi

    ln -sf /opt/lora/lora_pkt_fwd_sx1303 $pkt_fwd
    ln -sf /opt/lora/lora_pkt_fwd_sx1303 $pkt_fwd_2

    # ignore error of diff command
    set +e
    diff $GLOBAL_CONF /opt/lora/global_conf.json 1>/dev/null
    if [ $? -ne 0 ]; then
        cp $GLOBAL_CONF /opt/lora/global_conf.json
    fi
    set -e

    return 0
}

setup_mtcap3() {
    if [[ "$lora_id" = "$lora_mtcap3_id868" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP3.EU868
    elif [[ "$lora_id" = "$lora_mtcap3_id915" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP3.US915
    else
        return 1
    fi

    ln -sf /opt/lora/lora_pkt_fwd_sx1303 $pkt_fwd

    # ignore error of diff command
    set +e
    diff $GLOBAL_CONF /opt/lora/global_conf.json 1>/dev/null
    if [ $? -ne 0 ]; then
        cp $GLOBAL_CONF /opt/lora/global_conf.json
    fi
    set -e

    return 0
}

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

    if [ "$lora_hw" = "$lora_1_0_hw" ] && [[ ! "$lora_id" =~ .*-SPI ]]; then
        if [ "$lora_id" = "$lora_mtac_id868" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_1_0.EU868
        elif [ "$lora_id" = "$lora_mtac_id915" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_1_0.US915
        else
            echo "No valid configuration found for mtcdt exiting."
            exit 1
        fi
        ln -sf /opt/lora/basic_pkt_fwd-usb $pkt_fwd
    else
        if [ "$lora_id" = "$lora_mtac_h_id868" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_1_5.EU868
        elif [ "$lora_id" = "$lora_mtac_h_id915" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_1_5.US915
        else
            echo "No valid configuration found for mtcdt exiting."
            exit 1
        fi
        ln -sf /opt/lora/lora_pkt_fwd $pkt_fwd
        ln -sf /opt/lora/lora_pkt_fwd $pkt_fwd_2
    fi

    opt_conf_file=$opt_conf_dir/global_conf.json
    opt_conf_file_local=$opt_conf_dir/local_conf.json

    cp $GLOBAL_CONF $opt_conf_file

    if ! [ -f $conf_file ]; then
        mkdir -p /var/config/lora/
        cp $opt_conf_file $conf_file
        cp $opt_conf_file_local $conf_file_local
        cp $opt_conf_file $conf_file_2
        cp $opt_conf_file_local $conf_file_local_2
    fi
}

setup_mtcap() {
    hw=$(mts-io-sysfs show hw-version 2> /dev/null)

    if [ "$lora_id" = "$lora_mtcap_id868" ]; then
        if [ "$hw" = "MTCAP-0.1" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP_LORA_1_5.EU868
        else
            GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP2_LORA_1_5.EU868
        fi
    elif [ "$lora_id" = "$lora_mtcap_id915" ]; then
        if [ "$hw" = "MTCAP-0.1" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP_LORA_1_5.US915
        else
            GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP2_LORA_1_5.US915
        fi
    else
        return 1
    fi

    opt_conf_file=$opt_conf_dir/global_conf.json
    opt_conf_file_local=$opt_conf_dir/local_conf.json

    cp $GLOBAL_CONF $opt_conf_file

    if ! [ -f $conf_file ]; then
        mkdir -p /var/config/lora/
        cp $opt_conf_file $conf_file
        cp $opt_conf_file_local $conf_file_local
    fi

    ln -sf /opt/lora/lora_pkt_fwd $pkt_fwd
}

setup_mtcdt_2_1() {
    if [[ "$lora_id" = "$lora_mtac_g16_id868" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_2_1_loc_single_antenna_16ch_EU868
    elif [[ "$lora_id" = "$lora_mtac_g16_id915" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_2_1_loc_single_antenna_16ch_US915
    elif [[ "$lora_id" = "$lora_mtac_g64_id868" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_2_1_test_single_antenna_64ch_8x8_EU868
    elif [[ "$lora_id" = "$lora_mtac_g64_id915" ]]; then
        GLOBAL_CONF=/opt/lora/global_conf.json.MTAC_LORA_2_1_loc_single_antenna_64ch_US915
    else
        return 1
    fi

    opt_conf_file=$opt_conf_dir/global_conf.json

    cp $GLOBAL_CONF $opt_conf_file

    if ! [ -f $conf_file ]; then
        mkdir -p /var/config/lora/
        cp $opt_conf_file $conf_file
    fi

    ln -sf /opt/lora/pkt_forwarder $pkt_fwd

    if [ -f $gps_path ]; then
        pkt_fwd_options=" -g"
    fi
}

do_start() {

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

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

    read_lora_hw_info

    if hardware_found; then
        echo "Found $lora_id with $lora_hw hardware. Configuring"

        if [[ "$hw_id" =~ "$mtcdt3_id" ]]; then
            mts-io-sysfs store lora/reset 0
            mts-io-sysfs store lora-2/reset 0
            sleep 0.25
            mts-io-sysfs store lora/reset 1
            mts-io-sysfs store lora-2/reset 1
            sleep 0.25
        elif [[ "$lora_id" =~ "$lora_mtac_g_id" ]]; then
            echo Resetting 2.1 LoRa
            /usr/sbin/mts-util-lora2-reset -g -f >/dev/null 2>&1
            sleep 0.25
        elif [[ ! "$lora_id" =~ "$lora_mtcap3_id" ]] && [[ ! "$lora_id" =~ "$lora_mtac_003_id" ]]; then
            mts-io-sysfs store lora/reset 0
            if [ "$dual_cards_installed" == true ]; then
                mts-io-sysfs store lora-2/reset 0
            fi
            sleep 0.25
            mts-io-sysfs store lora/reset 1
            if [ "$dual_cards_installed" == true ]; then
            mts-io-sysfs store lora-2/reset 1
            fi
            sleep 0.25
        fi

    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/
    if [ -f $conf_file_local ]; then
        cp $conf_file_local $run_dir/1/
        sed -i.bak "s/\(.*gateway_ID\"\s*\:\s*\"\)<.*>[^\"]*\(.*\)/\1${lora_eui_raw}\2/g" /var/run/lora/1/local_conf.json
    fi

    sed -i.bak "s/\(.*gateway_ID\"\s*\:\s*\"\)<.*>[^\"]*\(.*\)/\1${lora_eui_raw}\2/g" /var/run/lora/1/global_conf.json

    if [ "$dual_cards_installed" == "true" ]; then
        cp $conf_file_2 $run_dir/2/global_conf.json
        if [ -f $conf_file_local_2 ]; then
            cp $conf_file_local_2 $run_dir/2/local_conf.json
            sed -i.bak "s/\(.*gateway_ID\"\s*\:\s*\"\)<.*>[^\"]*\(.*\)/\1${lora_2_eui_raw}\2/g" /var/run/lora/2/local_conf.json
        fi

        sed -i.bak "s/\(.*gateway_ID\"\s*\:\s*\"\)<.*>[^\"]*\(.*\)/\1${lora_2_eui_raw}\2/g" /var/run/lora/2/global_conf.json
    fi

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

    pkf_exec="exec $pkt_fwd $pkt_fwd_options -l $pkt_fwd_log"

    $daemon_start --chdir $run_dir/1 --make-pidfile --pidfile $pkt_fwd_pidfile --startas /bin/bash -- -c "$pkf_exec"

    echo "check process lora-pkt-fwd-1 with pidfile $pkt_fwd_pidfile
    program start = \"$daemon_start --chdir $run_dir/1 --make-pidfile --pidfile $pkt_fwd_pidfile --startas /bin/bash -- -c '$pkf_exec'\"
    program stop = \"$daemon_stop --pidfile $pkt_fwd_pidfile --retry 5\"" > /etc/monit.d/lora-pkt-fwd-1

    if [ "$dual_cards_installed" == "true" ]; then
        pkt_fwd_options=" -p 2"
        pkf_2_exec="exec $pkt_fwd_2 $pkt_fwd_options -l $pkt_fwd_2_log"
            $daemon_start --chdir $run_dir/2 --make-pidfile --pidfile $pkt_fwd_2_pidfile --startas /bin/bash -- -c "$pkf_2_exec"
            echo "check process lora-pkt-fwd-2 with pidfile $pkt_fwd_2_pidfile
            program start = \"$daemon_start --chdir $run_dir/2 --make-pidfile --pidfile $pkt_fwd_2_pidfile --startas /bin/bash -- -c '$pkf_2_exec'\"
            program stop = \"$daemon_stop --pidfile $pkt_fwd_2_pidfile --retry 5\"" > /etc/monit.d/lora-pkt-fwd-2
    fi

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

    echo "OK"
}


do_stop() {
    echo -n "Stopping $NAME: "

    if monit summary lora-pkt-fwd-1 > /dev/null 2>&1; then
        monit unmonitor lora-pkt-fwd-1
        start-stop-daemon --stop --quiet --oknodo --pidfile $pkt_fwd_pidfile --retry 5
        rm -f $pkt_fwd_pidfile
    fi
    if monit summary lora-pkt-fwd-2 > /dev/null 2>&1; then
        monit unmonitor lora-pkt-fwd-2
        start-stop-daemon --stop --quiet --oknodo --pidfile $pkt_fwd_2_pidfile --retry 5
        rm -f $pkt_fwd_2_pidfile
    fi
    rm -rf /etc/monit.d/lora*
    monit reload

    echo "OK"
}

force_stop() {
    do_stop
    rm -fr $LOCK
}

function try_lock() {
    if mkdir $LOCK; then
        trap "rm -fr $LOCK" EXIT
    else
        echo "Lora Packet Forwarder lock not acquired, resource in use"
        exit 1
    fi
}

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


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