blob: b732449f79b6a8d5bd0bf9b26bb61c9344aad623 [file] [log] [blame]
Harald Weltedbb0f5a2016-10-18 23:51:54 +02001#include <stdio.h>
Harald Welte74d1e342016-10-19 00:06:22 +02002#include <time.h>
Harald Weltef4032322016-10-19 00:23:10 +02003#include <unistd.h>
Harald Welte74d1e342016-10-19 00:06:22 +02004
Harald Weltedbb0f5a2016-10-18 23:51:54 +02005#include <sys/time.h>
6
7#include <osmocom/core/signal.h>
8#include <osmocom/core/logging.h>
9#include <osmocom/core/application.h>
10
11#include "storage.h"
12#include "recorder.h"
13
14struct e1_recorder g_recorder;
15
Harald Weltef4032322016-10-19 00:23:10 +020016enum mode {
17 MODE_PRINT,
18 MODE_BIN,
19};
20
21static enum mode g_mode = MODE_PRINT;
22static int g_filter_line = -1;
23static int g_filter_slot = -1;
24
Harald Welte74d1e342016-10-19 00:06:22 +020025static char *timeval2str(struct timeval *tv)
26{
27 time_t nowtime;
28 struct tm *nowtm;
29 char tmbuf[64];
30 static char buf[64];
31
32 nowtime = tv->tv_sec;
33 nowtm = localtime(&nowtime);
34 strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
35 snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, tv->tv_usec);
36 return buf;
37}
38
Harald Weltef4032322016-10-19 00:23:10 +020039static int handle_options(int argc, char **argv)
40{
41 int opt;
42
43 while ((opt = getopt(argc, argv, "l:s:b")) != -1) {
44 switch (opt) {
45 case 'l':
46 g_filter_line = atoi(optarg);
47 break;
48 case 's':
49 g_filter_slot = atoi(optarg);
50 break;
51 case 'b':
52 g_mode = MODE_BIN;
53 break;
54 default:
55 fprintf(stderr, "Unknown option '%c'\n", opt);
56 exit(EXIT_FAILURE);
57 break;
58 }
59 }
60
61 return 0;
62}
63
Harald Weltedbb0f5a2016-10-18 23:51:54 +020064int main(int argc, char **argv)
65{
66 struct osmo_e1cap_file *f;
67 struct osmo_e1cap_pkthdr *pkt;
Harald Weltef4032322016-10-19 00:23:10 +020068 unsigned long num_pkt = 0;
Harald Weltedbb0f5a2016-10-18 23:51:54 +020069
70 printf("sizeof(timeval) = %zu\n", sizeof(struct timeval));
71 printf("sizeof(osmo_e1cap_pkthdr) = %zu\n", sizeof(*pkt));
72
Harald Weltef4032322016-10-19 00:23:10 +020073 handle_options(argc, argv);
Harald Weltedbb0f5a2016-10-18 23:51:54 +020074
Harald Weltef4032322016-10-19 00:23:10 +020075 if (optind >= argc) {
76 fprintf(stderr, "Missing input file name\n");
77 exit(2);
78 }
79
80 f = osmo_e1cap_open(NULL, argv[optind++]);
81 if (!f) {
82 fprintf(stderr, "Unable to open input file\n");
Harald Weltedbb0f5a2016-10-18 23:51:54 +020083 exit(1);
Harald Weltef4032322016-10-19 00:23:10 +020084 }
Harald Weltedbb0f5a2016-10-18 23:51:54 +020085
86 while ((pkt = osmo_e1cap_read_next(f))) {
Harald Weltef4032322016-10-19 00:23:10 +020087 num_pkt++;
88
89 if (g_filter_line >= 0 && pkt->line_nr != g_filter_line)
90 continue;
91 if (g_filter_slot >= 0 && pkt->ts_nr != g_filter_slot)
92 continue;
93
94 switch (g_mode) {
95 case MODE_PRINT:
96 printf("%s %02u/%02u %u (%u): %s\n",
97 timeval2str(&pkt->ts),
98 pkt->line_nr, pkt->ts_nr, pkt->capture_mode,
99 pkt->len,
100 osmo_hexdump_nospc(pkt->data, pkt->len));
101 break;
102 case MODE_BIN:
103 write(1, pkt->data, pkt->len);
104 break;
105 }
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200106 talloc_free(pkt);
107 }
Harald Weltef4032322016-10-19 00:23:10 +0200108
109 fprintf(stderr, "Processed a total of %lu packets\n", num_pkt);
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200110}