Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit f4fff64

Browse files
author
Alex Helfet
committed
New Packet.kind field, enum, structs. Moved all to a new module in the crate lib.
1 parent 5ab22d0 commit f4fff64

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

src/bin/itmdump.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern crate env_logger;
77
#[macro_use]
88
extern crate error_chain;
99
extern crate heapless;
10+
extern crate itm;
1011
extern crate libc;
1112
#[macro_use]
1213
extern crate log;
@@ -15,6 +16,7 @@ extern crate ref_slice;
1516

1617
use clap::{Arg, App, ArgMatches};
1718
use heapless::Vec as HVec;
19+
use itm::packet::{self, Packet, UserData};
1820
use log::{LogRecord, LogLevelFilter};
1921
use std::fs::File;
2022
use std::io::{Read, Write};
@@ -38,21 +40,6 @@ mod errors {
3840
}
3941
}
4042

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-
5643
fn main() {
5744
// Initialise logging.
5845
env_logger::LogBuilder::new()
@@ -138,8 +125,11 @@ fn run() -> Result<()> {
138125
let p = read_packet(&mut stream);
139126
match p {
140127
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+
_ => (),
143133
}
144134
}
145135
Err(e @ Error(ErrorKind::UnknownHeader(_), _)) => {
@@ -189,26 +179,29 @@ fn read_packet(input: &mut Read) -> Result<Packet> {
189179
let mut header = [0; 1];
190180
input.read_exact(&mut header)?;
191181
let header = header[0];
192-
let mut packet = Packet {
193-
header: header,
194-
payload: HVec::new(),
195-
port: header >> 3,
196-
};
197182
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+
200190
let payload_size =
201191
match header & 0b11 {
202192
0b01 => 1,
203193
0b10 => 2,
204194
0b11 => 4,
205-
_ => return Err(Error::from(
206-
ErrorKind::UnknownHeader(header))),
195+
_ => unreachable!(), // Contradicts match on last 3 bits.
207196
};
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+
})
212205
},
213206
_ => {
214207
return Err(Error::from(ErrorKind::UnknownHeader(header)));

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@
8585
//!
8686
//! - [ARM CoreSight Technical Reference Manual section on ITM][CoreSight ITM]
8787
//! [CoreSight ITM]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0314h/CAAGGCDH.html
88+
89+
#![deny(warnings)]
90+
91+
extern crate heapless;
92+
93+
pub mod packet;

src/packet.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//! Defines ITM packets and their possible contents.
2+
3+
use heapless::Vec as HVec;
4+
5+
pub const MAX_PAYLOAD_SIZE: usize = 4;
6+
7+
pub struct Packet {
8+
/// The header byte received for this packet.
9+
pub header: u8,
10+
11+
/// The kind (type) of this packet.
12+
pub kind: Kind,
13+
}
14+
15+
pub enum Kind {
16+
UserData(UserData),
17+
18+
#[doc(hidden)]
19+
/// External consumers shouldn't expect the public variants to
20+
/// be complete: there are more variants to implement.
21+
_NoExhaustiveMatch,
22+
}
23+
24+
pub struct UserData {
25+
/// Data in this packet.
26+
pub payload: HVec<u8, [u8; MAX_PAYLOAD_SIZE]>,
27+
28+
/// Stimulus port this packet was sent from.
29+
pub port: u8,
30+
}

0 commit comments

Comments
 (0)