1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.

//! Provides userspace with access to distance sensor.
//!
//! Userspace Interface
//! -------------------
//!
//! ### `subscribe` System Call
//!
//! The `subscribe` system call supports the single `subscribe_number` zero,
//! which is used to provide a callback that will return back the result of
//! a distance sensor reading.
//! The `subscribe` call return codes indicate the following:
//!
//! * `Ok(())`: the callback has been successfully been configured.
//! * `ENOSUPPORT`: Invalid `subscribe_number`.
//! * `NOMEM`: No sufficient memory available.
//! * `INVAL`: Invalid address of the buffer or other error.
//!
//! ### `command` System Call
//!
//! The `command` system call supports one argument `cmd` which is used to
//! specify the specific operation. Currently, the following commands are supported:
//!
//! * `0`: check whether the driver exists.
//! * `1`: read the distance.
//! * `2`: get the minimum distance that the sensor can measure based on the datasheet, in millimeters.
//! * `3`: get the maximum distance that the sensor can measure based on the datasheet, in millimeters.
//!
//! The possible returns from the `command` system call indicate the following:
//!
//! * `Ok(())`: The operation has been successful.
//! * `NOACK`: No acknowledgment was received from the sensor during distance measurement.
//! * `INVAL`: Invalid measurement, such as when the object is out of range or no valid echo is received.
//! * `ENOSUPPORT`: Invalid `cmd`.
//! * `NOMEM`: Insufficient memory available.
//! * `INVAL`: Invalid address of the buffer or other error.
//!
//! The upcall has the following parameters:
//!
//! * `0`: Indicates a successful distance measurement, with the second parameter containing the distance, in millimeters.
//! * Non-zero: Indicates an error, with the first parameter containing the error code, and the second parameter being `0`.
//!
//! Components for the distance sensor.
//!
//! Usage
//! -----
//!
//! You need a device that provides the `hil::sensors::Distance` trait.
//! Here is an example of how to set up a distance sensor with the HC-SR04.
//!
//! ```rust,ignore
//! use components::hcsr04::HcSr04Component;

//! let trig_pin = peripherals.pins.get_pin(RPGpio::GPIO4);
//! let echo_pin = peripherals.pins.get_pin(RPGpio::GPIO5);
//!
//! let distance_sensor = components::hcsr04::HcSr04Component::new(
//!     mux_alarm,
//!     trig_pin,
//!     echo_pin
//! ).finalize(components::hcsr04_component_static!());
//!
//! distance_sensor.set_client(distance_sensor_client);
//! ```

use core::cell::Cell;

use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil;
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::{ErrorCode, ProcessId};

/// Syscall driver number.
use capsules_core::driver;
pub const DRIVER_NUM: usize = driver::NUM::Distance as usize;

#[derive(Default)]
pub struct App {
    subscribed: bool,
}

pub struct DistanceSensor<'a, T: hil::sensors::Distance<'a>> {
    driver: &'a T,
    apps: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
    busy: Cell<bool>,
}

impl<'a, T: hil::sensors::Distance<'a>> DistanceSensor<'a, T> {
    pub fn new(
        driver: &'a T,
        grant: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
    ) -> DistanceSensor<'a, T> {
        DistanceSensor {
            driver,
            apps: grant,
            busy: Cell::new(false),
        }
    }

    fn enqueue_command(&self, processid: ProcessId) -> CommandReturn {
        self.apps
            .enter(processid, |app, _| {
                // Unconditionally mark this client as subscribed so it will get
                // a callback when we get the distance reading.
                app.subscribed = true;

                // If we do not already have an ongoing read, start one now.
                if !self.busy.get() {
                    self.busy.set(true);
                    match self.driver.read_distance() {
                        Ok(()) => CommandReturn::success(),
                        Err(e) => {
                            self.busy.set(false);
                            app.subscribed = false;
                            CommandReturn::failure(e)
                        }
                    }
                } else {
                    // Just return success and we will get the upcall when the
                    // distance read is ready.
                    CommandReturn::success()
                }
            })
            .unwrap_or_else(|err| CommandReturn::failure(err.into()))
    }
}

impl<'a, T: hil::sensors::Distance<'a>> hil::sensors::DistanceClient for DistanceSensor<'a, T> {
    fn callback(&self, distance_val: Result<u32, ErrorCode>) {
        // We completed the operation so we clear the busy flag in case we get
        // another measurement request.
        self.busy.set(false);

        // Return the distance reading or an error to any waiting client.
        for cntr in self.apps.iter() {
            cntr.enter(|app, upcalls| {
                if app.subscribed {
                    app.subscribed = false; // Clear the subscribed flag.
                    match distance_val {
                        Ok(distance) => {
                            upcalls.schedule_upcall(0, (0, distance as usize, 0)).ok();
                        }
                        Err(e) => {
                            upcalls.schedule_upcall(0, (e as usize, 0, 0)).ok();
                        }
                    }
                }
            });
        }
    }
}

impl<'a, T: hil::sensors::Distance<'a>> SyscallDriver for DistanceSensor<'a, T> {
    fn command(
        &self,
        command_num: usize,
        _: usize,
        _: usize,
        processid: ProcessId,
    ) -> CommandReturn {
        match command_num {
            0 => {
                // Driver existence check.
                CommandReturn::success()
            }
            1 => {
                // Read distance.
                self.enqueue_command(processid)
            }
            2 => {
                // Get minimum distance.
                CommandReturn::success_u32(self.driver.get_minimum_distance())
            }
            3 => {
                // Get maximum distance.
                CommandReturn::success_u32(self.driver.get_maximum_distance())
            }
            _ => {
                // Command not supported.
                CommandReturn::failure(ErrorCode::NOSUPPORT)
            }
        }
    }

    fn allocate_grant(&self, process_id: ProcessId) -> Result<(), kernel::process::Error> {
        self.apps.enter(process_id, |_, _| {})
    }
}