15
15
// with this program; if not, write to the Free Software Foundation, Inc.,
16
16
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
18
- use anyhow:: { bail, Result } ;
18
+ use std:: ffi:: OsStr ;
19
+
20
+ use anyhow:: { anyhow, bail, Result } ;
19
21
use async_std:: sync:: Arc ;
20
22
use nix:: sys:: utsname:: uname;
21
23
use serde:: { Deserialize , Serialize } ;
@@ -24,6 +26,8 @@ use crate::broker::{BrokerBuilder, Topic};
24
26
25
27
#[ cfg( feature = "demo_mode" ) ]
26
28
mod read_dt_props {
29
+ use anyhow:: { anyhow, Result } ;
30
+
27
31
const DEMO_DATA_STR : & [ ( & str , & str ) ] = & [
28
32
(
29
33
"compatible" ,
@@ -53,14 +57,22 @@ mod read_dt_props {
53
57
) ,
54
58
] ;
55
59
56
- pub fn read_dt_property ( path : & str ) -> String {
57
- let ( _, content) = DEMO_DATA_STR . iter ( ) . find ( |( p, _) | * p == path) . unwrap ( ) ;
60
+ pub fn read_dt_property ( path : & str ) -> Result < String > {
61
+ let ( _, content) = DEMO_DATA_STR
62
+ . iter ( )
63
+ . find ( |( p, _) | * p == path)
64
+ . ok_or_else ( || anyhow ! ( "could not find devicetree property {path}" ) ) ?;
58
65
59
- content. to_string ( )
66
+ Ok ( content. to_string ( ) )
60
67
}
61
68
62
- pub fn read_dt_property_u32 ( path : & str ) -> u32 {
63
- DEMO_DATA_NUM . iter ( ) . find ( |( p, _) | * p == path) . unwrap ( ) . 1
69
+ pub fn read_dt_property_u32 ( path : & str ) -> Result < u32 > {
70
+ let ( _, content) = DEMO_DATA_NUM
71
+ . iter ( )
72
+ . find ( |( p, _) | * p == path)
73
+ . ok_or_else ( || anyhow ! ( "could not find devicetree property {path}" ) ) ?;
74
+
75
+ Ok ( * content)
64
76
}
65
77
}
66
78
@@ -69,17 +81,26 @@ mod read_dt_props {
69
81
use std:: fs:: read;
70
82
use std:: str:: from_utf8;
71
83
84
+ use anyhow:: { anyhow, Result } ;
85
+
72
86
const DT_BASE : & str = "/sys/firmware/devicetree/base/" ;
73
87
74
- pub fn read_dt_property ( path : & str ) -> String {
75
- let bytes = read ( [ DT_BASE , path] . join ( "/" ) ) . unwrap ( ) ;
76
- from_utf8 ( bytes. strip_suffix ( & [ 0 ] ) . unwrap ( ) )
77
- . unwrap ( )
78
- . to_string ( )
88
+ pub fn read_dt_property ( path : & str ) -> Result < String > {
89
+ let path = [ DT_BASE , path] . join ( "/" ) ;
90
+ let bytes = read ( & path) ?;
91
+ let stripped_bytes = bytes
92
+ . strip_suffix ( & [ 0 ] )
93
+ . ok_or_else ( || anyhow ! ( "Devicetree property {path} did not contain a value" ) ) ?;
94
+ let stripped = from_utf8 ( stripped_bytes) ?;
95
+
96
+ Ok ( stripped. to_string ( ) )
79
97
}
80
98
81
- pub fn read_dt_property_u32 ( path : & str ) -> u32 {
82
- read_dt_property ( path) . parse ( ) . unwrap ( )
99
+ pub fn read_dt_property_u32 ( path : & str ) -> Result < u32 > {
100
+ let raw = read_dt_property ( path) ?;
101
+ let value = raw. parse ( ) ?;
102
+
103
+ Ok ( value)
83
104
}
84
105
}
85
106
@@ -95,16 +116,25 @@ pub struct Uname {
95
116
}
96
117
97
118
impl Uname {
98
- fn get ( ) -> Self {
99
- let uts = uname ( ) . unwrap ( ) ;
100
-
101
- Self {
102
- sysname : uts. sysname ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
103
- nodename : uts. nodename ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
104
- release : uts. release ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
105
- version : uts. version ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
106
- machine : uts. machine ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
119
+ fn get ( ) -> Result < Self > {
120
+ let uts = uname ( ) ?;
121
+
122
+ fn to_string ( val : & OsStr , name : & str ) -> Result < String > {
123
+ let res = val
124
+ . to_str ( )
125
+ . ok_or_else ( || anyhow ! ( "uname entry {name} can not be converted to utf-8" ) ) ?
126
+ . to_string ( ) ;
127
+
128
+ Ok ( res)
107
129
}
130
+
131
+ Ok ( Self {
132
+ sysname : to_string ( uts. sysname ( ) , "sysname" ) ?,
133
+ nodename : to_string ( uts. nodename ( ) , "nodename" ) ?,
134
+ release : to_string ( uts. release ( ) , "release" ) ?,
135
+ version : to_string ( uts. version ( ) , "version" ) ?,
136
+ machine : to_string ( uts. machine ( ) , "machine" ) ?,
137
+ } )
108
138
}
109
139
}
110
140
@@ -118,31 +148,31 @@ pub struct Barebox {
118
148
}
119
149
120
150
impl Barebox {
121
- fn get ( ) -> Self {
151
+ fn get ( ) -> Result < Self > {
122
152
// Get info from devicetree chosen
123
- Self {
124
- version : read_dt_property ( "chosen/barebox-version" ) ,
153
+ Ok ( Self {
154
+ version : read_dt_property ( "chosen/barebox-version" ) ? ,
125
155
baseboard_release : {
126
156
let template =
127
- read_dt_property ( "chosen/baseboard-factory-data/pcba-hardware-release" ) ;
128
- let changeset = read_dt_property_u32 ( "chosen/baseboard-factory-data/modification" ) ;
129
-
157
+ read_dt_property ( "chosen/baseboard-factory-data/pcba-hardware-release" ) ?;
158
+ let changeset = read_dt_property_u32 ( "chosen/baseboard-factory-data/modification" ) ?;
130
159
template. replace ( "-C??" , & format ! ( "-C{changeset:02}" ) )
131
160
} ,
132
161
powerboard_release : {
133
162
let template =
134
- read_dt_property ( "chosen/powerboard-factory-data/pcba-hardware-release" ) ;
135
- let changeset = read_dt_property_u32 ( "chosen/powerboard-factory-data/modification" ) ;
163
+ read_dt_property ( "chosen/powerboard-factory-data/pcba-hardware-release" ) ?;
164
+ let changeset =
165
+ read_dt_property_u32 ( "chosen/powerboard-factory-data/modification" ) ?;
136
166
137
167
template. replace ( "-C??" , & format ! ( "-C{changeset:02}" ) )
138
168
} ,
139
169
baseboard_timestamp : {
140
- read_dt_property_u32 ( "chosen/baseboard-factory-data/factory-timestamp" )
170
+ read_dt_property_u32 ( "chosen/baseboard-factory-data/factory-timestamp" ) ?
141
171
} ,
142
172
powerboard_timestamp : {
143
- read_dt_property_u32 ( "chosen/powerboard-factory-data/factory-timestamp" )
173
+ read_dt_property_u32 ( "chosen/powerboard-factory-data/factory-timestamp" ) ?
144
174
} ,
145
- }
175
+ } )
146
176
}
147
177
}
148
178
@@ -155,7 +185,7 @@ pub enum HardwareGeneration {
155
185
156
186
impl HardwareGeneration {
157
187
pub fn get ( ) -> Result < Self > {
158
- let compatible = read_dt_property ( "compatible" ) ;
188
+ let compatible = read_dt_property ( "compatible" ) ? ;
159
189
160
190
// The compatible property consists of strings separated by NUL bytes.
161
191
// We are interested in the first of these strings.
@@ -182,17 +212,20 @@ pub struct System {
182
212
}
183
213
184
214
impl System {
185
- pub fn new ( bb : & mut BrokerBuilder , hardware_generation : HardwareGeneration ) -> Self {
215
+ pub fn new ( bb : & mut BrokerBuilder , hardware_generation : HardwareGeneration ) -> Result < Self > {
186
216
let version = env ! ( "VERSION_STRING" ) . to_string ( ) ;
187
217
188
- Self {
189
- uname : bb. topic_ro ( "/v1/tac/info/uname" , Some ( Arc :: new ( Uname :: get ( ) ) ) ) ,
190
- barebox : bb. topic_ro ( "/v1/tac/info/bootloader" , Some ( Arc :: new ( Barebox :: get ( ) ) ) ) ,
218
+ let uname = Uname :: get ( ) ?;
219
+ let barebox = Barebox :: get ( ) ?;
220
+
221
+ Ok ( Self {
222
+ uname : bb. topic_ro ( "/v1/tac/info/uname" , Some ( Arc :: new ( uname) ) ) ,
223
+ barebox : bb. topic_ro ( "/v1/tac/info/bootloader" , Some ( Arc :: new ( barebox) ) ) ,
191
224
tacd_version : bb. topic_ro ( "/v1/tac/info/tacd/version" , Some ( version) ) ,
192
225
hardware_generation : bb. topic_ro (
193
226
"/v1/tac/info/hardware_generation" ,
194
227
Some ( hardware_generation) ,
195
228
) ,
196
- }
229
+ } )
197
230
}
198
231
}
0 commit comments