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