Module kernel::hil::rng [] [src]

Interfaces for accessing a random number generator.

A random number generator produces a stream of random numbers, either from hardware or based on an initial seed. The RNG trait provides a simple, implementation agnostic interface for getting new random values.

The interface is designed to work well with random number generators that may not have values ready immediately. This is important when generating numbers from a low-bandwidth hardware random number generator or when the RNG is virtualized among many consumers.

Random numbers are yielded to the Client as an Iterator which only terminates when no more numbers are currently available. Clients can request more randmoness if needed and will be called again when more is available.

Example

The following example is a simple capsule that prints out a random number once a second using the Alarm and RNG traits.

struct RngTest<'a, A: Alarm + 'a> {
    rng: &'a RNG,
    alarm: &'a A
}

impl<'a, A: Alarm> RngTest<'a, A> {
    pub fn initialize(&self) {
        let interval = 1 * <A::Frequency>::frequency();
        let tics = self.alarm.now().wrapping_add(interval);
        self.alarm.set_alarm(tics);
    }
}

impl<'a, A: Alarm> time::Client for RngTest<'a, A> {
    fn fired(&self) {
        self.rng.get();
    }
}

impl<'a, A: Alarm> rng::Client for RngTest<'a, A> {
    fn randomness_available(&self, randomness: &mut Iterator<Item = u32>) -> rng::Continue {
        match randomness.next() {
            Some(random) => {
                println!("Rand {}", random);
                let interval = 1 * <A::Frequency>::frequency();
                let tics = self.alarm.now().wrapping_add(interval);
                self.alarm.set_alarm(tics);
                rng::Continue::Done
            },
            None => rng::Continue::More
        }
    }
}

Enums

Continue

Denotes whether the Client wants to be notified when More randomness is available or if they are Done

Traits

Client

An RNG client

RNG

Generic interface for a random number generator