Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / usrp / host / lib / inband / usrp_inband_usb_packet.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio 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 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifndef INCLUDED_USRP_INBAND_USB_PACKET_H_
23 #define INCLUDED_USRP_INBAND_USB_PACKET_H_
24
25 #include <usrp_bytesex.h>
26 #include <mb_mblock.h>
27
28 static const int USB_PKT_SIZE = 512;   // bytes
29 static const int MAX_PAYLOAD = USB_PKT_SIZE-2*sizeof(uint32_t);
30
31 class usrp_inband_usb_packet {
32   //
33   // keep raw packet in USRP-endian order
34   //
35   uint32_t            d_word0;
36   uint32_t            d_timestamp;
37   unsigned char   d_payload[MAX_PAYLOAD];
38
39 public:
40
41   enum flags {
42     FL_OVERRUN        = 0x80000000,
43     FL_UNDERRUN       = 0x40000000,
44     FL_DROPPED        = 0x20000000,
45     FL_END_OF_BURST   = 0x10000000,
46     FL_START_OF_BURST = 0x08000000,
47
48     FL_ALL_FLAGS      = 0xf8000000
49   };
50
51   static const int FL_OVERRUN_SHIFT = 31;
52   static const int FL_UNDERRUN_SHIFT = 30;
53   static const int FL_DROPPED_SHIFT = 29;
54   static const int FL_END_OF_BURST_SHIFT = 28;
55   static const int FL_START_OF_BURST_SHIFT = 27;
56   
57   static const int RSSI_MASK = 0x3f;
58   static const int RSSI_SHIFT = 21;
59
60   static const int CHAN_MASK = 0x1f;
61   static const int CHAN_SHIFT = 16;
62
63   static const int TAG_MASK = 0xf;
64   static const int TAG_SHIFT = 9;
65
66   static const int PAYLOAD_LEN_MASK = 0x1ff;
67   static const int PAYLOAD_LEN_SHIFT = 0;
68
69 public:
70   
71   void set_timestamp(uint32_t timestamp){
72     d_timestamp = host_to_usrp_u32(timestamp);
73   }
74
75   void set_end_of_burst() {
76     uint32_t word0 = usrp_to_host_u32(d_word0);
77     word0 |= 1<<FL_END_OF_BURST_SHIFT;
78     d_word0 = host_to_usrp_u32(word0);
79   }
80
81   void set_header(int flags, int chan, int tag, int payload_len){
82     uint32_t word0 =  ((flags & FL_ALL_FLAGS)
83                        | ((chan & CHAN_MASK) << CHAN_SHIFT)
84                        | ((tag & TAG_MASK) << TAG_SHIFT)
85                        | ((payload_len & PAYLOAD_LEN_MASK) << PAYLOAD_LEN_SHIFT));
86     d_word0 = host_to_usrp_u32(word0);
87   }
88   
89   uint32_t timestamp() const {
90     return usrp_to_host_u32(d_timestamp);
91   }
92
93   int rssi() const {
94     uint32_t word0 = usrp_to_host_u32(d_word0);
95     return (word0 >> RSSI_SHIFT) & RSSI_MASK;
96   }
97
98   int chan() const {
99     uint32_t word0 = usrp_to_host_u32(d_word0);
100     return (word0 >> CHAN_SHIFT) & CHAN_MASK;
101   }
102
103   int tag() const {
104     uint32_t word0 = usrp_to_host_u32(d_word0);
105     return (word0 >> TAG_SHIFT) & TAG_MASK;
106   }
107
108   int payload_len() const {
109     uint32_t word0 = usrp_to_host_u32(d_word0);
110     return (word0 >> PAYLOAD_LEN_SHIFT) & PAYLOAD_LEN_MASK;
111   }
112   
113   int flags() const {
114     return usrp_to_host_u32(d_word0) & FL_ALL_FLAGS;
115   }
116
117   int overrun() const {
118     return (usrp_to_host_u32(d_word0) & FL_OVERRUN) >> FL_OVERRUN_SHIFT;
119   }
120   
121
122   int underrun() const {
123     return (usrp_to_host_u32(d_word0) & FL_UNDERRUN) >> FL_UNDERRUN_SHIFT;
124   }
125
126
127   int start_of_burst() const {
128     return (usrp_to_host_u32(d_word0) & FL_START_OF_BURST) >> FL_START_OF_BURST_SHIFT;
129   }
130
131   int end_of_burst() const {
132     return (usrp_to_host_u32(d_word0) & FL_END_OF_BURST) >> FL_END_OF_BURST_SHIFT;
133   }
134
135   int dropped() const {
136     return (usrp_to_host_u32(d_word0) & FL_DROPPED) >> FL_DROPPED_SHIFT;
137   }
138
139   unsigned char *payload() { 
140     return d_payload; 
141   }
142
143   static int max_payload() {
144     return MAX_PAYLOAD;
145   }
146
147 };
148
149 #endif