@@ -7,6 +7,7 @@ extern crate env_logger;
7
7
#[ macro_use]
8
8
extern crate error_chain;
9
9
extern crate heapless;
10
+ extern crate itm;
10
11
extern crate libc;
11
12
#[ macro_use]
12
13
extern crate log;
@@ -15,6 +16,7 @@ extern crate ref_slice;
15
16
16
17
use clap:: { Arg , App , ArgMatches } ;
17
18
use heapless:: Vec as HVec ;
19
+ use itm:: packet:: { self , Packet , UserData } ;
18
20
use log:: { LogRecord , LogLevelFilter } ;
19
21
use std:: fs:: File ;
20
22
use std:: io:: { Read , Write } ;
@@ -38,21 +40,6 @@ mod errors {
38
40
}
39
41
}
40
42
41
- pub const MAX_PAYLOAD_SIZE : usize = 4 ;
42
-
43
- // TODO: Probably add a .kind field and Kind enum when we need to handle more
44
- // kinds of packets.
45
- struct Packet {
46
- // The header byte received for this packet.
47
- pub header : u8 ,
48
-
49
- /// Data in this packet.
50
- pub payload : HVec < u8 , [ u8 ; MAX_PAYLOAD_SIZE ] > ,
51
-
52
- /// Stimulus port this packet was sent from.
53
- pub port : u8 ,
54
- }
55
-
56
43
fn main ( ) {
57
44
// Initialise logging.
58
45
env_logger:: LogBuilder :: new ( )
@@ -138,8 +125,11 @@ fn run() -> Result<()> {
138
125
let p = read_packet ( & mut stream) ;
139
126
match p {
140
127
Ok ( p) => {
141
- if p. port == port {
142
- stdout. write_all ( & p. payload ) ?;
128
+ match p. kind {
129
+ packet:: Kind :: UserData ( ref ud) if ud. port == port => {
130
+ stdout. write_all ( & ud. payload ) ?;
131
+ }
132
+ _ => ( ) ,
143
133
}
144
134
}
145
135
Err ( e @ Error ( ErrorKind :: UnknownHeader ( _) , _) ) => {
@@ -189,26 +179,29 @@ fn read_packet(input: &mut Read) -> Result<Packet> {
189
179
let mut header = [ 0 ; 1 ] ;
190
180
input. read_exact ( & mut header) ?;
191
181
let header = header[ 0 ] ;
192
- let mut packet = Packet {
193
- header : header,
194
- payload : HVec :: new ( ) ,
195
- port : header >> 3 ,
196
- } ;
197
182
match header & 0b111 {
198
- 0b01 |0b10 |0b11 => {
199
- // Data packet.
183
+ 0b001 |0b010 |0b011 => {
184
+ // User data packet.
185
+ let mut ud = UserData {
186
+ payload : HVec :: new ( ) ,
187
+ port : header >> 3 ,
188
+ } ;
189
+
200
190
let payload_size =
201
191
match header & 0b11 {
202
192
0b01 => 1 ,
203
193
0b10 => 2 ,
204
194
0b11 => 4 ,
205
- _ => return Err ( Error :: from (
206
- ErrorKind :: UnknownHeader ( header) ) ) ,
195
+ _ => unreachable ! ( ) , // Contradicts match on last 3 bits.
207
196
} ;
208
- packet. payload . resize_default ( payload_size)
209
- . expect ( "payload_size <= packet.payload.capacity" ) ;
210
- input. read_exact ( & mut * packet. payload ) ?;
211
- Ok ( packet)
197
+ ud. payload . resize_default ( payload_size)
198
+ . expect ( "payload_size <= payload.capacity" ) ;
199
+ input. read_exact ( & mut * ud. payload ) ?;
200
+
201
+ Ok ( Packet {
202
+ header : header,
203
+ kind : packet:: Kind :: UserData ( ud) ,
204
+ } )
212
205
} ,
213
206
_ => {
214
207
return Err ( Error :: from ( ErrorKind :: UnknownHeader ( header) ) ) ;
0 commit comments