DESERT 3.5.1
Loading...
Searching...
No Matches
minterpreterTEL.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2016 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
36#include "minterpreterTEL.h"
37#include <uwmdriver.h>
38
39#include <cctype>
40#include <iostream>
41#include <string>
42
43static void
44hexdump(std::string name, std::string str)
45{
46 int len = str.size();
47 const char *data = str.c_str();
48
49 std::cout << name << "[" << len << "]: " << std::hex;
50 for (int i = 0; i < len; i++) {
51 std::cout.fill('0');
52 std::cout.width(2);
53 std::cout << std::right << (int) data[i];
54
55 if (std::isalnum(data[i]) || std::ispunct(data[i]))
56 std::cout << "(" << data[i] << ")";
57 std::cout << " ";
58 }
59
60 std::cout.width(0);
61 std::cout << std::dec << std::endl;
62}
63
64static std::string
65hexdumplog(std::string str)
66{
67 int len = str.size();
68 const char *data = str.c_str();
69
70 std::string str_out = "";
71
72 for (int i = 0; i < len; i++) {
73 if (std::isalnum(data[i]) || std::ispunct(data[i]))
74 str_out += data[i];
75 else {
76 std::string str;
77 std::stringstream sstr("");
78 sstr << "[" << std::hex << (unsigned int) (unsigned char) data[i]
79 << std::dec << "]";
80 sstr >> str;
81 str_out += str;
82 }
83 }
84
85 return str_out;
86}
87
88static std::string
89hexdumpdata(std::string str)
90{
91 size_t len = str.size();
92 const char *data = str.c_str();
93
94 std::string str_out = "";
95
96 for (size_t i = 0; i < len; i++) {
97 std::stringstream sstr("");
98 sstr.fill('0');
99 sstr.width(2);
100 sstr << std::hex << (unsigned int) (unsigned char) data[i] << std::dec;
101 str_out += sstr.str();
102 }
103 return str_out;
104}
105
106static std::string
107dechexdumpdata(std::string str)
108{
109 std::istringstream istr(str);
110 std::string str_out = "";
111 std::string str_sym("");
112
113 while (std::getline(istr, str_sym, ' ')) {
114 int symbol;
115 char ch;
116 std::stringstream sstr("");
117 sstr << "0x" << str_sym;
118 sstr >> std::hex >> symbol;
119 ch = symbol;
120 str_out += ch;
121 }
122
123 return str_out;
124}
125
127 : UWMinterpreter(pmDriver_)
128{
129 // Member initialization
130 rx_integrity = 0;
131}
132
136
137std::string
139{
140 std::string turnon_string;
141 std::stringstream astr("");
142 switch (_step) {
143 case 1:
144 astr << "gpio,clear,dsp_power";
145 break;
146 case 2:
147 astr << "gpio,set,ena_tx";
148 break;
149 case 3:
150 astr << "gpio,clear,fpga_reset";
151 break;
152 case 4:
153 astr << "gpio,clear,dsp_reset";
154 break;
155 }
156 astr >> turnon_string;
157 return turnon_string;
158}
159
160std::string
162{
163 std::string turnoff_string;
164 std::stringstream astr;
165 switch (_step) {
166 case 1:
167 astr << "gpio,set,dsp_reset";
168 break;
169 case 2:
170 astr << "gpio,set,fpga_reset";
171 break;
172 case 3:
173 astr << "gpio,set,dsp_power";
174 break;
175 }
176 astr >> turnoff_string;
177 return turnoff_string;
178}
179
180std::string
182{
183 return "gpio,get,fpga_busy";
184}
185
186std::string
188{
189 return "stop_listen";
190}
191
192std::string
194 int _gain, int _chipset, int _sl, int _th, int _mps_th)
195{
196 std::string config_string;
197 std::stringstream astr("");
198 astr << "config_dsp"
199 << ","
200 << "1"
201 << "," << _gain << "," << _chipset << "," << _sl << "," << _th << ","
202 << "0"
203 << "," << _mps_th;
204 astr >> config_string;
205
206 return config_string;
207}
208
209std::string
210MinterpreterTEL::build_recv_data(int msg_bytes, int _stop_f, double _delay)
211{
212 std::string recv_string;
213 std::stringstream astr;
214 if (msg_bytes != 0) {
215 astr << "rcv_sync_data"
216 << "," << msg_bytes << "," << _stop_f << "," << _delay;
217 } else {
218 astr << "rcv_sync_data"
219 << ","
220 << "0"
221 << "," << _stop_f << "," << _delay;
222 }
223 astr >> recv_string;
224 return recv_string;
225}
226
227std::string
228MinterpreterTEL::build_send_ctrl(std::string _ctrl, int _delay_f, double _delay)
229{
230 std::string ctrl_telegram;
231 std::stringstream astr;
232 std::string hexdumped_ctrl;
233
234 // hexdump the data
235 hexdumped_ctrl = hexdumpdata(_ctrl);
236
237 // ASCII coding: 8 bits per character
238 int bitlen = _ctrl.size() * 8;
239
240 astr << "send_sync_ctrl"
241 << "," << bitlen << "," << _delay_f << "," << _delay << ","
242 << hexdumped_ctrl;
243 astr >> ctrl_telegram;
244 return ctrl_telegram;
245}
246
247std::string
248MinterpreterTEL::build_send_data(std::string _data, int _delay_f, double _delay)
249{
250 std::string data_telegram;
251 std::stringstream astr;
252 std::string hexdumped_data;
253
254 // hexdump the data
255 hexdumped_data = hexdumpdata(_data);
256
257 // ASCII coding: 8 bits per character
258 int bitlen = hexdumped_data.size() * 4;
259
260 astr << "send_sync_data"
261 << "," << bitlen << "," << _delay_f << "," << _delay << ","
262 << hexdumped_data;
263 astr >> data_telegram;
264 return data_telegram;
265}
266
267std::string
269{
270 std::string bitrate_telegram;
271 std::stringstream astr;
272 if ((_bitrate < 0) || (_bitrate > 63)) {
273 std::cout << "ERROR: invalid bitrate code provided";
274 astr << "bitrate"
275 << ","
276 << "1";
277 astr >> bitrate_telegram;
278 } else {
279 astr << "bitrate"
280 << "," << _bitrate;
281 astr >> bitrate_telegram;
282 }
283 return bitrate_telegram;
284}
285
286std::string
288{
289 std::string clear_command("gpio,clear,tx_on");
290 return clear_command;
291}
292
293void
295{
296 // take everything up! for future needs...
297 std::string _TEL;
298 std::string _len, _data, _bitlen, _data_flag, _integrity, _rms, _error_code;
299 std::string _rcv_time, _sync_detected_time, _nshift, _speed;
300 std::string _sync_delta_phase, _symbol_phase_std, _symbol_mean_phase, _mps;
301
302 std::istringstream iastr(telegram);
303 getline(iastr, _TEL, ',');
304 getline(iastr, _len, ',');
305 getline(iastr, _data, ',');
306 getline(iastr, _bitlen, ',');
307 getline(iastr, _data_flag, ',');
308 getline(iastr, _integrity, ',');
309 getline(iastr, _rms, ',');
310 getline(iastr, _error_code, ',');
311
312 getline(iastr, _rcv_time, ',');
313 getline(iastr, _sync_detected_time, ',');
314 getline(iastr, _nshift, ',');
315 getline(iastr, _speed, ',');
316
317 getline(iastr, _sync_delta_phase, ',');
318 getline(iastr, _symbol_phase_std, ',');
319 getline(iastr, _symbol_mean_phase, ',');
320 getline(iastr, _mps, '\n');
321
322 // update the variable for packets: broadcast is set as dest, 0 as send
323 std::string c_data;
324 // erase double quotes
325 std::string _data_p = _data.erase(0, 1);
326 _data_p = _data_p.erase(_data_p.size() - 1, 1);
327
328 c_data = dechexdumpdata(_data_p);
329
330 pmDriver->updateRx(0, INT_MAX, c_data);
331 rx_integrity = atof(_integrity.c_str());
332}
std::string build_busy_FPGA()
Method which builds the string to ask the DSP if it is busy doing modulation/demodulation.
std::string build_send_data(std::string _data, int _delay_f, double _delay)
Method for building the TELEGRAM that will send DATA messages.
void parse_TELEGRAM(std::string telegram)
Method for parsing what is received upon receiving a TELEGRAM.
std::string build_poweron_DSP(int _step)
Method for building the string that allow turning on the DSP of the modem, which basically turns on t...
std::string build_clear_tx()
Method for building the COMMAND that will clear the tx_on pin.
std::string build_bitrate(int _bitrate)
Method for building the TELEGRAM that will set the bitrate used for transmission.
MinterpreterTEL(UWMdriver *)
Class constructor.
std::string build_stop_listen()
Method which builds the TELEGRAM that makes the physical layer to exit the listen mode.
~MinterpreterTEL()
Class destructor.
std::string build_send_ctrl(std::string _ctrl, int _delay_f, double _delay)
Method for building the TELEGRAM that will transmit CONTROL messages, which are short messanges of ma...
std::string build_recv_data(int msg_bytes, int _stop_f, double _delay)
Method for building the TELEGRAM that will allow receiving BITS, wheter CONTROL or DATA messages.
double rx_integrity
Integrity of the last received packet.
std::string build_poweroff_DSP(int _step)
Method for building the string for turning off the DSP of the modem which basically turns off the phy...
std::string build_config_DSP(int _gain, int _chipset, int _sl, int _th, int _mps_th)
Method for building the TELEGRAM that will allow configuring the DSP.
The class needed by UWMPhy_modem to handle the different transmissions cases and corresponding protoc...
void updateRx(int, int, std::string)
Method to write in UWMdriver::payload_rx, UWMdriver::src and UWMdriver::dstPktRx.
This class is in charge of building/parsing the necessary messages to make the UWMdriver able to comm...
UWMdriver * pmDriver
Link to the UWMdriver object which contains this UWMinterpreter object.
static std::string hexdumpdata(std::string str)
static std::string hexdumplog(std::string str)
static std::string dechexdumpdata(std::string str)
static void hexdump(std::string name, std::string str)
static std::string hexdumpdata(std::string str)
Class that is in charge of building and parsing the required messages to make the UWMdriver able to c...