-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluealsa-handsfree-control.bash.in
executable file
·336 lines (311 loc) · 7.66 KB
/
bluealsa-handsfree-control.bash.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
# bluealsa simple handsfree version @version@
# HFP/HSP controls
# Default ALSA microphone
# override by defining in environment
BA_HF_MIC_MIXER="${BA_HF_MIC_MIXER:-default}"
BA_HF_MIC_CONTROL="${BA_HF_MIC_CONTROL:-Capture}"
BA_HF_MIC_CONTROL_INDEX="${BA_HF_MIC_CONTROL_INDEX:-0}"
BA_HF_SPEAKER="${BA_HF_SPEAKER:-default}"
BA_HF_RINGTONE="${BA_HF_RINGTONE:-@datadir@/bluealsa-simple-handsfree/ring.wav}"
declare -A main=( [fifo]= )
declare -A monitor=( [pid]= )
declare -A rfcomm=( [path]= [pid]= [fifo]= )
declare -A input=( [path]= [pid]= [fifo]= )
declare -A device=( [path]= [callsetup]= [call]= )
declare -A sco=( [path]= [profile]= [vol]= [vcount]=0 [running]= )
declare -A a2dp=( [path]= [vcount]=0 [running]= )
declare -A indicator
declare -a -r a2dp_volume_scale=(
0 8 16 25 33 42 50 59
67 76 84 93 101 110 118 127
)
error=
input[path]="$1"
[[ "${input[path]}" ]] || error="Usage: $0 FIFO"
[[ "$error" || -p "${input[path]}" ]] || error="'${input[path]}' is not a FIFO"
[[ "$error" ]] || exec {input[fifo]}<>"${input[path]}" || error="Cannot open FIFO '${input[path]}'"
if [[ "$error" ]] ; then
echo "$error" >&2
exit 1
fi
create_fifo() {
declare -n descriptor="$1"
local path="$(mktemp -u)"
if mkfifo "$path" ; then
exec {descriptor}<>"$path"
rm "$path"
else
echo "Unable to create FIFO" >&2
return 1
fi
}
start_monitor() {
bluealsa-cli --quiet --verbose monitor --properties=Running,Volume >&${main[fifo]} &
monitor[pid]=$!
}
stop_monitor() {
if [[ "${monitor[pid]}" ]] ; then
kill ${monitor[pid]}
monitor[pid]=
fi
}
start_rfcomm() {
create_fifo rfcomm[fifo] || return 1
bluealsa-rfcomm "${rfcomm[path]}" <&${rfcomm[fifo]} >&${main[fifo]} &
rfcomm[pid]=$!
}
stop_rfcomm() {
[[ "${rfcomm[pid]}" ]] && kill ${rfcomm[pid]} 2>/dev/null
[[ "${rfcomm[fifo]}" ]] && exec {rfcomm[fifo]}>&-
rfcomm[pid]=
rfcomm[fifo]=
}
command_filter() {
local REPLY
while read -r -u "${input[fifo]}"; do
case "$REPLY" in
ACCEPT|CANCEL|VOLUP|VOLDOWN|MUTE)
printf "%s\n" "$REPLY" >&${main[fifo]}
;;
esac
done
}
start_input() {
dd iflag=nonblock if="${input[path]}" of=/dev/null 2>/dev/null
command_filter &
input[pid]=$!
}
stop_input() {
if [[ "${input[pid]}" ]] ; then
kill ${input[pid]}
input[pid]=
fi
}
reset() {
stop_rfcomm
rfcomm=()
sco=()
a2dp=()
device=()
}
alert_user() {
aplay -q -D "$BA_HF_SPEAKER" "$BA_HF_RINGTONE"
}
a2dp_vol_inc() {
local n newvol="${a2dp_volume_scale[15]}"
for n in {1..14}; do
if [[ "${a2dp[vol]}" -lt "${a2dp_volume_scale[$n]}" ]] ; then
newvol="${a2dp_volume_scale[$n]}"
break
fi
done
a2dp[vol]="$newvol"
bluealsa-cli volume "${a2dp[path]}" "$newvol"
}
a2dp_vol_dec() {
local n newvol=0
for n in {14..1}; do
if [[ "${a2dp[vol]}" -gt "${a2dp_volume_scale[$n]}" ]] ; then
newvol="${a2dp_volume_scale[$n]}"
break
fi
done
a2dp[vol]="$newvol"
bluealsa-cli volume "${a2dp[path]}" "$newvol"
(( a2dp[vcount]++ ))
}
sco_vol_inc() {
if [[ "${sco[vol]}" -lt 15 ]] ; then
(( sco[vol]++ ))
bluealsa-cli volume "${sco[path]}" ${sco[vol]}
(( sco[vcount]++ ))
fi
}
sco_vol_dec() {
if [[ "${sco[vol]}" -gt 0 ]] ; then
(( sco[vol]-- ))
bluealsa-cli volume "${sco[path]}" ${sco[vol]}
(( sco[vcount]++ ))
fi
}
cleanup() {
stop_input
reset
stop_monitor
exec {main[fifo]}>&-
exec {input[fifo]}>&-
}
set_volume() {
if [[ "$1" == */hfphf/* || "$1" == */hsphs/* ]] ; then
if [[ "${sco[vcount]}" == 0 ]] ; then
sco[vol]="$(( ($2 >> 8) & 0x7F ))"
else
(( sco[vcount]-- ))
fi
elif [[ "$1" == */a2dpsnk/* ]] ; then
if [[ "${a2dp[vcount]}" == 0 ]] ; then
a2dp[vol]="$(( ($2 >> 8) & 0x7F ))"
else
(( a2dp[vcount]-- ))
fi
fi
}
check_path() {
[[ -z "${device[path]}" || "$1" == "${device[path]}"* ]]
}
set_property() {
declare -n pcm_ref="$1"
pcm_ref[$2]="$3"
}
create_fifo main[fifo] || exit 1
start_monitor
start_input "$1"
trap 'trap "" EXIT INT TERM; cleanup 2>/dev/null; exit' EXIT INT TERM
while read -r -u ${main[fifo]}; do
case "$REPLY" in
PCMAdded*)
path="${REPLY#PCMAdded }"
[[ "$path" == */source ]] || continue
check_path "$path" || reset
if [[ "$path" == */hfphf/* ]] ; then
[[ "${device[path]}" ]] || device[path]="${path%/hfphf/*}"
sco[path]="$path"
sco[profile]="hfp"
pcm=sco
elif [[ "$path" == */hsphs/* ]] ; then
[[ "${device[path]}" ]] || device[path]="${path%/hsphs/*}"
sco[path]="$path"
sco[profile]="hsp"
pcm=sco
elif [[ "$path" == */a2dpsnk/* ]] ; then
[[ "${device[path]}" ]] || device[path]="${path%/a2dpsnk/*}"
a2dp[path]="$path"
pcm=a2dp
else
continue
fi
properties=1
;;
PCMRemoved*)
path="${REPLY#PCMRemoved }"
[[ "$path" == */source ]] || continue
if [[ "$path" == "${sco[path]}" ]] ; then
sco=()
elif [[ "$path" == "${a2dp[path]}" ]] ; then
a2dp=()
fi
[[ "${rfcomm[path]}${sco[path]}${a2dp[path]}" ]] || reset
;;
RFCOMMAdded*)
path="${REPLY#RFCOMMAdded }"
check_path "$path" || reset
[[ "${device[path]}" ]] || device[path]="${path%/rfcomm}"
rfcomm[path]="$path"
start_rfcomm
printf "AT+CIND=?\n" >&${rfcomm[fifo]}
;;
RFCOMMRemoved*)
stop_rfcomm
rfcomm=()
indicator=()
[[ "${sco[path]}${a2dp[path]}" ]] || reset
;;
PropertyChanged*)
reply=( $REPLY )
path="${reply[1]}"
property_name="${reply[2]}"
property_value="${reply[3]}"
case "$property_name" in
Volume)
[[ "$path" == */source ]] || continue
set_volume "$path" "${property_value}"
;;
Running)
[[ "$path" == */hfphf/source || "$path" == */hsphs/source ]] || continue
sco[running]=
[[ "$property_value" == "true" ]] && sco[running]=1
[[ "${sco[profile]}" == hsp ]] && device[callsetup]=
;;
esac
;;
ServiceStopped*)
reset
;;
"Running: "*)
[[ "$properties" ]] || continue
[[ "${REPLY#*: }" == "true" ]] && set_property "$pcm" running 1
;;
"Volume: "*)
[[ "$properties" ]] || continue
vol="${REPLY#*: }"
if [[ "$vol" == "L: "* ]] ; then
vol="${vol% R:*}"
vol="${vol#L: }"
fi
set_property "$pcm" vol "$vol"
;;
"")
properties= pcm=
;;
":RING")
[[ "${sco[profile]}" == hsp ]] && device[callsetup]=1
[[ "${device[callsetup]}" ]] || continue
[[ "${sco[running]}" ]] || alert_user
;;
":OK")
[[ "${sco[profile]}" == "hsp" ]] && device[callsetup]=
;;
"+CIEV:"*)
[[ "${sco[profile]}" == "hfp" ]] || continue
temp="${REPLY#+CIEV:}"
temp="${temp# }"
ind=${temp%,*}
val=${temp#*,}
if [[ "$ind" == "${indicator["callsetup"]}" ]] ; then
device[callsetup]=
[[ "$val" == 1 ]] && device[callsetup]=1
elif [[ "$ind" == "${indicator["call"]}" ]] ; then
device[call]=
[[ "$val" == 1 ]] && device[call]=1
fi
;;
"+CIND:"*)
len=0 i=1
temp="${REPLY#+CIND:}"
temp="${REPLY#*\"}"
indicator=()
until [[ ${#temp} -eq $len ]] ; do
len=${#temp}
indicator[${temp%%\"*}]="$((i++))"
temp="${temp#*,(\"}"
done
;;
"ACCEPT")
if [[ "${sco[profile]}" == "hfp" ]] ; then
[[ "${device[callsetup]}" ]] || continue
message="ATA"
else
message="AT+CKPD=200"
fi
[[ "${rfcomm[fifo]}" ]] && printf "%s\n" "$message" >&${rfcomm[fifo]}
;;
"CANCEL")
message=
[[ "${device[callsetup]}" || "${device[call]}" ]] && message="AT+CHUP"
[[ "${sco[profile]}" == "hsp" ]] && message="AT+CKPD=200"
[[ "${rfcomm[fifo]}" && "$message" ]] && printf "%s\n" "$message" >&${rfcomm[fifo]}
;;
"VOLUP")
[[ "${a2dp[path]}" ]] && a2dp_vol_inc
[[ "${sco[path]}" ]] && sco_vol_inc
;;
"VOLDOWN")
[[ "${a2dp[path]}" ]] && a2dp_vol_dec
[[ "${sco[path]}" ]] && sco_vol_dec
;;
"MUTE")
amixer -q -D "$BA_HF_MIC_MIXER" sset "'${BA_HF_MIC_CONTROL}',${BA_HF_MIC_CONTROL_INDEX}" capture toggle
;;
esac
done