summaryrefslogtreecommitdiffstats
path: root/blocks/src/lib/abstract_data_unit.h
blob: 2a84506bebf36ca691a269390646602137ef6bff (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
/* -*- C++ -*- */

/*
 * Copyright 2008 Steve Glass
 * 
 * This file is part of OP25.
 * 
 * OP25 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.
 * 
 * OP25 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 OP25; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Boston, MA
 * 02110-1301, USA.
 */

#ifndef INCLUDED_ABSTRACT_DATA_UNIT_H
#define INCLUDED_ABSTRACT_DATA_UNIT_H

#include <data_unit.h>
#include <string>
#include <vector>
#include <op25_yank.h>

#include <itpp/base/vec.h>
#include <vector>

typedef std::vector<bool> bit_vector;
typedef const std::vector<bool> const_bit_vector;

/**
 * Abstract P25 data unit.
 */
class abstract_data_unit : public data_unit
{

public:

   /**
    * abstract data_unit virtual destructor.
    */
   virtual ~abstract_data_unit();

   /**
    * Apply error correction to this data_unit.
    *
    * \precondition is_complete() == true.
    */
   virtual void correct_errors();

   /**
    * Decode compressed audio using the supplied imbe_decoder.
    *
    * \precondition is_complete() == true.
    * \param imbe The imbe_decoder to use to generate the audio.
    */
   virtual void decode_audio(imbe_decoder& imbe);

   /**
    * Decode the frame into an octet vector.
    *
    * \precondition is_complete() == true.
    * \param msg_sz The size of the message buffer.
    * \param msg A pointer to the message buffer.
    * \return The number of octets written to msg.
    */
   virtual size_t decode_frame(size_t msg_sz, uint8_t *msg);

   /**
    * Dump this data unit in human readable format to stream s.
    *
    * \param s The stream to write on
    */
   virtual void dump(std::ostream& os) const;

   /**
    * Extends this data_unit with the specified dibit. If this
    * data_unit is already complete a range_error is thrown.
    *
    * \precondition is_complete() == false.
    * \param d The dibit to extend the frame with.
    * \throws range_error When the frame already is at its maximum size.
    * \return true when the frame is complete otherwise false.
    */
   virtual void extend(dibit d);

   /**
    * Tests whether this data unit has enough data to begin decoding.
    *
    * \return true when this data_unit is complete; otherwise returns
    * false.
    */
   virtual bool is_complete() const;

   /**
    * Returns the size (in octets) of this data_unit.
    *
    * \return The size (in octets) of this data_unit.
    */
   virtual uint16_t size() const;

   /**
    * Return a snapshot of the key fields from this frame in a manner
    * suitable for display by the UI. The string is encoded as a
    * pickled Python dictionary.
    * 
    * \precondition is_complete() == true.
    * \return A string containing the fields to display.
    */
   virtual std::string snapshot() const;

protected:

   /**
    * abstract_data_unit constructor.
    *
    * \param frame_body A const_bit_queue representing the frame body.
    */
   abstract_data_unit(const_bit_queue& frame_body);

   /**
    * Applies error correction code to the specified bit_vector.
    *
    * \param frame_body The bit vector to decode.
    */
   virtual void do_correct_errors(bit_vector& frame_body);

   /**
    * Decode compressed audio using the supplied imbe_decoder.
    *
    * \precondition is_complete() == true.
    * \param frame_body The const_bit_vector to decode.
    * \param imbe The imbe_decoder to use.
    */
   virtual void do_decode_audio(const_bit_vector& frame_body, imbe_decoder& imbe);

   /**
    * Decode frame_body and write the decoded frame contents to msg.
    *
    * \param frame_body The bit vector to decode.
    * \param msg_sz The size of the message buffer.
    * \param msg A pointer to where the data unit content will be written.
    * \return The number of octets written to msg.
    */
   virtual size_t decode_frame(const_bit_vector& frame_body, size_t msg_sz, uint8_t *msg);

   /**
    * Returns a string describing the Data Unit ID (DUID).
    *
    * \return A string identifying the DUID.
    */
   virtual std::string duid_str() const = 0;

   /**
    * Return a reference to the frame body.
    */
   const_bit_vector& frame_body() const;

   /**
    * Returns the expected size (in bits) of this data_unit. For
    * variable-length data this should return UINT16_MAX until the
    * actual length of this frame is known.
    *
    * \return The expected size (in bits) of this data_unit when encoded.
    */
   virtual uint16_t frame_size_max() const = 0;

   /**
    * Returns the current size (in bits) of this data_unit.
    *
    * \return The current size (in bits) of this data_unit.
    */
   virtual uint16_t frame_size() const;

private:

   /**
    * A bit vector containing the frame body.
    */
   bit_vector d_frame_body;

};

#endif /* INCLUDED_ABSTRACT_DATA_UNIT_H */