mirror of
git://projects.qi-hardware.com/ben-wpan.git
synced 2024-11-04 23:49:42 +02:00
atrf-txrx: added ability to record received frames in pcap format
- pcap.h: basic pcap file structure definitions - atrf-txrx.c (usage, main): added option "-o file" to write received frames to a pcap file instead of displaying them - atrf-txrx.c (receive): moved code to receive and display a single message into new function receive_message - atrf-txrx.c (write_pcap_hdr, write_pcap_rec, receive_pcap): receive messages into a pcap file
This commit is contained in:
parent
5407180c32
commit
f9aee54323
@ -19,11 +19,14 @@
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "at86rf230.h"
|
||||
#include "atrf.h"
|
||||
#include "misctxrx.h"
|
||||
|
||||
#include "pcap.h"
|
||||
|
||||
|
||||
/*
|
||||
* According to IEEE 802.15.4-2003 section E.2.6, channel 15 is the only
|
||||
@ -148,19 +151,12 @@ static void set_power(struct atrf_dsc *dsc, double power, int crc)
|
||||
}
|
||||
|
||||
|
||||
static void receive(struct atrf_dsc *dsc)
|
||||
static void receive_message(struct atrf_dsc *dsc)
|
||||
{
|
||||
uint8_t buf[MAX_PSDU+1]; /* PSDU+LQI */
|
||||
int n, ok, i;
|
||||
uint8_t ed, lqi;
|
||||
|
||||
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_RX_ON);
|
||||
/*
|
||||
* 180 us, according to AVR2001 section 4.2. We time out after
|
||||
* nominally 200 us.
|
||||
*/
|
||||
wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 10, 20);
|
||||
|
||||
fprintf(stderr, "Ready.\n");
|
||||
wait_for_interrupt(dsc, IRQ_TRX_END, IRQ_TRX_END | IRQ_RX_START,
|
||||
10, 0);
|
||||
@ -185,6 +181,103 @@ static void receive(struct atrf_dsc *dsc)
|
||||
}
|
||||
|
||||
|
||||
static void write_pcap_hdr(FILE *file)
|
||||
{
|
||||
struct pcap_file_header hdr = {
|
||||
.magic = 0xa1b2c3d4,
|
||||
.version_major = 2,
|
||||
.version_minor = 4,
|
||||
.thiszone = 0,
|
||||
.sigfigs = 0,
|
||||
.snaplen = MAX_PSDU,
|
||||
.linktype = DLT_IEEE802_15_4
|
||||
};
|
||||
|
||||
if (fwrite(&hdr, sizeof(hdr), 1, file) != 1) {
|
||||
perror("fwrite");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void write_pcap_rec(FILE *file, const struct timeval *tv,
|
||||
const void *buf, int n)
|
||||
{
|
||||
struct pcap_pkthdr hdr = {
|
||||
.ts_sec = tv->tv_sec,
|
||||
.ts_usec = tv->tv_usec,
|
||||
.caplen = n,
|
||||
.len = n
|
||||
};
|
||||
|
||||
if (fwrite(&hdr, sizeof(hdr), 1, file) != 1) {
|
||||
perror("fwrite");
|
||||
exit(1);
|
||||
}
|
||||
if (fwrite(buf, n, 1, file) != 1) {
|
||||
perror("fwrite");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void receive_pcap(struct atrf_dsc *dsc, const char *name)
|
||||
{
|
||||
FILE *file;
|
||||
uint8_t buf[MAX_PSDU+1]; /* PSDU+LQI */
|
||||
struct timeval now;
|
||||
int n;
|
||||
int count = 0;
|
||||
|
||||
file = fopen(name, "w");
|
||||
if (!file) {
|
||||
perror(name);
|
||||
exit(1);
|
||||
}
|
||||
write_pcap_hdr(file);
|
||||
while (run) {
|
||||
wait_for_interrupt(dsc,
|
||||
IRQ_TRX_END, IRQ_TRX_END | IRQ_RX_START,
|
||||
10, 0);
|
||||
if (!run)
|
||||
break;
|
||||
gettimeofday(&now, NULL);
|
||||
n = atrf_buf_read(dsc, buf, sizeof(buf));
|
||||
if (n < 0)
|
||||
exit(1);
|
||||
if (n < 2) {
|
||||
fprintf(stderr, "%d bytes received\n", n);
|
||||
continue;
|
||||
}
|
||||
write_pcap_rec(file, &now, buf, n-1);
|
||||
(void) write(2, ".", 1);
|
||||
count++;
|
||||
}
|
||||
if (fclose(file) == EOF) {
|
||||
perror(name);
|
||||
exit(1);
|
||||
}
|
||||
fprintf(stderr, "%sreceived %d message%s\n", count ? "\n" : "",
|
||||
count, count == 1 ? "" : "s");
|
||||
}
|
||||
|
||||
|
||||
static void receive(struct atrf_dsc *dsc, const char *name)
|
||||
{
|
||||
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_RX_ON);
|
||||
/*
|
||||
* 180 us, according to AVR2001 section 4.2. We time out after
|
||||
* nominally 200 us.
|
||||
*/
|
||||
wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 10, 20);
|
||||
|
||||
if (name)
|
||||
receive_pcap(dsc, name);
|
||||
else
|
||||
receive_message(dsc);
|
||||
}
|
||||
|
||||
|
||||
static void transmit(struct atrf_dsc *dsc, const char *msg, int times)
|
||||
{
|
||||
uint8_t buf[MAX_PSDU];
|
||||
@ -332,10 +425,12 @@ static void usage(const char *name)
|
||||
" wave in MHz: -2, -0.5, or +0.5\n"
|
||||
" command shell command to run while transmitting (default: wait for\n"
|
||||
" SIGINT instead)\n\n"
|
||||
" common options: [-c channel|-f freq] [-C mhz] [-p power] [-t trim]\n"
|
||||
" common options: [-c channel|-f freq] [-C mhz] [-o file] [-p power] "
|
||||
"[-t trim]\n"
|
||||
" -c channel channel number, 11 to 26 (default %d)\n"
|
||||
" -C mhz output clock at 1, 2, 4, 8, or 16 MHz (default: off)\n"
|
||||
" -f freq frequency in MHz, 2405 to 2480 (default %d)\n"
|
||||
" -o file write received data to a file in pcap format\n"
|
||||
" -p power transmit power, -17.2 to 3.0 dBm (default %.1f)\n"
|
||||
" -t trim trim capacitor, 0 to 15 (default 0)\n"
|
||||
, name, name, DEFAULT_CHANNEL, 2405+5*(DEFAULT_CHANNEL-11),
|
||||
@ -354,9 +449,10 @@ int main(int argc, char *const *argv)
|
||||
int c, freq;
|
||||
unsigned tmp, clkm = 0;
|
||||
int status = 0;
|
||||
const char *pcap_file = NULL;
|
||||
struct atrf_dsc *dsc;
|
||||
|
||||
while ((c = getopt(argc, argv, "c:C:f:p:t:T:")) != EOF)
|
||||
while ((c = getopt(argc, argv, "c:C:f:o:p:t:T:")) != EOF)
|
||||
switch (c) {
|
||||
case 'c':
|
||||
channel = strtoul(optarg, &end, 0);
|
||||
@ -375,6 +471,9 @@ int main(int argc, char *const *argv)
|
||||
if (channel < 11 || channel > 26)
|
||||
usage(*argv);
|
||||
break;
|
||||
case 'o':
|
||||
pcap_file = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
power = strtod(optarg, &end);
|
||||
if (*end)
|
||||
@ -419,7 +518,7 @@ int main(int argc, char *const *argv)
|
||||
dsc = init_txrx(trim, clkm);
|
||||
set_channel(dsc, channel);
|
||||
if (!cont_tx)
|
||||
receive(dsc);
|
||||
receive(dsc, pcap_file);
|
||||
else {
|
||||
set_power(dsc, power, 0);
|
||||
status = test_mode(dsc, cont_tx, NULL);
|
||||
|
49
tools/atrf-txrx/pcap.h
Normal file
49
tools/atrf-txrx/pcap.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* pcap.h - Minimum pcap file definitions
|
||||
*
|
||||
* Written 2011 by Werner Almesberger
|
||||
* Copyright 2011 Werner Almesberger
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This header defines the few things we need to write files in the pcap
|
||||
* format. The identifiers are the same as in the system's pcap/pcap.h, but
|
||||
* the types have been standardized. Note that the timestamp parts have to be
|
||||
* put separately, since "struct timeval" may be padded.
|
||||
*
|
||||
* The reason for having our own header instead of just using pcap/pcap.h is
|
||||
* to avoid a build-dependency on libpcap.
|
||||
*/
|
||||
|
||||
#ifndef PCAP_H
|
||||
#define PCAP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
|
||||
#define DLT_IEEE802_15_4 195
|
||||
|
||||
struct pcap_file_header {
|
||||
uint32_t magic;
|
||||
uint16_t version_major;
|
||||
uint16_t version_minor;
|
||||
int32_t thiszone;
|
||||
uint32_t sigfigs;
|
||||
uint32_t snaplen;
|
||||
uint32_t linktype;
|
||||
};
|
||||
|
||||
struct pcap_pkthdr {
|
||||
uint32_t ts_sec;
|
||||
uint32_t ts_usec;
|
||||
uint32_t caplen;
|
||||
uint32_t len;
|
||||
};
|
||||
|
||||
#endif /* !PCAP_H */
|
Loading…
Reference in New Issue
Block a user