1 //
2 // Copyright 2013-2014 Ettus Research LLC
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef _UHD_DUMP_H_
19 #define _UHD_DUMP_H_
20 
21 #define FALSE 0
22 #define TRUE 1
23 #define UNKNOWN 2
24 
25 // Define directions for Host->USRP & USRP->Host
26 #define H2U 0
27 #define U2H 1
28 
29 // Endpoint encodings for USRP3 from SID LSB's
30 #define RADIO 0
31 #define RADIO_CTRL 1
32 #define SRC_FLOW_CTRL 2
33 #define RESERVED 3
34 
35 /* // VRT Type definitions */
36 /* #define IF_DATA_NO_SID 0 */
37 /* #define IF_DATA_WITH_SID 1 */
38 /* #define EXT_DATA_NO_SID 2 */
39 /* #define EXT_DATA_WITH_SID 3 */
40 /* #define IF_CONTEXT 4 */
41 /* #define EXT_CONTEXT 5 */
42 
43 // CHDR bit masks
44 #define EXT_CONTEXT 1<<31
45 #define HAS_TIME 1<<29
46 #define EOB 1<<28
47 #define SIZE (1<<16)-1
48 
49 
50 // UDP used as source for all CHDR comms.
51 #define CHDR_PORT 49153
52 
53 typedef unsigned char bool;
54 typedef unsigned char u8;
55 typedef unsigned short u16;
56 typedef unsigned int u32;
57 typedef unsigned long u64;
58 
59 union ip_addr_decoder {
60   unsigned long s_addr;
61   unsigned char octet[4];
62 };
63 
64 /*
65 Ethernet header structure
66 */
67 
68 struct ethernet_header {
69   u8 eth_dst[6]; // MAC addr of destination
70   u8 eth_src[6]; // MAC addr of source
71   u16 eth_typ;   // Payload protocol type
72 };
73 
74 #define ETH_SIZE 14
75 
76 /*
77   IP Header strcture
78   NOTE: Network byte order (Big Endian)
79 */
80 
81 struct ip_header {
82   u8 ip_vhl;   // [7:4] Version, [3:0] Header Length
83   u8 ip_tos;   // Type of Service/DIff Serv/ECN
84   u16 ip_len;  // Total Length
85   u16 ip_id;   // Identification
86   u16 ip_off;  // [15:14] Flags, [13:0] Fragment Offset
87   u8 ip_ttl;   // Time To Live
88   u8 ip_pro;   // Protocol
89   u16 ip_sum;  // Checksum
90   struct in_addr ip_src; // IP Source address
91   struct in_addr ip_dst; // IP Destination address
92 };
93 
94 #define IP_SIZE 20
95 
96 /*
97   UDP Header Structure
98   NOTE: Network byte order (Big Endian)
99 */
100 
101 struct udp_header {
102   u16 udp_src;  // Source Port
103   u16 udp_dst;  // Destination Port
104   u16 udp_len;  // Length
105   u16 udp_sum;  // Checksum
106 };
107 
108 #define UDP_SIZE 8
109 
110 /* /\* */
111 /* VITA49 VRLP Header */
112 /* NOTE: Network byte order (Big Endian) */
113 /* *\/ */
114 
115 /* struct vrlp_header { */
116 /*   u32 vrlp_start; // Hardcoded to ASCII "VRLP" */
117 /*   u32 vrlp_size;  // [31:20] Frame Count, [19:0] Frame Size */
118 /* }; */
119 
120 /* #define VRLP_SIZE 8 */
121 
122 /* #define VRLP_SEQID(x)  (((x & 0xff)<<4) | ((x & 0xf000) >> 12)) */
123 
124 /* /\* */
125 /* VITA49 VRLP Trailer */
126 /* NOTE: Network byte order (Big Endian) */
127 /* *\/ */
128 
129 /* struct vrlp_trailer { */
130 /*   u32 vrlp_end; // Hardcoded to ASCII "VEND" */
131 /* }; */
132 
133 /* #define VRLP_TRAILER_SIZE 4 */
134 
135 /* /\* */
136 /* VITA49 VRT Header  */
137 /* NOTE: Network byte order (Big Endian) */
138 /* *\/ */
139 
140 /* struct vrt_header { */
141 /*   u8 vrt_type; // [7:4] type, [3] Class ID flag, [2] Trailer flag, [1] SOB, [0] EOB  */
142 /*   u8 vrt_count; // [7:6] TSI, [5:4] TSF, [3:0] Packet Count modulo 16 */
143 /*   u16 vrt_size; // Number of 32bit words in VRT packet including headers and payload. */
144 /*   u32 vrt_sid; // Stream ID */
145 /* }; */
146 
147 /* #define VRT_SIZE 8 */
148 
149 
150 /*
151 Ettus Research CHDR header
152 NOTE: Little endian byte order (must be unswizzled)
153 */
154 
155 struct chdr_header {
156   u32 chdr_type;// [31] Ext Context, [30] RSVD, [29] Has_time, [28] EOB], [27:16] SEQ_ID, [15:0] Size
157   u32 chdr_sid; // Stream ID
158 };
159 
160 #define CHDR_SIZE 8
161 
162 /*
163 Break down SID into CHDR defined fields
164 */
165 struct chdr_sid {
166   u8 src_device;
167   u8 src_endpoint;
168   u8 dst_device;
169   u8 dst_endpoint;
170 };
171 
172 struct radio_ctrl_payload {
173   u32 addr;
174   u32 data;
175 };
176 
177 struct radio_response {
178   u64 data;
179 };
180 
181 struct tx_response {
182   u32 error_code;
183   u32 seq_id;
184 };
185 
186 #define TX_ACK 0x00
187 #define TX_EOB 0x01
188 #define TX_UNDERRUN 0x02
189 #define TX_SEQ_ERROR 0x04
190 #define TX_TIME_ERROR 0x08
191 #define TX_MIDBURST_SEQ_ERROR 0x20
192 
193 struct src_flow_ctrl {
194   u32 unused;
195   u32 seq_id;
196 };
197 
198 struct vita_time {
199   u64 time;
200 };
201 
202 #define VITA_TIME_SIZE 8
203 #define RADIO_CTRL_SIZE 8
204 #define RADIO_RESPONSE_SIZE 4
205 
206 
207 /*
208   Packet storage
209 */
210 
211 struct pbuf {
212   struct pbuf *next;
213   struct pbuf *last;
214   struct timeval ts;
215   int size;                 // Size stored in pcap file
216   int orig_size;            // Original capture size on the wire
217   char *payload;
218 };
219 
220 struct pbuf_info {
221   struct pbuf *start;
222   struct pbuf *current;
223   struct pbuf *end;
224 };
225 
226 struct radio_ctrl_names {
227   u32 addr;
228   char *name;
229 };
230 
231 
232 //
233 // Prototypes
234 //
235 
236 unsigned long swaplong (unsigned long);
237 unsigned int swapint (unsigned int);
238 unsigned short swapshort (unsigned short);
239 char *format_gmt(const struct timeval *, char *);
240 double timeval2double(struct timeval *);
241 double relative_time(struct timeval *, struct timeval *);
242 void get_packet(struct pbuf_info * , const struct pcap_pkthdr *, const u_char *);
243 void get_start_time(struct timeval * , const struct pcap_pkthdr *, const u_char *);
244 void get_udp_port_from_file(u16, const char *, struct pbuf_info *, struct timeval *);
245 void get_everything_from_file(const char *, struct pbuf_info *, struct timeval *);
246 void get_connection_endpoints( struct pbuf_info *, struct in_addr *, struct in_addr *);
247 void print_direction(const struct pbuf_info *, const struct in_addr *, const struct in_addr *);
248 void print_size( const struct pbuf_info *);
249 void print_sid( const struct pbuf_info *);
250 void print_vita_header( const struct pbuf_info *, const struct in_addr *);
251 
252 #endif
253