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