use alloc::string::String;
use usb_device::bus::{UsbBus, UsbBusAllocator};
use usb_device::device::{StringDescriptors, UsbDevice, UsbDeviceBuilder, UsbVidPid};
use wasefire_sync::Once;
use crate::Error;
pub mod protocol;
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, PartialEq, Eq)]
pub enum Event {
Protocol(protocol::Event),
}
impl<B: crate::Api> From<Event> for crate::Event<B> {
fn from(event: Event) -> Self {
crate::Event::Platform(event)
}
}
pub trait Api: Send {
type Protocol: protocol::Api;
type Update: crate::transfer::Api;
fn serial() -> alloc::borrow::Cow<'static, [u8]>;
fn running_side() -> wasefire_common::platform::Side;
fn running_info() -> wasefire_protocol::platform::SideInfo0<'static>;
fn opposite_info() -> Result<wasefire_protocol::platform::SideInfo0<'static>, Error>;
fn reboot() -> Result<!, Error>;
}
pub type Protocol<B> = <super::Platform<B> as Api>::Protocol;
pub type Update<B> = <super::Platform<B> as Api>::Update;
pub fn usb_device<U: UsbBus, B: crate::Api>(usb_bus: &UsbBusAllocator<U>) -> UsbDevice<'_, U> {
static SERIAL: Once<String> = Once::new();
let serial = SERIAL.call_once(|| data_encoding::HEXLOWER.encode(&B::Platform::serial()));
UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x18d1, 0x0239))
.strings(&[StringDescriptors::new(usb_device::LangID::EN_US)
.manufacturer("Google Inc.")
.product("Wasefire")
.serial_number(serial)])
.unwrap()
.build()
}