-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
executable file
·340 lines (277 loc) · 11.4 KB
/
Copy pathtest.cpp
File metadata and controls
executable file
·340 lines (277 loc) · 11.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <algorithm>
#include <arpa/inet.h>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cxxabi.h>
#include <dlfcn.h>
#include <execinfo.h>
#include <fcntl.h>
#include <filesystem>
#include <iostream>
#include <netinet/in.h>
#include <random>
#include <regex>
#include <signal.h>
#include <string>
#include <unistd.h>
#include "logging.h"
#include "utilities.h"
#include "TMessage.h"
#include "config.h"
#include "DataItems/ADC_.h"
#include "DataItems/BRD_.h"
#include "DataItems/CFG_.h"
#include "DataItems/DAC_.h"
#include "DataItems/REG_.h"
#include "DataItems/TDataItem.h"
using namespace std;
void SendControlHello();
int apci;
bool RunMessage(TMessage &aMessage)
{
Trace("Executing Message DataItems[].Go(), " + std::to_string(aMessage.DataItems.size()) + " total DataItems");
try
{
for (auto anItem : aMessage.DataItems)
{
Debug(anItem->AsString(true), anItem->AsBytes(true));
anItem->Go();
}
aMessage.setMId('R'); // FIX: should be performed based on anItem.getResultCode() indicating no errors
}
catch (std::logic_error e)
{
aMessage.setMId('X');
Error("EXCEPTION! " + std::string(e.what()));
Log("Error Message built: \n " + aMessage.AsString(true));
return false;
}
Log("Control Reply Message built: \n " + aMessage.AsString(true));
return true;
}
// Received: 4D 48 00 00 00 10 02 05 00 00 00 00 80 3F 0E 02 05 00 00 00 00 00 00 10 02 05 00 01 00 00 80 3F 0E 02 05 00 01 00 00 00 00 10 02 05 00 02 00 00 80 3F 0E 02 05 00 02 00 00 00 00 10 02 05 00 03 00 00 80 3F 0E 02 05 00 03 00 00 00 00 B3
int main(void) // "TEST"
{
std::string devicefile = "";
std::string devicepath = "/dev/apci";
// if apci directory exists
if(std::filesystem::exists(devicepath) && std::filesystem::is_directory(devicepath))
{
for (const auto &devfile : std::filesystem::directory_iterator(devicepath))
{
apci = open(devfile.path().c_str(), O_RDONLY);
if (apci >= 0)
{
devicefile = devfile.path().c_str();
break;
}
}
}
else
{
Error("APCI driver did not find a device in /dev/apci/");
return 1;
}
Log("Found device @ " + devicefile);
try
{
cout << '\n' << "0 ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
//TBytes msg = {0x4D, 0x48, 0x00, 0x00, 0x00, 0x10, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x0E, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x02, 0x05, 0x00, 0x01, 0x00, 0x00, 0x80, 0x3F, 0x0E, 0x02, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x02, 0x05, 0x00, 0x02, 0x00, 0x00, 0x80, 0x3F, 0x0E, 0x02, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x02, 0x05, 0x00, 0x03, 0x00, 0x00, 0x80, 0x3F, 0x0E, 0x02, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0xB3};
TBytes msg = {0x4D, 0x24, 0x00, 0x00, 0x00, 0x05, 0x01, 0x05, 0x00, 0x30, 0x00, 0x80, 0x30, 0x00, 0x05, 0x01, 0x05, 0x00, 0x30, 0x00, 0x80, 0x31, 0x00, 0x05, 0x01, 0x05, 0x00, 0x30, 0x00, 0x80, 0x32, 0x00, 0x05, 0x01, 0x05, 0x00, 0x30, 0x00, 0x80, 0x33, 0x00, 0xDD};
TError result;
TMessage *aMessage = new TMessage;
*aMessage = TMessage::FromBytes(msg, result);
if (result == ERR_SUCCESS)
Debug("GotMessage says valid");
RunMessage(*aMessage);
if (result != ERR_SUCCESS)
{
Error("TMessage::fromBytes(buf) returned " + std::to_string(-result) + err_msg[-result]);
return false;
}
Log("Received on Control connection:\n " + aMessage->AsString());
cout << '\n' << "1 ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
SendControlHello();
TMessage Message = TMessage('M');
cout << '\n' << "2 Constructor(TBytes Message) ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
TMessage Msg = TMessage(msg);
cout << '\n' << "3 Message.Go ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
Msg.Go();
cout << '\n' << "4 As Bytes ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
TBytes rbuf = Msg.AsBytes(true);
Trace("Replied with:\n bytes: ", rbuf);
cout << '\n' << "5 As String ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
cout << Msg.AsString(true) << '\n' << "------------------------------------------------" << '\n';
cout << "6 Constructor(id) ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
TBytes REG_Read3C = {0x4D, 0x05, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x3C, 0x6F };
TMessage Msg2 = TMessage(REG_Read3C);
cout << '\n' << "7 As String ----------------------------------------------------------------------------------------------" << '\n'<< '\n';
cout << Msg2.AsString(true) << '\n' << "-----------------------------------------------" << '\n';
cout << '\n' << "8 As Bytes -----------------------------------------------------------------------------------------------" << '\n'<< '\n';
Trace("8) bytes: ",Msg2.AsBytes(true));
printf("9 Testing\n-----------------------------------\n");
__u8 dacNum = 1;
float scale = 1.0;
float offset = 0.0;
__u16 len;
TMessageId MId_C = 'C';
TMessage Message2 = TMessage(MId_C);
PTDataItem item;
TBytes bytes;
bytes.push_back(0x0C);
bytes.push_back(0x02);
bytes.push_back(0x09);
bytes.push_back(0x00);
bytes.push_back(0x01); // DAC# 0
stuff<float>(bytes, 1.0); // scale
stuff<float>(bytes, -1.0); // offset
Trace("9) bytes == ", bytes);
TError res;
item = TDataItem::fromBytes(bytes, res);
item->setDId(DataItemIds::DAC_Calibrate1);
// std::shared_ptr<TConfigField<__u8, float, float>>
// item2(new TConfigField<__u8, float, float>(dacNum, scale, offset));
// Message.addDataItem(item2);
Message2.addDataItem(item);
// printf("Item pointer is %p", item->Data.data());
cout << Message2.AsString() << '\n';
Debug("bytes: ", Message2.AsBytes(true));
printf("\nTESTING\n\n");
//test(Message.AsBytes(), "TMessage(TDAC_Calibrate1(0, 1.0, 0.0))", ERR_SUCCESS);
Config.dacScaleCoefficients[1] = 9.1;
Config.dacOffsetCoefficients[1] = -66.66;
std::cout << "before " << Config.dacScaleCoefficients[1] << ", " << Config.dacOffsetCoefficients[1] << '\n';
item->Go();
std::cout << "after " << Config.dacScaleCoefficients[1] << ", " << Config.dacOffsetCoefficients[1]<<'\n';
}
catch (logic_error e)
{
cout << '\n'
<< "!! Exception caught: " << e.what() << '\n';
}
printf("\nDone.\n");
return 0;
}
TError test(TBytes Msg, const char *TestDescription, TError expectedResult = ERR_SUCCESS)
{
printf("TEST %s", TestDescription);
if (expectedResult != ERR_SUCCESS)
printf(" (testing error code %d, %s), ", expectedResult, err_msg[-expectedResult]);
printf("\n");
TError result;
try
{
result = TMessage::validateMessage(Msg);
if (result != expectedResult)
printf("FAIL: result (%d, %s) is not expected result (%d, %s)\n",
result, err_msg[-result], expectedResult, err_msg[-expectedResult]);
else
printf("PASS: result is as expected [%d, %s]\n", result, err_msg[-result]);
}
catch (logic_error e)
{
cout << '\n'
<< "Exception caught: " << e.what() << '\n'
<< "if the exception is " << expectedResult << " then this is PASS" << '\n';
}
printf("----------------------\n");
return result;
}
extern "C" {
void __cyg_profile_func_enter(void* func, void* callsite) __attribute__((no_instrument_function));
void __cyg_profile_func_exit(void* func, void* callsite) __attribute__((no_instrument_function));
}
bool is_ignored_function(const std::string& func_name) __attribute__((no_instrument_function));
bool is_ignored_function(const std::string& func_name) {
static std::regex ignored_patterns(R"(^.*\b(std::|__cxx|cxxabi|__gnu_cxx|operator new)\b.*)");
return std::regex_search(func_name, ignored_patterns);
}
void log_function(const char* action, void* func) __attribute__((no_instrument_function));
void log_function(const char* action, void* func) {
static thread_local bool inside_log_function = false;
if (inside_log_function) {
return;
}
inside_log_function = true;
Dl_info info;
if (dladdr(func, &info) && info.dli_sname) {
int status = -1;
char* demangled_name = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
if (status == 0) {
std::string demangled_str(demangled_name);
if (!is_ignored_function(demangled_str)) {
std::cout << action << ": " << demangled_name << '\n';
}
free(demangled_name);
} else {
std::string mangled_str(info.dli_sname);
if (!is_ignored_function(mangled_str)) {
std::cout << action << ": " << info.dli_sname << '\n';
}
}
}
inside_log_function = false;
}
void __cyg_profile_func_enter(void* func, void* callsite) {
static thread_local bool inside_log_function = false;
if (inside_log_function) {
return;
}
inside_log_function = true;
log_function("[ ENTER ] ", func);
inside_log_function = false;
}
void __cyg_profile_func_exit(void* func, void* callsite) {
static thread_local bool inside_log_function = false;
if (inside_log_function) {
return;
}
inside_log_function = true;
log_function("[ EXIT ] ", func);
inside_log_function = false;
}
bool done = false;
void SendControlHello()
{
TMessageId MId_Hello = 'H';
TPayload Payload;
TBytes data{4,3,2,1};
PTDataItem d2 = std::unique_ptr<TDataItem>(new TDataItem(DataItemIds::TCP_ConnectionID, data));
Payload.push_back(d2);
//__u32 dacRangeDefault = 0x3031E142;
//__u32 dacRangeDefault = 0x35303055;
for (int channel = 0; channel < 4; channel++)
{
data.clear();
data.push_back(channel);
for (int byt = 0; byt < sizeof(Config.dacRanges[channel]); byt++)
data.push_back((Config.dacRanges[channel] >> (8 * byt)) & 0x000000FF);
d2 = std::unique_ptr<TDataItem>(new TDataItem(DataItemIds::DAC_Range1, data));
Payload.push_back(d2);
}
PTDataItem features = std::unique_ptr<TBRD_Features>(new TBRD_Features());
PTDataItem deviceID = std::unique_ptr<TBRD_DeviceID>(new TBRD_DeviceID());
PTDataItem adcBaseClock = std::unique_ptr<TADC_BaseClock>(new TADC_BaseClock());
PTDataItem fpgaId = std::unique_ptr<TBRD_FpgaId>(new TBRD_FpgaId());
try
{
features->Go();
Payload.push_back(features);
deviceID->Go();
Payload.push_back(deviceID);
adcBaseClock->Go();
Payload.push_back(adcBaseClock);
fpgaId->Go();
Payload.push_back(fpgaId);
}
catch (std::logic_error e)
{
Error(e.what());
perror(e.what());
}
TMessage HelloControl = TMessage(MId_Hello, Payload);
TBytes rbuf = HelloControl.AsBytes(true);
Log("Sent 'Hello' to Control Client#:\n " + HelloControl.AsString(true)+"\n", HelloControl.AsBytes());
}