Skip to main content

firefly_types/
spi.rs

1use crate::encode::Encode;
2use serde::{Deserialize, Serialize};
3
4/// Request that the main chip sends to the IO chip.
5#[derive(Serialize, Deserialize, Clone, Debug)]
6pub enum Request<'a> {
7    /// Start listening for messages.
8    NetStart,
9    /// Stop accepting new messages and connections.
10    NetStop,
11    /// Get MAC address of this device's IO chip.
12    NetLocalAddr,
13    /// Broadcast advertisement message.
14    NetAdvertise,
15    /// Read an incoming message (if any) from the IO chip.
16    NetRecv,
17    /// Send an outgoing message to the IO chip.
18    NetSend([u8; 6], &'a [u8]),
19    /// Get send status of the previous message for the peer.
20    NetSendStatus([u8; 6]),
21    /// Get the latest touchpad and buttons inputs.
22    ReadInput,
23
24    /// Scan the air for available non-hidden wifi access points.
25    WifiScan,
26
27    /// Connect to an access point using the given SSID and password.
28    ///
29    /// Async. Check [`Request::WifiStatus`] to see if the device is actually connected.
30    WifiConnect(&'a str, &'a str),
31
32    /// Get the current connection status to an AP.
33    ///
34    /// Since [`Request::WifiConnect`] is async, make sure to account for races.
35    /// For instance, shortly after requesting a connect, the status might
36    /// still indicate the status of the previous connection.
37    WifiStatus,
38
39    /// Disconnect from the currently connected wifi access point.
40    WifiDisconnect,
41
42    /// Connect to the TCP server with the given IP address and port number.
43    ///
44    /// There can be only one open TCP connection at a time.
45    TcpConnect(u32, u16),
46
47    /// Fetch the state of the currently open TCP connection.
48    TcpStatus,
49
50    /// Send the given bytes into the currently open TCP connection.
51    TcpSend(&'a [u8]),
52
53    /// Read a bytes chunk from the currently open TCP connection.
54    TcpRecv,
55
56    /// Close the currently open TCP connection.
57    TcpClose,
58
59    /// Get information about the firmware running on the IO chip.
60    FirmwareInfo,
61
62    FlashWrite(u32, &'a [u8]),
63
64    /// Switch firmware to use the given partition.
65    PartitionSwitch(u8),
66}
67
68impl<'a> Encode<'a> for Request<'a> {}
69
70/// Response that the IO chip sends back to the main chip.
71#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
72pub enum Response<'a> {
73    Error(&'a str),
74
75    /// Confirmation for [`Request::NetStart`].
76    NetStarted,
77    /// Confirmation for [`Request::NetStop`].
78    NetStopped,
79    /// Response for [`Request::NetLocalAddr`].
80    NetLocalAddr([u8; 6]),
81    /// Confirmation for [`Request::NetAdvertise`].
82    NetAdvertised,
83    NetIncoming([u8; 6], &'a [u8]),
84    NetNoIncoming,
85    /// Confirmation for [`Request::NetSend`].
86    NetSent,
87    /// Response for [`Request::NetSendStatus`].
88    NetSendStatus(SendStatus),
89
90    /// Response for [`Request::ReadInput`].
91    ///
92    /// The first item is the touch coordinates on the pad (if any).
93    /// The second is serialized bitflags of pressed buttons.
94    Input(Option<(u16, u16)>, u8),
95
96    /// List of SSIDs of up to 6 available wifi Access Points.
97    ///
98    /// Includes 6 of the first detected APs.
99    /// So, it's not the top closest APs but the closer AP
100    /// the higher its chance to make it to the list.
101    ///
102    /// SSID is up to 30 bytes. 6 SSIDs take up to 180 bits.
103    /// The SPI packet size is limited to 255 bits
104    /// because we use a single byte to transfer the packet size.
105    WifiScan([&'a str; 6]),
106
107    /// The status of current connection to a wifi AP.
108    ///
109    /// For firefly-types and firefly-runtime, it's opaque.
110    /// It doesn't know which integer corresponds to which state.
111    /// The status is encoded in firefly-io and decoded in firefly-installer.
112    /// firefly-hal is also aware of status encoding, though, for the purpose
113    /// of mocking wifi on the hosted environment (emulator).
114    WifiStatus(u8),
115
116    /// Confirmation for [`Request::WifiConnect`].
117    ///
118    /// The connection request is async, so the response doesn't mean
119    /// that the device is actually connected to the AP (yet).
120    /// Use [`Request::WifiStatus`] to get the actual connection status.
121    WifiConnected,
122
123    /// Confirmation for [`Request::WifiDisconnect`].
124    WifiDisconnected,
125    /// Confirmation for [`Request::TcpConnect`].
126    TcpConnected,
127    /// Response for [`Request::TcpStatus`].
128    TcpStatus(u8),
129    /// Confirmation for [`Request::TcpSend`].
130    TcpSent,
131    /// Response for [`Request::TcpRecv`].
132    TcpChunk(&'a [u8]),
133    /// Confirmation for [`Request::TcpClose`].
134    TcpClosed,
135
136    /// Response for [`Request::FirmwareInfo`].
137    FirmwareInfo {
138        version: (u8, u8, u8),
139        partition: u8,
140    },
141    /// Response for [`Request::FlashWrite`].
142    FlashWritten,
143    /// Response for [`Request::PartitionSwitch`].
144    PartitionSwitched,
145}
146
147impl<'a> Encode<'a> for Response<'a> {}
148
149#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
150pub enum SendStatus {
151    /// Trying to send the message. The value is the number of attempts so far.
152    Sending(u8),
153    /// Message is delivered. The value is the number of attempts that it took.
154    Delivered(u8),
155    /// Message delivery failed.
156    Failed,
157    /// No messages were sent to the peer.
158    Empty,
159}