@@ -70,7 +70,7 @@ impl<R: Read, W: Write> DaemonServer<R, W> {
70
70
board_ids : RefCell :: new ( board_ids) ,
71
71
} ;
72
72
73
- self_. refresh ( ) ;
73
+ self_. refresh ( ) ? ;
74
74
75
75
Ok ( self_)
76
76
}
@@ -88,59 +88,6 @@ impl<R: Read, W: Write> DaemonServer<R, W> {
88
88
false
89
89
}
90
90
91
- fn refresh ( & self ) {
92
- if let Some ( api) = & mut * self . hidapi . borrow_mut ( ) {
93
- if let Err ( err) = api. refresh_devices ( ) {
94
- error ! ( "Failed to refresh hidapi devices: {}" , err) ;
95
- }
96
- for info in api. device_list ( ) {
97
- match ( info. vendor_id ( ) , info. product_id ( ) , info. interface_number ( ) ) {
98
- // System76 launch_1
99
- //TODO: better way to determine this
100
- ( 0x3384 , 0x0001 , 1 ) => {
101
- // Skip if device already open
102
- if self . have_device ( & info) {
103
- continue ;
104
- }
105
-
106
- match info. open_device ( & api) {
107
- Ok ( device) => match AccessHid :: new ( device, 10 , 100 ) {
108
- Ok ( access) => match unsafe { Ec :: new ( access) } {
109
- Ok ( ec) => {
110
- info ! ( "Adding USB HID EC at {:?}" , info. path( ) ) ;
111
- let id = BoardId ( Uuid :: new_v4 ( ) . as_u128 ( ) ) ;
112
- self . boards
113
- . borrow_mut ( )
114
- . insert ( id, ( ec. into_dyn ( ) , Some ( info. clone ( ) ) ) ) ;
115
- self . board_ids . borrow_mut ( ) . push ( id) ;
116
- }
117
- Err ( err) => {
118
- error ! (
119
- "Failed to probe USB HID EC at {:?}: {:?}" ,
120
- info. path( ) ,
121
- err
122
- ) ;
123
- }
124
- } ,
125
- Err ( err) => {
126
- error ! (
127
- "Failed to access USB HID EC at {:?}: {:?}" ,
128
- info. path( ) ,
129
- err
130
- ) ;
131
- }
132
- } ,
133
- Err ( err) => {
134
- error ! ( "Failed to open USB HID EC at {:?}: {:?}" , info. path( ) , err) ;
135
- }
136
- }
137
- }
138
- _ => ( ) ,
139
- }
140
- }
141
- }
142
- }
143
-
144
91
pub fn run ( mut self ) -> io:: Result < ( ) > {
145
92
println ! ( "Daemon started" ) ;
146
93
@@ -265,6 +212,69 @@ impl<R: Read, W: Write> Daemon for DaemonServer<R, W> {
265
212
unsafe { ec. led_save ( ) . map_err ( err_str) }
266
213
}
267
214
215
+ fn refresh ( & self ) -> Result < ( ) , String > {
216
+ if let Some ( api) = & mut * self . hidapi . borrow_mut ( ) {
217
+ // Remove USB boards that are no longer attached
218
+ {
219
+ let mut boards = self . boards . borrow_mut ( ) ;
220
+ let mut board_ids = self . board_ids . borrow_mut ( ) ;
221
+
222
+ boards. retain ( |_, ( ec, _) | unsafe {
223
+ !( ec. access ( ) . is :: < AccessHid > ( ) && ec. probe ( ) . is_err ( ) )
224
+ } ) ;
225
+ board_ids. retain ( |i| boards. contains_key ( i) ) ;
226
+ }
227
+
228
+ if let Err ( err) = api. refresh_devices ( ) {
229
+ error ! ( "Failed to refresh hidapi devices: {}" , err) ;
230
+ }
231
+
232
+ for info in api. device_list ( ) {
233
+ match ( info. vendor_id ( ) , info. product_id ( ) , info. interface_number ( ) ) {
234
+ // System76 launch_1
235
+ //TODO: better way to determine this
236
+ ( 0x3384 , 0x0001 , 1 ) => {
237
+ // Skip if device already open
238
+ if self . have_device ( & info) {
239
+ continue ;
240
+ }
241
+
242
+ match info. open_device ( & api) {
243
+ Ok ( device) => match AccessHid :: new ( device, 10 , 100 ) {
244
+ Ok ( access) => match unsafe { Ec :: new ( access) } {
245
+ Ok ( ec) => {
246
+ info ! ( "Adding USB HID EC at {:?}" , info. path( ) ) ;
247
+ let id = BoardId ( Uuid :: new_v4 ( ) . as_u128 ( ) ) ;
248
+ self . boards
249
+ . borrow_mut ( )
250
+ . insert ( id, ( ec. into_dyn ( ) , Some ( info. clone ( ) ) ) ) ;
251
+ self . board_ids . borrow_mut ( ) . push ( id) ;
252
+ }
253
+ Err ( err) => error ! (
254
+ "Failed to probe USB HID EC at {:?}: {:?}" ,
255
+ info. path( ) ,
256
+ err
257
+ ) ,
258
+ } ,
259
+ Err ( err) => error ! (
260
+ "Failed to access USB HID EC at {:?}: {:?}" ,
261
+ info. path( ) ,
262
+ err
263
+ ) ,
264
+ } ,
265
+ Err ( err) => {
266
+ error ! ( "Failed to open USB HID EC at {:?}: {:?}" , info. path( ) , err)
267
+ }
268
+ }
269
+ }
270
+ _ => ( ) ,
271
+ }
272
+ }
273
+ }
274
+
275
+ Ok ( ( ) )
276
+ }
277
+
268
278
fn exit ( & self ) -> Result < ( ) , String > {
269
279
self . running . set ( false ) ;
270
280
Ok ( ( ) )
0 commit comments