Skip to content

Commit

Permalink
sof_helper: add an argument to output in json format
Browse files Browse the repository at this point in the history
Adds the new argument "--json" to output in json format for the usage of
autotest items.

BUG=b:349072680
TEST=run `sof_helper cstate` on SOF-backed devices and check output
is expected.

Change-Id: Ic172d51e96deba5f403c3b1f9aea57f81aa9aaac
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5859683
Tested-by: Pin-chih Lin <johnylin@google.com>
Reviewed-by: Curtis Malainey <cujomalainey@chromium.org>
Auto-Submit: Pin-chih Lin <johnylin@google.com>
Commit-Queue: Pin-chih Lin <johnylin@google.com>
Reviewed-by: Li-Yu Yu <aaronyu@google.com>
Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com <chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com>
  • Loading branch information
johnylin76 authored and Chromeos LUCI committed Sep 24, 2024
1 parent 98d3cf8 commit 3991a56
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions sof_helper/src/cstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,28 @@ fn get_support_components(card: &str) -> Result<Vec<(Control, &dyn Component)>>
Ok(support_comps)
}

// For JSON format printing
#[derive(serde::Serialize, Debug)]
struct CompStateInfo {
component: String,
state: String,
mixer: String,
}

fn print_state_json(support_comps: &[(Control, &dyn Component)]) -> Result<()> {
let mut infos: Vec<CompStateInfo> = Vec::new();
for (ctl, comp) in support_comps.iter() {
let state = comp.get_state(&ctl)?;
infos.push(CompStateInfo {
component: comp.get_type().to_string(),
state: format!("{:?}", state),
mixer: format!("numid={},name='{}'", ctl.numid, ctl.name),
});
}
println!("{}", serde_json::to_string(&infos)?);
Ok(())
}

#[derive(PartialEq, Debug, Args)]
pub struct CstateOptions {
/// The component type
Expand All @@ -417,6 +439,9 @@ pub struct CstateOptions {
/// Print "On"/"Off"/"Exists" for the state, return error if not exist
#[arg(long, short, requires = "component")]
pub expect: bool,
/// Print all available states in JSON format, use this without [COMPONENT]
#[arg(long, conflicts_with_all = ["component", "expect"])]
pub json: bool,
}

pub fn cstate(args: CstateOptions) -> Result<()> {
Expand All @@ -429,6 +454,11 @@ pub fn cstate(args: CstateOptions) -> Result<()> {
support_comps.extend(card_comps);
}

if args.json {
print_state_json(&support_comps[..])?;
return Ok(());
}

let mut expect_state: Option<ControlState> = None;
for (ctl, comp) in support_comps.iter() {
let ctype = comp.get_type();
Expand Down

0 comments on commit 3991a56

Please sign in to comment.