Skip to content

Conversation

@arikgreen
Copy link
Contributor

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

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds a new test case to verify the mic privacy mode by toggling a USB relay and checking audio behavior using alsabat.

  • Adds the new test script test-case/test-mic-privacy.sh.
  • Implements test steps including checking for usbrelay installation, running alsabat tests before and after toggling the mic privacy switch.
  • Logs relevant parameters and captures output for troubleshooting.

Comment on lines 147 to 162
if [ $alsabat_status -ne 0 ]; then
if grep -q -e "Amplitude: 0.0; Percentage: \[0\]" -e "Return value is -1001" "$alsabat_output"
then
# Do nothing if signal is zero, this is expected
# Return value is -1001
dlogi "Alsabat output indicates zero signal as expected."
:
else
dloge "alsabat failed with status $alsabat_status, but signal is not zero."
__upload_wav_file
dloge "alsabat output: $(cat "$alsabat_output")."
exit 1
fi
else
dloge "alsabat passed, upload the wav files."
dloge "alsabat output: $(cat "$alsabat_output")"
exit 1
fi
Copy link

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the alsabat result check and its error handling logic into a separate function to reduce complexity in the main test flow and improve maintainability.

Suggested change
if [ $alsabat_status -ne 0 ]; then
if grep -q -e "Amplitude: 0.0; Percentage: \[0\]" -e "Return value is -1001" "$alsabat_output"
then
# Do nothing if signal is zero, this is expected
# Return value is -1001
dlogi "Alsabat output indicates zero signal as expected."
:
else
dloge "alsabat failed with status $alsabat_status, but signal is not zero."
__upload_wav_file
dloge "alsabat output: $(cat "$alsabat_output")."
exit 1
fi
else
dloge "alsabat passed, upload the wav files."
dloge "alsabat output: $(cat "$alsabat_output")"
exit 1
fi
handle_alsabat_result "$alsabat_status" "$alsabat_output"
handle_alsabat_result "$alsabat_status" "$alsabat_output"

Copilot uses AI. Check for mistakes.
Comment on lines 66 to 71
exit 2
fi

# check if usbrelay tool is installed
if ! command -v usbrelay >/dev/null 2>&1; then
dloge "usbrelay command not found. Please install usbrelay to control the mic privacy switch."
exit 2
Copy link

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using hard-coded exit codes (such as exit 2) may reduce clarity; consider defining constants or adding inline comments to explain the meaning of these values.

Suggested change
exit 2
fi
# check if usbrelay tool is installed
if ! command -v usbrelay >/dev/null 2>&1; then
dloge "usbrelay command not found. Please install usbrelay to control the mic privacy switch."
exit 2
exit $EXIT_CODE_MISSING_PCM # Exit due to missing playback or capture PCM
fi
# check if usbrelay tool is installed
if ! command -v usbrelay >/dev/null 2>&1; then
dloge "usbrelay command not found. Please install usbrelay to control the mic privacy switch."
exit $EXIT_CODE_MISSING_PCM # Exit due to missing usbrelay command

Copilot uses AI. Check for mistakes.
@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch from 9675aae to dba4f4a Compare June 17, 2025 07:10
Comment on lines +15 to +19
## Enable MIC privacy.
## Run alsabat process perform both playback and capture again.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test should do two more steps:

  • Disable MIC privacy
  • playback/capture to check the MIC privacy mode is disabled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is this check before runs test

fi

dlogi "Turn off the mic privacy switch"
usbrelay HURTM_1=0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is a check possible on success for relay switch on/off ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is. implemented in the function usbrelay_switch in lib.sh

alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r $rate || {
# upload failed wav file
__upload_wav_file
exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
exit 1
skip_test "No audio loopback connected."

@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch from dba4f4a to fed81ff Compare June 25, 2025 11:17

if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then
dloge "No playback or capture PCM is specified. Skip the alsabat test."
exit 2
Copy link
Collaborator

@marc-hb marc-hb Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a skip_test 'reason' function, please use it (AI was right!)

done
}

function check_playback_capture
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function check_playback_capture
check_playback_capture

Unnecessary bashism.

case-lib/lib.sh Outdated
# needs usbrelay package: https://github.com/darrylb123/usbrelay
# param1: switch name
# param2: switch state
usbrelay_switch() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency nit

