Skip to content

Conversation

@arikgreen
Copy link
Contributor

@arikgreen arikgreen commented Jul 7, 2025

Add a new test case to test jack detection during playback.
Verify jack detection functionality by simulating plugging and unplugging of audio jack using a USB relay switch.
The unplugging and plugging of the jack will be during playback operations to ensure the system can handle jack detection events correctly.
The test will check if the jack detection status is updated correctly when the jack is plugged in and unplugged.

For unplug/plug headset jack is using a USB relay. https://github.com/darrylb123/usbrelay

@arikgreen arikgreen requested review from a team, golowanow, lgirdwood and marc-hb as code owners July 7, 2025 13:20
@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch 2 times, most recently from b5952dd to 61a5222 Compare July 14, 2025 21:24
@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch 2 times, most recently from fd37446 to 2859a70 Compare July 23, 2025 09:12
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.

I didn't look at the code much but there is seems to be some duplication with #1278?

# Repeat for both headphone and headset jacks.
# Repeat for all pipelines.

source "$(dirname "${BASH_SOURCE[0]}")"/../case-lib/lib.sh
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing this line, see other scripts.

shellcheck source=...

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
Collaborator

Choose a reason for hiding this comment

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

Discarding stderr is still very wrong:

#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.

yes, you right Marc I missed it

@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from 2859a70 to 6b859f9 Compare July 24, 2025 12:31
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.

You are very close to fixing ALL shellcheck warnings!

local expected_switch_state="$2"
local switch_state

switch_state=$(echo -e $(amixer -c 0 contents | grep -i "$switch_name .* *jack" -A 2) | sed -n '1s/^.*values=//p')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use $SOFCARD instead of hardcoding to 0.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What does the echo -e achieve?

BTW: https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo

But better use none if none needed in the first place.

dlogi "Reset - plug jack audio"
usbrelay_switch "$relay" 0

for round in $(seq 1 "$round_cnt")
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 use while or shellcheck disable=SC2034

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why should I use while?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you never use shellcheck?

Copy link
Collaborator

@marc-hb marc-hb Jul 24, 2025

Choose a reason for hiding this comment

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

Very strange: in https://github.com/thesofproject/sof-test/actions/runs/16506744373/job/46679195194 shellcheck complained like this:

In test-case/test-jack-detection-playback-capture.sh line 108:
    for round in $(seq 1 "$round_cnt")
    ^-^ SC2034 (warning): round appears unused. Verify use (or export if used externally).

But now it's gone in newer https://github.com/thesofproject/sof-test/actions/runs/16507901117/job/46683162054?pr=1280

EDIT: maybe that went away when you added shellcheck source=?

(The dubious echo on line 79 is flagged in both)

done
done

sof-kernel-log-check.sh "$KERNEL_CHECKPOINT"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe check after each iteration to "fail fast"?

for round in $(seq 1 "$round_cnt")
do
for idx in $(seq 0 $((PIPELINE_COUNT - 1)))
do
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be more readable to move this to some new test_one_idx() function (better name wanted), that would avoid a lot of indentation and a lot of distance between the start and end of the for loops.

@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch 3 times, most recently from 4fcce37 to 4ecdc5c Compare July 24, 2025 21:08
case-lib/lib.sh Outdated
die "Error: Unknown format: %s\n"
fi
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

To keep git clean, don't mix pure whitespace changes with code changes; especially not when it's far away in a completely different file.
https://wiki.openstack.org/wiki/GitCommitMessages#Things_to_avoid_when_creating_commits

local state=$2

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

@marc-hb marc-hb Jul 24, 2025

Choose a reason for hiding this comment

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

How verbose is usbrelay, can you share an example of what is getting discarded here?

This is test code; as long as their size stays reasonable, logs files should have as much information as possible. Imagine trying to make sense of test logs captured on a system you don't have access to.

BTW usbrelay has a --quiet option, why not just use it instead of /dev/null ? Only if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ubuntu@gk-ptlh-rvp-05:~$ usbrelay HURTM_1=1
HURTM_1=0
HURTM_2=0

the verbose is unnecessary for us and it is confused because display previous state of the relay.

Copy link
Member

Choose a reason for hiding this comment

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

the verbose is unnecessary for us and it is confused because display previous state of the relay.

so just call it twice then: before and after to log the change is done (as it is at line 35 already)

Copy link
Contributor Author

@arikgreen arikgreen Aug 7, 2025

Choose a reason for hiding this comment

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

the current state after switching is verified in this line
[[ "$current_state" == "$state" ]]

sleep "$USBRELAY_SETTLE_TIME"

# Display current state of the switch
current_state=$(usbrelay | grep "$switch_name" | awk -F= '{print $2}')
Copy link
Collaborator

