Skip to content

Commit db19952

Browse files
authored
Merge pull request #29 from Jim-Hodapp-Coaching/improve_nina_protocol_function_architecture_2
Improve NINA protocol function architecture 2
2 parents 6e26db5 + b9246df commit db19952

File tree

4 files changed

+181
-89
lines changed

4 files changed

+181
-89
lines changed

cross/join/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ fn main() -> ! {
111111
// ACK on pin x (GPIO10)
112112
ack: pins.gpio10.into_mode::<hal::gpio::FloatingInput>(),
113113
};
114-
let passphrase: &str = "Passphrase";
115114

116115
let mut wifi = esp32_wroom_rp::wifi::Wifi::init(spi, esp_pins, &mut delay).unwrap();
117116
let result = wifi.join(SSID, PASSPHRASE);

esp32-wroom-rp/src/protocol.rs

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub mod operation;
2+
13
use super::*;
24

35
use embedded_hal::blocking::delay::DelayMs;
@@ -7,7 +9,7 @@ use heapless::{String, Vec};
79
pub const MAX_NINA_PARAM_LENGTH: usize = 255;
810

911
#[repr(u8)]
10-
#[derive(Debug)]
12+
#[derive(Copy, Clone, Debug)]
1113
pub enum NinaCommand {
1214
GetFwVersion = 0x37u8,
1315
SetPassphrase = 0x11u8,
@@ -20,11 +22,43 @@ pub trait NinaParam {
2022
type LengthAsBytes: IntoIterator<Item = u8>;
2123

2224
fn new(data: &str) -> Self;
25+
2326
fn from_bytes(bytes: &[u8]) -> Self;
2427

25-
fn data(&mut self) -> &[u8];
28+
fn data(&self) -> &[u8];
29+
30+
fn length_as_bytes(&self) -> Self::LengthAsBytes;
31+
32+
fn length(&self) -> u16;
33+
}
34+
35+
// Used for Nina protocol commands with no parameters
36+
pub struct NinaNoParams {
37+
_placeholder: u8,
38+
}
39+
40+
impl NinaParam for NinaNoParams {
41+
type LengthAsBytes = [u8; 0];
42+
43+
fn new(_data: &str) -> Self {
44+
Self { _placeholder: 0 }
45+
}
46+
47+
fn from_bytes(_bytes: &[u8]) -> Self {
48+
Self { _placeholder: 0 }
49+
}
50+
51+
fn data(&self) -> &[u8] {
52+
&[0u8]
53+
}
54+
55+
fn length_as_bytes(&self) -> Self::LengthAsBytes {
56+
[]
57+
}
2658

27-
fn length_as_bytes(&mut self) -> Self::LengthAsBytes;
59+
fn length(&self) -> u16 {
60+
0u16
61+
}
2862
}
2963

3064
// Used for single byte params
@@ -71,11 +105,15 @@ impl NinaParam for NinaByteParam {
71105
}
72106
}
73107

74-
fn data(&mut self) -> &[u8] {
108+
fn data(&self) -> &[u8] {
75109
self.data.as_slice()
76110
}
77111

78-
fn length_as_bytes(&mut self) -> Self::LengthAsBytes {
112+
fn length(&self) -> u16 {
113+
self.length as u16
114+
}
115+
116+
fn length_as_bytes(&self) -> Self::LengthAsBytes {
79117
[self.length as u8]
80118
}
81119
}
@@ -100,11 +138,15 @@ impl NinaParam for NinaWordParam {
100138
}
101139
}
102140

103-
fn data(&mut self) -> &[u8] {
141+
fn data(&self) -> &[u8] {
104142
self.data.as_slice()
105143
}
106144

107-
fn length_as_bytes(&mut self) -> Self::LengthAsBytes {
145+
fn length(&self) -> u16 {
146+
self.length as u16
147+
}
148+
149+
fn length_as_bytes(&self) -> Self::LengthAsBytes {
108150
[self.length as u8]
109151
}
110152
}
@@ -129,11 +171,15 @@ impl NinaParam for NinaSmallArrayParam {
129171
}
130172
}
131173

