Coverage Report

Created: 2026-11-11 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/adhd/cras/server/feature_tier/src/lib.rs
Line
Count
Source
1
// Copyright 2022 The ChromiumOS Authors
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
use serde::Serialize;
6
7
/// Support status for CRAS features.
8
#[derive(Default, Serialize)]
9
pub struct CrasFeatureTier {
10
    pub initialized: bool,
11
    pub is_x86_64_v2: bool,
12
    pub sr_bt_supported: bool,
13
    pub ap_nc_supported: bool,
14
    pub krisp_nc_supported: bool,
15
}
16
17
impl CrasFeatureTier {
18
    /// Construct a CrasFeatureTier. `board_name` should be the name of the
19
    /// reference board. `cpu_name` should be the model name of the CPU.
20
6
    pub fn new(board_name: &str, cpu_name: &str) -> Self {
21
6
        let x86_64_v2 = is_x86_64_v2();
22
        Self {
23
            initialized: true,
24
6
            sr_bt_supported: match board_name {
25
6
                "eve" | "soraka" | "nautilus" | "nami" | "nocturne" | "rammus" | "fizz" => {
26
0
                    !has_substr(cpu_name, &["celeron", "pentium"])
27
                }
28
6
                _ => false,
29
            },
30
            ap_nc_supported: false,
31
0
            krisp_nc_supported: matches!(
32
6
                board_name,
33
6
                "brya" | "fizz" | "puff" | "sarien" | "drallion" | "brask"
34
            ),
35
6
            is_x86_64_v2: x86_64_v2,
36
        }
37
6
    }
38
}
39
40
#[cfg(any(target_arch = "x86_64"))]
41
6
fn is_x86_64_v2() -> bool {
42
6
    is_x86_feature_detected!("cmpxchg16b")
43
6
        && is_x86_feature_detected!("popcnt")
44
6
        && is_x86_feature_detected!("sse3")
45
6
        && is_x86_feature_detected!("sse4.1")
46
6
        && is_x86_feature_detected!("sse4.2")
47
6
        && is_x86_feature_detected!("ssse3")
48
6
}
49
50
#[cfg(not(any(target_arch = "x86_64")))]
51
fn is_x86_64_v2() -> bool {
52
    false
53
}
54
55
// Returns true only if `string` contains any substring in `substrings`.
56
// Note that it's case-insensitive.
57
0
fn has_substr(string: &str, substrings: &[&str]) -> bool {
58
0
    let string_lowercase = string.to_lowercase();
59
0
    substrings
60
0
        .iter()
61
0
        .any(|&substr| string_lowercase.contains(&substr.to_lowercase()))
62
0
}
63
64
#[cfg(test)]
65
mod tests {
66
    use super::*;
67
68
    #[test]
69
    fn eve_i7() {
70
        let tier = CrasFeatureTier::new("eve", "Intel(R) Core(TM) i7-7Y75 CPU @ 1.30GHz");
71
        assert!(tier.sr_bt_supported);
72
    }
73
74
    #[test]
75
    fn random_board() {
76
        let tier = CrasFeatureTier::new("random-board", "random");
77
        assert!(!tier.sr_bt_supported);
78
        assert!(!tier.krisp_nc_supported);
79
    }
80
81
    #[test]
82
    fn fizz_celeron() {
83
        let tier = CrasFeatureTier::new("fizz", "Celeron-3865U");
84
        assert!(!tier.sr_bt_supported);
85
    }
86
87
    #[test]
88
    fn nami_pentium() {
89
        let tier = CrasFeatureTier::new("nami", "PENTIUM-4417U");
90
        assert!(!tier.sr_bt_supported);
91
    }
92
93
    #[test]
94
    fn krisp_nc_supported_boards() {
95
        for board in &["brya", "fizz", "puff", "sarien", "drallion", "brask"] {
96
            let tier = CrasFeatureTier::new(board, "some cpu");
97
            assert!(
98
                tier.krisp_nc_supported,
99
                "Board {} should support Krisp NC",
100
                board
101
            );
102
        }
103
    }
104
105
    #[test]
106
    fn check_has_substr() {
107
        assert!(has_substr("DisalLoWed", &["abc", "Dis"]));
108
        assert!(!has_substr("DisalLoWed", &["abc"]));
109
    }
110
}