summaryrefslogtreecommitdiffstats
path: root/blocks/src/lib/abstract_data_unit.cc
blob: fa00cceca658cba14043b384f86d6d0e3fd3e856 (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
/* -*- 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.
 */

#include <abstract_data_unit.h>

#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <utility>

using namespace std;

abstract_data_unit::~abstract_data_unit()
{
}

void
abstract_data_unit::correct_errors()
{
   if(is_complete()) {
      do_correct_errors(d_frame_body);
   } else {
      ostringstream msg;
      msg << "cannot correct errors - frame is not complete" << endl;
      msg << "(size now: "  << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
      msg << "func: " << __PRETTY_FUNCTION__ << endl;
      msg << "file: " << __FILE__ << endl;
      msg << "line: " << __LINE__ << endl;
      throw logic_error(msg.str());
   }
}

void
abstract_data_unit::decode_audio(imbe_decoder& imbe)
{
   if(is_complete()) {
      do_decode_audio(d_frame_body, imbe);
   } else {
      ostringstream msg;
      msg << "cannot decode audio - frame is not complete" << endl;
      msg << "(size now: "  << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
      msg << "func: " << __PRETTY_FUNCTION__ << endl;
      msg << "file: " << __FILE__ << endl;
      msg << "line: " << __LINE__ << endl;
      throw logic_error(msg.str());
   }
}

size_t
abstract_data_unit::decode_frame(size_t msg_sz, uint8_t *msg)
{
   return decode_frame(d_frame_body, msg_sz, msg);
}

size_t
abstract_data_unit::decode_frame(const_bit_vector& frame_body, size_t msg_sz, uint8_t *msg)
{
   size_t n = 0;
   if(is_complete()) {
      if(size() <= msg_sz) {
         n = extract(frame_body, 0, static_cast<int>(frame_body.size()), msg);
      } else {
         ostringstream msg;
         msg << "cannot decode frame body ";
         msg << "(msg size: "  << msg_sz << ", actual size: " << size() << ")" << endl;
         msg << "func: " << __PRETTY_FUNCTION__ << endl;
         msg << "file: " << __FILE__ << endl;
         msg << "line: " << __LINE__ << endl;
         throw length_error(msg.str());
      }
   } else {
      ostringstream msg;
      msg << "cannot decode frame - frame is not complete" << endl;
      msg << "(size now: "  << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
      msg << "func: " << __PRETTY_FUNCTION__ << endl;
      msg << "file: " << __FILE__ << endl;
      msg << "line: " << __LINE__ << endl;
      throw logic_error(msg.str());
   }
   return n;

}

void
abstract_data_unit::extend(dibit d)
{
   if(frame_size() < frame_size_max()) {
      d_frame_body.push_back(d & 0x2);
      d_frame_body.push_back(d & 0x1);
   } else {
      ostringstream msg;
      msg << "cannot extend frame " << endl;
      msg << "(size now: " << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
      msg << "func: " << __PRETTY_FUNCTION__ << endl;
      msg << "file: " << __FILE__ << endl;
      msg << "line: " << __LINE__ << endl;
      throw length_error(msg.str());
   }
}

bool
abstract_data_unit::is_complete() const
{
   return frame_size() >= frame_size_max();
}

uint16_t
abstract_data_unit::size() const
{
   return (7 + frame_size_max()) >> 3;
}

std::string
abstract_data_unit::snapshot() const
{
   string empty;
   return empty;
}

void
abstract_data_unit::dump(ostream& os) const
{
   uint32_t nbits = d_frame_body.size();
   os << setw(4) << nbits << " ";
   for(size_t i = 48; i < nbits; ++i) {
      os << (d_frame_body[i] ? "#" : "-");
   }
   os << endl;
}

abstract_data_unit::abstract_data_unit(const_bit_queue& frame_body) :
   d_frame_body(frame_body.size())
{
   copy(frame_body.begin(), frame_body.end(), d_frame_body.begin());
}

void
abstract_data_unit::do_correct_errors(bit_vector& frame_body)
{
}

void
abstract_data_unit::do_decode_audio(const_bit_vector& frame_body, imbe_decoder& imbe)
{
}

const_bit_vector& 
abstract_data_unit::frame_body() const
{
   return d_frame_body;
}

uint16_t 
abstract_data_unit::frame_size() const
{
   return d_frame_body.size();
}