Date:2011-01-19 21:52:10 (8 years 10 months ago)
Author:Werner Almesberger
Commit:600275946454f816656a80f6e3ae15f4d3b3d2fa
Message:tools/atrf-txrx/perdump: new utility to analyze a PER dump

- Makefile: added perdump
- perdump.h, perdump.c: read and analyze a PER dump in pcap format
- per-text.c: report PER test results as text on the console
Files: tools/atrf-txrx/Makefile (2 diffs)
tools/atrf-txrx/per-text.c (1 diff)
tools/atrf-txrx/perdump.c (1 diff)
tools/atrf-txrx/perdump.h (1 diff)

Change Details

tools/atrf-txrx/Makefile
11#
22# atrf-txrx/Makefile - Build the TX/RX test utility
33#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 Werner Almesberger
4# Written 2010, 2011 by Werner Almesberger
5# Copyright 2010, 2011 Werner Almesberger
66#
77# This program is free software; you can redistribute it and/or modify
88# it under the terms of the GNU General Public License as published by
...... 
1616include ../Makefile.common
1717
1818LDLIBS += -lm
19CFLAGS += -g
20
21all: perdump
22
23perdump: perdump.o per-text.o
24
25clean::
26        rm -f perdump.o per-text.o
tools/atrf-txrx/per-text.c
1/*
2 * atrf-txrx/per-text.h - Report PER on console as text
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdio.h>
15
16#include "perdump.h"
17
18
19static char line[65] = "";
20static int packets = 0, garbled = 0, bad = 0, skipped = 0;
21static int is_bad = 0;
22
23
24static void flush(void)
25{
26    if (*line)
27        printf("%s\n", line);
28    *line = 0;
29}
30
31
32static void text_undecided(int symbols)
33{
34    int i;
35
36    flush();
37    for (i = 0; i != symbols/4; i++)
38        putchar('?');
39    putchar('\n');
40}
41
42
43static void text_packet(int symbols, int skip)
44{
45    int i;
46
47    flush();
48    skipped += skip;
49    if (skip < 4)
50        for (i = 0; i != skip; i++)
51            putchar('\n');
52    else
53        printf("\n(%d)\n\n", skip);
54    for (i = 0; i != symbols/4; i++)
55        line[i] = '-';
56    line[i] = 0;
57    packets++;
58    is_bad = 0;
59}
60
61
62static void text_error(int symbol)
63{
64    line[symbol >> 2] = '*';
65    if (!is_bad) {
66        bad++;
67        is_bad = 1;
68    }
69}
70
71
72static void text_finish(void)
73{
74    double per;
75
76    flush();
77    if (packets+garbled)
78        per = (double) (bad+garbled)/(packets+garbled);
79    else
80        per = 0;
81    printf("\n%d total, %d bad, %d garbled, PER %f%%. %d skipped.\n",
82        packets+garbled, bad, garbled, 100*per, skipped);
83}
84
85
86struct result_ops text_ops = {
87    .begin = NULL,
88    .undecided = text_undecided,
89    .packet = text_packet,
90    .error = text_error,
91    .finish = text_finish,
92};
tools/atrf-txrx/perdump.c
1/*
2 * atrf-txrx/perdump.c - Analyze and dump a recorded PER test
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#include "pcap.h"
19#include "perdump.h"
20
21
22#define MAX_PSDU 127
23
24
25static const struct result_ops *ops;
26
27
28static void analyze(uint8_t *buf, int len)
29{
30    static int last = -1;
31    int freq[256];
32    uint8_t best = 0;
33    int i;
34
35    for (i = 0; i != 256; i++)
36        freq[i] = 0;
37    for (i = 0; i != len; i++) {
38        freq[buf[i]]++;
39        if (!i || freq[buf[i]] > freq[best])
40            best = buf[i];
41    }
42    if (freq[best] <= len >> 1 && freq[best] != len) {
43        ops->undecided(len*2);
44        if (last != -1)
45            last++; /* probably :-) */
46        return;
47    }
48    ops->packet(len*2, last == -1 ? 0 : (uint8_t) (best-last-1));
49    last = best;
50    for (i = 0; i != len; i++) {
51        uint8_t delta = buf[i] ^ best;
52
53        if (delta & 0x0f)
54            ops->error(i*2);
55        if (delta & 0xf0)
56            ops->error(i*2+1);
57    }
58
59}
60
61
62static int pcap_record(FILE *file, const char *name)
63{
64    struct pcap_pkthdr hdr;
65    uint8_t buf[MAX_PSDU];
66    size_t got;
67
68    got = fread(&hdr, sizeof(hdr), 1, file);
69    if (!got) {
70        if (ferror(file)) {
71            perror(name);
72            exit(1);
73        }
74        return 0;
75    }
76    if (hdr.caplen > MAX_PSDU) {
77        fprintf(stderr, "packet too big %u > %u\n",
78            hdr.caplen, MAX_PSDU);
79        exit(1);
80    }
81    got = fread(buf, hdr.caplen, 1, file);
82    if (!got) {
83        if (ferror(file)) {
84            perror(name);
85            exit(1);
86        }
87        fprintf(stderr, "file truncated\n");
88        exit(1);
89    }
90    analyze(buf, hdr.caplen);
91    return 1;
92}
93
94
95static void process_pcap(const char *name)
96{
97    FILE *file;
98    struct pcap_file_header hdr;
99    size_t got;
100
101    file = fopen(name, "r");
102    if (!file) {
103        perror(name);
104        exit(1);
105    }
106    got = fread(&hdr, sizeof(hdr), 1, file);
107    if (!got) {
108        if (ferror(file)) {
109            perror(name);
110            exit(1);
111        }
112        return;
113        return;
114    }
115    if (hdr.magic != PCAP_FILE_MAGIC) {
116        fprintf(stderr, "unrecognized magic number 0x%08x "
117            "(expected 0x%08x)\n", hdr.magic, PCAP_FILE_MAGIC);
118        exit(1);
119    }
120    if (hdr.version_major != 2) {
121        fprintf(stderr, "unrecognized major number %u (expected %u)\n",
122            hdr.version_major, 2);
123        exit(1);
124    }
125    if (hdr.linktype != DLT_IEEE802_15_4) {
126        fprintf(stderr, "unrecognized link type 0x%x "
127            "(expected 0x%0x)\n", hdr.linktype, DLT_IEEE802_15_4);
128        exit(1);
129    }
130    if (ops->begin)
131        ops->begin();
132    while (pcap_record(file, name));
133    if (ops->finish)
134        ops->finish();
135    fclose(file);
136}
137
138
139static void usage(const char *name)
140{
141    fprintf(stderr, "usage: %s pcap-file\n", name);
142    exit(1);
143}
144
145
146int main(int argc, char **argv)
147{
148    if (argc != 2)
149        usage(*argv);
150    ops = &text_ops;
151    process_pcap(argv[1]);
152    return 0;
153}
tools/atrf-txrx/perdump.h
1/*
2 * atrf-txrx/perdump.h - Analyze and dump a recorded PER test
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#ifndef PERDUMP_H
15#define PERDUMP_H
16
17struct result_ops {
18    void (*begin)(void);
19    void (*undecided)(int symbols);
20    void (*packet)(int symbols, int skip);
21    void (*error)(int symbol);
22    void (*finish)(void);
23};
24
25
26extern struct result_ops text_ops;
27
28#endif /* !PERDUMP_H */

Archive Download the corresponding diff file



interactive