blob: ac09120d18a7acdf7a40726f15bd30de6b1f6c09 [file] [log] [blame]
Alistair Francis0f7416e2020-09-10 18:54:51 -07001use crate::callback::CallbackSubscription;
2use crate::callback::Consumer;
3use crate::result::TockResult;
4use crate::syscalls;
5use core::marker::PhantomData;
6use libtock_core::shared_memory::SharedMemory;
7
8const DRIVER_NUMBER: usize = 0x40004;
9
10pub const RECV_BUFFER_SIZE: usize = 64;
11pub const SEND_BUFFER_SIZE: usize = 64;
12
13mod command_nr {
14 pub const SEND_DATA: usize = 0;
15 pub const ALLOW_RECEIVE: usize = 1;
16}
17
18mod subscribe_nr {
19 pub const SUBSCRIBE_CALLBACK: usize = 0;
20}
21
22mod allow_nr {
23 pub const RECV: usize = 0;
24 pub const SEND: usize = 1;
25}
26
27#[non_exhaustive]
28pub struct CtapDriverFactory;
29
30impl CtapDriverFactory {
31 pub fn init_driver(&mut self) -> TockResult<CtapDriver> {
32 let ctap = CtapDriver {
33 lifetime: PhantomData,
34 };
35 Ok(ctap)
36 }
37}
38
39struct CtapEventConsumer;
40
41impl<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
47pub struct CtapRecvBuffer {
48 buffer: [u8; RECV_BUFFER_SIZE],
49}
50
51impl Default for CtapRecvBuffer {
52 fn default() -> Self {
53 CtapRecvBuffer {
54 buffer: [0; RECV_BUFFER_SIZE],
55 }
56 }
57}
58
59pub struct CtapSendBuffer {
60 pub buffer: [u8; SEND_BUFFER_SIZE],
61}
62
63impl CtapSendBuffer {
64 pub fn new(buf: [u8; SEND_BUFFER_SIZE]) -> Self {
65 CtapSendBuffer { buffer: buf }
66 }
67}
68
69impl Default for CtapSendBuffer {
70 fn default() -> Self {
71 CtapSendBuffer {
72 buffer: [0; SEND_BUFFER_SIZE],
73 }
74 }
75}
76
77pub struct CtapDriver<'a> {
78 lifetime: PhantomData<&'a ()>,
79}
80
81impl<'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}