diff --git a/case-lib/control_state.awk b/case-lib/control_state.awk new file mode 100644 index 00000000..a462d51c --- /dev/null +++ b/case-lib/control_state.awk @@ -0,0 +1,22 @@ +#!/usr/bin/gawk -f + +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2025 Intel Corporation. All rights reserved. + +BEGIN { + IGNORECASE = 1 + found = 0 +} + +# Detect the line with the control name +/name='/ { + if (tolower($0) ~ tolower(name)) found = 1 + else found = 0 +} + +# When in a matching section, extract the "values" field +found && /: values=/ { + sub(/^.*: values=/, "", $0) + print $0 + found = 0 +} diff --git a/case-lib/relay.sh b/case-lib/relay.sh index 04034702..cc0d52dc 100644 --- a/case-lib/relay.sh +++ b/case-lib/relay.sh @@ -10,11 +10,13 @@ usbrelay_switch() { if [[ "$1" == "--debug" ]]; then dlogi "Debug mode: Current status of all relays:" - usbrelay || { - die "Failed to get usbrelay status. - The usbrelay hw module is not responding or no relays detected. - Check hardware connection." - } + if usbrelay --debug 2>&1 | grep -q "Found 0 devices"; then + dloge "No relays detected. Found 0 devices. Check hardware connection." + dloge "The usbrelay hw module is not responding or no relays detected. Check hardware connection." + # Skip the test if no relays are detected + exit 2 + fi + return 0 fi # Declare a constant for the relay settle time diff --git a/test-case/test-jack-detection-playback-capture.sh b/test-case/test-jack-detection-playback-capture.sh index 8ebeb2f0..f2533cdb 100755 --- a/test-case/test-jack-detection-playback-capture.sh +++ b/test-case/test-jack-detection-playback-capture.sh @@ -47,25 +47,35 @@ source "${TESTLIB}/lib.sh" source "${TESTLIB}/relay.sh" # shellcheck disable=SC2153 -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['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['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['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" +OPT_NAME['d']='dsp-settle-sleep' OPT_DESC['d']="Waitng time to change control state" +OPT_HAS_ARG['d']=1 OPT_VAL['d']=3 + +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" + +OPT_NAME['H']='headphone' OPT_DESC['H']='name of pcm control for headphone jack' +OPT_HAS_ARG['H']=1 OPT_VAL['H']="headphone jack" + +OPT_NAME['M']='headset' OPT_DESC['M']='name of pcm control for headset mic jack' +OPT_HAS_ARG['M']=1 OPT_VAL['M']="headset [a-z ]*jack" func_opt_parse_option "$@" tplg=${OPT_VAL['t']} relay=${OPT_VAL['u']} loop_cnt=${OPT_VAL['l']} - -DSP_SETTLE_TIME=2 +dsp_settle_time=${OPT_VAL['d']} +headphone_jack_name=${OPT_VAL['H']} +headset_mic_jack_name=${OPT_VAL['M']} check_control_switch_state() { @@ -77,21 +87,15 @@ check_control_switch_state() local expected_control_state="$2" local control_state - control_state=$(amixer -c "$SOFCARD" contents | awk -v name="$control_name" ' - BEGIN { - RS = ""; - IGNORECASE = 1; - split(name, parts, " "); - }; - $0 ~ parts[1] && $0 ~ parts[2] { - if (match($0, /values=(on|off)/, m)) print m[1]; - } - ') + dlogi "Check if the state of control: $control_name is correct." + control_state=$(amixer -c "$SOFCARD" contents | \ + gawk -v name="$control_name" -f "${TESTLIB}/control_state.awk") dlogi "$control_name switch is: $control_state" if [[ "$expected_control_state" == "$control_state" ]]; then return 0 else + dloge "Expected control state ($expected_control_state) but got ($control_state)." return 1 fi } @@ -116,7 +120,7 @@ testing_one_pcm() usbrelay_switch "$relay" 1 # Wait for a short period to allow the system to detect the unplug event - sleep $DSP_SETTLE_TIME + sleep "$dsp_settle_time" # check if the aplay process is still running after unplugging the jack ps -p "$pid_playback" > /dev/null || { @@ -124,19 +128,19 @@ testing_one_pcm() die "Playback process terminated unexpectedly after unplugging the jack." } - check_control_switch_state "headset" "off" || { - die "unplug headset jack failed." + check_control_switch_state "$headset_mic_jack_name" "off" || { + die "unplug $headset_mic_jack_name jack failed." } - check_control_switch_state "headphone" 'off' || { - die "unplug headphone jack failed." + check_control_switch_state "$headphone_jack_name" "off" || { + die "unplug $headphone_jack_name jack failed." } dlogi "Plug jack audio." usbrelay_switch "$relay" 0 # Wait for a short period to allow the system to detect the plug event - sleep $DSP_SETTLE_TIME + sleep "$dsp_settle_time" # check if the aplay process is still running after unplugging the jack ps -p "$pid_playback" > /dev/null || { @@ -144,12 +148,12 @@ testing_one_pcm() die "Playback process terminated unexpectedly after plugging the jack." } - check_control_switch_state "headset" "on" || { - die "Plug headset jack failed." + check_control_switch_state "$headset_mic_jack_name" "on" || { + die "Plug $headset_mic_jack_name failed." } - check_control_switch_state "headphone" "on" || { - die "Plug headphone jack failed." + check_control_switch_state "$headphone_jack_name" "on" || { + die "Plug $headphone_jack_name jack failed." } kill -9 $pid_playback > /dev/null 2>&1 @@ -171,20 +175,25 @@ main() start_test - logger_disabled || func_lib_start_log_collect - - # Check if usbrelay tool is installed + dlogi "Checking usbrelay availability..." command -v usbrelay || { # If usbrelay package is not installed - skip_test "usbrelay command not found. Please install usbrelay package." + skip_test "usbrelay command not found." } # display current status of relays - usbrelay --debug + usbrelay_switch --debug || { + skip_test "Failed to get usbrelay status." + } + + logger_disabled || func_lib_start_log_collect dlogi "Reset - plug jack audio" usbrelay_switch "$relay" 0 + dlogi "Headphone patten: $headphone_jack_name" + dlogi "Headset mic pattern: $headset_mic_jack_name" + for idx in $(seq 0 $((PIPELINE_COUNT - 1))) do initialize_audio_params "$idx"