@marc-hb marc-hb Jul 24, 2025

Choose a reason for hiding this comment

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

awk has grep built-in.

Suggested change
current_state=$(usbrelay | grep "$switch_name" | awk -F= '{print $2}')
current_state=$(usbrelay | awk -F= "/$switch_name/ {print \$2}")

Or, if you prefer

Suggested change
current_state=$(usbrelay | grep "$switch_name" | awk -F= '{print $2}')
current_state=$(usbrelay | awk -F= "/$switch_name/"' {print $2}')

dlogi "Reset - plug jack audio"
usbrelay_switch "$relay" 0

for round in $(seq 1 "$round_cnt")
Copy link
Collaborator

@marc-hb marc-hb Jul 24, 2025

Choose a reason for hiding this comment

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

Very strange: in https://github.com/thesofproject/sof-test/actions/runs/16506744373/job/46679195194 shellcheck complained like this:

In test-case/test-jack-detection-playback-capture.sh line 108:
    for round in $(seq 1 "$round_cnt")
    ^-^ SC2034 (warning): round appears unused. Verify use (or export if used externally).

But now it's gone in newer https://github.com/thesofproject/sof-test/actions/runs/16507901117/job/46683162054?pr=1280

EDIT: maybe that went away when you added shellcheck source=?

(The dubious echo on line 79 is flagged in both)

@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from 4ecdc5c to 5f74c00 Compare July 29, 2025 19:04
@arikgreen
Copy link
Contributor Author

Thank you @marc-hb for your review, it was very helpful. I hope the code now meets your requirements.

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.

it was very helpful.

My pleasure.

I hope the code now meets your requirements.

I don't have any requirement; I just try to spot common shell mistakes (after shellcheck caught most of them).

The shell is quirky, everyone keeps making the same mistakes.

}

kill -9 $pid_playback > /dev/null 2>&1
wait $pid_playback 2>/dev/null || true
Copy link
Collaborator

Choose a reason for hiding this comment

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

I hope we already have some code for this, maybe in case-lib/

(every script tries to re-invent this)

sleep $TWO_SECONDS

