Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions case-lib/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,48 @@ reset_sof_volume()
done
}

# Initialize and restore ALSA mixer state to the last saved on the DUT,
# reset volume, and apply the HW MODEL-specific settings, if defined.
#
# This initialization procedure assumes that the DUT's ALSA init files,
# UCM2, and default state (/var/lib/alsa/asound.state) are either kept
# unchanged as a 'golden' config, or _mostly_ consistent with the MODEL.
# The latter case is when the alsa-restore service is active on the DUT and
# the state file keeps the last active mixer settings persistent to restore
# them after the DUT's reboot.
# The init files and the persistent state file should diverge not more
# than set_alsa_settings() aligns and should be reset to a 'golden' config
# during the DUT's deployment step of the test plan execution,
# which is external to the test case scope of operations.
#
set_alsa()
{
local alsa_log="${LOG_ROOT}/alsa_setup.log"
local asound_state="${LOG_ROOT}/asound_state"
local rc=0

# Read the ALSA restore service status
systemctl --no-pager status alsa-restore.service > "${alsa_log}" 2>&1 || rc=$?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suppose you could just rc=$? unconditionally after the systemctl call and remove the initialisation of rc

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

right, its my intent to deal with rc explicitly and to tie command calls and rc set, so it is declared as a local variable at this function with init value and, despite any change in the following command calls, it will have the default value.

With command || rc=$? the script works the same way if it runs with bash -e - continue its execution.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You could probably do this but && has many pitfalls and is best avoided

systemctl --no-pager status alsa-restore.service > "${alsa_log}" 2>&1 && local rc=$?

@lyakh check the && pitfalls in #312

[[ "${rc}" -ne 0 ]] && dlogw "alsa-restore check error=${rc}" && rc=0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if for whatever reason dlogw exits with an error, rc won't be reset. I'd just use if here for determinism. More of these below, I'd be more explicit with rc

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

when dlogw fails (something really bad ? hopefully with some stderr output) the rc should not reset to zero as well.

[ -f /lib/systemd/system/alsa-restore.service ] && cat /lib/systemd/system/alsa-restore.service >> "${alsa_log}"

alsactl store -f "${asound_state}_old.txt" 2>&1 || rc=$?
[[ "${rc}" -ne 0 ]] && dlogw "alsactl store error=${rc}" && rc=0

dlogi "Try to initialize all devices to their default ALSA state (alsactl init)."
printf '%s\n' '-vv------- ALSA init -------vv-' >> "${alsa_log}"
alsactl -d init >> "${alsa_log}" 2>&1 || rc=$?
[[ "${rc}" -ne 0 ]] && dlogw "alsactl init error=${rc}" && rc=0
printf '%s\n' '-^^------- ALSA init -------^^-' >> "${alsa_log}"

dlogi "Restore ALSA defaults from /var/lib/alsa/asound.state"
printf '%s\n' '-vv------- Restore ALSA defaults -------vv-' >> "${alsa_log}"
# We don't need in sudo to write our log file, but to call `alsactl`
# shellcheck disable=SC2024
sudo alsactl -d restore >> "${alsa_log}" 2>&1 || rc=$?
[[ "${rc}" -ne 0 ]] && dlogw "alsactl restore error=${rc}" && rc=0
printf '%s\n' '-^^------- Restore ALSA defaults -------^^-' >> "${alsa_log}"

reset_sof_volume

# If MODEL is defined, set proper gain for the platform
Expand All @@ -1257,6 +1297,9 @@ set_alsa()
else
set_alsa_settings "$MODEL"
fi

alsactl store -f "${asound_state}.txt" 2>&1 || rc=$?
[[ "${rc}" -ne 0 ]] && dlogw "alsactl store error=${rc}"
}

DO_PERF_ANALYSIS=0
Expand Down