Skip to content

Commit

Permalink
refactor(debug)!: refactor handling of the exit code
Browse files Browse the repository at this point in the history
This makes it consistent with `std::process::ExitCode` and
`semihosting::process::ExitCode`.
  • Loading branch information
ROMemories committed Dec 12, 2024
1 parent 5a9119c commit 94d2c18
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 38 deletions.
4 changes: 2 additions & 2 deletions examples/device-metadata/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![no_std]
#![feature(used_with_arg)]

use ariel_os::debug::{exit, log::*, EXIT_SUCCESS};
use ariel_os::debug::{exit, log::*, ExitCode};

#[ariel_os::thread(autostart)]
fn main() {
Expand All @@ -17,5 +17,5 @@ fn main() {
info!("Device's first EUI-48 address: {}", eui48);
}

exit(EXIT_SUCCESS);
exit(ExitCode::SUCCESS);
}
4 changes: 2 additions & 2 deletions examples/hello-world-threading/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#![no_std]
#![feature(used_with_arg)]

use ariel_os::debug::{exit, log::*};
use ariel_os::debug::{exit, log::*, ExitCode};

#[ariel_os::thread(autostart)]
fn main() {
info!("Hello World!");

exit(Ok(()));
exit(ExitCode::SUCCESS);
}
4 changes: 2 additions & 2 deletions examples/hello-world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#![feature(impl_trait_in_assoc_type)]
#![feature(used_with_arg)]

use ariel_os::debug::{exit, log::*};
use ariel_os::debug::{exit, log::*, ExitCode};

#[ariel_os::task(autostart)]
async fn main() {
info!("Hello World!");

exit(Ok(()));
exit(ExitCode::SUCCESS);
}
5 changes: 3 additions & 2 deletions examples/storage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ariel_os::debug::{
exit,
log::{defmt, info},
ExitCode,
};

// Imports for using [`ariel_os::storage`]
Expand Down Expand Up @@ -35,7 +36,7 @@ async fn main() {

if value > 10 {
info!("counter value > 10, aborting test to safe flash cycles.");
exit(Ok(()));
exit(ExitCode::SUCCESS);
}

storage::insert("counter", value + 1).await.unwrap();
Expand Down Expand Up @@ -110,5 +111,5 @@ async fn main() {
}
info!("bye from storage test!");

exit(Ok(()));
exit(ExitCode::SUCCESS);
}
4 changes: 2 additions & 2 deletions examples/thread-async-interop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal}

use ariel_os::{
asynch::{blocker, spawner},
debug::{exit, log::*},
debug::{exit, log::*, ExitCode},
time::{Duration, Instant, Timer},
};

Expand Down Expand Up @@ -47,5 +47,5 @@ fn main() {

info!("main(): all good, exiting.");

exit(Ok(()));
exit(ExitCode::SUCCESS);
}
4 changes: 2 additions & 2 deletions examples/threading-event/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![no_std]
#![feature(used_with_arg)]

use ariel_os::debug::{log::*, EXIT_SUCCESS};
use ariel_os::debug::{log::*, ExitCode};
use ariel_os::thread::{sync::Event, ThreadId};

static EVENT: Event = Event::new();
Expand All @@ -16,7 +16,7 @@ fn waiter() {

if my_id == ThreadId::new(0) {
info!("All five threads should have reported \"Done.\". exiting.");
ariel_os::debug::exit(EXIT_SUCCESS);
ariel_os::debug::exit(ExitCode::SUCCESS);
}
}

Expand Down
34 changes: 26 additions & 8 deletions src/ariel-os-debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,35 @@ compile_error!(
r#"feature "debug-console" enabled but no backend. Select feature "rtt-target" or feature "esp-println"."#
);

pub const EXIT_SUCCESS: Result<(), ()> = Ok(());
pub const EXIT_FAILURE: Result<(), ()> = Err(());
pub fn exit(code: Result<(), ()>) {
let code = match code {
EXIT_FAILURE => 1,
EXIT_SUCCESS => 0,
};
/// Represents the exit code of a debug output.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ExitCode {
#[doc(hidden)]
Success,
#[doc(hidden)]
Failure,
}

impl ExitCode {
/// The [`ExitCode`] for success.
pub const SUCCESS: Self = Self::Success;
/// The [`ExitCode`] for failure.
pub const FAILURE: Self = Self::Failure;

#[allow(dead_code, reason = "not always used due to conditional compilation")]
fn to_semihosting_code(self) -> i32 {
match self {
Self::Success => 0,
Self::Failure => 1,
}
}
}

