-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
286 lines (231 loc) · 8.18 KB
/
main.cpp
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
#include <string>
#include <iomanip> // std::setprecision
#include <sched.h>
#include <csignal>
#include <fstream>
#include <condition_variable>
#include <future>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "common.h"
#include "LLController.h"
#include "utility/Config.h"
//#define TEST_LLSERVER
#ifdef TEST_LLSERVER
#include <thread>
#include "can/CANManager.h"
#include "can_houbolt/can_cmds.h"
#include "can_houbolt/channels/generic_channel_def.h"
#include <utility>
#include <string>
#include "utility/utils.h"
#define CAN_TEST_NODE_ID 8
#define TEST_NODE_INIT
//#define TEST_SPEAKER
//#define TEST_DATA
std::thread *testThread = nullptr;
void testFnc()
{
try{
//wait a sec before executing function
using namespace std::chrono_literals;
std::this_thread::sleep_for(2000ms);
CANManager *manager = CANManager::Instance();
#ifdef TEST_NODE_INIT
Can_MessageData_t msg = {0};
msg.bit.info.buffer = DIRECT_BUFFER;
msg.bit.info.channel_id = GENERIC_CHANNEL_ID;
msg.bit.cmd_id = GENERIC_RES_NODE_INFO;
NodeInfoMsg_t *info = (NodeInfoMsg_t *)msg.bit.data.uint8;
info->firmware_version = 0xdeadbeef;
info->channel_mask = 0x00000FFF;
info->channel_type[0] = CHANNEL_TYPE_ADC16;
// info->channel_type[1] = CHANNEL_TYPE_ADC16;
info->channel_type[1] = CHANNEL_TYPE_ADC16;
info->channel_type[2] = CHANNEL_TYPE_ADC24;
info->channel_type[3] = CHANNEL_TYPE_ADC16;
info->channel_type[4] = CHANNEL_TYPE_ADC16;
info->channel_type[5] = CHANNEL_TYPE_ADC16;
info->channel_type[6] = CHANNEL_TYPE_ADC16;
info->channel_type[7] = CHANNEL_TYPE_ADC16;
info->channel_type[8] = CHANNEL_TYPE_ADC16;
info->channel_type[9] = CHANNEL_TYPE_ADC16;
info->channel_type[10] = CHANNEL_TYPE_ADC16;
info->channel_type[11] = CHANNEL_TYPE_ADC16;
Can_MessageId_t canID = {0};
canID.info.direction = 0;
canID.info.priority = STANDARD_PRIORITY;
canID.info.special_cmd = STANDARD_SPECIAL_CMD;
canID.info.node_id = CAN_TEST_NODE_ID;
manager->OnCANRecv(0, canID.uint32, msg.uint8, sizeof(msg), utils::getCurrentTimestamp(), manager->canDriver);
std::this_thread::sleep_for(1000ms);
#endif
#ifdef TEST_DATA
msg = {0};
msg.bit.info.buffer = DIRECT_BUFFER;
msg.bit.info.channel_id = GENERIC_CHANNEL_ID;
msg.bit.cmd_id = GENERIC_RES_DATA;
SensorMsg_t sensorMsg = {0};
sensorMsg.channel_mask = 0x0000001D;
Can_MessageId_t dataCanID = {0};
dataCanID.info.direction = 1;
dataCanID.info.priority = STANDARD_PRIORITY;
dataCanID.info.special_cmd = STANDARD_SPECIAL_CMD;
dataCanID.info.node_id = CAN_TEST_NODE_ID;
uint8_t counter = 0;
while(running)
{
//adc24
sensorMsg.channel_data[0] = 0x20;
sensorMsg.channel_data[1] = 0x00;
sensorMsg.channel_data[2] = counter;
//digital out
sensorMsg.channel_data[3] = counter;
sensorMsg.channel_data[4] = 0x04;
sensorMsg.channel_data[5] = counter;
sensorMsg.channel_data[6] = 0x00;
sensorMsg.channel_data[7] = 0x00;
sensorMsg.channel_data[8] = 0x00;
sensorMsg.channel_data[9] = counter;
sensorMsg.channel_data[10] = 0xA0;
std::copy_n((uint8_t*)&sensorMsg.channel_mask, 4, msg.bit.data.uint8);
std::copy_n(sensorMsg.channel_data, 11, &msg.bit.data.uint8[4]);
//manager->OnCANRecv(0, dataCanID.uint32, msg.uint8, sizeof(msg), utils::getCurrentTimestamp());
std::this_thread::sleep_for(100ms);
counter++;
}
#endif
#ifdef TEST_SPEAKER
while (!controller->IsInitialized())
{
std::this_thread::sleep_for(1000ms);
}
Debug::error("setting test node state...");
StateController *stateController = StateController::Instance();
std::vector<std::string> states = {"testNode", "testNodeGetDiv", "testNodeSetDiv"};
stateController->AddUninitializedStates(states);
stateController->SetState("testNode", 1, std::chrono::high_resolution_clock::now().time_since_epoch().count());
stateController->SetState("testNodeGetDiv", 1, std::chrono::high_resolution_clock::now().time_since_epoch().count());
stateController->SetState("testNodeSetDiv", 1, std::chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
}
catch (std::exception &e)
{
Debug::print("error: %s", e.what());
testThread = nullptr;
raise(SIGTERM);
}
}
#endif
static int latency_target_fd = -1;
static int32_t latency_target_value = 0;
/* Latency trick
* if the file /dev/cpu_dma_latency exists,
* open it and write a zero into it. This will tell
* the power management system not to transition to
* a high cstate (in fact, the system acts like idle=poll)
* When the fd to /dev/cpu_dma_latency is closed, the behavior
* goes back to the system default.
*
* Documentation/power/pm_qos_interface.txt
*/
static void set_latency_target(void)
{
struct stat s;
int ret;
if (stat("/dev/cpu_dma_latency", &s) == 0) {
latency_target_fd = open("/dev/cpu_dma_latency", O_RDWR);
if (latency_target_fd == -1)
return;
ret = write(latency_target_fd, &latency_target_value, 4);
if (ret == 0) {
printf("# error setting cpu_dma_latency to %d!: %s\n", latency_target_value, strerror(errno));
close(latency_target_fd);
return;
}
printf("# /dev/cpu_dma_latency set to %dus\n", latency_target_value);
}
}
int main(int argc, char const *argv[])
{
// system("clear");
struct sched_param sp;
sp.sched_priority = 60;
sched_setscheduler(0, SCHED_FIFO, &sp);
set_latency_target();
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGINT);
sigaddset(&sigset, SIGTERM);
sigaddset(&sigset, SIGABRT);
pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
std::atomic<bool> shutdown_requested(false);
std::atomic<bool> initialized(false);
std::mutex cv_mutex;
std::condition_variable cv;
auto signal_handler = [&shutdown_requested, &initialized, &cv, &sigset]() {
int signum = 0;
// wait until a signal is delivered:
sigwait(&sigset, &signum);
shutdown_requested = true;
//signal before initialization done, kill early (NOT SIGNAL SAFE but QOL)
if (!initialized) {
std::cout << "\nearly shutdown, NOT SIGNAL SAFE\n" << std::endl;
LLController::Destroy();
exit(signum);
}
// notify all waiting workers to check their predicate:
cv.notify_all();
return signum;
};
auto ft_signal_handler = std::async(std::launch::async, signal_handler);
std::string configPath = "";
std::stringstream ss;
if (argc > 1)
{
std::string cfgPath(argv[1]);
configPath = cfgPath;
ss << "Config path given as argument, using this.\n";
}
else
{
if(const char* configPathFile = std::getenv("ECUI_CONFIG_PATH")) {
std::cout << "Config path env variable found: " << configPathFile << '\n';
configPath = std::string(configPathFile);
}
else {
std::cerr << "Config path is missing, try to add as an argument configure docker env variable 'ECUI_CONFIG_PATH=<your_path>' or use 'echo \"ECUI_CONFIG_PATH=<your_path>\" >> /etc/environment'!" << std::endl;
exit(EXIT_FAILURE);
}
}
ss << "Config path: " << configPath << "\n";
#ifdef TEST_LLSERVER
testThread = new std::thread(testFnc);
#endif
LLController *llController = LLController::Instance();
llController->Init(configPath);
std::cout << ss.str() << std::endl;
// wait for signal handler to complete
initialized = true;
int signum = ft_signal_handler.get();
#ifdef TEST_LLSERVER
if (testThread != nullptr && testThread->joinable())
{
testThread->join();
delete testThread;
}
#endif
Debug::print("posix signal fired: %d, shutting down...", signum);
try
{
LLController::Destroy();
}
catch (std::exception &e)
{
Debug::error("signal handler: failed to shutdown LLController, %s", e.what());
}
Debug::close();
return signum;
}