Skip to content

Random Number Generator API#27

Open
alexandruCarp wants to merge 8 commits into
UPB-CS-OpenSourceUpstream:masterfrom
alexandruCarp:rng-api
Open

Random Number Generator API#27
alexandruCarp wants to merge 8 commits into
UPB-CS-OpenSourceUpstream:masterfrom
alexandruCarp:rng-api

Conversation

@alexandruCarp
Copy link
Copy Markdown

@alexandruCarp alexandruCarp commented Apr 10, 2023

Fixes #25.
Implemented the API and an example app which asks for some random numbers and prints them.
The app works fine on the microbit.
Added fake driver and tests.

@alexandruCarp alexandruCarp marked this pull request as ready for review April 17, 2023 10:03
Comment thread apis/rng/src/lib.rs Outdated
Comment thread apis/rng/src/lib.rs
@alexandruradovici alexandruradovici added the needs-rebase This pull request needs a rebase label Apr 24, 2023
@alexandruradovici
Copy link
Copy Markdown

Please rebase this.

@alexandruradovici
Copy link
Copy Markdown

@alexandruCarp any updates here?

@alexandruCarp
Copy link
Copy Markdown
Author

I don't think it is possible to also check the buffer size for the async generation, so I guess I will leave that with the behavior of the driver (which is to only write buf.len() bytes if more are requested). The sync function now returns SIZE error in this case. Is there anything else I should do?

@alexandruradovici alexandruradovici removed the needs-rebase This pull request needs a rebase label May 4, 2023
Comment thread apis/rng/src/lib.rs Outdated
Comment thread apis/rng/src/lib.rs Outdated
Comment thread apis/rng/src/lib.rs

/// Register a listener to be called when the random generation is finished
pub fn register_listener<'share>(
listener: &'share Cell<Option<(u32, u32)>>,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do that two u32 values from the listener mean?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one is always 0, and the second one represents the number of random bytes successfuly written in the buffer.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to

https://github.com/DanutAldea/libtock-rs/blob/b70eb0cbe1766a37585472235e77f755a7c12786/apis/ambient_light/src/lib.rs#L67-L73

I suggest adding a RandomDataListener, as the user wants to get a function called when data is available.

pub struct RandomDataListener<share etc, F: Fn(&'share [u8])>(pub F);

impl<F: Fn(u32)> Upcall<OneId<DRIVER_NUM, 0>> for IntensityListener<F> {
    fn upcall(&self, _arg0: u32, len: u32, _arg2: u32) {
        // unshare buffer
        f(buffer[0..len]);
        // share buffer back
    }
}

Add an unregister_listener function that returns Result<&'share [u8], ErrorCode> to unshare the buffer earlier.

@alexandruradovici
Copy link
Copy Markdown

I suggest writing a RandomListener similar to:

struct RandomListener<'a> {
    buffer: &'a mut [u8]
}

impl RandomListener {
   pub fn new (buffer: &'a mut [u8]) {...}
}

The generate_random function will get this listener as an argument an share it with the driver.

Comment thread apis/rng/Cargo.toml
Comment thread apis/rng/src/lib.rs

/// Register a listener to be called when the random generation is finished
pub fn register_listener<'share>(
listener: &'share Cell<Option<(u32, u32)>>,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to

https://github.com/DanutAldea/libtock-rs/blob/b70eb0cbe1766a37585472235e77f755a7c12786/apis/ambient_light/src/lib.rs#L67-L73

I suggest adding a RandomDataListener, as the user wants to get a function called when data is available.

pub struct RandomDataListener<share etc, F: Fn(&'share [u8])>(pub F);

impl<F: Fn(u32)> Upcall<OneId<DRIVER_NUM, 0>> for IntensityListener<F> {
    fn upcall(&self, _arg0: u32, len: u32, _arg2: u32) {
        // unshare buffer
        f(buffer[0..len]);
        // share buffer back
    }
}

Add an unregister_listener function that returns Result<&'share [u8], ErrorCode> to unshare the buffer earlier.

Comment thread apis/rng/src/lib.rs
}

/// Register a listener to be called when the random generation is finished
pub fn register_listener<'share>(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn register_listener<'share>(
pub fn register_listener<'share>(&'share mut buffer,
listener: &'share RandomDataListener,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to implement these changes but I don't know how could that unregister_listener function return the buffer (where would it get it from). I also don't understand how could I share the buffer back from the listener.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Implement the Random Number Generator API

2 participants