summaryrefslogtreecommitdiff
path: root/vrt/lib/expanded_header.cc
blob: 74a156dd243a4febee67c57ac98f825c2f6cbcb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- c++ -*- */
/*
 * Copyright 2009 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio 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 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <vrt/expanded_header.h>
#include <gruel/inet.h>
#include <boost/format.hpp>
#include "header_utils.h"

using boost::format;
using boost::io::group;


namespace vrt {

  using namespace detail;

  // lookup tables indexed by packet type
  unsigned char expanded_header::s_if_data[16] = {
    1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };

  unsigned char expanded_header::s_ext_data[16] = {
    0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };

  unsigned char expanded_header::s_data[16] = {
    1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };

  unsigned char expanded_header::s_context[16] = {
    0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };

  unsigned char expanded_header::s_stream_id[16] = {
    0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };


  // dispatch codeword bits
  static const int HAS_STREAM_ID       = 1 << 0;
  static const int HAS_CLASS_ID        = 1 << 1;
  static const int HAS_INTEGER_SECS    = 1 << 2;
  static const int HAS_FRACTIONAL_SECS = 1 << 3;
  static const int HAS_TRAILER         = 1 << 4;

  static int
  compute_codeword(const expanded_header &h)
  {
    int cw = 0;
    if (h.stream_id_p())       cw |= HAS_STREAM_ID;
    if (h.class_id_p())        cw |= HAS_CLASS_ID;
    if (h.integer_secs_p())    cw |= HAS_INTEGER_SECS;
    if (h.fractional_secs_p()) cw |= HAS_FRACTIONAL_SECS;
    if (h.trailer_p())         cw |= HAS_TRAILER;
    return cw;
  }

  void expanded_header::pack(const expanded_header *h,      	// in
			     size_t n32_bit_words_payload,  	// in
			     uint32_t *header,              	// out
			     size_t *n32_bit_words_header,  	// out
			     uint32_t *trailer,             	// out
			     size_t *n32_bit_words_trailer)	// out
  {
    int cw = compute_codeword(*h);
    //fills in the header (except word0), header length, trailer, trailer length
    switch (cw & 0x1f){
#include "expanded_header_pack_switch_body.h"
    }
    //fill in the header word 0 with the calculated length
    size_t n32_bit_words_packet = *n32_bit_words_header + n32_bit_words_payload + *n32_bit_words_trailer;
    header[0] = htonl((h->header & ~VRTH_PKT_SIZE_MASK) | (n32_bit_words_packet & VRTH_PKT_SIZE_MASK));
  }

  bool 
  expanded_header::unpack(const uint32_t *packet,		// in
			  size_t n32_bit_words_packet,		// in
			  expanded_header *h,			// out
			  const uint32_t **payload,		// out
			  size_t *n32_bit_words_payload)	// out
  {
    size_t n32_bit_words_header = 0;
    size_t n32_bit_words_trailer = 0;
    size_t len = n32_bit_words_packet;
    const uint32_t *p = packet;

    *payload = 0;
    *n32_bit_words_payload = 0;

    // printf("unpack: n32_bit_words_packet = %zd\n", n32_bit_words_packet);

    if (len < 1){		// must have at least the header word
      h->header = 0;
      return false;
    }

    h->header = ntohl(p[0]);

    if (h->pkt_size() > len)
      return false;		// VRT header says packet is bigger than what we've got

    len = h->pkt_size();	// valid length of packet

    int cw = compute_codeword(*h);
    switch (cw & 0x1f){
#include "expanded_header_unpack_switch_body.h"
    }

    if (n32_bit_words_header + n32_bit_words_trailer > len)
      return false;		// negative payload len

    *payload = p + n32_bit_words_header;
    *n32_bit_words_payload = len - (n32_bit_words_header + n32_bit_words_trailer);

    // printf("unpack: hdr = 0x%08x, cw = 0x%02x, n32_bit_words_header = %d, n32_bit_words_trailer = %d\n",
    //   h->header, cw, n32_bit_words_header, n32_bit_words_trailer);

    return true;
  }

  std::string
  pkt_type_name(const vrt::expanded_header *hdr)
  {
    if (hdr->if_data_p())
      return "IF-Data";
    if (hdr->ext_data_p())
      return "Ext-Data";
    if (hdr->if_context_p())
      return "IF-Context";
    if (hdr->ext_context_p())
      return "Ext-Context";
    else
      return "<unknown pkt type>";
  }

  void
  expanded_header::write(std::ostream &port) const
  {
    port << format("%s:\n") % pkt_type_name(this);
    if (1){
      wr_name(port, "header");
      wr_header(port, header);
    }

    if (stream_id_p()){
      wr_name(port, "stream_id");
      wr_uint32_hex(port, stream_id);
    }

    if (class_id_p()){
      wr_name(port, "class_id");
      port << format("0x%016llx\n") % class_id;
    }

    if (integer_secs_p()){
      wr_name(port, "int secs");
      //port << format("%10d\n") % integer_secs;
      wr_int_secs(port, integer_secs);
    }

    if (fractional_secs_p()){
      wr_name(port, "frac secs");
      port << format("%10d\n") % fractional_secs;
    }
  }

  std::ostream& operator<<(std::ostream &os, const expanded_header &obj)
  {
    obj.write(os);
    return os;
  }

}; // vrt