11use super :: * ;
22
3- use eh_02:: blocking:: spi:: Transfer ;
43use embedded_hal:: delay:: blocking:: DelayUs ;
54
6- pub const PARAMS_ARRAY_LEN : usize = 8 ;
5+ use heapless:: { String , Vec } ;
6+
7+ pub const MAX_NINA_PARAM_LENGTH : usize = 255 ;
78
89#[ repr( u8 ) ]
910#[ derive( Debug ) ]
1011pub enum NinaCommand {
11- StartClientTcp = 0x2Du8 ,
1212 GetFwVersion = 0x37u8 ,
13+ SetPassphrase = 0x11u8 ,
14+ GetConnStatus = 0x20u8 ,
15+ }
16+
17+ pub trait NinaParam {
18+ // Length of parameter in bytes
19+ type LengthAsBytes : IntoIterator < Item = u8 > ;
20+
21+ fn new ( data : & str ) -> Self ;
22+
23+ fn data ( & mut self ) -> & [ u8 ] ;
24+
25+ fn length_as_bytes ( & mut self ) -> Self :: LengthAsBytes ;
26+ }
27+
28+ // Used for single byte params
29+ pub struct NinaByteParam {
30+ length : u8 ,
31+ data : Vec < u8 , 1 > ,
32+ }
33+
34+ // Used for 2-byte params
35+ pub struct NinaWordParam {
36+ length : u8 ,
37+ data : Vec < u8 , 2 > ,
38+ }
39+
40+ // Used for params that are smaller than 255 bytes
41+ pub struct NinaSmallArrayParam {
42+ length : u8 ,
43+ data : Vec < u8 , MAX_NINA_PARAM_LENGTH > ,
44+ }
45+
46+ // Used for params that can be larger than 255 bytes up to MAX_NINA_PARAM_LENGTH
47+ pub struct NinaLargeArrayParam {
48+ length : u16 ,
49+ data : Vec < u8 , MAX_NINA_PARAM_LENGTH > ,
50+ }
51+
52+ impl NinaParam for NinaByteParam {
53+ type LengthAsBytes = [ u8 ; 1 ] ;
54+
55+ fn new ( data : & str ) -> Self {
56+ let data_as_bytes: Vec < u8 , 1 > = String :: from ( data) . into_bytes ( ) ;
57+ Self {
58+ length : data_as_bytes. len ( ) as u8 ,
59+ data : data_as_bytes,
60+ }
61+ }
62+
63+ fn data ( & mut self ) -> & [ u8 ] {
64+ self . data . as_slice ( )
65+ }
66+
67+ fn length_as_bytes ( & mut self ) -> Self :: LengthAsBytes {
68+ [ self . length as u8 ]
69+ }
70+ }
71+
72+ impl NinaParam for NinaWordParam {
73+ type LengthAsBytes = [ u8 ; 1 ] ;
74+
75+ fn new ( data : & str ) -> Self {
76+ let data_as_bytes: Vec < u8 , 2 > = String :: from ( data) . into_bytes ( ) ;
77+ Self {
78+ length : data_as_bytes. len ( ) as u8 ,
79+ data : data_as_bytes,
80+ }
81+ }
82+
83+ fn data ( & mut self ) -> & [ u8 ] {
84+ self . data . as_slice ( )
85+ }
86+
87+ fn length_as_bytes ( & mut self ) -> Self :: LengthAsBytes {
88+ [ self . length as u8 ]
89+ }
90+ }
91+
92+ impl NinaParam for NinaSmallArrayParam {
93+ type LengthAsBytes = [ u8 ; 1 ] ;
94+
95+ fn new ( data : & str ) -> Self {
96+ let data_as_bytes: Vec < u8 , MAX_NINA_PARAM_LENGTH > = String :: from ( data) . into_bytes ( ) ;
97+ Self {
98+ length : data_as_bytes. len ( ) as u8 ,
99+ data : data_as_bytes,
100+ }
101+ }
102+
103+ fn data ( & mut self ) -> & [ u8 ] {
104+ self . data . as_slice ( )
105+ }
106+
107+ fn length_as_bytes ( & mut self ) -> Self :: LengthAsBytes {
108+ [ self . length as u8 ]
109+ }
110+ }
111+
112+ impl NinaParam for NinaLargeArrayParam {
113+ type LengthAsBytes = [ u8 ; 2 ] ;
114+
115+ fn new ( data : & str ) -> Self {
116+ let data_as_bytes: Vec < u8 , MAX_NINA_PARAM_LENGTH > = String :: from ( data) . into_bytes ( ) ;
117+ Self {
118+ length : data_as_bytes. len ( ) as u16 ,
119+ data : data_as_bytes,
120+ }
121+ }
122+
123+ fn data ( & mut self ) -> & [ u8 ] {
124+ self . data . as_slice ( )
125+ }
126+
127+ fn length_as_bytes ( & mut self ) -> Self :: LengthAsBytes {
128+ [
129+ ( ( self . length & 0xff00 ) >> 8 ) as u8 ,
130+ ( self . length & 0xff ) as u8 ,
131+ ]
132+ }
13133}
14134
15135pub trait ProtocolInterface {
16136 fn init ( & mut self ) ;
17137 fn reset < D : DelayUs > ( & mut self , delay : & mut D ) ;
18138 fn get_fw_version ( & mut self ) -> Result < FirmwareVersion , self :: Error > ;
139+ fn set_passphrase ( & mut self , ssid : & str , passphrase : & str ) -> Result < ( ) , Error > ;
140+ fn get_conn_status ( & mut self ) -> Result < u8 , self :: Error > ;
19141
20142 fn send_cmd ( & mut self , cmd : NinaCommand , num_params : u8 ) -> Result < ( ) , self :: Error > ;
21143 fn wait_response_cmd (
22144 & mut self ,
23145 cmd : NinaCommand ,
24146 num_params : u8 ,
25- ) -> Result < [ u8 ; PARAMS_ARRAY_LEN ] , self :: Error > ;
147+ ) -> Result < [ u8 ; ARRAY_LENGTH_PLACEHOLDER ] , self :: Error > ;
26148 fn send_end_cmd ( & mut self ) -> Result < ( ) , self :: Error > ;
27149
28150 fn get_param ( & mut self ) -> Result < u8 , self :: Error > ;
29151 fn wait_for_byte ( & mut self , wait_byte : u8 ) -> Result < bool , self :: Error > ;
30152 fn check_start_cmd ( & mut self ) -> Result < bool , self :: Error > ;
31153 fn read_and_check_byte ( & mut self , check_byte : u8 ) -> Result < bool , self :: Error > ;
154+ fn send_param < P : NinaParam > ( & mut self , param : P ) -> Result < ( ) , self :: Error > ;
155+ fn send_param_length < P : NinaParam > ( & mut self , param : & mut P ) -> Result < ( ) , self :: Error > ;
156+ fn pad_to_multiple_of_4 ( & mut self , command_size : u16 ) ;
32157}
33158
34159#[ derive( Debug , Default ) ]
@@ -38,4 +163,3 @@ pub struct NinaProtocolHandler<B, C> {
38163 /// A EspControlPins instance
39164 pub control_pins : C ,
40165}
41-
0 commit comments