132-
fn data(&mut self) -> &[u8] {
174+
fn data(&self) -> &[u8] {
133175
self.data.as_slice()
134176
}
135177

136-
fn length_as_bytes(&mut self) -> Self::LengthAsBytes {
178+
fn length(&self) -> u16 {
179+
self.length as u16
180+
}
181+
182+
fn length_as_bytes(&self) -> Self::LengthAsBytes {
137183
[self.length as u8]
138184
}
139185
}
@@ -158,11 +204,15 @@ impl NinaParam for NinaLargeArrayParam {
158204
}
159205
}
160206

161-
fn data(&mut self) -> &[u8] {
207+
fn data(&self) -> &[u8] {
162208
self.data.as_slice()
163209
}
164210

165-
fn length_as_bytes(&mut self) -> Self::LengthAsBytes {
211+
fn length(&self) -> u16 {
212+
self.length
213+
}
214+
215+
fn length_as_bytes(&self) -> Self::LengthAsBytes {
166216
[
167217
((self.length & 0xff00) >> 8) as u8,
168218
(self.length & 0xff) as u8,
@@ -177,22 +227,6 @@ pub trait ProtocolInterface {
177227
fn set_passphrase(&mut self, ssid: &str, passphrase: &str) -> Result<(), Error>;
178228
fn disconnect(&mut self) -> Result<(), self::Error>;
179229
fn get_conn_status(&mut self) -> Result<u8, self::Error>;
180-
181-
fn send_cmd(&mut self, cmd: NinaCommand, num_params: u8) -> Result<(), self::Error>;
182-
fn wait_response_cmd(
183-
&mut self,
184-
cmd: NinaCommand,
185-
num_params: u8,
186-
) -> Result<[u8; ARRAY_LENGTH_PLACEHOLDER], self::Error>;
187-
fn send_end_cmd(&mut self) -> Result<(), self::Error>;
188-
189-
fn get_byte(&mut self) -> Result<u8, self::Error>;
190-
fn wait_for_byte(&mut self, wait_byte: u8) -> Result<bool, self::Error>;
191-
fn check_start_cmd(&mut self) -> Result<bool, self::Error>;
192-
fn read_and_check_byte(&mut self, check_byte: u8) -> Result<bool, self::Error>;
193-
fn send_param<P: NinaParam>(&mut self, param: P) -> Result<(), self::Error>;
194-
fn send_param_length<P: NinaParam>(&mut self, param: &mut P) -> Result<(), self::Error>;
195-
fn pad_to_multiple_of_4(&mut self, command_size: u16);
196230
}
197231

198232
#[derive(Debug, Default)]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use super::protocol::NinaCommand;
2+
3+
use heapless::Vec;
4+
const MAX_NUMBER_OF_PARAMS: usize = 4;
5+
6+
// Encapsulates all information needed to execute commands against Nina Firmware.
7+
// along with user supplied data. Ex. SSID, passphrase, etc.
8+
9+
pub struct Operation<P> {
10+
pub params: Vec<P, MAX_NUMBER_OF_PARAMS>,
11+
pub command: NinaCommand,
12+
pub has_params: bool,
13+
pub number_of_params_to_receive: u8,
14+
}
15+
16+
impl<P> Operation<P> {
17+
// Initializes new Operation instance.
18+
//
19+
// `has_params` defaults to `true`
20+
pub fn new(command: NinaCommand, number_of_params_to_receive: u8) -> Self {
21+
Self {
22+
params: Vec::new(),
23+
command: command,
24+
has_params: true,
25+
number_of_params_to_receive: number_of_params_to_receive,
26+
}
27+
}
28+
29+
// Pushes a new param into the internal `params` Vector which
30+
// builds up an internal byte stream representing one Nina command
31+
// on the data bus.
32+
pub fn param(mut self, param: P) -> Self {
33+
self.params.push(param);
34+
self
35+
}
36+
37+
// Used for denoting an Operation where no params are provided.
38+
//
39+
// Sets `has_params` to `false`
40+
pub fn with_no_params(mut self, no_param: P) -> Self {
41+
self.params.push(no_param);
42+
self.has_params = false;
43+
self
44+
}
45+
}

0 commit comments

Comments
 (0)