Skip to content

Commit 32e0fc7

Browse files
committed
test-case: add new test case for check mic privacy
Add new test case to check hardware mic privacy mode enablement feature. For switching mic privacy state is using a USB relay. https://github.com/darrylb123/usbrelay Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
1 parent 4a5a1d9 commit 32e0fc7

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

case-lib/relay.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
# Copyright(c) 2021-2025 Intel Corporation. All rights reserved.
5+
6+
# test-mic_privacy.sh needs to control mic privacy settings (on/off)
7+
# needs usbrelay package: https://github.com/darrylb123/usbrelay
8+
# param1: switch name
9+
# param2: switch state
10+
usbrelay_switch()
11+
{
12+
# Declare a constant for the relay settle time
13+
USBRELAY_SETTLE_TIME=0.5
14+
15+
local switch_name=$1
16+
local state=$2
17+
18+
# Check if usbrelay is installed
19+
command -v usbrelay || {
20+
# If usbrelay package is not installed
21+
skip_test "usbrelay command not found. Please install usbrelay package."
22+
}
23+
24+
dlogi "Setting usbrelay switch $switch_name to $state."
25+
usbrelay "$switch_name=$state" || {
26+
# if not detect relays hw module, skip the test
27+
die "Failed to set usbrelay switch $switch_name to $state.
28+
The usbrelay hw module is not responding or no relays detected.
29+
Check hardware connection."
30+
}
31+
32+
# wait for the switch to settle
33+
sleep "$USBRELAY_SETTLE_TIME"
34+
35+
# Display current state of the switch
36+
current_state=$(usbrelay | grep "$switch_name" | awk -F= '{print $2}')
37+
38+
# Check if current_state is equal to the requested state
39+
[[ "$current_state" == "$state" ]] || {
40+
die "usbrelay switch $switch_name failed to set to $state (current: $current_state)"
41+
}
42+
43+
if [[ "$current_state" == "1" ]]; then
44+
dlogi "Current state of $switch_name is: on"
45+
elif [[ "$current_state" == "0" ]]; then
46+
dlogi "Current state of $switch_name is: off"
47+
else
48+
die "Invalid state for $switch_name: $current_state"
49+
fi
50+
}

env-check.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ func_check_pkg aplay
8787
func_check_pkg sox
8888
func_check_pkg tinycap
8989
func_check_pkg tinyplay
90+
# MIC privacy / JACK Audio detection relay switch
91+
func_check_pkg usbrelay
9092
# JACK Audio Connection Kit
9193
func_check_pkg jackd
9294
func_check_pkg jack_iodelay

