-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasecodeword.h
More file actions
219 lines (186 loc) · 6.22 KB
/
basecodeword.h
File metadata and controls
219 lines (186 loc) · 6.22 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef BASE_CODEWORD_H_233920180208
#define BASE_CODEWORD_H_233920180208
#include <exception>
#include <math.h>
#include <memory>
#include <sstream>
#include <vector>
#include "dataelements.h"
#include "json.hpp"
namespace L16 __attribute__((visibility ("hidden"))) {
char codeword_format(unsigned int v);
class ICodeword
{
public:
friend std::ostream& operator<<(std::ostream& os, L16::ICodeword const& elt);
std::vector<std::shared_ptr<IDataElement> > fields;
virtual nlohmann::json to_json(bool with_technical=false) const = 0;
virtual uint1024_t to_bin() const = 0;
virtual void decode(uint1024_t data) = 0;
virtual void encode(nlohmann::json const& json_info, int pos) = 0;
virtual std::string codeword_name() const = 0;
};
template <unsigned int wf, class ...Fields> struct Codeword: public ICodeword
{
Codeword()
{
fields = {std::shared_ptr<IDataElement>(new T_1550_1_WORD_FORMAT<0, wf>()), std::shared_ptr<IDataElement>(new(Fields))... };
}
nlohmann::json to_json(bool with_technical=false) const override
{
nlohmann::json j;
nlohmann::json data_element;
for (std::vector<std::shared_ptr<IDataElement> >::const_iterator i = fields.begin(); i != fields.end(); ++i)
{
if (!(*i)->is_technical() || with_technical)
data_element[(*i)->name()] = (*i)->value();
}
j["name"] = codeword_name();
j["data_elements"] = data_element;
return j;
}
uint1024_t to_bin() const override
{
uint1024_t data = 0;
for (std::vector<std::shared_ptr<IDataElement> >::const_iterator i = fields.begin(); i != fields.end(); ++i)
{
data << **i;
//std::cout << std::hex << data << " " << (*i)->name() << " " << (*i)->value() << std::endl;
}
return data;
}
virtual void decode(uint1024_t data)
{
check(data);
for (std::vector<std::shared_ptr<IDataElement> >::iterator i = fields.begin(); i != fields.end(); ++i)
{
data >> **i;
}
}
void encode(nlohmann::json const& json_info, int pos)
{
for (std::vector<std::shared_ptr<IDataElement> >::iterator i = fields.begin(); i != fields.end(); ++i)
{
json_info["codewords"][pos]["data_elements"] >> **i;
if (!(*i)->is_valid())
{
json_info >> **i;
}
}
}
virtual void check(uint1024_t const& data) const = 0;
};
class IllegalCodeWord: public std::exception
{
public:
IllegalCodeWord(std::string const& txt) throw(): text(txt)
{}
virtual const char* what() const throw()
{
return text.c_str();
}
virtual ~IllegalCodeWord() throw()
{}
private:
std::string text;
};
template <unsigned int label, unsigned int subLabel, class ... Fields> struct Initial: public Codeword<0, T_270_4_LABEL_J_SERIES<2, label>,
T_271_5_SUBLABEL_J_SERIES<7, subLabel>,
T_800_1_MESSAGE_LENGTH_INDICATOR<10>, Fields...>
{
void check(uint1024_t const& data) const override
{
unsigned int data_wf = static_cast<unsigned int>(data & 0b0011);
if (data_wf == 0)
return;
unsigned int data_cont_label = static_cast<unsigned int>((data & 0b111100) >> 2);
std::stringstream ss;
ss << "Expected Initial word";
ss << ". But found " << codeword_format(data_wf);
if (data_wf == 1)
ss << data_cont_label;
throw IllegalCodeWord(ss.str());
}
static bool match(uint1024_t const& data)
{
unsigned int data_wf = static_cast<unsigned int>(data & 0b0011);
return (data_wf == 0);
}
static std::string name()
{
return "I";
}
std::string codeword_name() const override
{
return Initial::name();
}
};
template <unsigned int label, class ... Fields> struct Extension: public Codeword<2, Fields...>
{
void check(uint1024_t const& data) const override
{
unsigned int data_wf = static_cast<unsigned int>(data & 0b0011);
if (data_wf == 0b10)
return;
unsigned int data_cont_label = static_cast<unsigned int>((data & 0b111100) >> 2);
std::stringstream ss;
ss << "Expected Extension word";
ss << ". But found " << codeword_format(data_wf);
if (data_wf == 1)
ss << data_cont_label;
throw IllegalCodeWord(ss.str());
}
static bool match(uint1024_t const& data)
{
unsigned int data_wf = static_cast<unsigned int>(data & 0b0011);
return (data_wf == 0b10);
}
static std::string name() {
std::stringstream ss;
ss << "E" << label;
return ss.str();
}
std::string codeword_name() const override
{
return Extension::name();
}
};
template<unsigned int cont_label, class ...Fields> struct Continuation: public Codeword<1,
T_1551_1_CONTINUATION_WORD_LABEL<2>,
Fields...>
{
void check(uint1024_t const& data) const override
{
unsigned int data_wf = static_cast<unsigned int>(data & 0b0011);
unsigned int data_cont_label = static_cast<unsigned int>((data & 0b111100) >> 2);
if ((data_wf == 0b01) && (data_cont_label == cont_label))
return;
std::stringstream ss;
ss << "Expected Continuation Word C" << cont_label;
ss << ". But found " << codeword_format(data_wf);
if (data_wf == 1)
ss << data_cont_label;
throw IllegalCodeWord(ss.str());
}
static bool match(uint1024_t const& data)
{
unsigned int data_wf = static_cast<unsigned int>(data & 0b0011);
unsigned int data_cont_label = static_cast<unsigned int>((data & 0b111100) >> 2);
return ((data_wf == 0b01) && (data_cont_label == cont_label));
}
static unsigned int get_continuation_word_label(uint1024_t const& data){
return static_cast<unsigned int>((data & 0b111100) >> 2);
}
static std::string name() {
std::stringstream ss;
ss << "C" << cont_label;
return ss.str();
}
std::string codeword_name() const override
{
return Continuation::name();
}
};
}
std::ostream& operator<<(std::ostream& os, L16::ICodeword const& elt);
#endif // BASE_CODEWORD_H_233920180208