wasefire_board_api/platform/protocol.rs
1// Copyright 2024 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Platform protocol interface.
16
17use alloc::boxed::Box;
18
19use crate::Error;
20
21/// Request received.
22#[cfg_attr(feature = "defmt", derive(defmt::Format))]
23#[derive(Debug, PartialEq, Eq)]
24pub struct Event;
25
26impl<B: crate::Api> From<Event> for crate::Event<B> {
27 fn from(event: Event) -> Self {
28 super::Event::Protocol(event).into()
29 }
30}
31
32/// Platform protocol interface.
33pub trait Api {
34 /// Reads the last request, if any.
35 fn read() -> Result<Option<Box<[u8]>>, Error>;
36
37 /// Writes a response for the last request.
38 fn write(response: &[u8]) -> Result<(), Error>;
39
40 /// Starts accepting requests.
41 ///
42 /// Also triggers an event each time a request is received.
43 fn enable() -> Result<(), Error>;
44
45 /// Handles vendor-specific requests.
46 fn vendor(request: &[u8]) -> Result<Box<[u8]>, Error>;
47}