test-case/test-mic-privacy.sh

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#!/bin/bash
2+
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
# Copyright(c) 2025 Intel Corporation. All rights reserved.
5+
6+
##
7+
## Case Name: test-mic-privacy
8+
##
9+
## Preconditions:
10+
## HW managed mode (Only for DMIC PCH and SNDW interfaces).
11+
## This test case requires physical loopback between playback and capture.
12+
## playback <=====> capture
13+
## USB relay switch is connected. The usbrelay app is installed.
14+
## Instruction: https://github.com/darrylb123/usbrelay
15+
##
16+
## Description:
17+
## Run alsabat process perform both playback and capture.
18+
## Enable MIC privacy.
19+
## Run alsabat process perform both playback and capture again.
20+
##
21+
## Case step:
22+
## 1. Specify the pcm IDs for playback and capture
23+
## 2. Check if usbrelay is installed and connected properly.
24+
## 3. Run alsabat process perform both playback and capture.
25+
## 4. Switch relay 1 to enable MIC privacy.
26+
## 5. Run alsabat process perform both playback and capture.
27+
##
28+
## Expect result:
29+
## After step 3 the return value is 0.
30+
## After step 5 the return value is -1001 (no peak be detected).
31+
32+
# remove the existing alsabat wav files
33+
ALSABAT_WAV_FILES="/tmp/mc.wav.*"
34+
rm -f "$ALSABAT_WAV_FILES"
35+
36+
# shellcheck source=case-lib/lib.sh
37+
source "$(dirname "${BASH_SOURCE[0]}")"/../case-lib/lib.sh
38+
39+
OPT_NAME['p']='pcm_p' OPT_DESC['p']='pcm for playback. Example: hw:0,0'
40+
OPT_HAS_ARG['p']=1 OPT_VAL['p']='hw:0,0'
41+
42+
OPT_NAME['N']='channel_p' OPT_DESC['N']='channel number for playback.'
43+
OPT_HAS_ARG['N']=1 OPT_VAL['N']='2'
44+
45+
OPT_NAME['c']='pcm_c' OPT_DESC['c']='pcm for capture. Example: hw:0,1'
46+
OPT_HAS_ARG['c']=1 OPT_VAL['c']='hw:0,1'
47+
48+
OPT_NAME['C']='channel_c' OPT_DESC['C']='channel number for capture.'
49+
OPT_HAS_ARG['C']=1 OPT_VAL['C']='2'
50+
51+
OPT_NAME['s']='sof-logger' OPT_DESC['s']="Open sof-logger trace the data will store at $LOG_ROOT"
52+
OPT_HAS_ARG['s']=0 OPT_VAL['s']=1
53+
54+
OPT_NAME['r']='rate' OPT_DESC['r']='sample rate'
55+
OPT_HAS_ARG['r']=1 OPT_VAL['r']=48000
56+
57+
OPT_NAME['R']='relay' OPT_DESC['R']='name of usbrelay switch, default value is HURTM_1'
58+
OPT_HAS_ARG['R']=1 OPT_VAL['R']='HURTM_1'
59+
60+
func_opt_parse_option "$@"
61+
62+
pcm_p=${OPT_VAL['p']}
63+
pcm_c=${OPT_VAL['c']}
64+
channel_c=${OPT_VAL['C']}
65+
channel_p=${OPT_VAL['N']}
66+
rate=${OPT_VAL['r']}
67+
relay=${OPT_VAL['R']}
68+
69+
dlogi "Params: pcm_p=$pcm_p, pcm_c=$pcm_c, channel_c=$channel_c, channel_p=$channel_p, rate=$rate, LOG_ROOT=$LOG_ROOT"
70+
71+
__upload_wav_files()
72+
{
73+
# upload the alsabat wav file
74+
for file in $ALSABAT_WAV_FILES; do
75+
# alsabat has a bug where it creates an empty record in playback mode
76+
if test -s "$file"; then
77+
cp -v "$file" "$LOG_ROOT/"
78+
fi
79+
done
80+
}
81+
82+
check_playback_capture()
83+
{
84+
# check if capture and playback work
85+
dlogc "alsabat -P$pcm_p -C$pcm_c -c 2 -r $rate"
86+
alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" || {
87+
# upload failed wav file
88+
__upload_wav_files
89+
dle "alsabat failed"
90+
}
91+
}
92+
93+
main()
94+
{
95+
setup_kernel_check_point
96+
97+
start_test
98+
99+
logger_disabled || func_lib_start_log_collect
100+
101+
if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then
102+
skip_test "No playback or capture PCM is specified. Skip the alsabat test."
103+
fi
104+
105+
# check if usbrelay tool is installed
106+
command -v usbrelay || {
107+
skip_test "usbrelay command not found. Please install usbrelay to control the mic privacy switch."
108+
}
109+
110+
check_locale_for_alsabat
111+
112+
# reset sof volume to 0dB
113+
reset_sof_volume
114+
115+
# If MODEL is defined, set proper gain for the platform
116+
if [ -z "$MODEL" ]; then
117+
# treat as warning only
118+
dlogw "NO MODEL is defined. Please define MODEL to run alsa_settings/MODEL.sh"
119+
else
120+
dlogi "apply alsa settings for alsa_settings/MODEL.sh"
121+
set_alsa_settings "$MODEL"
122+
fi
123+
124+
dlogi "Turn off the mic privacy switch"
125+
usbrelay_switch "$relay" 0
126+
127+
# check the PCMs before mic privacy test
128+
dlogi "check the PCMs before mic privacy test"
129+
aplay -Dplug"$pcm_p" -d 1 /dev/zero -q || die "Failed to play on PCM: $pcm_p"
130+
arecord -Dplug"$pcm_c" -d 1 /dev/null -q || die "Failed to capture on PCM: $pcm_c"
131+
132+
# Select the first card
133+
first_card_name=$(aplay -l | awk '/^card ([0-9]+)/ {print $3; exit}')
134+
# dump amixer contents always.
135+
# Good case amixer settings is for reference, bad case for debugging.
136+
amixer -c "${first_card_name}" contents > "$LOG_ROOT"/amixer_settings.txt
137+
138+
check_playback_capture
139+
140+
dlogi "Turn on the mic privacy switch"
141+
usbrelay_switch "$relay" 1
142+
143+
alsabat_output=$(mktemp)
144+
dlogc "alsabat -P$pcm_p -C$pcm_c -c 2 -r $rate"
145+
alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" >"$alsabat_output" 2>&1
146+
alsabat_status=$?
147+
148+
if [ $alsabat_status -ne 0 ]; then
149+
if grep -q -e "Amplitude: 0.0; Percentage: \[0\]" -e "Return value is -1001" "$alsabat_output"
150+
then
151+
# Do nothing if signal is zero, this is expected
152+
# Return value is -1001
153+
dlogi "Alsabat output indicates zero signal as expected."
154+
:
155+
else
156+
dloge "alsabat failed with status $alsabat_status, but signal is not zero."
157+
__upload_wav_files
158+
dle "alsabat failed with: $(cat "$alsabat_output")."
159+
fi
160+
else
161+
dloge "alsabat passed unexpectedly, upload the wav files."
162+
__upload_wav_files
163+
dle "MIC privacy doesn't work. alsabat output: $(cat "$alsabat_output")"
164+
fi
165+
166+
dlogi "Turn off the mic privacy switch."
167+
usbrelay_switch "$relay" 0
168+
169+
check_playback_capture
170+
171+
rm -rf "$alsabat_output"
172+
}
173+
174+
{
175+
main "$@"; exit "$?"
176+
}

0 commit comments

Comments
 (0)