4 #include "usbdev_stream.h"
11 #include "stream_test.h"
12 #include "usb_device.h"
13 #include "usbdev_utils.h"
16 #define STREAM_SIGNATURE_HEAD 0x579EA01AU
17 #define STREAM_SIGNATURE_TAIL 0x160AE975U
21 #define USBTST_LFSR_SEED(s) (uint8_t)(0x10U + (s)*7U)
22 #define USBDPI_LFSR_SEED(s) (uint8_t)(0x9BU - (s)*7U)
25 #define LFSR_ADVANCE(lfsr) \
27 ((((lfsr) >> 1) ^ ((lfsr) >> 2) ^ ((lfsr) >> 3) ^ ((lfsr) >> 7)) & 1u))
29 USBDevStream::USBDevStream(
unsigned id, uint32_t transfer_bytes,
bool retrieve,
30 bool check,
bool send,
bool verbose) {
67 uint32_t dropped = 0U;
77 uint8_t *sigp =
reinterpret_cast<uint8_t *
>(sig);
83 const size_t tail_offset = offsetof(usbdev_stream_sig_t, tail_sig);
87 case kSigStateCheckHead: {
88 const unsigned sh = sig_cnt_ << 3;
89 uint8_t match = (uint8_t)(STREAM_SIGNATURE_HEAD >> sh);
91 if (++sig_cnt_ >= 4U) {
100 case kSigStateSkipBody: {
101 if (++sig_cnt_ >= tail_offset) {
107 case kSigStateCheckTail: {
108 const unsigned sh = (sig_cnt_ - tail_offset) << 3;
109 const uint8_t match = (uint8_t)(STREAM_SIGNATURE_TAIL >> sh);
111 if (sig_cnt_ >=
sizeof(usbdev_stream_sig_t) - 1U) {
120 if (sig->num_bytes > 0U && sig->num_bytes < 0x10000000U &&
121 stream < STREAMS_MAX) {
123 std::cout <<
PrefixID() <<
"Signature accepted" << std::endl;
127 std::cout <<
PrefixID() <<
"Signature rejected" << std::endl;
142 assert(!
"Invalid/undefined sig_recvd state");
148 dropped += sig_cnt_ + 1U;
154 }
while (--nrecv > 0U &&
sig_recvd_ != kSigStateReceived);
169 if (!(sig.seq_hi | sig.seq_lo)) {
177 uint16_t seq = sig_read8(offsetof(usbdev_stream_sig_t, seq_lo)) |
178 (sig_read8(offsetof(usbdev_stream_sig_t, seq_hi)) << 8);
186 printf(
"Signature detected: stream #%u LFSR 0x%02x bytes 0x%x\n", stream,
187 sig.init_lfsr, sig.num_bytes);
188 printf(
" - retrieve %c check %c send %c\n", retrieve ?
'Y' :
'N',
189 check ?
'Y' :
'N', send ?
'Y' :
'N');
190 printf(
" - seq #%u\n", seq);
200 if (space_bytes >= len) {
222 uint8_t *start = &
buf_.data[
buf_.wr_idx];
226 space_bytes = (
buf_.rd_idx - 1U) -
buf_.wr_idx;
227 }
else if (!
buf_.rd_idx) {
242 if (space_bytes < len) {
256 buf_.wr_idx += chunk;
273 for (
unsigned idx = 0U; idx < len; idx++) {
275 next_lfsr = LFSR_ADVANCE(next_lfsr);
285 start_time = time_us();
290 std::cout <<
"S" <<
ID()
291 << (cfg.
retrieve ?
": Received " :
": Generated ") << len
292 <<
" byte(s)" << std::endl;
296 const uint8_t *sp = dp;
297 for (uint32_t idx = 0U; idx < len; idx++) {
299 uint8_t recvd = sp[idx];
303 if (recvd != expected) {
304 printf(
"S%u: Mismatched data from device 0x%02x, expected 0x%02x\n",
305 id_, recvd, expected);
313 printf(
"S%u: 0x%02x <- 0x%02x ^ 0x%02x\n",
id_, dp[idx], recvd,
336 uint32_t data_bytes =
buf_.end_idx -
buf_.rd_idx;
337 uint8_t *start = &
buf_.data[
buf_.rd_idx];
340 data_bytes =
buf_.wr_idx -
buf_.rd_idx;
351 if (data_bytes < len) {
356 uint32_t chunk =
buf_.end_idx -
buf_.rd_idx;
357 assert(len <= chunk);
358 if (len < chunk || (len == chunk &&
buf_.wr_idx >=
buf_.end_idx)) {
361 buf_.rd_idx = len - chunk;
380 assert(
buf_.wr_idx <=
buf_.end_idx);
388 printf(
"S%u : rd_idx 0x%x wr_idx 0x%x\n",
id_,
buf_.rd_idx,
buf_.wr_idx);