Skip to content

Commit 8977123

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 5124961 commit 8977123

File tree

4 files changed

+267
-1
lines changed

4 files changed

+267
-1
lines changed

case-lib/lib.sh

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ extract_format_number() {
791791
else
792792
die "Error: Unknown format: %s\n"
793793
fi
794-
}
794+
}
795795

796796
# Initialize the parameters using for audio testing.
797797
# shellcheck disable=SC2034
@@ -1272,3 +1272,38 @@ perf_analyze()
12721272
fi
12731273
}
12741274

1275+
# test-mic_privacy.sh needs to control mic privacy settings (on/off)
1276+
# test-jack-detection* need to control unplug / plug jack connector
1277+
# needs usbrelay package: https://github.com/darrylb123/usbrelay
1278+
# param1: switch name
1279+
# param2: switch state
1280+
usbrelay_switch()
1281+
{
1282+
local switch_name=$1
1283+
local state=$2
1284+
1285+
dlogi "Setting usbrelay switch $switch_name to $state."
1286+
usbrelay "$switch_name=$state" > /dev/null || {
1287+
# If usbrelay is not installed or no relays detected, skip the test
1288+
die "Failed to set usbrelay switch $switch_name to $state."
1289+
}
1290+
1291+
# wait for the switch to settle
1292+
sleep 0.5
1293+
1294+
# Display current state of the switch
1295+
current_state=$(usbrelay | grep "$switch_name" | awk -F= '{print $2}')
1296+
1297+
# Check if current_state is equal to the requested state
1298+
[[ "$current_state" == "$state" ]] || {
1299+
die "Usbrelay switch $switch_name failed to set to $state. Current state is $current_state."
1300+
}
1301+
1302+
if [[ "$current_state" == "1" ]]; then
1303+
dlogi "Current state of $switch_name is: on"
1304+
elif [[ "$current_state" == "0" ]]; then
1305+
dlogi "Current state of $switch_name is: off"
1306+
else
1307+
die "Invalid state for $switch_name: $current_state"
1308+
fi
1309+
}

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: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
33+
TESTLIB="${TESTDIR}/case-lib"
34+
35+
# remove the existing alsabat wav files
36+
ALSABAT_WAV_FILES="/tmp/mc.wav.*"
37+
rm -f "$ALSABAT_WAV_FILES"
38+
39+
# shellcheck source=case-lib/lib.sh
40+
source "${TESTLIB}/lib.sh"
41+
source "${TESTLIB}/relay.sh"
42+
43+
OPT_NAME['p']='pcm_p' OPT_DESC['p']='pcm for playback. Example: hw:0,0'
44+
OPT_HAS_ARG['p']=1 OPT_VAL['p']='hw:0,0'
45+
46+
OPT_NAME['N']='channel_p' OPT_DESC['N']='channel number for playback.'
47+
OPT_HAS_ARG['N']=1 OPT_VAL['N']='2'
48+
49+
OPT_NAME['c']='pcm_c' OPT_DESC['c']='pcm for capture. Example: hw:0,1'
50+
OPT_HAS_ARG['c']=1 OPT_VAL['c']='hw:0,1'
51+
52+
OPT_NAME['C']='channel_c' OPT_DESC['C']='channel number for capture.'
53+
OPT_HAS_ARG['C']=1 OPT_VAL['C']='2'
54+
55+
OPT_NAME['s']='sof-logger' OPT_DESC['s']="Open sof-logger trace the data will store at $LOG_ROOT"
56+
OPT_HAS_ARG['s']=0 OPT_VAL['s']=1
57+
58+
OPT_NAME['r']='rate' OPT_DESC['r']='sample rate'
59+
OPT_HAS_ARG['r']=1 OPT_VAL['r']=48000
60+
61+
OPT_NAME['u']='relay' OPT_DESC['u']='name of usbrelay switch, default value is HURTM_1'
62+
OPT_HAS_ARG['u']=1 OPT_VAL['u']='HURTM_1'
63+
64+
func_opt_parse_option "$@"
65+
66+
pcm_p=${OPT_VAL['p']}
67+
pcm_c=${OPT_VAL['c']}
68+
channel_c=${OPT_VAL['C']}
69+
channel_p=${OPT_VAL['N']}
70+
rate=${OPT_VAL['r']}
71+
relay=${OPT_VAL['u']}
72+
73+
dlogi "Params: pcm_p=$pcm_p, pcm_c=$pcm_c, channel_c=$channel_c, channel_p=$channel_p, rate=$rate, LOG_ROOT=$LOG_ROOT"
74+
75+
__upload_wav_files()
76+
{
77+
# upload the alsabat wav file
78+
for file in $ALSABAT_WAV_FILES; do
79+
# alsabat has a bug where it creates an empty record in playback mode
80+
if test -s "$file"; then
81+
cp -v "$file" "$LOG_ROOT/"
82+
fi
83+
done
84+
}
85+
86+
check_playback_capture()
87+
{
88+
# check if capture and playback work
89+
dlogc "alsabat -P$pcm_p -C$pcm_c -c 2 -r $rate"
90+
alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" || {
91+
# upload failed wav file
92+
__upload_wav_files
93+
die "alsabat failed"
94+
}
95+
}
96+
97+
main()
98+
{
99+
setup_kernel_check_point
100+
101+
start_test
102+
103+
logger_disabled || func_lib_start_log_collect
104+
105+
if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then
106+
skip_test "No playback or capture PCM is specified. Skip the $0 test."
107+
fi
108+
109+
# check if usbrelay tool is installed
110+
command -v usbrelay || {
111+
skip_test "usbrelay command not found. Please install usbrelay to control the mic privacy switch."
112+
}
113+
114+
check_locale_for_alsabat
115+
116+
# reset sof volume to 0dB
117+
reset_sof_volume
118+
119+
# If MODEL is defined, set proper gain for the platform
120+
if [ -z "$MODEL" ]; then
121+
# treat as warning only
122+
dlogw "NO MODEL is defined. Please define MODEL to run alsa_settings/MODEL.sh"
123+
else
124+
dlogi "apply alsa settings for alsa_settings/MODEL.sh"
125+
set_alsa_settings "$MODEL"
126+
fi
127+
128+
dlogi "Turn off the mic privacy switch"
129+
usbrelay_switch "$relay" 0
130+
131+
# check the PCMs before mic privacy test
132+
dlogi "check the PCMs before mic privacy test"
133+
aplay -Dplug"$pcm_p" -d 1 /dev/zero -q || die "Failed to play on PCM: $pcm_p"
134+
arecord -Dplug"$pcm_c" -d 1 /dev/null -q || die "Failed to capture on PCM: $pcm_c"
135+
136+
# Select the first card
137+
first_card_name=$(aplay -l | awk '/^card ([0-9]+)/ {print $3; exit}')
138+
# dump amixer contents always.
139+
# Good case amixer settings is for reference, bad case for debugging.
140+
amixer -c "${first_card_name}" contents > "$LOG_ROOT"/amixer_settings.txt
141+
142+
check_playback_capture
143+
144+
dlogi "Turn on the mic privacy switch"
145+
usbrelay_switch "$relay" 1
146+
147+
alsabat_output=$(mktemp)
148+
dlogc "alsabat -P$pcm_p -C$pcm_c -c 2 -r $rate"
149+
alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" > "$alsabat_output" 2>&1
150+
alsabat_status=$?
151+
152+
if [ $alsabat_status -ne 0 ]; then
153+
if grep -q -e "Amplitude: 0.0; Percentage: \[0\]" -e "Return value is -1001" "$alsabat_output"
154+
then
155+
# Do nothing if signal is zero, this is expected
156+
# Return value is -1001
157+
dlogi "Alsabat output indicates zero signal as expected."
158+
else
159+
dloge "Alsabat failed as expected but signal is not zero."
160+
__upload_wav_files
161+
die "Alsabat failed with: $(cat "$alsabat_output")."
162+
fi
163+
else
164+
dloge "Alsabat passed unexpectedly, upload the wav files."
165+
__upload_wav_files
166+
die "MIC privacy doesn't work. Alsabat output: $(cat "$alsabat_output")"
167+
fi
168+
169+
dlogi "Turn off the mic privacy switch."
170+
usbrelay_switch "$relay" 0
171+
172+
check_playback_capture
173+
174+
rm -rf "$alsabat_output"
175+
}
176+
177+
{
178+
main "$@"; exit "$?"
179+
}

0 commit comments

Comments
 (0)