ctaphid_dispatch/
dispatch.rs

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
use core::sync::atomic::Ordering;

use crate::types::{InterchangeResponse, Message, Responder, MESSAGE_SIZE};

use ctaphid_app::{App, Command, Error};
use ref_swap::OptionRefSwap;
use trussed_core::InterruptFlag;

pub struct Dispatch<'pipe, 'interrupt> {
    responder: Responder<'pipe>,
    interrupt: Option<&'interrupt OptionRefSwap<'interrupt, InterruptFlag>>,
}

impl<'pipe> Dispatch<'pipe, '_> {
    pub fn new(responder: Responder<'pipe>) -> Self {
        Dispatch {
            responder,
            interrupt: None,
        }
    }
}

impl<'pipe, 'interrupt> Dispatch<'pipe, 'interrupt> {
    pub fn with_interrupt(
        responder: Responder<'pipe>,
        interrupt: Option<&'interrupt OptionRefSwap<'interrupt, InterruptFlag>>,
    ) -> Self {
        Dispatch {
            responder,
            interrupt,
        }
    }

    fn find_app<'a, 'b>(
        command: Command,
        apps: &'a mut [&'b mut dyn App<'interrupt, MESSAGE_SIZE>],
    ) -> Option<&'a mut &'b mut dyn App<'interrupt, MESSAGE_SIZE>> {
        apps.iter_mut()
            .find(|app| app.commands().contains(&command))
    }

    // // Using helper here to take potentially large stack burden off of call chain to application.
    // #[inline(never)]
    // fn reply_with_request_buffer(&mut self){
    //     let (_command, message) = self.responder.take_request().unwrap();
    //     let message = message.clone();
    //     self.responder.respond(&Ok(message)).expect("responder failed");
    // }

    // Using helper here to take potentially large stack burden off of call chain to application.
    #[inline(never)]
    fn reply_with_error(&mut self, error: Error) {
        self.reply_or_cancel(InterchangeResponse(Err(error)))
    }

    fn reply_or_cancel(&mut self, response: InterchangeResponse) {
        if self.responder.respond(response).is_ok() {
            return;
        }

        if self.responder.acknowledge_cancel().is_err() {
            panic!("Unexpected state: {:?}", self.responder.state());
        }
    }
    fn send_reply_or_cancel(&mut self) {
        if self.responder.send_response().is_ok() {
            return;
        }

        if self.responder.acknowledge_cancel().is_err() {
            panic!("Unexpected state: {:?}", self.responder.state());
        }
    }

    #[inline(never)]
    fn call_app(
        &mut self,
        app: &mut dyn App<'interrupt, MESSAGE_SIZE>,
        command: Command,
        request: &Message,
    ) {
        let response_buffer = self
            .responder
            .response_mut()
            .expect("App calls should only happen when a respose can be constructed")
            .0
            .as_mut()
            .unwrap();

        // Cancellation is best-effort, and not relevant for actual synchronisation, so relaxed is used
        let res =
            if let (Some(app_interrupt), Some(interrupt_ptr)) = (app.interrupt(), self.interrupt) {
                app_interrupt.set_working();
                interrupt_ptr.store(Some(app_interrupt), Ordering::Relaxed);
                let res = app.call(command, request, response_buffer);
                app_interrupt.set_idle();
                interrupt_ptr.store(None, Ordering::Relaxed);
                res
            } else {
                app.call(command, request, response_buffer)
            };

        info_now!("Got res: {:?}", res);
        if let Err(error) = res {
            self.reply_with_error(error)
        } else {
            self.send_reply_or_cancel()
        }
    }

    #[inline(never)]
    pub fn poll(&mut self, apps: &mut [&mut dyn App<'interrupt, MESSAGE_SIZE>]) -> bool {
        // We could call take_request directly, but for some reason this doubles stack usage.
        let mut message_buffer = Message::new();
        if let Ok((command, message)) = self.responder.request() {
            // info_now!("cmd: {}", u8::from(command));
            // info_now!("cmd: {:?}", command);

            message_buffer.extend_from_slice(message).unwrap();

            if let Some(app) = Self::find_app(*command, apps) {
                // match app.call(command, self.responder.response_mut().unwrap()) {
                self.call_app(*app, *command, &message_buffer);
            } else {
                self.reply_with_error(Error::InvalidCommand);
            }
            self.responder.state() == interchange::State::Responded
        } else {
            match self.responder.state() {
                interchange::State::Canceled => self.responder.acknowledge_cancel().is_ok(),
                interchange::State::Responded => true,
                _ => false,
            }
        }
    }
}