#!/bin/sh
# Description: android debug bridage daemon

# Process name ( For display )
NAME=adbd
# Daemon name, where is the actual executable
DAEMON=/usr/bin/adbd
# pid file for the daemon
PIDFILE=/var/run/adbd.lock
# usb config file system
USBCFS=/sys/kernel/config

# If the daemon is not there, then exit.
test -x $DAEMON || exit 5

is_usb_gadget() {
    # 0: true
    # 1: false
    # usb_mode=$(cat /sys/devices/platform/soc/39820000.usb/39820000.dwc3/role)
    # if [ -z $usb_mode ]; then
    #     return 1
    # fi

    # if [ $usb_mode == "device" ]; then
    #     return 0
    # fi

    # return 1
    return 0
}

adbfs_cleanup() {
    if [ -d $USBCFS/usb_gadget/g1 ]; then
        echo ">>>>>>>>>>>>> g1 is exist. delete it!"
        cd $USBCFS/usb_gadget/g1

        # remove function, configs & strings
        rm configs/c.1/ffs.adb
        rmdir configs/c.1/strings/0x409
        rmdir configs/c.1
        rmdir functions/ffs.adb
        rmdir strings/0x409
        cd ..
        rmdir g1
    else
        echo "adb configfs already cleanup"
    fi
}

adbd_start() {
    #https://wiki.linaro.org/LMG/Kernel/AndroidConfigFSGadgets#ADB_gadget_configuration
    #prepare gadgetfs environment

    # modprobe g_ether
    # rmmod g_ether
    modprobe dwc3
    modprobe dwc3-of-simple
    modprobe usb_f_fs
    modprobe configfs
    if cat /sys/class/boardinfo/board_name | grep -q EVM; then
        modprobe hb_usb
    fi

    # set ultrasoc bypass
    soc_name="$(cat /sys/class/boardinfo/soc_name)"
    if echo "$soc_name" | grep -qE 'S100';then
        devmem 0x39400030 32 0
    else
        devmem 0x39030040 32 0
    fi
    sleep 0.5

    #adb configfs cleanup
    adbfs_cleanup

    #Mount ConfigFS and create Gadget
    #mount -t configfs none /sys/kernel/config
    mkdir -p $USBCFS/usb_gadget/g1
    cd $USBCFS/usb_gadget/g1

    #Set default Vendor and Product IDs for now
    echo 0x18d1 > idVendor
    echo 0x4e26 > idProduct

    #Create English strings and add random deviceID
    mkdir strings/0x409
    soc_uid="$(cat /sys/class/boardinfo/soc_uid)"
    if echo "$soc_uid" | grep -qE '^0+$';then
        serial="$(cat /proc/cmdline  | grep -oP '(?<=hobotboot.serial=)\S+')"
    else
        serial="$(cat /sys/class/boardinfo/soc_uid | cut -c1-16)"
    fi

    # if [ -z $serial ]; then
    #     serial=$(hrut_socuid)       # use hrut_socuid as serial number

    #     if [ -n "$serial" ]; then
    #         serial=${serial:0-30}       # use socuid last 64 bit data as in dafault
    #     fi

    #     if [ -z $serial ]; then
    #         serial=$(cat /sys/devices/platform/soc/a5010000.dwmmc/mmc_host/mmc0/mmc0:0001/serial)

    #         if [ -z $serial ]; then
    #             serial=0123456789ABCD   # Use fixed serial number for other cases(eg. Nand Boot without emmc)
    #         fi
    #     fi
    # fi

    if [ -z $serial ]; then
        serial="Horizon Robotics Adb"   # Use default serial number for other cases(eg. Nand Boot without emmc)
    fi

    echo $serial> strings/0x409/serialnumber

    #Update following if you want to
    echo "Horizon Robotics" > strings/0x409/manufacturer
    echo "$soc_name" > strings/0x409/product

    #Create gadget configuration
    mkdir configs/c.1
    mkdir configs/c.1/strings/0x409
    echo "Conf 1" > configs/c.1/strings/0x409/configuration
    echo 120 > configs/c.1/MaxPower

    #Create Adb FunctionFS function
    #And link it to the gadget configuration
    #stop adbd
    mkdir -p functions/ffs.adb
    ln -s $USBCFS/usb_gadget/g1/functions/ffs.adb $USBCFS/usb_gadget/g1/configs/c.1/ffs.adb

    #Start adbd application
    mkdir -p /dev/usb-ffs/adb
    mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb
    adbd &

    echo "waiting"
    for i in `seq 1 3`
    do
        echo "."
        sleep 0.3
    done

    #Enable the Gadget
    #Replace "39820000.usb" with your platform UDC driver as found in /sys/class/udc/
    if  is_usb_gadget; then
        ls /sys/bus/platform/devices |grep dwc3 |head -n 1 > $USBCFS/usb_gadget/g1/UDC
        echo "device mode adb gadget enabled"
    fi
}

adbd_stop() {
    #stop adb daemon
    killall -9 $NAME

    sleep 0.5

    #adb configfs cleanup
    adbfs_cleanup
}

case $1 in
    start)
    # Checked the PID file exists and check the actual status of process
    if [ -e $PIDFILE ]; then
        echo "$DAEMON is already running"
        exit 0
    fi

    # config & start adb daemon
    adbd_start
    pid=$(pidof $DAEMON)
    echo $pid > $PIDFILE
    ;;
    stop)
    # Stop the daemon.
    if [ -e $PIDFILE ]; then
        # cleanup adb configfs/functionfs
        adbd_stop
        rm -rf $PIDFILE
    else
        echo "$DAEMON is not runnning"
    fi
    ;;
    restart)
    # Restart the daemon.
    $0 stop && sleep 2 && $0 start
    ;;
    status)
    # Check the status of the process.
    if [ -e $PIDFILE ]; then
        echo "$DAEMON is running"
    else
        echo "$DAEMON is not runnning"
    fi
    ;;
    # adbd_hotplug)
    # # host switch to device , enable the Gadget
    # sleep 1
    # echo "39820000.dwc3" > $USBCFS/usb_gadget/g1/UDC
    # echo "adb gadget hotplug"
    # ;;
    #reload)
    # Reload the process. Basically sending some signal to a daemon to reload
    # it configurations.
    #;;
    *)
    # For invalid arguments, print the usage message.
    echo "Usage: $0 {start|stop|restart|status}"
    exit 2
    ;;
esac
