Move enablemodem from ramips to new package adb-enablemodem and make it used also...
[openwrt/openwrt.git] / package / network / utils / adb-enablemodem / files / adb-enablemodem
1 #!/bin/sh /etc/rc.common
2
3 START=99
4
5 adb_exec() {
6 adb -s "$serial" shell "( $1 ) >/dev/null 2>&1"'; printf "\nEXIT_CODE: %i\n" $?' | head -c 64 | grep -qx 'EXIT_CODE: 0\r\?'
7 }
8
9 enablemodem_do() {
10 logger -t adb-enablemodem 'INFO: waiting for device'
11 adb wait-for-device
12 serial="$(adb get-serialno)"
13
14 vendor_id="$(adb -s "$serial" shell 'uci get product.usb.vid' | head -c 16 | tr -d '\r\n')"
15 product_id="$(adb -s "$serial" shell 'uci get product.usb.pid' | head -c 16 | tr -d '\r\n')"
16
17 case "$vendor_id:$product_id" in
18 "0x2357:0x000D") # TP-LINK LTE MODULE
19 case "$1" in
20 start)
21 if adb_exec '
22 chmod +x /WEBSERVER/www/cgi-bin/*
23 fds="$(ls /proc/$$/fd | grep -v "^[012]$")"
24 for fd in $fds; do
25 eval "exec $fd>&-"
26 done
27 start-stop-daemon -x httpd -S -- -h /WEBSERVER/www/
28 '; then
29 logger -t adb-enablemodem 'INFO: httpd on modem started'
30 else
31 logger -t adb-enablemodem 'ERROR: failed to start httpd on modem'
32 fi
33 option_newid='/sys/bus/usb-serial/drivers/option1/new_id'
34 if [ -e "$option_newid" ]; then
35 printf '%s %s' "$vendor_id" "$product_id" > "$option_newid"
36 fi
37 ;;
38 stop)
39 if adb_exec 'start-stop-daemon -x httpd -K'; then
40 logger -t adb-enablemodem 'INFO: httpd on modem stopped'
41 else
42 logger -t adb-enablemodem 'ERROR: failed to stop httpd on modem'
43 fi
44 ;;
45 esac
46 ;;
47 *)
48 logger -t adb-enablemodem "ERROR: unknown device $vendor_id:$product_id"
49 ;;
50 esac
51 }
52
53 start() {
54 ( enablemodem_do start ) &
55 }
56
57 stop() {
58 ( enablemodem_do stop ) &
59 }
60
61 restart() {
62 ( enablemodem_do stop; enablemodem_do start ) &
63 }
64