Skip to content

Commit 1586831

Browse files
committed
feat: referesh() as member of Daemon
Removes detached boards, adds new ones. Currently unused/untested in frontend.
1 parent 6cfb29b commit 1586831

File tree

4 files changed

+73
-54
lines changed

4 files changed

+73
-54
lines changed

backend/src/daemon/dummy.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ impl Daemon for DaemonDummy {
125125
Ok(())
126126
}
127127

128+
fn refresh(&self) -> Result<(), String> {
129+
Ok(())
130+
}
131+
128132
fn exit(&self) -> Result<(), String> {
129133
Ok(())
130134
}

backend/src/daemon/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ macro_rules! commands {
105105
commands! {
106106
fn boards(&self) -> Result<Vec<BoardId>, String>;
107107
fn model(&self, board: BoardId) -> Result<String, String>;
108+
fn refresh(&self) -> Result<(), String>;
108109
fn keymap_get(&self, board: BoardId, layer: u8, output: u8, input: u8) -> Result<u16, String>;
109110
fn keymap_set(&self, board: BoardId, layer: u8, output: u8, input: u8, value: u16) -> Result<(), String>;
110111
fn matrix_get(&self, board: BoardId) -> Result<Matrix, String>;

backend/src/daemon/s76power.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ impl Daemon for DaemonS76Power {
213213
Err("Unimplemented".to_string())
214214
}
215215

216+
fn refresh(&self) -> Result<(), String> {
217+
Ok(())
218+
}
219+
216220
fn exit(&self) -> Result<(), String> {
217221
Ok(())
218222
}

backend/src/daemon/server.rs

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<R: Read, W: Write> DaemonServer<R, W> {
7070
board_ids: RefCell::new(board_ids),
7171
};
7272

73-
self_.refresh();
73+
self_.refresh()?;
7474

7575
Ok(self_)
7676
}
@@ -88,59 +88,6 @@ impl<R: Read, W: Write> DaemonServer<R, W> {
8888
false
8989
}
9090

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-
14491
pub fn run(mut self) -> io::Result<()> {
14592
println!("Daemon started");
14693

@@ -265,6 +212,69 @@ impl<R: Read, W: Write> Daemon for DaemonServer<R, W> {
265212
unsafe { ec.led_save().map_err(err_str) }
266213
}
267214

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+
268278
fn exit(&self) -> Result<(), String> {
269279
self.running.set(false);
270280
Ok(())

0 commit comments

Comments
 (0)