2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
+ use std:: ffi:: CStr ;
6
+
7
+ use serde:: Serialize ;
8
+
5
9
/// Support status for CRAS features.
6
- #[ repr ( C ) ]
10
+ #[ derive ( Default , Serialize ) ]
7
11
pub struct CrasFeatureTier {
8
12
pub initialized : bool ,
9
13
pub is_x86_64_v2 : bool ,
@@ -28,6 +32,42 @@ impl CrasFeatureTier {
28
32
is_x86_64_v2 : x86_64_v2,
29
33
}
30
34
}
35
+
36
+ /// Construct a CrasFeatureTier from NULL terminated strings.
37
+ ///
38
+ /// # Safety
39
+ ///
40
+ /// board_name and cpu_name must be a NULL terminated strings or NULLs.
41
+ pub unsafe fn new_c ( board_name : * const libc:: c_char , cpu_name : * const libc:: c_char ) -> Self {
42
+ let board_name = if board_name. is_null ( ) {
43
+ ""
44
+ } else {
45
+ match CStr :: from_ptr ( board_name) . to_str ( ) {
46
+ Ok ( name) => name,
47
+ Err ( _) => "" ,
48
+ }
49
+ } ;
50
+
51
+ let cpu_name = if cpu_name. is_null ( ) {
52
+ ""
53
+ } else {
54
+ match CStr :: from_ptr ( cpu_name) . to_str ( ) {
55
+ Ok ( name) => name,
56
+ Err ( _) => "" ,
57
+ }
58
+ } ;
59
+
60
+ let tier = CrasFeatureTier :: new ( board_name, cpu_name) ;
61
+
62
+ log:: info!(
63
+ "cras_feature_tier initialized with board={:?} cpu={:?} x86_64_v2={:?}" ,
64
+ board_name,
65
+ cpu_name,
66
+ tier. is_x86_64_v2
67
+ ) ;
68
+
69
+ tier
70
+ }
31
71
}
32
72
33
73
#[ cfg( any( target_arch = "x86_64" ) ) ]
@@ -93,68 +133,9 @@ mod tests {
93
133
assert ! ( has_substr( "DisalLoWed" , & [ "abc" , "Dis" ] ) ) ;
94
134
assert ! ( !has_substr( "DisalLoWed" , & [ "abc" ] ) ) ;
95
135
}
96
- }
97
-
98
- pub mod bindings {
99
- use std:: ffi:: CStr ;
100
-
101
- pub use super :: CrasFeatureTier ;
102
-
103
- #[ no_mangle]
104
- /// Initialize the cras feature tier struct.
105
- /// On error, a negative error code is returned.
106
- ///
107
- /// # Safety
108
- ///
109
- /// out must be non-NULL.
110
- pub unsafe extern "C" fn cras_feature_tier_init (
111
- out : * mut CrasFeatureTier ,
112
- board_name : * const libc:: c_char ,
113
- cpu_name : * const libc:: c_char ,
114
- ) -> libc:: c_int {
115
- let board_name = if board_name. is_null ( ) {
116
- ""
117
- } else {
118
- match CStr :: from_ptr ( board_name) . to_str ( ) {
119
- Ok ( name) => name,
120
- Err ( _) => return -libc:: EINVAL ,
121
- }
122
- } ;
123
-
124
- let cpu_name = if cpu_name. is_null ( ) {
125
- ""
126
- } else {
127
- match CStr :: from_ptr ( cpu_name) . to_str ( ) {
128
- Ok ( name) => name,
129
- Err ( _) => return -libc:: EINVAL ,
130
- }
131
- } ;
132
-
133
- let tier = CrasFeatureTier :: new ( board_name, cpu_name) ;
134
-
135
- log:: info!(
136
- "cras_feature_tier initialized with board={:?} cpu={:?} x86_64_v2={:?}" ,
137
- board_name,
138
- cpu_name,
139
- tier. is_x86_64_v2
140
- ) ;
141
-
142
- * out = tier;
143
-
144
- 0
145
- }
146
136
147
- #[ cfg( test) ]
148
- mod tests {
149
- use super :: * ;
150
-
151
- #[ test]
152
- fn null_safety ( ) {
153
- let mut tier = std:: mem:: MaybeUninit :: < CrasFeatureTier > :: zeroed ( ) ;
154
- let rc = unsafe {
155
- cras_feature_tier_init ( tier. as_mut_ptr ( ) , std:: ptr:: null ( ) , std:: ptr:: null ( ) )
156
- } ;
157
- assert_eq ! ( 0 , rc) ;
158
- }
137
+ #[ test]
138
+ fn null_safety ( ) {
139
+ unsafe { CrasFeatureTier :: new_c ( std:: ptr:: null ( ) , std:: ptr:: null ( ) ) } ;
159
140
}
160
141
}
0 commit comments