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