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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Provides userspace with access to proximity sensors.
//!
//! 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 proximity reading.
//! The `subscribe`call return codes indicate the following:
//!
//! * `Ok(())`: the callback been successfully been configured.
//! * `ENOSUPPORT`: Invalid allow_num.
//!
//!
//! ### `command` System Call
//!
//! The `command` system call support one argument `cmd` which is used to specify the specific
//! operation, currently the following cmd's are supported:
//!
//! * `0`: driver existence check
//! * `1`: read proximity
//! * `2`: read proximity on interrupt
//!
//!
//! The possible return from the 'command' system call indicates the following:
//!
//! * `Ok(())`:    The operation has been successful.
//! * `BUSY`:      The driver is busy.
//! * `ENOSUPPORT`: Invalid `cmd`.
//!
//! Usage
//! -----
//!
//! You need a device that provides the `hil::sensors::ProximityDriver` trait.
//! Here is an example of how to set up a proximity sensor with the apds9960 IC
//!
//! ```rust,ignore
//! # use kernel::static_init;
//!
//!let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
//!
//!let proximity = static_init!(
//!   capsules::proximity::ProximitySensor<'static>,
//!   capsules::proximity::ProximitySensor::new(apds9960 , board_kernel.create_grant(&grant_cap)));
//!
//!kernel::hil::sensors::ProximityDriver::set_client(apds9960, proximity);
//! ```

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::Proximity as usize;

#[derive(Default)]
pub struct App {
    subscribed: bool,
    enqueued_command_type: ProximityCommand,
    lower_proximity: u8,
    upper_proximity: u8,
}

#[derive(Clone, Copy, PartialEq, Default)]
pub enum ProximityCommand {
    ReadProximity = 1,
    ReadProximityOnInterrupt = 2,
    #[default]
    NoCommand = 3,
}

#[derive(Default)]
pub struct Thresholds {
    lower: u8,
    upper: u8,
}

pub struct ProximitySensor<'a> {
    driver: &'a dyn hil::sensors::ProximityDriver<'a>,
    apps: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
    command_running: Cell<ProximityCommand>,
}

impl<'a> ProximitySensor<'a> {
    pub fn new(
        driver: &'a dyn hil::sensors::ProximityDriver<'a>,
        grant: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
    ) -> ProximitySensor<'a> {
        ProximitySensor {
            driver,
            apps: grant,
            command_running: Cell::new(ProximityCommand::NoCommand),
        }
    }

    fn enqueue_command(
        &self,
        command: ProximityCommand,
        arg1: usize,
        arg2: usize,
        processid: ProcessId,
    ) -> CommandReturn {
        // Enqueue command by saving command type, args, processid within app struct in grant region
        self.apps
            .enter(processid, |app, _| {
                // Return busy if same app attempts to enqueue second command before first one is "callbacked"
                if app.subscribed {
                    return CommandReturn::failure(ErrorCode::BUSY);
                }

                if command == ProximityCommand::ReadProximityOnInterrupt {
                    app.lower_proximity = arg1 as u8;
                    app.upper_proximity = arg2 as u8;
                }

                app.subscribed = true; // enqueue
                app.enqueued_command_type = command;

                // If driver is currently processing a ReadProximityOnInterrupt command then we allow the current ReadProximityOnInterrupt command
                // to interrupt it.  With new thresholds set, we can account for all apps waiting on ReadProximityOnInterrupt with different thresholds set
                // to all receive a callback when appropriate.
                // Doing so ensures that the app issuing the current command can have it serviced without having to wait for the previous command to fire.
                if (self.command_running.get() == ProximityCommand::ReadProximityOnInterrupt)
                    && (command == ProximityCommand::ReadProximityOnInterrupt)
                {
                    let mut t: Thresholds = self.find_thresholds();
                    if t.lower < app.lower_proximity {
                        t.lower = app.lower_proximity;
                    }
                    if t.upper > app.upper_proximity {
                        t.upper = app.upper_proximity;
                    }
                    let _ = self.driver.read_proximity_on_interrupt(t.lower, t.upper);
                    self.command_running
                        .set(ProximityCommand::ReadProximityOnInterrupt);
                    return CommandReturn::success();
                }

                // If driver is currently processing a ReadProximityOnInterrupt command and current command is a ReadProximity then
                // then command the driver to interrupt the former and replace with the latter.  The former will still be in the queue as the app region in the
                // grant will have the `subscribed` boolean field set
                if (self.command_running.get() == ProximityCommand::ReadProximityOnInterrupt)
                    && (command == ProximityCommand::ReadProximity)
                {
                    let _ = self.driver.read_proximity();
                    self.command_running.set(ProximityCommand::ReadProximity);
                    return CommandReturn::success();
                }

                if self.command_running.get() == ProximityCommand::NoCommand {
                    match app.enqueued_command_type {
                        ProximityCommand::ReadProximity => {
                            let _ = self.driver.read_proximity();
                        }
                        ProximityCommand::ReadProximityOnInterrupt => {
                            let mut t: Thresholds = self.find_thresholds();
                            if t.lower < app.lower_proximity {
                                t.lower = app.lower_proximity;
                            }
                            if t.upper > app.upper_proximity {
                                t.upper = app.upper_proximity;
                            }
                            let _ = self.driver.read_proximity_on_interrupt(t.lower, t.upper);
                            self.command_running
                                .set(ProximityCommand::ReadProximityOnInterrupt);
                        }
                        ProximityCommand::NoCommand => {}
                    }
                }

                CommandReturn::success()
            })
            .unwrap_or_else(|err| CommandReturn::failure(err.into()))
    }

    fn run_next_command(&self) -> Result<(), ErrorCode> {
        // Find thresholds before entering any grant regions
        let t: Thresholds = self.find_thresholds();
        // Find and run another command
        for cntr in self.apps.iter() {
            let break_flag = cntr.enter(|app, _| {
                if app.subscribed {
                    // run it
                    match app.enqueued_command_type {
                        ProximityCommand::ReadProximity => {
                            let _ = self.driver.read_proximity();
                            self.command_running.set(ProximityCommand::ReadProximity);
                        }
                        ProximityCommand::ReadProximityOnInterrupt => {
                            let _ = self.driver.read_proximity_on_interrupt(t.lower, t.upper);
                            self.command_running
                                .set(ProximityCommand::ReadProximityOnInterrupt);
                        }
                        ProximityCommand::NoCommand => {}
                    }
                    true
                } else {
                    false
                }
            });

            if break_flag {
                break;
            }
        }

        Ok(())
    }

    fn find_thresholds(&self) -> Thresholds {
        // Get the lowest upper prox and highest lower prox of all enqueued apps waiting on a readproximityoninterrupt command
        // With the IC thresholds set to these two values, we ensure to never miss an interrupt-causing proximity value for any of the
        // apps waiting on a proximity interrupt
        // Interrupts for thresholds t1,t2 where t1 < t2 are triggered when proximity > t2 or proximity < t1.
        let mut highest_lower_proximity: u8 = 0;
        let mut lowest_upper_proximity: u8 = 255;

        for cntr in self.apps.iter() {
            cntr.try_enter(|app, _| {
                if (app.lower_proximity > highest_lower_proximity)
                    && app.subscribed
                    && app.enqueued_command_type == ProximityCommand::ReadProximityOnInterrupt
                {
                    highest_lower_proximity = app.lower_proximity;
                }
                if (app.upper_proximity < lowest_upper_proximity)
                    && app.subscribed
                    && app.enqueued_command_type == ProximityCommand::ReadProximityOnInterrupt
                {
                    lowest_upper_proximity = app.upper_proximity;
                }
            });
        }

        // return values
        Thresholds {
            lower: highest_lower_proximity,
            upper: lowest_upper_proximity,
        }
    }
}

