#!/bin/sh
. /lib/functions.sh

help() {
    cat <<_HELP_
CP210X device process.
Usage: $0 {add|remove} --slot SLOT
        --slot SLOT # the slot number

Example:
    $0 add -slot 1 
_HELP_
}

function detect_USB() {
    dev=$1
    slot=$2

    # sleep a while to wait the OS int model start up
    sleep 5

    # write dev:/dev/ttyUSB0 speed:9600 data:41542b5645520D0A buf[0]:41 buf[7]=0A
    # +Ai-thinker Lora module, version: Ra06 V2.1.1
    # +OK
    # AT+VER
    rm /dev/slot${slot}_*
    ret=$(uart_tool -d $dev -m hex --speed 9600 --stop 1 --parity 2 --bit 8 --pmode string --data 41542B5645520D0A)
    result=$(echo $ret | grep "Ai-thinker Lora module, version: Ra06")
    if [[ "$result" != "" ]]; then
        echo "find Ai-thinker Lora module, version: Ra06 V2.1.1"
        ln -s $dev /dev/slot${slot}_Ra06
        /etc/init.d/ra06_${slot} start
        return 1
    fi

    ret=$(uart_tool -d $dev -m hex --speed 57600 --stop 1 --parity 0 --bit 8 --pmode hex --data ABBCCDD1AA)
    result=$(echo $ret | grep "ABBCCDD1")
    if [[ "$result" != "" ]]; then
        echo "find ZLG Device"
        logger -t Hotplug find ZLG device
        ln -s $dev /dev/slot${slot}_ZLGzigbee
        /etc/init.d/fastzigbee_${slot} start
        return 1
    fi
}

case "$1" in
add) action="$1" ;;
remove) action="$1" ;;
*)
    help
    exit 1
    ;;
esac
shift 1

while [ -n "$1" ]; do
    case $1 in
    --slot)
        slot="$2"
        shift 2
        ;;
    -*)
        help
        exit 1
        ;;
    *) break ;;
    esac
done

[ -z "$slot" ] && help && exit 1

board=$(board_name)

case "$board" in
    woolink,mt7621-64m)
        case "$slot" in
        "1")
            dev_path="/sys/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.1/1-2.1:1.0"
            ;;
        "2")
            dev_path="/sys/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.2/1-2.2:1.0"
            ;;
        "3")
            dev_path="/sys/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.3/1-2.3:1.0"
            ;;
        esac
        ;;
    *)
        case "$slot" in
        "1")
            dev_path="/sys/devices/platform/101c0000.ehci/usb1/1-1/1-1.1/1-1.1:1.0"
            ;;
        "2")
            dev_path="/sys/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.0"
            ;;
        "3")
            dev_path="/sys/devices/platform/101c0000.ehci/usb1/1-1/1-1.3/1-1.3:1.0"
            ;;
        "4")
            dev_path="/sys/devices/platform/101c0000.ehci/usb1/1-1/1-1.4/1-1.4:1.0"
            ;;
        esac
        ;;
esac
[ -z "$slot" ] && echo "slot not right" && exit 1

case "$action" in
"add")
    echo "add $dev_path"
    DEVICE_NAME=$(ls $dev_path | grep tty)
    if [ -z ${DEVICE_NAME} ]; then
        echo "Warning DEVICE_NAME is empty"
        exit 1
    fi
    if [ -f "/dev/ttyMPCIE${slot}" ]; then
        rm /dev/ttyMPCIE${slot}
    fi
    ln -s /dev/$DEVICE_NAME /dev/ttyMPCIE${slot}
    logger -t Hotplug Symlink from /dev/$DEVICE_NAME to /dev/ttyMPCIE${slot} created
    detect_USB /dev/ttyMPCIE${slot} ${slot}
    ;;
"remove")
    echo "remove $dev_path"
    rm /dev/ttyMPCIE${slot}
    rm /dev/slot${slot}_*
    /etc/init.d/ra06_${slot} stop
    /etc/init.d/fastzigbee_${slot} stop
    logger -t Hotplug Symlink /dev/ttyMPCIE${slot} removed
    ;;
esac
