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
63impl<'a> Encode<'a> for Request<'a> {}
64
65/// Response that the IO chip sends back to the main chip.
66#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
67pub enum Response<'a> {
68    Error(&'a str),
69
70    /// Confirmation for [`Request::NetStart`].
71    NetStarted,
72    /// Confirmation for [`Request::NetStop`].
73    NetStopped,
74    /// Response for [`Request::NetLocalAddr`].
75    NetLocalAddr([u8; 6]),
76    /// Confirmation for [`Request::NetAdvertise`].
77    NetAdvertised,
78    NetIncoming([u8; 6], &'a [u8]),
79    NetNoIncoming,
80    /// Confirmation for [`Request::NetSend`].
81    NetSent,
82    /// Response for [`Request::NetSendStatus`].
83    NetSendStatus(SendStatus),
84
85    /// Response for [`Request::ReadInput`].
86    ///
87    /// The first item is the touch coordinates on the pad (if any).
88    /// The second is serialized bitflags of pressed buttons.
89    Input(Option<(u16, u16)>, u8),
90
91    /// List of SSIDs of up to 6 available wifi Access Points.
92    ///
93    /// Includes 6 of the first detected APs.
94    /// So, it's not the top closest APs but the closer AP
95    /// the higher its chance to make it to the list.
96    ///
97    /// SSID is up to 30 bytes. 6 SSIDs take up to 180 bits.
98    /// The SPI packet size is limited to 255 bits
99    /// because we use a single byte to transfer the packet size.
100    WifiScan([&'a str; 6]),
101
102    /// The status of current connection to a wifi AP.
103    ///
104    /// For firefly-types and firefly-runtime, it's opaque.
105    /// It doesn't know which integer corresponds to which state.
106    /// The status is encoded in firefly-io and decoded in firefly-installer.
107    /// firefly-hal is also aware of status encoding, though, for the purpose
108    /// of mocking wifi on the hosted environment (emulator).
109    WifiStatus(u8),
110
111    /// Confirmation for [`Request::WifiConnect`].
112    ///
113    /// The connection request is async, so the response doesn't mean
114    /// that the device is actually connected to the AP (yet).
115    /// Use [`Request::WifiStatus`] to get the actual connection status.
116    WifiConnected,
117
118    /// Confirmation for [`Request::WifiDisconnect`].
119    WifiDisconnected,
120    /// Confirmation for [`Request::TcpConnect`].
121    TcpConnected,
122    /// Response for [`Request::TcpStatus`].
123    TcpStatus(u8),
124    /// Confirmation for [`Request::TcpSend`].
125    TcpSent,
126    /// Response for [`Request::TcpRecv`].
127    TcpChunk(&'a [u8]),
128    /// Confirmation for [`Request::TcpClose`].
129    TcpClosed,
130
131    /// Response for [`Request::FirmwareInfo`].
132    FirmwareInfo {
133        version: (u8, u8, u8),
134        partition: u8,
135    },
136}
137
138impl<'a> Encode<'a> for Response<'a> {}
139
140#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
141pub enum SendStatus {
142    /// Trying to send the message. The value is the number of attempts so far.
143    Sending(u8),
144    /// Message is delivered. The value is the number of attempts that it took.
145    Delivered(u8),
146    /// Message delivery failed.
147    Failed,
148    /// No messages were sent to the peer.
149    Empty,
150}