-
Notifications
You must be signed in to change notification settings - Fork 325
/
termux-usb.in
69 lines (63 loc) · 1.95 KB
/
termux-usb.in
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
#!@TERMUX_PREFIX@/bin/bash
set -e -u
SCRIPTNAME=termux-usb
show_usage () {
echo "Usage: $SCRIPTNAME [-l | [-r] [-E] [-e command] [device | vendorId productId]]"
echo "List or access USB devices. Devices cannot be accessed directly,"
echo " only using $SCRIPTNAME."
echo " -l list available devices"
echo " -r show permission request dialog if necessary"
echo " -e command execute the specified command with a file descriptor"
echo " referring to the device as an argument (unless -E"
echo " argument is given)"
echo " -E transfer file descriptor as env var instead of as"
echo " command line argument"
exit 0
}
ACTION="permission"
PARAMS=""
while getopts :hlre:E option
do
case "$option" in
h) show_usage;;
l) ACTION="list";;
r) PARAMS="$PARAMS --ez request true";;
e) ACTION="open"; export TERMUX_CALLBACK="$OPTARG";;
E) export TERMUX_EXPORT_FD=true;;
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
esac
done
shift $((OPTIND-1))
if [ "$ACTION" == "list" ]
then
if [ $# -gt 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
else
if [ $# -lt 1 ]; then
echo "$SCRIPTNAME: missing -l or device path"
exit 1
elif [ $# -eq 1 ]; then
# The device's usbfs path has been provided
PARAMS="$PARAMS --es device $1"
elif [ $# -eq 2 ]; then
# A vendorId and ProductId of the device has been provided
PARAMS="$PARAMS --es vendorId $1"
PARAMS="$PARAMS --es productId $2"
else
echo "$SCRIPTNAME: too many arguments"
exit 1
fi
fi
CMD="@TERMUX_PREFIX@/libexec/termux-api Usb -a $ACTION $PARAMS"
if [ "$ACTION" == "permission" ]
then
if [ "$($CMD)" == "yes" ]
then
echo "Access granted."
exit 0
else
echo "Access denied."
exit 1
fi
else
$CMD
fi