Suggested change
usbrelay_switch() {
usbrelay_switch()
{

case-lib/lib.sh Outdated
# param2: switch state
usbrelay_switch() {
switch_name=$1
state=$2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
state=$2
local state=$2

case-lib/lib.sh Outdated
switch_name=$1
state=$2
dlogi "Setting usbrelay switch $switch_name to $state."
if ! usbrelay "$switch_name=$state" >/dev/null 2>&1; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can easily avoid negations

Suggested change
if ! usbrelay "$switch_name=$state" >/dev/null 2>&1; then
usbrelay "$switch_name=$state" >/dev/null 2>&1 || {

Reads as plain english "do or skip"

case-lib/lib.sh Outdated
current_state=$(usbrelay | grep "$switch_name" | awk -F= '{print $2}')

# Check if current_state is equal to the requested state
if [[ "$current_state" != "$state" ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
if [[ "$current_state" != "$state" ]]; then
[[ "$current_state" == "$state" ]] || {

Reads as "assert or die"

set_alsa_settings "$MODEL"
fi

logger_disabled || func_lib_start_log_collect
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's coming surprisingly late... are you sure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you'll might right. move it upper.

@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch from fed81ff to 2b296b1 Compare June 25, 2025 16:48
@arikgreen
Copy link
Contributor Author

@golowanow & @marc-hb thanks for review. Many valuable comments.

@arikgreen arikgreen requested review from golowanow and marc-hb July 5, 2025 22:25
case-lib/lib.sh Outdated
local switch_name=$1
local state=$2
dlogi "Setting usbrelay switch $switch_name to $state."
usbrelay "$switch_name=$state" >/dev/null 2>&1 || {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think usbrelay-s output with possible errors is not useful for diagnostics and troubleshooting ?

Suggested change
usbrelay "$switch_name=$state" >/dev/null 2>&1 || {
usbrelay "$switch_name=$state" 2>&1 || {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discarding stderr is wrong 99.999% of the time. The remaining 0.001% requires a comment with a very strong rationale.

That's because 99.999% of the time, developers use stderr because they think the error is so bad that the message should never be discarded.

Discarding stderr even more wrong in TEST code! It's a really bad code smell.

case-lib/lib.sh Outdated
Comment on lines 1217 to 1308
if [[ "$current_state" == "1" ]]; then
dlogi "Current state of $switch_name is: on"
elif [[ "$current_state" == "0" ]]; then
dlogi "Current state of $switch_name is: off"
else
dloge "Invalid state for $switch_name: $current_state"
exit 1
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems this if/elif/else chunk should be before $current_state == $state comparison, or this function should reject the incorrect $state parameter early at the entry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the most important is state of $current_state and it should be equal expected $state, so we should verify it.
If the $state will not be 0 || 1 then the command usbrelay "$switch_name=$state" returns error.
IMO moving the chunk before $current_state == $state doesn't matter.

fi

# check if usbrelay tool is installed
command -v usbrelay >/dev/null 2>&1 || {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
command -v usbrelay >/dev/null 2>&1 || {
command -v usbrelay 2>&1 || {

dlogi "Turn on the mic privacy switch"
usbrelay_switch "$relay" 1

alsabat_output=$(mktemp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mktemp needs the same explicit template name here, isn't it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no needs the explicit template name, without template the cmd return /tmp/tmp.xxxxxx

@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch 4 times, most recently from 32e0fc7 to 74def0a Compare July 14, 2025 21:26
elif [[ "$current_state" == "0" ]]; then
dlogi "Current state of $switch_name is: off"
else
die "Invalid state for $switch_name: $current_state"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case ... in works great for this. Find one example (among many others) at the bottom of 41c1427


logger_disabled || func_lib_start_log_collect

if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then
if [ -z "$pcm_p" ] || [ -z "$pcm_c" ]; then

logger_disabled || func_lib_start_log_collect

if [ "$pcm_p" = "" ]||[ "$pcm_c" = "" ]; then
skip_test "No playback or capture PCM is specified. Skip the alsabat test."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
skip_test "No playback or capture PCM is specified. Skip the alsabat test."
skip_test "No playback or capture PCM is specified. Skip the $0 test."

There is already some test-case/alsabat.sh somewhere.

alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" >"$alsabat_output" 2>&1
alsabat_status=$?

if [ $alsabat_status -ne 0 ]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the "early return" pattern

Suggested change
if [ $alsabat_status -ne 0 ]; then
alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" >"$alsabat_output" 2>&1 || {
dloge "alsabat passed unexpectedly, upload the wav files."
__upload_wav_files
die "MIC privacy doesn't work. alsabat output: $(cat "$alsabat_output")"
}

@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch 4 times, most recently from 7e5bff3 to 3c4a6f7 Compare July 29, 2025 19:42
local state=$2

dlogi "Setting usbrelay switch $switch_name to $state."
usbrelay "$switch_name=$state" > /dev/null || {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
usbrelay "$switch_name=$state" > /dev/null || {
usbrelay "$switch_name=$state" || {

as already discussed #1278 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command returns a previous state of relays this could be confusing and imo isn't usable..


dlogi "Setting usbrelay switch $switch_name to $state."
usbrelay "$switch_name=$state" > /dev/null || {
# if not detect relays hw module, skip the test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# if not detect relays hw module, skip the test

The error message explains already.

ALSABAT_WAV_FILES="/tmp/mc.wav.*"
rm -f "$ALSABAT_WAV_FILES"

TWO_SECONDS=2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it serve the same purpose as USBRELAY_SETTLE_TIME=0.5s ?
or is it a real USBRELAY_SETTLE_TIME ?

anyway, better to name this variable differently than TWO_SECONDS, just in case someone needs to increase it to 3 seconds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, might you right

alsabat -P"$pcm_p" -C"$pcm_c" -c 2 -r "$rate" || {
# upload failed wav file
__upload_wav_files
die "alsabat failed"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
die "alsabat failed"
die "check_playback_capture() failed: no loopback connected ?"

Comment on lines 119 to 129
# reset sof volume to 0dB
reset_sof_volume

# If MODEL is defined, set proper gain for the platform
if [ -z "$MODEL" ]; then
# treat as warning only
dlogw "NO MODEL is defined. Please define MODEL to run alsa_settings/MODEL.sh"
else
dlogi "apply alsa settings for alsa_settings/MODEL.sh"
set_alsa_settings "$MODEL"
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# reset sof volume to 0dB
reset_sof_volume
# If MODEL is defined, set proper gain for the platform
if [ -z "$MODEL" ]; then
# treat as warning only
dlogw "NO MODEL is defined. Please define MODEL to run alsa_settings/MODEL.sh"
else
dlogi "apply alsa settings for alsa_settings/MODEL.sh"
set_alsa_settings "$MODEL"
fi
set_alsa()


local switch_name=$1
local state=$2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add something like this to log the initial usbrelay status, to see all its relays, also to check it is runnable:

    usbrelay --debug  || die "usbrelay actual state read failed"

@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch 4 times, most recently from e547530 to 2d582c3 Compare August 4, 2025 17:49
@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch from 2d582c3 to 43b6d50 Compare September 3, 2025 11:11
Copy link
Collaborator

@marc-hb marc-hb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, someone must review the actual audio logic.

# param2: switch state
usbrelay_switch()
{
[[ "$1" == "--debug" ]] && {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[[ "$1" == "--debug" ]] && {
if [[ "$1" == "--debug" ]]; then {

The && "shortcut" can sometimes save a couple lines but it does not save anything here. Also, && can be deceptive, for instance:

true && grep notfound /etc/passwd || echo 'ELSE is also run!!'

|| saves a negation and is less subtle than &&

See explanation in "Prefer || over && when possible" in #312

@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch 2 times, most recently from 9e69829 to d74ea44 Compare September 4, 2025 13:15
@marc-hb
Copy link
Collaborator

marc-hb commented Sep 5, 2025

This has the same relay.sh file as #1280 but it's not added in a separate commit.

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>
The file is added in the separate PR 1280:
thesofproject#1280

Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
@arikgreen arikgreen force-pushed the awilczax/add-test-mic-prv branch from d74ea44 to 6ecdb54 Compare September 5, 2025 13:32
@arikgreen arikgreen requested a review from cgturner1 September 8, 2025 15:07
arikgreen added a commit to arikgreen/sof-test that referenced this pull request Sep 8, 2025
The file is added in the separate PR 1278:
thesofproject#1278

Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
@cgturner1 cgturner1 merged commit 5e71e20 into thesofproject:main Sep 8, 2025
5 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants