-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathservice.sh
More file actions
111 lines (94 loc) · 3.6 KB
/
service.sh
File metadata and controls
111 lines (94 loc) · 3.6 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh
# Developed by mmtrt (https://gist.github.com/mmtrt)
# Improoved by Danil Vyazikov (https://github.com/overzero-git/) and barsikus007 (https://gist.github.com/barsikus007/)
# Published under GPLv3
# run while loop for boot_completed status & sleep 10 needed for magisk service.d
while [ "$(getprop sys.boot_completed | tr -d '\r')" != "1" ]; do sleep 1; done
sleep 10
get_fn_type() {
# get currently active function name
if ls /config/usb_gadget/g1/configs/b.1/function* > /dev/null 2>&1
then
echo "function"
else
echo "f"
fi
}
fn_type=$(get_fn_type)
get_chkfn() {
# get currently active function name
ls -al /config/usb_gadget/g1/configs/b.1/ | grep -Eo "$fn_type[0-9]+[[:space:]].*" | awk '{print $3}' | cut -d/ -f8
}
get_last_fn() {
# get currently free function number
num=$(ls -al /config/usb_gadget/g1/configs/b.1/ | grep -Eo "$fn_type[0-9]+[[:space:]]" | tail -1 | cut -dn -f 3)
echo "$fn_type"$((num+1))
}
is_mass_storage_present() {
# returns 1 if mass_storage.0 is present
ls -al /config/usb_gadget/g1/configs/b.1/ | grep -Eo "mass_storage.0" | wc -l
}
get_mass_storage_path() {
# get path to mass_storage.0
ls -al /config/usb_gadget/g1/configs/b.1/ | grep -Eo "$fn_type[0-9]+[[:space:]].*mass_storage.0" | cut -d' ' -f1
}
#this options for some devices uses mass_storage.usb0 instead mass_storage.0 (some Samsung devices for example)
is_mass_storage_usb0_present() {
# returns 1 if mass_storage.usb0 is present
ls -al /config/usb_gadget/g1/configs/b.1/ | grep -Eo "mass_storage.usb0" | wc -l
}
get_mass_storage_usb0_path() {
# get path to mass_storage.usb0
ls -al /config/usb_gadget/g1/configs/b.1/ | grep -Eo "$fn_type[0-9]+[[:space:]].*mass_storage.usb0" | cut -d' ' -f1
}
# save currently active function name
if [ "$fn_type" = "f" ]; then
get_chkfn > /data/adb/.fixdd
fi
# loop
# run every 0.5 seconds
while true
do
# check the app is active
chkapp="$(pgrep -f drivedroid | wc -l)"
# check if mass_storage.0 is active function
mass_storage_active=$(is_mass_storage_present)
if [ "$chkapp" -eq "1" ] && [ "$mass_storage_active" -eq "0" ]; then
# add mass_storage.0 to currently active functions
if [ "$fn_type" = "f" ]; then
setprop sys.usb.config cdrom
setprop sys.usb.configfs 1
rm /config/usb_gadget/g1/configs/b.1/f*
fi
mkdir -p /config/usb_gadget/g1/functions/mass_storage.0/lun.0/
ln -s /config/usb_gadget/g1/functions/mass_storage.0 "/config/usb_gadget/g1/configs/b.1/$(get_last_fn)"
if [ "$fn_type" = "f" ]; then
getprop sys.usb.controller > /config/usb_gadget/g1/UDC
setprop sys.usb.state cdrom
fi
elif [ "$chkapp" -eq "0" ] && [ "$mass_storage_active" -eq "1" ]; then
# remove mass_storage.0 function
rm "/config/usb_gadget/g1/configs/b.1/$(get_mass_storage_path)"
# it seems, that pixel 7 doesn't use sys.usb.config at all
if [ "$fn_type" = "f" ]; then
# reload of configfs to fix samsung android auto
setprop sys.usb.configfs 0
sleep 0.5
setprop sys.usb.configfs 1
# load previous active function
chkfrstfn="$(cat /data/adb/.fixdd)"
ln -s /config/usb_gadget/g1/functions/"$chkfrstfn" /config/usb_gadget/g1/configs/b.1/f1
# Need more code cleanup & optimization here
ls /sys/class/udc/ | grep -Eo ".*\.dwc3" > /config/usb_gadget/g1/UDC
setprop sys.usb.state mtp
if [ "$chkfrstfn" = "ffs.adb" ]; then
setprop sys.usb.config adb
elif [ "$chkfrstfn" = "ffs.mtp" ]; then
setprop sys.usb.config mtp
elif [ "$chkfrstfn" = "mtp.gs0" ]; then
setprop sys.usb.config mtp
fi
fi
fi
sleep 0.5
done