Skip to content

Commit 847a339

Browse files
authored
Merge pull request #585 from tock/api-screen-command-fail
api: screen: return if command fails
2 parents 5544210 + b428727 commit 847a339

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

apis/display/screen/src/lib.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
3838
let called: Cell<Option<(u32,)>> = Cell::new(None);
3939
share::scope(|subscribe| {
4040
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
41-
let val = S::command(DRIVER_NUM, command::SET_BRIGHTNESS, value as u32, 0).to_result();
41+
S::command(DRIVER_NUM, command::SET_BRIGHTNESS, value as u32, 0)
42+
.to_result::<(), _>()?;
4243
loop {
4344
S::yield_wait();
4445
if let Some((_,)) = called.get() {
45-
return val;
46+
return Ok(());
4647
}
4748
}
4849
})
@@ -53,11 +54,11 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
5354
let called: Cell<Option<(u32,)>> = Cell::new(None);
5455
share::scope(|subscribe| {
5556
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
56-
let val = S::command(DRIVER_NUM, command::SET_INVERT_ON, 0, 0).to_result();
57+
S::command(DRIVER_NUM, command::SET_INVERT_ON, 0, 0).to_result::<(), _>()?;
5758
loop {
5859
S::yield_wait();
5960
if let Some((_,)) = called.get() {
60-
return val;
61+
return Ok(());
6162
}
6263
}
6364
})
@@ -68,11 +69,11 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
6869
let called: Cell<Option<(u32,)>> = Cell::new(None);
6970
share::scope(|subscribe| {
7071
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
71-
let val = S::command(DRIVER_NUM, command::SET_INVERT_OFF, 0, 0).to_result();
72+
S::command(DRIVER_NUM, command::SET_INVERT_OFF, 0, 0).to_result::<(), _>()?;
7273
loop {
7374
S::yield_wait();
7475
if let Some((_,)) = called.get() {
75-
return val;
76+
return Ok(());
7677
}
7778
}
7879
})
@@ -119,6 +120,7 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
119120
share::scope(|subscribe| {
120121
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
121122
let val = S::command(DRIVER_NUM, command::GET_ROTATION, 0, 0).to_result();
123+
val?;
122124
loop {
123125
S::yield_wait();
124126
if let Some((_,)) = called.get() {
@@ -133,11 +135,12 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
133135
let called: Cell<Option<(u32,)>> = Cell::new(None);
134136
share::scope(|subscribe| {
135137
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
136-
let val = S::command(DRIVER_NUM, command::SET_ROTATION, rotation as u32, 0).to_result();
138+
S::command(DRIVER_NUM, command::SET_ROTATION, rotation as u32, 0)
139+
.to_result::<(), _>()?;
137140
loop {
138141
S::yield_wait();
139142
if let Some((_,)) = called.get() {
140-
return val;
143+
return Ok(());
141144
}
142145
}
143146
})
@@ -153,17 +156,17 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
153156
let called: Cell<Option<(u32,)>> = Cell::new(None);
154157
share::scope(|subscribe| {
155158
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
156-
let val = S::command(
159+
S::command(
157160
DRIVER_NUM,
158161
command::SET_RESOLUTION,
159162
width as u32,
160163
height as u32,
161164
)
162-
.to_result();
165+
.to_result::<(), _>()?;
163166
loop {
164167
S::yield_wait();
165168
if let Some((_,)) = called.get() {
166-
return val;
169+
return Ok(());
167170
}
168171
}
169172
})
@@ -179,12 +182,12 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
179182
let called: Cell<Option<(u32,)>> = Cell::new(None);
180183
share::scope(|subscribe| {
181184
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
182-
let val =
183-
S::command(DRIVER_NUM, command::SET_PIXEL_FORMAT, format as u32, 0).to_result();
185+
S::command(DRIVER_NUM, command::SET_PIXEL_FORMAT, format as u32, 0)
186+
.to_result::<(), _>()?;
184187
loop {
185188
S::yield_wait();
186189
if let Some((_,)) = called.get() {
187-
return val;
190+
return Ok(());
188191
}
189192
}
190193
})
@@ -197,11 +200,11 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
197200
let called: Cell<Option<(u32,)>> = Cell::new(None);
198201
share::scope(|subscribe| {
199202
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
200-
let val = S::command(DRIVER_NUM, command::SET_WRITE_FRAME, data1, data2).to_result();
203+
S::command(DRIVER_NUM, command::SET_WRITE_FRAME, data1, data2).to_result::<(), _>()?;
201204
loop {
202205
S::yield_wait();
203206
if let Some((_,)) = called.get() {
204-
return val;
207+
return Ok(());
205208
}
206209
}
207210
})
@@ -221,11 +224,11 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
221224
let (allow_ro, subscribe) = handle.split();
222225
S::allow_ro::<C, DRIVER_NUM, { allow_ro::WRITE_BUFFER_ID }>(allow_ro, s)?;
223226
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
224-
let val = S::command(DRIVER_NUM, command::WRITE, s.len() as u32, 0).to_result();
227+
S::command(DRIVER_NUM, command::WRITE, s.len() as u32, 0).to_result::<(), _>()?;
225228
loop {
226229
S::yield_wait();
227230
if let Some((_,)) = called.get() {
228-
return val;
231+
return Ok(());
229232
}
230233
}
231234
})
@@ -249,11 +252,11 @@ impl<S: Syscalls, C: Config> Screen<S, C> {
249252
let (allow_ro, subscribe) = handle.split();
250253
S::allow_ro::<C, DRIVER_NUM, { allow_ro::WRITE_BUFFER_ID }>(allow_ro, s)?;
251254
S::subscribe::<_, _, C, DRIVER_NUM, { subscribe::WRITE }>(subscribe, &called)?;
252-
let val = S::command(DRIVER_NUM, command::FILL, 0, 0).to_result();
255+
S::command(DRIVER_NUM, command::FILL, 0, 0).to_result::<(), _>()?;
253256
loop {
254257
S::yield_wait();
255258
if let Some((_,)) = called.get() {
256-
return val;
259+
return Ok(());
257260
}
258261
}
259262
})

unittest/src/fake/screen/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ impl crate::fake::SyscallDriver for Screen {
163163
}
164164

165165
GET_RESOLUTION_WIDTH_HEIGHT => {
166-
self.share_ref
167-
.schedule_upcall(0, (0, 0, 0))
168-
.expect("Unable to schedule upcall {}");
169166
if argument0 < self.screen_resolution_width_height.len() as u32 {
170167
if Option::is_some(&self.screen_setup) {
168+
self.share_ref
169+
.schedule_upcall(0, (0, 0, 0))
170+
.expect("Unable to schedule upcall {}");
171171
crate::command_return::success_2_u32(
172172
self.screen_resolution_width_height[argument0 as usize]
173173
.unwrap()
@@ -193,11 +193,11 @@ impl crate::fake::SyscallDriver for Screen {
193193
}
194194

195195
PIXEL_FORMAT => {
196-
self.share_ref
197-
.schedule_upcall(0, (0, 0, 0))
198-
.expect("Unable to schedule upcall {}");
199196
if argument0 < self.screen_pixel_format.len() as u32 {
200197
if Option::is_some(&self.screen_setup) {
198+
self.share_ref
199+
.schedule_upcall(0, (0, 0, 0))
200+
.expect("Unable to schedule upcall {}");
201201
crate::command_return::success_u32(
202202
self.screen_pixel_format[argument0 as usize] as u32,
203203
)
@@ -217,13 +217,13 @@ impl crate::fake::SyscallDriver for Screen {
217217
}
218218

219219
SET_ROTATION => {
220-
self.share_ref
221-
.schedule_upcall(0, (0, 0, 0))
222-
.expect("Unable to schedule upcall {}");
223220
if argument0 > 359 {
224221
command_return::failure(ErrorCode::Invalid)
225222
} else {
226223
self.rotation.set(argument0 as u16);
224+
self.share_ref
225+
.schedule_upcall(0, (0, 0, 0))
226+
.expect("Unable to schedule upcall {}");
227227
command_return::success()
228228
}
229229
}
@@ -245,11 +245,11 @@ impl crate::fake::SyscallDriver for Screen {
245245
GET_PIXEL_FORMAT => command_return::success_u32(self.pixel_format.get()),
246246

247247
SET_PIXEL_FORMAT => {
248-
self.share_ref
249-
.schedule_upcall(0, (0, 0, 0))
250-
.expect("Unable to schedule upcall {}");
251248
if argument0 < self.pixel_modes.unwrap() as u32 {
252249
self.pixel_format.set(argument0);
250+
self.share_ref
251+
.schedule_upcall(0, (0, 0, 0))
252+
.expect("Unable to schedule upcall {}");
253253
command_return::success()
254254
} else {
255255
command_return::failure(ErrorCode::Invalid)

unittest/src/fake/screen/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ fn command() {
6969
// Set and validate pixel format
7070
assert!(screen.command(SET_PIXEL_FORMAT, 1, 0).is_success());
7171

72+
// Ensure that an invalid pixel format returns an error.
73+
assert!(screen.command(SET_PIXEL_FORMAT, 99999, 0).is_failure());
74+
7275
// Kernel setup for screen and buffer simulation
7376
let kernel = fake::Kernel::new();
7477
kernel.add_driver(&screen);

0 commit comments

Comments
 (0)