From 658432e20391414491891450cdb9e3676269fae8 Mon Sep 17 00:00:00 2001 From: Judy Hsiao Date: Wed, 14 Oct 2020 12:44:12 +0800 Subject: sound_card_init: skip boot time calibration when speakers may be hot To protect the speaker, we need to skip boot calibration if the latest speaker play time is less than 3 min. The CL implements the following logic: 1. CRAS will store the latest shutdown timestamp /var/lib/cras/stop. 2. If "(Current time - the latest CRAS shutdown time) < 3 mins", we assume the speaker hasn't been replaced, and we don't need to recalibrate. 3. If (Current time - the latest CRAS shutdown time) >= 3 mins, we assume that the speakers are at thermal equilibrium with the temperature measured by the amplifier chip and thus it'd be safe to do a calibration. 4. If /var/lib/cras/stop is invalid, we set the speaker volume to low and delete the datastore as we can not assume that the speakers are not replaced or not heated. BUG=b:169377240 TEST=tast run ${DUT_IP} soundcardinit.Max98390.* Change-Id: I7769e955579be96ca7bf4ec048431b4ca61778bf Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/2475313 Tested-by: Judy Hsiao Reviewed-by: Chih-Yang Hsia Commit-Queue: Judy Hsiao Auto-Submit: Judy Hsiao --- sound_card_init/utils/src/lib.rs | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 sound_card_init/utils/src/lib.rs (limited to 'sound_card_init/utils/src/lib.rs') diff --git a/sound_card_init/utils/src/lib.rs b/sound_card_init/utils/src/lib.rs new file mode 100644 index 00000000..e8edce77 --- /dev/null +++ b/sound_card_init/utils/src/lib.rs @@ -0,0 +1,87 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +//! It contains common utils shared within sound_card_init. +#![deny(missing_docs)] + +//! The error definitions for utils. +pub mod error; + +use std::fs::File; +use std::io::{prelude::*, BufReader, BufWriter}; +use std::path::PathBuf; +use std::time::Duration; + +use crate::error::{Error, Result}; + +/// The path of datastore. +pub const DATASTORE_DIR: &str = "/var/lib/sound_card_init"; + +fn duration_from_file(path: &PathBuf) -> Result { + let reader = + BufReader::new(File::open(&path).map_err(|e| Error::FileIOFailed(path.clone(), e))?); + serde_yaml::from_reader(reader).map_err(|e| Error::SerdeError(path.clone(), e)) +} + +/// The utils to parse CRAS shutdown time file. +pub mod shutdown_time { + use super::*; + // The path of CRAS shutdown time file. + const SHUTDOWN_TIME_FILE: &str = "/var/lib/cras/stop"; + + /// Reads the unix time from CRAS shutdown time file. + pub fn from_file() -> Result { + duration_from_file(&PathBuf::from(SHUTDOWN_TIME_FILE)) + } +} + +/// The utils to create and parse sound_card_init run time file. +pub mod run_time { + use std::time::SystemTime; + + use super::*; + // The filename of sound_card_init run time file. + const RUN_TIME_FILE: &str = "run"; + + /// Returns the sound_card_init run time file existence. + pub fn exists(snd_card: &str) -> bool { + run_time_file(snd_card).exists() + } + + /// Reads the unix time from sound_card_init run time file. + pub fn from_file(snd_card: &str) -> Result { + duration_from_file(&run_time_file(snd_card)) + } + + /// Saves the current unix time to sound_card_init run time file. + pub fn now_to_file(snd_card: &str) -> Result<()> { + match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { + Ok(t) => to_file(snd_card, t), + Err(e) => Err(Error::SystemTimeError(e)), + } + } + + /// Saves the unix time to sound_card_init run time file. + pub fn to_file(snd_card: &str, duration: Duration) -> Result<()> { + let path = run_time_file(snd_card); + let mut writer = + BufWriter::new(File::create(&path).map_err(|e| Error::FileIOFailed(path.clone(), e))?); + writer + .write_all( + serde_yaml::to_string(&duration) + .map_err(|e| Error::SerdeError(path.clone(), e))? + .as_bytes(), + ) + .map_err(|e| Error::FileIOFailed(path.clone(), e))?; + writer + .flush() + .map_err(|e| Error::FileIOFailed(path.clone(), e))?; + Ok(()) + } + + fn run_time_file(snd_card: &str) -> PathBuf { + PathBuf::from(DATASTORE_DIR) + .join(snd_card) + .join(RUN_TIME_FILE) + } +} -- cgit v1.2.3