pub fn exit(code: ExitCode) {
loop {
#[cfg(feature = "semihosting")]
semihosting::process::exit(code);
semihosting::process::exit(code.to_semihosting_code());

#[cfg(not(feature = "semihosting"))]
{
let _ = code;
Expand Down
2 changes: 1 addition & 1 deletion src/ariel-os-rt/src/cortexm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ unsafe fn DefaultHandler(_irqn: i16) {
#[cfg(not(feature = "silent-panic"))]
{
ariel_os_debug::log::debug!("IRQn = {}", _irqn);
ariel_os_debug::exit(ariel_os_debug::EXIT_FAILURE);
ariel_os_debug::exit(ariel_os_debug::ExitCode::FAILURE);
}
#[allow(clippy::empty_loop)]
loop {}
Expand Down
2 changes: 1 addition & 1 deletion src/ariel-os-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
#[cfg(not(feature = "silent-panic"))]
{
ariel_os_debug::println!("panic: {}\n", _info);
ariel_os_debug::exit(ariel_os_debug::EXIT_FAILURE);
ariel_os_debug::exit(ariel_os_debug::ExitCode::FAILURE);
}
#[allow(clippy::empty_loop)]
loop {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use cortex_m_rt::entry;
use cortex_m_semihosting::{
debug::{self, EXIT_SUCCESS},
debug::{self, ExitCode},
hprintln as println,
};

Expand Down Expand Up @@ -80,7 +80,7 @@ fn main() -> ! {

println!("main() shouldn't be here");
// exit via semihosting call
debug::exit(EXIT_SUCCESS);
debug::exit(ExitCode::SUCCESS);

// the cortex_m_rt `entry` macro requires `main()` to never return
loop {}
Expand Down
4 changes: 2 additions & 2 deletions src/ariel-os-threads/et-tests/src/bin/thread_flags_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use cortex_m_rt::entry;
use cortex_m_semihosting::{
debug::{self, EXIT_SUCCESS},
debug::{self, ExitCode},
hprintln as println,
};

Expand Down Expand Up @@ -56,7 +56,7 @@ fn main() -> ! {

println!("main() shouldn't be here");
// exit via semihosting call
debug::exit(EXIT_SUCCESS);
debug::exit(ExitCode::SUCCESS);

// the cortex_m_rt `entry` macro requires `main()` to never return
loop {}
Expand Down
4 changes: 2 additions & 2 deletions src/ariel-os-threads/et-tests/src/bin/twothread_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use cortex_m_rt::entry;
use cortex_m_semihosting::{
debug::{self, EXIT_SUCCESS},
debug::{self, ExitCode},
hprintln as println,
};

Expand Down Expand Up @@ -72,7 +72,7 @@ fn main() -> ! {
unsafe { start_threading() };

// exit via semihosting call
debug::exit(EXIT_SUCCESS);
debug::exit(ExitCode::SUCCESS);

// the cortex_m_rt `entry` macro requires `main()` to never return
loop {}
Expand Down
4 changes: 2 additions & 2 deletions src/ariel-os-threads/et-tests/src/bin/twothread_locktest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use cortex_m_rt::entry;
use cortex_m_semihosting::{
debug::{self, EXIT_SUCCESS},
debug::{self, ExitCode},
hprintln as println,
};

Expand Down Expand Up @@ -60,7 +60,7 @@ fn main() -> ! {

println!("main() shouldn't be here");
// exit via semihosting call
debug::exit(EXIT_SUCCESS);
debug::exit(ExitCode::SUCCESS);

// the cortex_m_rt `entry` macro requires `main()` to never return
loop {}
Expand Down
4 changes: 2 additions & 2 deletions src/ariel-os-threads/et-tests/src/bin/twothreads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use cortex_m_rt::entry;
use cortex_m_semihosting::{
debug::{self, EXIT_SUCCESS},
debug::{self, ExitCode},
hprintln as println,
};

Expand Down Expand Up @@ -35,7 +35,7 @@ fn main() -> ! {

println!("main() shouldn't be here");
// exit via semihosting call
debug::exit(EXIT_SUCCESS);
debug::exit(ExitCode::SUCCESS);

// the cortex_m_rt `entry` macro requires `main()` to never return
loop {}
Expand Down
4 changes: 2 additions & 2 deletions tests/i2c-controller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ariel_os::{
debug::{
exit,
log::{debug, info},
EXIT_SUCCESS,
ExitCode,
},
hal,
i2c::controller::{highest_freq_in, I2cDevice, Kilohertz},
Expand Down Expand Up @@ -57,5 +57,5 @@ async fn main(peripherals: pins::Peripherals) {

info!("Test passed!");

exit(EXIT_SUCCESS);
exit(ExitCode::SUCCESS);
}
4 changes: 2 additions & 2 deletions tests/spi-loopback/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ariel_os::{
debug::{
exit,
log::{debug, info},
EXIT_SUCCESS,
ExitCode,
},
gpio, hal,
spi::{
Expand Down Expand Up @@ -63,5 +63,5 @@ async fn main(peripherals: pins::Peripherals) {

info!("Test passed!");

exit(EXIT_SUCCESS);
exit(ExitCode::SUCCESS);
}
4 changes: 2 additions & 2 deletions tests/spi-main/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ariel_os::{
debug::{
exit,
log::{debug, info},
EXIT_SUCCESS,
ExitCode,
},
gpio, hal,
spi::{
Expand Down Expand Up @@ -73,7 +73,7 @@ async fn main(peripherals: pins::Peripherals) {

info!("Test passed!");

exit(EXIT_SUCCESS);
exit(ExitCode::SUCCESS);
}

fn get_spi_read_command(addr: u8) -> u8 {
Expand Down

0 comments on commit 94d2c18

Please sign in to comment.