Alistair Francis | 0f7416e | 2020-09-10 18:54:51 -0700 | [diff] [blame] | 1 | use crate::callback::CallbackSubscription; |
| 2 | use crate::callback::Consumer; |
| 3 | use crate::result::TockResult; |
| 4 | use crate::syscalls; |
| 5 | use core::marker::PhantomData; |
| 6 | use libtock_core::shared_memory::SharedMemory; |
| 7 | |
| 8 | const DRIVER_NUMBER: usize = 0x40004; |
| 9 | |
| 10 | pub const RECV_BUFFER_SIZE: usize = 64; |
| 11 | pub const SEND_BUFFER_SIZE: usize = 64; |
| 12 | |
| 13 | mod command_nr { |
| 14 | pub const SEND_DATA: usize = 0; |
| 15 | pub const ALLOW_RECEIVE: usize = 1; |
| 16 | } |
| 17 | |
| 18 | mod subscribe_nr { |
| 19 | pub const SUBSCRIBE_CALLBACK: usize = 0; |
| 20 | } |
| 21 | |
| 22 | mod allow_nr { |
| 23 | pub const RECV: usize = 0; |
| 24 | pub const SEND: usize = 1; |
| 25 | } |
| 26 | |
| 27 | #[non_exhaustive] |
| 28 | pub struct CtapDriverFactory; |
| 29 | |
| 30 | impl CtapDriverFactory { |
| 31 | pub fn init_driver(&mut self) -> TockResult<CtapDriver> { |
| 32 | let ctap = CtapDriver { |
| 33 | lifetime: PhantomData, |
| 34 | }; |
| 35 | Ok(ctap) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | struct CtapEventConsumer; |
| 40 | |
| 41 | impl<CB: FnMut(usize, usize)> Consumer<CB> for CtapEventConsumer { |
| 42 | fn consume(callback: &mut CB, sent: usize, _: usize, _: usize) { |
| 43 | callback(sent, 0); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub struct CtapRecvBuffer { |
| 48 | buffer: [u8; RECV_BUFFER_SIZE], |
| 49 | } |
| 50 | |
| 51 | impl Default for CtapRecvBuffer { |
| 52 | fn default() -> Self { |
| 53 | CtapRecvBuffer { |
| 54 | buffer: [0; RECV_BUFFER_SIZE], |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub struct CtapSendBuffer { |
| 60 | pub buffer: [u8; SEND_BUFFER_SIZE], |
| 61 | } |
| 62 | |
| 63 | impl CtapSendBuffer { |
| 64 | pub fn new(buf: [u8; SEND_BUFFER_SIZE]) -> Self { |
| 65 | CtapSendBuffer { buffer: buf } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl Default for CtapSendBuffer { |
| 70 | fn default() -> Self { |
| 71 | CtapSendBuffer { |
| 72 | buffer: [0; SEND_BUFFER_SIZE], |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pub struct CtapDriver<'a> { |
| 78 | lifetime: PhantomData<&'a ()>, |
| 79 | } |
| 80 | |
| 81 | impl<'a> CtapDriver<'a> { |
| 82 | pub fn init_recv_buffer(&self, buffer: &'a mut CtapRecvBuffer) -> TockResult<SharedMemory> { |
| 83 | syscalls::allow(DRIVER_NUMBER, allow_nr::RECV, &mut buffer.buffer).map_err(Into::into) |
| 84 | } |
| 85 | |
| 86 | pub fn init_send_buffer(&self, buffer: &'a mut CtapSendBuffer) -> TockResult<SharedMemory> { |
| 87 | syscalls::allow(DRIVER_NUMBER, allow_nr::SEND, &mut buffer.buffer).map_err(Into::into) |
| 88 | } |
| 89 | |
| 90 | pub fn subscribe<CB: FnMut(usize, usize)>( |
| 91 | &self, |
| 92 | callback: &'a mut CB, |
| 93 | ) -> TockResult<CallbackSubscription> { |
| 94 | syscalls::subscribe::<CtapEventConsumer, _>( |
| 95 | DRIVER_NUMBER, |
| 96 | subscribe_nr::SUBSCRIBE_CALLBACK, |
| 97 | callback, |
| 98 | ) |
| 99 | .map_err(Into::into) |
| 100 | } |
| 101 | pub fn send_data(&self) -> TockResult<()> { |
| 102 | syscalls::command(DRIVER_NUMBER, command_nr::SEND_DATA, 0, 0)?; |
| 103 | Ok(()) |
| 104 | } |
| 105 | |
| 106 | pub fn allow_receive(&self) -> TockResult<()> { |
| 107 | syscalls::command(DRIVER_NUMBER, command_nr::ALLOW_RECEIVE, 0, 0)?; |
| 108 | Ok(()) |
| 109 | } |
| 110 | } |