#!/bin/sh
#

# PROVIDE: sigcell
# REQUIRE: LOGIN
# BEFORE:  securelevel signetik
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable `sigcell':
#
# sigcell_enable="YES"
# sigcell_flags="<set as needed>"
#

. /etc/rc.subr

name="sigcell"
rcvar=sigcell_enable

#command="/usr/sbin/daemon"
start_cmd="sigcell_start"
start_precmd="sigcell_prestart"
stop_cmd="sigcell_stop"
pidfile="/var/run/$name.pid"

# read configuration and set defaults
load_rc_config "$name"
: ${sigcell_enable="NO"}

#command_args="--daemon --config ${rsyncd_configfile}"

sigcell_prestart()
{
	#/root/test/cell.sh
	#/usr/sbin/daemon -r /usr/sbin/lora_pkt_fwd
}

sigcell_start()
{
    # GPIO 0_16 EN_CELL_3V3
    memtool -a0x44e1091c -w0x7
    memtool -a0x44e07134 -w0xfffeffff
    memtool -a0x44e0713c -w0x00000000
    sleep 2
    memtool -a0x44e0713c -w0x00010000
    
    # GPIO 3_21 SigCell Enable
    memtool -a0x44e109ac -w0x7
    memtool -a0x481ae134 -w0xffdfffff
    memtool -a0x481ae13c -w0x00000000
    sleep 2
    memtool -a0x481ae13c -w0x00200000

    /usr/sbin/daemon -S -r -P $pidfile /usr/sbin/cell_service
}

sigcell_stop()
{
    kill `cat $pidfile`

    # GPIO 0_16 EN_CELL_3V3
    memtool -a0x44e1091c -w0x7
    memtool -a0x44e07134 -w0xfffeffff
    memtool -a0x44e0713c -w0x00000000
    
    # GPIO 3_21 SigCell Disable
    memtool -a0x44e109ac -w0x7
    memtool -a0x481ae134 -w0xffdfffff
    memtool -a0x481ae13c -w0x00000000
}

run_rc_command "$1"