impl hil::sensors::ProximityClient for ProximitySensor<'_> {
    fn callback(&self, temp_val: u8) {
        // Here we callback the values only to the apps which are relevant for the callback
        // We also dequeue any command for a callback so as to remove it from the wait list and add other commands to continue

        // Schedule callbacks for appropriate apps (any apps waiting for a proximity command)
        // For apps waiting on an interrupt, the reading is checked against the upper and lower thresholds of the app's enqueued command
        // to notice if this reading will fulfill the app's command.
        // The reading is also delivered to any apps waiting on an immediate reading.
        for cntr in self.apps.iter() {
            cntr.enter(|app, upcalls| {
                if app.subscribed {
                    if app.enqueued_command_type == ProximityCommand::ReadProximityOnInterrupt {
                        // Case: ReadProximityOnInterrupt
                        // Only callback to those apps which we expect would want to know about this threshold reading.
                        if (temp_val > app.upper_proximity) || (temp_val < app.lower_proximity) {
                            upcalls.schedule_upcall(0, (temp_val as usize, 0, 0)).ok();
                            app.subscribed = false; // dequeue
                        }
                    } else {
                        // Case: ReadProximity
                        // Upcall to all apps waiting on read_proximity.
                        upcalls.schedule_upcall(0, (temp_val as usize, 0, 0)).ok();
                        app.subscribed = false; // dequeue
                    }
                }
            });
        }

        // No command is temporarily being run here as we have performed the callback for our last command
        self.command_running.set(ProximityCommand::NoCommand);

        // When we are done with callback (one command) then find another waiting command to run and run it
        let _ = self.run_next_command();
    }
}

impl SyscallDriver for ProximitySensor<'_> {
    fn command(
        &self,
        command_num: usize,
        arg1: usize,
        arg2: usize,
        processid: ProcessId,
    ) -> CommandReturn {
        match command_num {
            // Driver existence check
            0 => CommandReturn::success(),

            // Instantaneous proximity measurement
            1 => self.enqueue_command(ProximityCommand::ReadProximity, arg1, arg2, processid),

            // Upcall occurs only after interrupt is fired
            2 => self.enqueue_command(
                ProximityCommand::ReadProximityOnInterrupt,
                arg1,
                arg2,
                processid,
            ),

            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
        }
    }

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