Skip to content

Commit bc1848d

Browse files
cranelwChromeos LUCI
authored andcommitted
Move CrasDlcId into types_internal.rs
This is mainly for s2 to access CrasDlcId and avoid cyclic dependency. Before this change, dlc depends on s2, and CrasDlcId is defined in dlc. After this change, dlc depends on types_internal and s2, and s2 can depend on types_internal. Per discussion, we decided not to make it a crate to keep the original design: 1 crate with 1 header. BUG=b:359803173 TEST=bazel test //... Change-Id: I474fc294d5e6e0d6db4f7378c6b7ebd7d2ea41e5 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5790042 Tested-by: Cranel W <[email protected]> Commit-Queue: Cranel W <[email protected]> Tested-by: [email protected] <[email protected]> Reviewed-by: Li-Yu Yu <[email protected]>
1 parent 7c089db commit bc1848d

File tree

13 files changed

+114
-77
lines changed

13 files changed

+114
-77
lines changed

Cargo.Bazel.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"checksum": "fade509b4eee7c50395810a98977ace70cfd3e4a024a4716fef35e79a100a004",
2+
"checksum": "ec8622ad7e424559f85247a40b5f3b070428ba7a3012b6ed374ae6a1f5e86867",
33
"crates": {
44
"addr2line 0.20.0": {
55
"name": "addr2line",
@@ -2196,6 +2196,10 @@
21962196
"id": "serde_json 1.0.96",
21972197
"target": "serde_json"
21982198
},
2199+
{
2200+
"id": "static_assertions 1.1.0",
2201+
"target": "static_assertions"
2202+
},
21992203
{
22002204
"id": "syslog 6.0.1",
22012205
"target": "syslog"

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cras/common/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ cras_rust_library(
3333
),
3434
crate_name = "cras_common",
3535
edition = "2021",
36-
visibility = ["//cras/src/server/rust:__pkg__"],
36+
visibility = [
37+
"//cras/server/platform:__subpackages__",
38+
"//cras/src/server/rust:__pkg__",
39+
],
3740
deps = [
3841
"@pkg_config//openssl",
3942
] + all_crate_deps(normal = True),

cras/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ nix = { version = "0.28.0", features = ["process"] }
1414
once_cell = "1.17.0"
1515
openssl = "0.10.48"
1616
serde_json = "1.0.64"
17+
static_assertions = "1.1.0"
1718
syslog = "6.0.1"

cras/common/rust_common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ extern "C" {
1818
#include <stdint.h>
1919
#include <stdlib.h>
2020

21+
#define NUM_CRAS_DLCS 3
22+
23+
#define CRAS_DLC_ID_STRING_MAX_LENGTH 50
24+
25+
/**
26+
* All supported DLCs in CRAS.
27+
*/
28+
enum CrasDlcId {
29+
CrasDlcSrBt,
30+
CrasDlcNcAp,
31+
CrasDlcIntelligoBeamforming,
32+
};
33+
2134
enum CRAS_FRA_SIGNAL {
2235
PeripheralsUsbSoundCard = 0,
2336
USBAudioConfigureFailed,

cras/common/src/types_internal.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use std::borrow::Cow;
66
use std::ffi::CString;
7+
use std::fmt::Display;
78

9+
use anyhow::bail;
810
use bitflags::bitflags;
911
use itertools::Itertools;
1012

@@ -43,3 +45,57 @@ pub extern "C" fn cras_stream_active_ap_effects_string(
4345
.unwrap()
4446
.into_raw()
4547
}
48+
49+
/// All supported DLCs in CRAS.
50+
#[repr(C)]
51+
#[derive(Clone, Copy, PartialEq, Hash, Eq, Debug)]
52+
pub enum CrasDlcId {
53+
CrasDlcSrBt,
54+
CrasDlcNcAp,
55+
CrasDlcIntelligoBeamforming,
56+
}
57+
58+
// The list of DLCs that are installed automatically.
59+
pub const MANAGED_DLCS: &[CrasDlcId] = &[
60+
CrasDlcId::CrasDlcSrBt,
61+
CrasDlcId::CrasDlcNcAp,
62+
CrasDlcId::CrasDlcIntelligoBeamforming,
63+
];
64+
65+
pub const NUM_CRAS_DLCS: usize = 3;
66+
// Assert that NUM_CRAS_DLCS is updated.
67+
// We cannot assign MANAGED_DLCS.len() to NUM_CRAS_DLCS because cbindgen does
68+
// not seem to understand it.
69+
static_assertions::const_assert_eq!(NUM_CRAS_DLCS, MANAGED_DLCS.len());
70+
71+
pub const CRAS_DLC_ID_STRING_MAX_LENGTH: i32 = 50;
72+
impl CrasDlcId {
73+
pub fn as_str(&self) -> &'static str {
74+
match self {
75+
// The length of these strings should be bounded by
76+
// CRAS_DLC_ID_STRING_MAX_LENGTH
77+
CrasDlcId::CrasDlcSrBt => "sr-bt-dlc",
78+
CrasDlcId::CrasDlcNcAp => "nc-ap-dlc",
79+
CrasDlcId::CrasDlcIntelligoBeamforming => "intelligo-beamforming-dlc",
80+
}
81+
}
82+
}
83+
84+
impl TryFrom<&str> for CrasDlcId {
85+
type Error = anyhow::Error;
86+
87+
fn try_from(value: &str) -> anyhow::Result<Self> {
88+
for dlc in MANAGED_DLCS {
89+
if dlc.as_str() == value {
90+
return Ok(dlc.clone());
91+
}
92+
}
93+
bail!("unknown DLC {value}");
94+
}
95+
}
96+
97+
impl Display for CrasDlcId {
98+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99+
f.write_str(self.as_str())
100+
}
101+
}

cras/server/platform/dlc/BUILD.bazel

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ cras_rust_library(
1111
name = "dlc",
1212
srcs = glob(include = ["src/*.rs"]),
1313
crate_name = "cras_dlc",
14-
visibility = ["//cras/src/server/rust:__pkg__"],
14+
visibility = [
15+
"//cras/server/platform:__pkg__",
16+
"//cras/src/server/rust:__pkg__",
17+
],
1518
deps = all_crate_deps() + [
19+
"//cras/common:rust_common",
1620
"//cras/server/s2",
1721
"@pkg_config//dbus-1",
1822
],
@@ -24,11 +28,18 @@ cras_cbindgen(
2428
srcs = glob(include = ["src/*.rs"]),
2529
out = "dlc.h",
2630
copyright_year = 2023,
31+
extra_args = [
32+
"--with-include=cras/common/rust_common.h",
33+
"--add-keyword-enum",
34+
],
2735
)
2836

2937
cc_library(
3038
name = "cc",
3139
hdrs = ["dlc.h"],
3240
visibility = ["//visibility:public"],
33-
deps = [":dlc"],
41+
deps = [
42+
":dlc",
43+
"//cras/common:rust_common_cc",
44+
],
3445
)

cras/server/platform/dlc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66

77
[dependencies]
88
anyhow = "1.0.32"
9+
cras_common = "*"
910
cras_s2 = "*"
1011
dbus = "0.9"
1112
libc = "0.2.44"

cras/server/platform/dlc/dlc.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,7 @@ extern "C" {
1717
#include <stddef.h>
1818
#include <stdint.h>
1919
#include <stdlib.h>
20-
21-
#define NUM_CRAS_DLCS 3
22-
23-
#define CRAS_DLC_ID_STRING_MAX_LENGTH 50
24-
25-
/**
26-
* All supported DLCs in CRAS.
27-
*/
28-
enum CrasDlcId {
29-
CrasDlcSrBt,
30-
CrasDlcNcAp,
31-
CrasDlcIntelligoBeamforming,
32-
};
20+
#include "cras/common/rust_common.h"
3321

3422
struct CrasDlcDownloadConfig {
3523
bool dlcs_to_download[NUM_CRAS_DLCS];

cras/server/platform/dlc/src/lib.rs

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ mod chromiumos;
88
mod stub;
99

1010
use std::collections::HashMap;
11-
use std::fmt::Display;
1211
use std::sync::Mutex;
1312
use std::thread::sleep;
1413
use std::time;
1514

16-
use anyhow::bail;
15+
use cras_common::types_internal::CrasDlcId;
1716
use once_cell::sync::Lazy;
1817
use thiserror::Error;
1918

@@ -41,60 +40,6 @@ pub enum Error {
4140

4241
pub type Result<T, E = Error> = std::result::Result<T, E>;
4342

44-
/// All supported DLCs in CRAS.
45-
#[repr(C)]
46-
#[derive(Clone, Copy, PartialEq, Hash, Eq, Debug)]
47-
pub enum CrasDlcId {
48-
CrasDlcSrBt,
49-
CrasDlcNcAp,
50-
CrasDlcIntelligoBeamforming,
51-
}
52-
53-
// The list of DLCs that are installed automatically.
54-
const MANAGED_DLCS: &[CrasDlcId] = &[
55-
CrasDlcId::CrasDlcSrBt,
56-
CrasDlcId::CrasDlcNcAp,
57-
CrasDlcId::CrasDlcIntelligoBeamforming,
58-
];
59-
60-
pub const NUM_CRAS_DLCS: usize = 3;
61-
// Assert that NUM_CRAS_DLCS is updated.
62-
// We cannot assign MANAGED_DLCS.len() to NUM_CRAS_DLCS because cbindgen does
63-
// not seem to understand it.
64-
static_assertions::const_assert_eq!(NUM_CRAS_DLCS, MANAGED_DLCS.len());
65-
66-
pub const CRAS_DLC_ID_STRING_MAX_LENGTH: i32 = 50;
67-
impl CrasDlcId {
68-
fn as_str(&self) -> &'static str {
69-
match self {
70-
// The length of these strings should be bounded by
71-
// CRAS_DLC_ID_STRING_MAX_LENGTH
72-
CrasDlcId::CrasDlcSrBt => "sr-bt-dlc",
73-
CrasDlcId::CrasDlcNcAp => "nc-ap-dlc",
74-
CrasDlcId::CrasDlcIntelligoBeamforming => "intelligo-beamforming-dlc",
75-
}
76-
}
77-
}
78-
79-
impl TryFrom<&str> for CrasDlcId {
80-
type Error = anyhow::Error;
81-
82-
fn try_from(value: &str) -> anyhow::Result<Self> {
83-
for dlc in MANAGED_DLCS {
84-
if dlc.as_str() == value {
85-
return Ok(dlc.clone());
86-
}
87-
}
88-
bail!("unknown DLC {value}");
89-
}
90-
}
91-
92-
impl Display for CrasDlcId {
93-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94-
f.write_str(self.as_str())
95-
}
96-
}
97-
9843
trait ServiceTrait: Sized {
9944
fn new() -> Result<Self>;
10045
fn install(&mut self, id: CrasDlcId) -> Result<()>;
@@ -152,7 +97,7 @@ type CrasServerMetricsDlcInstallRetriedTimesOnSuccessFunc =
15297

15398
#[repr(C)]
15499
pub struct CrasDlcDownloadConfig {
155-
pub dlcs_to_download: [bool; NUM_CRAS_DLCS],
100+
pub dlcs_to_download: [bool; cras_common::types_internal::NUM_CRAS_DLCS],
156101
}
157102

158103
fn download_dlcs_until_installed(
@@ -164,7 +109,7 @@ fn download_dlcs_until_installed(
164109
let mut todo: Vec<_> = download_config
165110
.dlcs_to_download
166111
.iter()
167-
.zip(MANAGED_DLCS.iter())
112+
.zip(cras_common::types_internal::MANAGED_DLCS.iter())
168113
.filter_map(|(download, id)| if *download { Some(id) } else { None })
169114
.collect();
170115
for retry_count in 0..i32::MAX {

0 commit comments

Comments
 (0)