summaryrefslogtreecommitdiff
path: root/gr-digital/lib/header_format_counter.cc
blob: 078d7d4be199e4cc127618e7ccfadd4f0fb886ae (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
/* -*- c++ -*- */
/* Copyright 2016 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 GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <iostream>
#include <iomanip>
#include <string.h>
#include <volk/volk.h>
#include <gnuradio/digital/header_format_counter.h>
#include <gnuradio/digital/header_buffer.h>
#include <gnuradio/math.h>

namespace gr {
  namespace digital {

    header_format_counter::sptr
    header_format_counter::make(const std::string &access_code,
                                int threshold, int bps)
    {
      return header_format_counter::sptr
        (new header_format_counter(access_code, threshold, bps));
    }

    header_format_counter::header_format_counter(const std::string &access_code,
                                                 int threshold, int bps)
      : header_format_default(access_code, threshold, bps)
    {
      d_counter = 0;
    }

    header_format_counter::~header_format_counter()
    {
    }

    bool
    header_format_counter::format(int nbytes_in,
                                  const unsigned char *input,
                                  pmt::pmt_t &output,
                                  pmt::pmt_t &info)

    {
      uint8_t* bytes_out = (uint8_t*)volk_malloc(header_nbytes(),
                                                 volk_get_alignment());

      header_buffer header(bytes_out);
      header.add_field64(d_access_code, d_access_code_len);
      header.add_field16((uint16_t)(nbytes_in));
      header.add_field16((uint16_t)(nbytes_in));
      header.add_field16((uint16_t)(d_bps));
      header.add_field16((uint16_t)(d_counter));

      // Package output data into a PMT vector
      output = pmt::init_u8vector(header_nbytes(), bytes_out);

      // Creating the output pmt copies data; free our own here.
      volk_free(bytes_out);

      d_counter++;

      return true;
    }

    size_t
    header_format_counter::header_nbits() const
    {
      return d_access_code_len + 8*4*sizeof(uint16_t);
    }

    bool
    header_format_counter::header_ok()
    {
      // confirm that two copies of header info are identical
      uint16_t len0 = d_hdr_reg.extract_field16(0);
      uint16_t len1 = d_hdr_reg.extract_field16(16);
      return (len0 ^ len1) == 0;
    }

    int
    header_format_counter::header_payload()
    {
      uint16_t len = d_hdr_reg.extract_field16(0);
      uint16_t bps = d_hdr_reg.extract_field16(32);
      uint16_t counter = d_hdr_reg.extract_field16(48);

      d_bps = bps;

      d_info = pmt::make_dict();
      d_info = pmt::dict_add(d_info, pmt::intern("payload symbols"),
                             pmt::from_long(8*len / d_bps));
      d_info = pmt::dict_add(d_info, pmt::intern("bps"),
                             pmt::from_long(bps));
      d_info = pmt::dict_add(d_info, pmt::intern("counter"),
                             pmt::from_long(counter));
      return static_cast<int>(len);
    }

  } /* namespace digital */
} /* namespace gr */