summaryrefslogtreecommitdiff
path: root/recipes-connectivity/lora/lora-packet-forwarder/mtcap/lora-packet-forwarder.init
blob: b17a3d81ecb1269c28e2996a5431d425c74d77e5 (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
#!/bin/bash

NAME="lora-packet-forwarder"
ENABLED="yes"

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

run_dir=/var/run/lora
conf_file=/var/config/lora/global_conf.json
conf_file_local=/var/config/lora/local_conf.json
opt_conf_file=/opt/lora/global_conf.json
opt_conf_file_local=/opt/lora/local_conf.json
log_file=/var/log/lora-pkt-fwd.log

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

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

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

hardware_found() {
    if [[ "$lora_id" =~ "$lora_mtcap_id" ]]; then
        #
        # create /opt/lora/global_conf.json based on detected LORA card
        #
        if [ "$lora_id" = "$lora_mtcap_id868" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP_LORA_1_5.EU868
        elif [ "$lora_id" = "$lora_mtcap_id915" ]; then
            GLOBAL_CONF=/opt/lora/global_conf.json.MTCAP_LORA_1_5.US915
        else
            return 1
        fi

        cp $GLOBAL_CONF $opt_conf_file
        echo "Setting gateway_id in $opt_conf_file to $lora_eui_raw"
        sed -i "s/\"gateway_ID\": \".*\"/\"gateway_ID\": \"$lora_eui_raw\"/" $opt_conf_file

        #
        # copy global_json.conf and local_json.conf files to /var/config/ if it is not there yet
        #
        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
    else
        return 1
    fi

}

do_start() {
    # create run directory
    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"
    else
        echo "$0: LORA not detected"
        exit 1
    fi

    # copy conf files to the run directory
    if [ -f $conf_file ]; then
        cp $conf_file $run_dir/1/
    fi

    if [ -f $conf_file_local ]; then
        cp $conf_file_local $run_dir/1/
    fi

    # 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 > $log_file 2>&1"

    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