# check if the aplay process is still running after unplugging the jack
ps -p "$pid_playback" > /dev/null || {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I hope we already have some code for this, maybe in case-lib/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO ps -p is more explicit about checking for a process

@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch 3 times, most recently from f4090f4 to 13d9d4e Compare August 4, 2025 17:51

##
## Preconditions
# 1. Runtime PM status is on.
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
# 1. Runtime PM status is on.
# 1. Runtime PM status is on.

please elaborate what is PM here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PM is power management but this point was required in previous version. Should be removed.

## Preconditions
# 1. Runtime PM status is on.
# 2. aplay (playback) is running.
# 3. USB relay switch is available and configured.
Copy link
Member

Choose a reason for hiding this comment

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

Can it be any USB relay switch or of some particular type is required ?
How it should be configured ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It must be same type of USB relay switch
https://github.com/darrylb123/usbrelay
if change type of USB relay switch we will must update the usbrelay_switch() function.

## Test Description
# Verify jack detection functionality by simulating plugging and unplugging
# of audio jack using a USB relay switch.
# The unplugging and plugging of the jack will be during playback operations
Copy link
Member

Choose a reason for hiding this comment

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

Should the test also try jack detection while no playback is running and its handling is correct in this mode ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this scenario will test in a different test - test_jack_detection_during_suspend

# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2021-2025 Intel Corporation. All rights reserved.

# test-mic_privacy.sh needs to control mic privacy settings (on/off)
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
# test-mic_privacy.sh needs to control mic privacy settings (on/off)

# Declare a constant for the relay settle time
USBRELAY_SETTLE_TIME=0.5

local switch_name=$1
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 run with $1 == --debug ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

# Declare a constant for the relay settle time
USBRELAY_SETTLE_TIME=0.5
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_SETTLE_TIME=0.5
local USBRELAY_SETTLE_TIME=0.5

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.

the verbose is unnecessary for us and it is confused because display previous state of the relay.

so just call it twice then: before and after to log the change is done (as it is at line 35 already)

Comment on lines 178 to 185
# Check if usbrelay tool is installed
command -v usbrelay || {
# If usbrelay package is not installed
skip_test "usbrelay command not found. Please install usbrelay package."
}

# display current status of relays
usbrelay --debug || die "usbrelay actual state read failed."
Copy link
Member

Choose a reason for hiding this comment

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

looks like a good function at relay.sh

for i in $(seq 1 "$loop_cnt")
do
testing_one_pcm
sof-kernel-log-check.sh "$KERNEL_CHECKPOINT"
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 in these two nested loops it should either die on the first errors here, or set new kernel check point on each iteration.

@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from 13d9d4e to 739afef Compare August 7, 2025 19:19
@arikgreen arikgreen requested a review from majunkier September 2, 2025 08:37
@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from 739afef to 4651e82 Compare September 3, 2025 11:06
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 PR adds a new test case to verify jack detection functionality during audio playback operations using a USB relay switch to simulate physical plugging/unplugging of audio jacks.

  • Adds test script that uses USB relay to simulate jack plug/unplug events during playback
  • Creates relay utility library for controlling USB relay switches
  • Updates environment check to include usbrelay package dependency

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
test-case/test-jack-detection-playback-capture.sh Main test script implementing jack detection testing during playback
env-check.sh Adds usbrelay package check to environment validation
case-lib/relay.sh Utility library for controlling USB relay switches

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

# check if the aplay process is still running after unplugging the jack
ps -p "$pid_playback" > /dev/null || {
func_lib_lsof_error_dump "$snd"
die "Playback process terminated unexpectedly after unplugging the jack."
Copy link

Copilot AI Sep 3, 2025

Choose a reason for hiding this comment

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

The error message refers to 'unplugging' but this code path is executed after plugging the jack (line 136). The message should say 'after plugging the jack' to match the actual context.

Suggested change
die "Playback process terminated unexpectedly after unplugging the jack."
die "Playback process terminated unexpectedly after plugging the jack."

Copilot uses AI. Check for mistakes.
Comment on lines 7 to 8
# param1: --debug
# param1: switch name
# param2: switch state
Copy link

Copilot AI Sep 3, 2025

Choose a reason for hiding this comment

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

The parameter documentation is inconsistent. Both lines refer to 'param1' but they describe different parameters. The first should be 'param1: --debug' and the second should be 'param2: switch name' or document the overloaded behavior more clearly.

Suggested change
# param1: --debug
# param1: switch name
# param2: switch state
# param1: Either '--debug' to show relay status, or switch name to set state
# param2: switch state (required if param1 is switch name)

Copilot uses AI. Check for mistakes.
usbrelay_switch()
{
[[ "$1" == "--debug" ]] && {
dlogi "Debug mode: Current status of all relays is:"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
dlogi "Debug mode: Current status of all relays is:"
dlogi "Debug mode: Current status of all relays:"

#
# Repeat steps 3-11 for all pcm jack playback devices.

TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
Copy link
Contributor

@redzynix redzynix Sep 3, 2025

Choose a reason for hiding this comment

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

Suggested change
TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
TESTDIR=$(realpath -e "$(dirname "${BASH_SOURCE[0]}")/..")

Comment on lines 50 to 60
OPT_NAME['t']='tplg' OPT_DESC['t']="tplg file, default value is env TPLG: $TPLG"
OPT_HAS_ARG['t']=1 OPT_VAL['t']="$TPLG"

OPT_NAME['l']='loop' OPT_DESC['l']='loop count'
OPT_HAS_ARG['l']=1 OPT_VAL['l']=1

OPT_NAME['s']='sof-logger' OPT_DESC['s']="Open sof-logger trace the data will store at $LOG_ROOT"
OPT_HAS_ARG['s']=0 OPT_VAL['s']=1

OPT_NAME['u']='relay' OPT_DESC['u']='name of usbrelay switch, default value is HURTM_2'
OPT_HAS_ARG['u']=1 OPT_VAL['u']="HURTM_2"
Copy link
Contributor

Choose a reason for hiding this comment

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

inconsistent indents between OPTS.

@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from 4651e82 to ae2db23 Compare September 4, 2025 12:51
@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from ae2db23 to 60ed279 Compare September 4, 2025 12:55
@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from 60ed279 to 91dfa5c Compare September 4, 2025 13:10
@marc-hb
Copy link
Collaborator

marc-hb commented Sep 5, 2025

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

arikgreen added a commit to arikgreen/sof-test that referenced this pull request Sep 5, 2025
The file is added in the separate PR 1280:
thesofproject#1280

Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
@arikgreen arikgreen requested a review from cgturner1 September 8, 2025 15:06
Add a new test case to test jack detection
during playback. The test checks all playback Jack PCM devices
For unplug/plug headset jack 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 1278:
thesofproject#1278

Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
@arikgreen arikgreen force-pushed the awilczax/add-test-jack-detection branch from 91dfa5c to ce0d396 Compare September 8, 2025 15:11
cgturner1 pushed a commit that referenced this pull request Sep 8, 2025
The file is added in the separate PR 1280:
#1280

Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
@cgturner1 cgturner1 merged commit 09130c9 into thesofproject:main Sep 8, 2025
3 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