1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2011/08/31
|
---|
6 | // filename: FrameworkManager.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: Classe de base de la librairie
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "FrameworkManager_impl.h"
|
---|
19 | #include "FrameworkManager.h"
|
---|
20 | #include "Widget_impl.h"
|
---|
21 | #include <unistd.h>
|
---|
22 | #include "IODevice.h"
|
---|
23 | #include "IODevice_impl.h"
|
---|
24 | #include "TabWidget.h"
|
---|
25 | #include "Layout.h"
|
---|
26 | #include "PushButton.h"
|
---|
27 | #include "communication.h"
|
---|
28 | #include "config.h"
|
---|
29 | #include "Watchdog.h"
|
---|
30 | #include <errno.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include <arpa/inet.h>
|
---|
33 | #include <sys/mman.h>
|
---|
34 | #include <execinfo.h>
|
---|
35 | #include <signal.h>
|
---|
36 | #include <fcntl.h>
|
---|
37 | #ifdef __XENO__
|
---|
38 | #include <native/task.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | using namespace std;
|
---|
42 | using namespace flair::core;
|
---|
43 | using namespace flair::gui;
|
---|
44 |
|
---|
45 | ui_com *FrameworkManager_impl::com = NULL;
|
---|
46 | FrameworkManager_impl *FrameworkManager_impl::_this = NULL;
|
---|
47 |
|
---|
48 | namespace {
|
---|
49 | #ifdef __XENO__
|
---|
50 |
|
---|
51 | #ifdef SIGDEBUG
|
---|
52 | static const char *reason_str[] = {
|
---|
53 | "undefined", "received signal", "invoked syscall", "triggered fault",
|
---|
54 | "affected by priority inversion", "missing mlockall", "runaway thread",
|
---|
55 | };
|
---|
56 |
|
---|
57 | void warn_upon_switch(int sig, siginfo_t *si, void *context) {
|
---|
58 | unsigned int reason = si->si_value.sival_int;
|
---|
59 | void *bt[32];
|
---|
60 | int nentries;
|
---|
61 | #ifdef SIGDEBUG_WATCHDOG
|
---|
62 | printf("\nSIGDEBUG received, reason %d: %s\n", reason,
|
---|
63 | reason <= SIGDEBUG_WATCHDOG ? reason_str[reason] : "<unknown>");
|
---|
64 | #endif
|
---|
65 | // Dump a backtrace of the frame which caused the switch to secondary mode:
|
---|
66 | nentries = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
|
---|
67 | backtrace_symbols_fd(bt, nentries, fileno(stdout));
|
---|
68 | }
|
---|
69 | #else // SIGDEBUG
|
---|
70 | void warn_upon_switch(int sig __attribute__((unused))) {
|
---|
71 | void *bt[32];
|
---|
72 | int nentries;
|
---|
73 |
|
---|
74 | /* Dump a backtrace of the frame which caused the switch to
|
---|
75 | secondary mode: */
|
---|
76 | nentries = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
|
---|
77 | backtrace_symbols_fd(bt, nentries, fileno(stdout));
|
---|
78 | }
|
---|
79 | #endif // SIGDEBUG
|
---|
80 | #endif //__XENO__
|
---|
81 | void seg_fault(int sig __attribute__((unused))) {
|
---|
82 | void *bt[32];
|
---|
83 | int nentries;
|
---|
84 |
|
---|
85 | printf("Segmentation fault:\n");
|
---|
86 | /* Dump a backtrace of the frame which caused the segfault: */
|
---|
87 | nentries = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
|
---|
88 | backtrace_symbols_fd(bt, nentries, fileno(stdout));
|
---|
89 |
|
---|
90 | exit(1);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | FrameworkManager_impl::FrameworkManager_impl(FrameworkManager *self,
|
---|
95 | string name)
|
---|
96 | : Thread(self, "FrameworkManager", FRAMEWORK_TASK_PRIORITY) {
|
---|
97 | this->self = self;
|
---|
98 | is_logging = false;
|
---|
99 | logger_defined = false;
|
---|
100 | disable_errors = false;
|
---|
101 | ui_defined = false;
|
---|
102 | rcv_buf = NULL;
|
---|
103 | _this = this;
|
---|
104 |
|
---|
105 | // Avoids memory swapping for this program
|
---|
106 | mlockall(MCL_CURRENT | MCL_FUTURE);
|
---|
107 | struct sigaction sa;
|
---|
108 |
|
---|
109 | // catch segfault
|
---|
110 | signal(SIGSEGV, seg_fault);
|
---|
111 |
|
---|
112 | // catch primary->secondary switch
|
---|
113 | #ifdef __XENO__
|
---|
114 | #ifdef SIGDEBUG
|
---|
115 | sigemptyset(&sa.sa_mask);
|
---|
116 | sa.sa_sigaction = warn_upon_switch;
|
---|
117 | sa.sa_flags = SA_SIGINFO;
|
---|
118 | sigaction(SIGDEBUG, &sa, NULL);
|
---|
119 | #else
|
---|
120 | signal(SIGXCPU, warn_upon_switch);
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | string task_name = "Framework_" + name;
|
---|
124 | int status = rt_task_shadow(NULL, task_name.c_str(), 10, 0);
|
---|
125 | if (status != 0) {
|
---|
126 | self->Err("rt_task_shadow error (%s)\n", strerror(-status));
|
---|
127 | }
|
---|
128 |
|
---|
129 | #endif //__XENO__
|
---|
130 | }
|
---|
131 |
|
---|
132 | void FrameworkManager_impl::ConnectionLost(void) {
|
---|
133 | Err("connection lost\n");
|
---|
134 | gcs_watchdog->SafeStop();
|
---|
135 | connection_lost = true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | FrameworkManager_impl::~FrameworkManager_impl() {
|
---|
139 | // Printf("destruction FrameworkManager_impl\n");
|
---|
140 | int status;
|
---|
141 |
|
---|
142 | SafeStop();
|
---|
143 | Join();
|
---|
144 |
|
---|
145 | if (rcv_buf != NULL)
|
---|
146 | free(rcv_buf);
|
---|
147 |
|
---|
148 | if (logger_defined == true) {
|
---|
149 | continuer = false;
|
---|
150 | (void)pthread_join(log_th, NULL);
|
---|
151 |
|
---|
152 | status = DeletePipe(&cmd_pipe);
|
---|
153 | if (status != 0) {
|
---|
154 | Err("Error deleting pipe (%s)\n", strerror(-status));
|
---|
155 | }
|
---|
156 | status = DeletePipe(&data_pipe);
|
---|
157 | if (status != 0) {
|
---|
158 | Err("Error deleting pipe (%s)\n", strerror(-status));
|
---|
159 | }
|
---|
160 |
|
---|
161 | #ifdef __XENO__
|
---|
162 | status = rt_heap_delete(&log_heap);
|
---|
163 | if (status != 0) {
|
---|
164 | Err("rt_heap_delete error (%s)\n", strerror(-status));
|
---|
165 | }
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | logs.clear();
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (file_doc != NULL)
|
---|
172 | xmlFreeDoc(file_doc);
|
---|
173 |
|
---|
174 | if (ui_defined)
|
---|
175 | delete top_layout;
|
---|
176 |
|
---|
177 | if (com != NULL) {
|
---|
178 | delete com;
|
---|
179 | status = UDT::close(com_sock);
|
---|
180 | if (status != 0)
|
---|
181 | printf("Error udt::close %s", UDT::getlasterror().getErrorMessage());
|
---|
182 |
|
---|
183 | status = UDT::close(file_sock);
|
---|
184 | if (status != 0)
|
---|
185 | printf("Error udt::close %s", UDT::getlasterror().getErrorMessage());
|
---|
186 |
|
---|
187 | SleepMS(200); // a revoir, sinon UDT::cleanup bloque en RT
|
---|
188 | UDT::cleanup();
|
---|
189 | }
|
---|
190 |
|
---|
191 | // Printf("destruction FrameworkManager_impl ok\n");
|
---|
192 | }
|
---|
193 |
|
---|
194 | void FrameworkManager_impl::SetupConnection(string address, uint16_t port,
|
---|
195 | size_t rcv_buf_size) {
|
---|
196 | UDT::startup();
|
---|
197 | this->rcv_buf_size = rcv_buf_size;
|
---|
198 |
|
---|
199 | // socket file_socket, doit être créé en premier, cf station sol
|
---|
200 | Printf("Connecting to %s:%i\n", address.c_str(), port);
|
---|
201 | file_sock = GetSocket(address, port);
|
---|
202 | com_sock = GetSocket(address, port);
|
---|
203 |
|
---|
204 | // receive buffer allocation
|
---|
205 | rcv_buf = (char *)malloc(rcv_buf_size);
|
---|
206 | if (rcv_buf == NULL) {
|
---|
207 | Err("receive buffer malloc error\n");
|
---|
208 | }
|
---|
209 |
|
---|
210 | com = new ui_com(this, com_sock);
|
---|
211 |
|
---|
212 | // file managment
|
---|
213 | bool blocking = true;
|
---|
214 | UDT::setsockopt(file_sock, 0, UDT_SNDSYN, &blocking, sizeof(bool));
|
---|
215 | UDT::setsockopt(file_sock, 0, UDT_RCVSYN, &blocking, sizeof(bool));
|
---|
216 |
|
---|
217 | // configure timeout of blocking receive, short timeout to have priority on
|
---|
218 | // the other socket (see run method)
|
---|
219 | int timeout = 1; // ms
|
---|
220 | UDT::setsockopt(file_sock, 0, UDT_RCVTIMEO, &timeout, sizeof(int));
|
---|
221 |
|
---|
222 | timeout = 100; // ms
|
---|
223 | UDT::setsockopt(com_sock, 0, UDT_RCVTIMEO, &timeout, sizeof(int));
|
---|
224 |
|
---|
225 | Start();
|
---|
226 |
|
---|
227 | // watchdog for connection with ground station
|
---|
228 | connection_lost = false;
|
---|
229 | gcs_watchdog = new Watchdog(
|
---|
230 | this, std::bind(&FrameworkManager_impl::ConnectionLost, this),
|
---|
231 | (Time)1000000000);
|
---|
232 | gcs_watchdog->Start();
|
---|
233 | }
|
---|
234 |
|
---|
235 | void FrameworkManager_impl::SetupUserInterface(string xml_file) {
|
---|
236 | ui_defined = true;
|
---|
237 | this->xml_file = xml_file;
|
---|
238 |
|
---|
239 | // top_layout=new Layout(NULL,XML_ROOT_ELEMENT,XML_ROOT_TYPE);
|
---|
240 | top_layout = new Layout(NULL, self->ObjectName(), XML_ROOT_TYPE);
|
---|
241 |
|
---|
242 | // xml setup of the main widget
|
---|
243 | if (xml_file != "") {
|
---|
244 | xmlNodePtr *file_node = &(((Widget *)(top_layout))->pimpl_->file_node);
|
---|
245 | file_doc = xmlParseFile(xml_file.c_str());
|
---|
246 | if (file_doc == NULL) {
|
---|
247 | self->Warn("XML document not parsed successfully. Creating a new one.\n");
|
---|
248 | file_doc = xmlNewDoc((xmlChar *)"1.0");
|
---|
249 | *file_node = xmlNewNode(NULL, (xmlChar *)XML_ROOT_TYPE);
|
---|
250 | xmlSetProp(*file_node, (xmlChar *)"name",
|
---|
251 | (xmlChar *)ObjectName().c_str());
|
---|
252 | xmlDocSetRootElement(file_doc, *file_node);
|
---|
253 | // PrintXml();
|
---|
254 | } else {
|
---|
255 | *file_node = xmlDocGetRootElement(file_doc);
|
---|
256 | if (xmlStrcmp((*file_node)->name, (xmlChar *)XML_ROOT_TYPE)) {
|
---|
257 | self->Warn("%s, no match found in xml file\n", XML_ROOT_TYPE);
|
---|
258 | *file_node = xmlNewNode(NULL, (xmlChar *)XML_ROOT_TYPE);
|
---|
259 | xmlSetProp(*file_node, (xmlChar *)"name",
|
---|
260 | (xmlChar *)ObjectName().c_str());
|
---|
261 | xmlDocSetRootElement(file_doc, *file_node);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | } else {
|
---|
265 | self->Err("xml file not defined\n");
|
---|
266 | }
|
---|
267 |
|
---|
268 | // gui
|
---|
269 | tabwidget =
|
---|
270 | new TabWidget(top_layout->At(0, 0), XML_MAIN_TABWIDGET, TabWidget::North);
|
---|
271 | save_button =
|
---|
272 | new PushButton(top_layout->At(1, 0),
|
---|
273 | "save config on target (" + self->ObjectName() + ")");
|
---|
274 | // load_button=new PushButton(top_layout->At(1,1),"load config on target (" +
|
---|
275 | // self->ObjectName() + ")");
|
---|
276 | }
|
---|
277 |
|
---|
278 | // in case of RT, this thread switches to secondary mode when calling
|
---|
279 | // com->Receive
|
---|
280 | // it switches back to RT in ProcessXML when mutex are locked
|
---|
281 | void FrameworkManager_impl::Run(void) {
|
---|
282 | while (!ToBeStopped()) {
|
---|
283 | ssize_t bytesRead;
|
---|
284 |
|
---|
285 | bytesRead = com->Receive(rcv_buf, rcv_buf_size);
|
---|
286 |
|
---|
287 | if (bytesRead == (ssize_t)rcv_buf_size)
|
---|
288 | Err("FrameworkManager max receive size, augmenter le buffer size!\n");
|
---|
289 |
|
---|
290 | if (bytesRead > 0) {
|
---|
291 | // printf("recu %ld, trame %x\n",bytesRead,(uint8_t)rcv_buf[0]);
|
---|
292 | // rcv_buf[bytesRead-1]=0;//pour affichage
|
---|
293 | // printf("%s\n",rcv_buf);
|
---|
294 |
|
---|
295 | switch ((uint8_t)rcv_buf[0]) {
|
---|
296 | case XML_HEADER: {
|
---|
297 | xmlDoc *doc;
|
---|
298 | rcv_buf[bytesRead] = 0;
|
---|
299 |
|
---|
300 | doc = xmlReadMemory(rcv_buf, (int)bytesRead, "include.xml",
|
---|
301 | "ISO-8859-1", 0);
|
---|
302 | xmlNode *cur_node = NULL;
|
---|
303 |
|
---|
304 | for (cur_node = xmlDocGetRootElement(doc); cur_node;
|
---|
305 | cur_node = cur_node->next) {
|
---|
306 | if (cur_node->type == XML_ELEMENT_NODE) {
|
---|
307 | if (!xmlStrcmp(cur_node->name, (xmlChar *)XML_ROOT_TYPE)) {
|
---|
308 | #ifdef __XENO__
|
---|
309 | WarnUponSwitches(
|
---|
310 | true); // ProcessXML should not switch to secondary mode
|
---|
311 | #endif
|
---|
312 | top_layout->Widget::pimpl_->ProcessXML(cur_node->children);
|
---|
313 | #ifdef __XENO__
|
---|
314 | WarnUponSwitches(false); // other parts of this thread can switch
|
---|
315 | // to secondary mode
|
---|
316 | #endif
|
---|
317 | break;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | xmlFreeDoc(doc);
|
---|
323 |
|
---|
324 | if (save_button->Clicked())
|
---|
325 | SaveXml();
|
---|
326 |
|
---|
327 | SaveXmlChange(rcv_buf);
|
---|
328 |
|
---|
329 | break;
|
---|
330 | }
|
---|
331 | case WATCHDOG_HEADER: {
|
---|
332 | gcs_watchdog->Touch();
|
---|
333 | break;
|
---|
334 | }
|
---|
335 | default:
|
---|
336 | Err("unknown id: %x\n", (uint8_t)rcv_buf[0]);
|
---|
337 | break;
|
---|
338 | }
|
---|
339 | } else {
|
---|
340 | if (com->ConnectionLost())
|
---|
341 | SleepMS(10); // avoid infinite loop in this case
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | void FrameworkManager_impl::SaveXml(void) {
|
---|
347 | if (ui_defined)
|
---|
348 | xmlSaveFormatFile(xml_file.c_str(), file_doc, 1);
|
---|
349 | }
|
---|
350 |
|
---|
351 | void FrameworkManager_impl::SaveXmlChange(char *buf) {
|
---|
352 | if (is_logging == true) {
|
---|
353 | FILE *xml_change;
|
---|
354 | char filename[256];
|
---|
355 | Time time = GetTime();
|
---|
356 |
|
---|
357 | sprintf(filename, "%s/changes_at_%lld.xml", log_path.c_str(), time);
|
---|
358 |
|
---|
359 | xml_change = fopen(filename, "a");
|
---|
360 | fprintf(xml_change, "%s", buf);
|
---|
361 | fclose(xml_change);
|
---|
362 |
|
---|
363 | sprintf(filename, "changes_at_%lld.xml", time);
|
---|
364 | xml_changes.push_back(filename);
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | void FrameworkManager_impl::SendFile(string path, string name) {
|
---|
369 | char *buf, *more_buf;
|
---|
370 | int size;
|
---|
371 | filebuf *pbuf;
|
---|
372 | ssize_t nb_write;
|
---|
373 | string filename = path + "/" + name;
|
---|
374 |
|
---|
375 | // open the file
|
---|
376 | fstream ifs(filename.c_str(), ios::in | ios::binary);
|
---|
377 | ifs.seekg(0, ios::end);
|
---|
378 | size = ifs.tellg();
|
---|
379 | ifs.seekg(0, ios::beg);
|
---|
380 | pbuf = ifs.rdbuf();
|
---|
381 |
|
---|
382 | if (size <= 0) {
|
---|
383 | Err("error opening file %s\n", filename.c_str());
|
---|
384 | return;
|
---|
385 | }
|
---|
386 |
|
---|
387 | buf = (char *)malloc(sizeof(uint8_t) + sizeof(int) + name.size());
|
---|
388 | if (buf == NULL) {
|
---|
389 | Err("malloc error, not sending file\n");
|
---|
390 | return;
|
---|
391 | }
|
---|
392 |
|
---|
393 | if (IsBigEndian()) {
|
---|
394 | buf[0] = FILE_INFO_BIG_ENDIAN;
|
---|
395 | } else {
|
---|
396 | buf[0] = FILE_INFO_LITTLE_ENDIAN;
|
---|
397 | }
|
---|
398 |
|
---|
399 | memcpy(buf + 1, &size, sizeof(int));
|
---|
400 | memcpy(buf + 1 + sizeof(int), name.c_str(), name.size());
|
---|
401 | Printf("sending %s, size: %i\n", filename.c_str(), size);
|
---|
402 | // send file information
|
---|
403 | UDT::sendmsg(file_sock, buf, sizeof(uint8_t) + sizeof(int) + name.size(), -1,
|
---|
404 | true);
|
---|
405 |
|
---|
406 | more_buf = (char *)realloc((void *)buf, size);
|
---|
407 | if (more_buf == NULL) {
|
---|
408 | Err("realloc error, not sending file\n");
|
---|
409 | free(buf);
|
---|
410 | return;
|
---|
411 | } else {
|
---|
412 | buf = more_buf;
|
---|
413 | }
|
---|
414 |
|
---|
415 | pbuf->sgetn(buf, size);
|
---|
416 | // send the file
|
---|
417 | nb_write = UDT::sendmsg(file_sock, buf, size, -1, true);
|
---|
418 |
|
---|
419 | if (nb_write < 0) {
|
---|
420 | Err("UDT::sendmsg error (%s)\n", UDT::getlasterror().getErrorMessage());
|
---|
421 | } else if (nb_write != size) {
|
---|
422 | Err("UDT::sendmsg error, sent %ld/%i\n", nb_write, size);
|
---|
423 | }
|
---|
424 |
|
---|
425 | ifs.close();
|
---|
426 | free(buf);
|
---|
427 | }
|
---|
428 |
|
---|
429 | void FrameworkManager_impl::FinishSending() {
|
---|
430 | char rm_cmd[256];
|
---|
431 |
|
---|
432 | // send orignal xml
|
---|
433 | SendFile(log_path, "setup.xml");
|
---|
434 | sprintf(rm_cmd, "rm %s/setup.xml", log_path.c_str());
|
---|
435 | system(rm_cmd);
|
---|
436 |
|
---|
437 | // send xml changes
|
---|
438 | for (size_t i = 0; i < xml_changes.size(); i++) {
|
---|
439 | // Printf("%s\n",xml_changes.at(i).c_str());
|
---|
440 | SendFile(log_path, xml_changes.at(i).c_str());
|
---|
441 | sprintf(rm_cmd, "rm %s/%s", log_path.c_str(), xml_changes.at(i).c_str());
|
---|
442 | system(rm_cmd);
|
---|
443 | }
|
---|
444 | xml_changes.clear();
|
---|
445 |
|
---|
446 | // end notify
|
---|
447 | char buf = END;
|
---|
448 | int nb_write = UDT::sendmsg(file_sock, &buf, 1, -1, true);
|
---|
449 |
|
---|
450 | if (nb_write < 0) {
|
---|
451 | Err("UDT::sendmsg error (%s)\n", UDT::getlasterror().getErrorMessage());
|
---|
452 | } else if (nb_write != 1) {
|
---|
453 | Err("UDT::sendmsg error, sent %i/%i\n", nb_write, 1);
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | UDTSOCKET FrameworkManager_impl::GetSocket(string address, uint16_t port) {
|
---|
458 | while (1) {
|
---|
459 | UDTSOCKET new_fd;
|
---|
460 | new_fd = UDT::socket(AF_INET, SOCK_DGRAM, 0);
|
---|
461 | if (new_fd == UDT::INVALID_SOCK) {
|
---|
462 | Err("socket error: %s\n", UDT::getlasterror().getErrorMessage());
|
---|
463 | return 0;
|
---|
464 | }
|
---|
465 |
|
---|
466 | sockaddr_in serv_addr;
|
---|
467 | serv_addr.sin_family = AF_INET;
|
---|
468 | serv_addr.sin_port = htons(short(port));
|
---|
469 |
|
---|
470 | if (inet_pton(AF_INET, address.c_str(), &serv_addr.sin_addr) <= 0) {
|
---|
471 | Err("incorrect network address\n");
|
---|
472 | return 0;
|
---|
473 | }
|
---|
474 |
|
---|
475 | memset(&(serv_addr.sin_zero), '\0', 8);
|
---|
476 |
|
---|
477 | if (UDT::ERROR ==
|
---|
478 | UDT::connect(new_fd, (sockaddr *)&serv_addr, sizeof(serv_addr))) {
|
---|
479 | // Printf("connect error: %s
|
---|
480 | // %i\n",UDT::getlasterror().getErrorMessage(),UDT::getlasterror().getErrorCode());
|
---|
481 | UDT::close(new_fd);
|
---|
482 | if (UDT::getlasterror().getErrorCode() != 1001 &&
|
---|
483 | UDT::getlasterror().getErrorCode() != 1002) {
|
---|
484 | Err("connect error: %s\n", UDT::getlasterror().getErrorMessage());
|
---|
485 | return 0;
|
---|
486 | }
|
---|
487 | } else {
|
---|
488 | // printf("connected to
|
---|
489 | // %s:%i\n",inet_ntoa(serv_addr.sin_addr),serv_addr.sin_port);
|
---|
490 | return new_fd;
|
---|
491 | }
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | #ifdef __XENO__
|
---|
496 | int FrameworkManager_impl::CreatePipe(RT_PIPE *fd, string name) {
|
---|
497 | name = self->ObjectName() + "-" + name;
|
---|
498 | // xenomai limitation
|
---|
499 | if (name.size() > 31)
|
---|
500 | self->Err("rt_pipe_create error (%s is too long)\n", name.c_str());
|
---|
501 | // start log writter
|
---|
502 | #ifdef RT_PIPE_SIZE
|
---|
503 | return rt_pipe_create(fd, name.c_str(), P_MINOR_AUTO, RT_PIPE_SIZE);
|
---|
504 | #else
|
---|
505 | return rt_pipe_create(fd, name.c_str(), P_MINOR_AUTO, 0);
|
---|
506 | #endif
|
---|
507 | }
|
---|
508 | #else
|
---|
509 | int FrameworkManager_impl::CreatePipe(int (*fd)[2], string name) {
|
---|
510 | // if(pipe2(fd[0],O_NONBLOCK) == -1)
|
---|
511 | if (pipe(fd[0]) == -1) {
|
---|
512 | return errno;
|
---|
513 | } else {
|
---|
514 | int attr = fcntl((*fd)[0], F_GETFL, 0);
|
---|
515 | if (attr == -1) {
|
---|
516 | return errno;
|
---|
517 | }
|
---|
518 | if (fcntl((*fd)[0], F_SETFL, attr | O_NONBLOCK) == -1) {
|
---|
519 | return errno;
|
---|
520 | }
|
---|
521 | attr = fcntl((*fd)[1], F_GETFL, 0);
|
---|
522 | if (attr == -1) {
|
---|
523 | return errno;
|
---|
524 | }
|
---|
525 | if (fcntl((*fd)[1], F_SETFL, attr | O_NONBLOCK) == -1) {
|
---|
526 | return errno;
|
---|
527 | }
|
---|
528 |
|
---|
529 | return 0;
|
---|
530 | }
|
---|
531 | }
|
---|
532 | #endif
|
---|
533 |
|
---|
534 | #ifdef __XENO__
|
---|
535 | int FrameworkManager_impl::DeletePipe(RT_PIPE *fd) {
|
---|
536 | return rt_pipe_delete(fd);
|
---|
537 | }
|
---|
538 | #else
|
---|
539 | int FrameworkManager_impl::DeletePipe(int (*fd)[2]) {
|
---|
540 | int status1 = close((*fd)[0]);
|
---|
541 | int status2 = close((*fd)[1]);
|
---|
542 | if (status1 == 0 && status2 == 0)
|
---|
543 | return 0;
|
---|
544 | if (status1 != 0)
|
---|
545 | return status1;
|
---|
546 | if (status2 != 0)
|
---|
547 | return status2;
|
---|
548 | }
|
---|
549 | #endif
|
---|
550 |
|
---|
551 | void FrameworkManager_impl::SetupLogger(string log_path) {
|
---|
552 | if (logger_defined == true) {
|
---|
553 | Warn("SetupLogger() was already called.\n");
|
---|
554 | return;
|
---|
555 | }
|
---|
556 |
|
---|
557 | this->log_path = log_path;
|
---|
558 |
|
---|
559 | int status = CreatePipe(&cmd_pipe, "log_cmd");
|
---|
560 | if (status != 0) {
|
---|
561 | Err("Error creating pipe (%s)\n", strerror(-status));
|
---|
562 | return;
|
---|
563 | }
|
---|
564 |
|
---|
565 | status = CreatePipe(&data_pipe, "log_data");
|
---|
566 | if (status != 0) {
|
---|
567 | Err("Error creating pipe (%s)\n", strerror(-status));
|
---|
568 | return;
|
---|
569 | }
|
---|
570 |
|
---|
571 | #ifdef __XENO__
|
---|
572 | string tmp_name;
|
---|
573 | tmp_name = self->ObjectName() + "-log_heap";
|
---|
574 | status = rt_heap_create(&log_heap, tmp_name.c_str(), LOG_HEAP, H_FIFO);
|
---|
575 | if (status != 0) {
|
---|
576 | Err("rt_heap_create error (%s)\n", strerror(-status));
|
---|
577 | return;
|
---|
578 | }
|
---|
579 | #endif //__XENO__
|
---|
580 |
|
---|
581 | continuer = true;
|
---|
582 |
|
---|
583 | #ifdef NRT_STACK_SIZE
|
---|
584 | // Initialize thread creation attributes
|
---|
585 | pthread_attr_t attr;
|
---|
586 | if (pthread_attr_init(&attr) != 0) {
|
---|
587 | Err("pthread_attr_init error\n");
|
---|
588 | return;
|
---|
589 | }
|
---|
590 |
|
---|
591 | if (pthread_attr_setstacksize(&attr, NRT_STACK_SIZE) != 0) {
|
---|
592 | Err("pthread_attr_setstacksize error\n");
|
---|
593 | return;
|
---|
594 | }
|
---|
595 |
|
---|
596 | if (pthread_create(&log_th, &attr, write_log_user, (void *)this) < 0)
|
---|
597 | #else
|
---|
598 | if (pthread_create(&log_th, NULL, write_log_user, (void *)this) < 0)
|
---|
599 | #endif
|
---|
600 | {
|
---|
601 | Err("pthread_create error\n");
|
---|
602 | return;
|
---|
603 | }
|
---|
604 | #ifdef NRT_STACK_SIZE
|
---|
605 | if (pthread_attr_destroy(&attr) != 0) {
|
---|
606 | Err("pthread_attr_destroy error\n");
|
---|
607 | return;
|
---|
608 | }
|
---|
609 | #endif
|
---|
610 |
|
---|
611 | logger_defined = true;
|
---|
612 | }
|
---|
613 |
|
---|
614 | void FrameworkManager_impl::AddDeviceToLog(IODevice *device) {
|
---|
615 | if (logger_defined == false) {
|
---|
616 | Err("SetupLogger() was not called, not starting log\n");
|
---|
617 | return;
|
---|
618 | }
|
---|
619 |
|
---|
620 | if (is_logging == false) {
|
---|
621 | if (device->pimpl_->SetToBeLogged()) {
|
---|
622 | log_desc_t tmp;
|
---|
623 | tmp.device = device;
|
---|
624 | logs.push_back(tmp);
|
---|
625 | } else {
|
---|
626 | Warn("not adding it twice\n");
|
---|
627 | }
|
---|
628 | } else {
|
---|
629 | Err("impossible while logging\n");
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 | void FrameworkManager_impl::StartLog(void) {
|
---|
634 | if (logger_defined == false) {
|
---|
635 | Err("SetupLogger() was not called, not starting log\n");
|
---|
636 | return;
|
---|
637 | }
|
---|
638 |
|
---|
639 | ssize_t written;
|
---|
640 | size_t nb_err = 0;
|
---|
641 |
|
---|
642 | if (logs.size() == 0) {
|
---|
643 | Warn("Not starting log: nothing to log!\n");
|
---|
644 | return;
|
---|
645 | }
|
---|
646 |
|
---|
647 | if (is_logging == false) {
|
---|
648 | for (size_t i = 0; i < logs.size(); i++) {
|
---|
649 |
|
---|
650 | logs.at(i).running = true;
|
---|
651 | logs.at(i).dbtFile = NULL;
|
---|
652 | logs.at(i).size = logs.at(i).device->pimpl_->LogSize();
|
---|
653 | #ifdef __XENO__
|
---|
654 | written =
|
---|
655 | rt_pipe_write(&cmd_pipe, &logs.at(i), sizeof(log_desc_t), P_NORMAL);
|
---|
656 | #else
|
---|
657 | written = write(cmd_pipe[1], &logs.at(i), sizeof(log_desc_t));
|
---|
658 | #endif
|
---|
659 |
|
---|
660 | if (written < 0) {
|
---|
661 | Err("write pipe error (%s)\n", strerror(-written));
|
---|
662 | nb_err++;
|
---|
663 | logs.at(i).running = false;
|
---|
664 | } else if (written != sizeof(log_desc_t)) {
|
---|
665 | Err("write pipe error %ld/%ld\n", written, sizeof(log_desc_t));
|
---|
666 | nb_err++;
|
---|
667 | logs.at(i).running = false;
|
---|
668 | }
|
---|
669 | }
|
---|
670 |
|
---|
671 | if (nb_err != logs.size())
|
---|
672 | is_logging = true;
|
---|
673 | } else {
|
---|
674 | Warn("Already logging\n");
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 | void FrameworkManager_impl::StopLog(void) {
|
---|
679 | ssize_t written;
|
---|
680 |
|
---|
681 | if (is_logging == true) {
|
---|
682 | for (size_t i = 0; i < logs.size(); i++) {
|
---|
683 | logs.at(i).running = false;
|
---|
684 | }
|
---|
685 | // send only one running false condition, user thread will stop and send all
|
---|
686 | #ifdef __XENO__
|
---|
687 | written =
|
---|
688 | rt_pipe_write(&cmd_pipe, &logs.at(0), sizeof(log_desc_t), P_NORMAL);
|
---|
689 | #else
|
---|
690 | written = write(cmd_pipe[1], &logs.at(0), sizeof(log_desc_t));
|
---|
691 | #endif
|
---|
692 |
|
---|
693 | if (written < 0) {
|
---|
694 | Err("write pipe error (%s)\n", strerror(-written));
|
---|
695 | return;
|
---|
696 | } else if (written != sizeof(log_desc_t)) {
|
---|
697 | Err("write pipe error %ld/%ld\n", written, sizeof(log_desc_t));
|
---|
698 | return;
|
---|
699 | }
|
---|
700 |
|
---|
701 | is_logging = false;
|
---|
702 | } else {
|
---|
703 | Warn("Not logging\n");
|
---|
704 | }
|
---|
705 | }
|
---|
706 |
|
---|
707 | char *FrameworkManager_impl::GetBuffer(size_t sz) {
|
---|
708 | // Printf("alloc %i\n",sz);
|
---|
709 | #ifdef __XENO__
|
---|
710 | void *ptr;
|
---|
711 | int status = rt_heap_alloc(&log_heap, sz, TM_NONBLOCK, &ptr);
|
---|
712 | if (status != 0) {
|
---|
713 | Err("rt_heap_alloc error (%s)\n", strerror(-status));
|
---|
714 | ptr = NULL;
|
---|
715 | }
|
---|
716 | return (char *)ptr;
|
---|
717 | #else
|
---|
718 | return (char *)malloc(sz);
|
---|
719 | #endif
|
---|
720 | }
|
---|
721 |
|
---|
722 | void FrameworkManager_impl::ReleaseBuffer(char *buf) {
|
---|
723 | #ifdef __XENO__
|
---|
724 | int status = rt_heap_free(&log_heap, buf);
|
---|
725 |
|
---|
726 | if (status != 0) {
|
---|
727 | Err("rt_heap_free error (%s)\n", strerror(-status));
|
---|
728 | }
|
---|
729 | #else
|
---|
730 | free(buf);
|
---|
731 | #endif
|
---|
732 | }
|
---|
733 |
|
---|
734 | void FrameworkManager_impl::WriteLog(const char *buf, size_t size) {
|
---|
735 | ssize_t written;
|
---|
736 |
|
---|
737 | #ifdef __XENO__
|
---|
738 | written = rt_pipe_write(&data_pipe, buf, size, P_NORMAL);
|
---|
739 | #else
|
---|
740 | written = write(data_pipe[1], buf, size);
|
---|
741 | #endif
|
---|
742 |
|
---|
743 | if (written < 0) {
|
---|
744 | Err("erreur write pipe (%s)\n", strerror(-written));
|
---|
745 | } else if (written != (ssize_t)size) {
|
---|
746 | Err("erreur write pipe %ld/%ld\n", written, size);
|
---|
747 | }
|
---|
748 | }
|
---|
749 |
|
---|
750 | void *FrameworkManager_impl::write_log_user(void *arg) {
|
---|
751 | int cmd_pipe = -1;
|
---|
752 | int data_pipe = -1;
|
---|
753 | string filename;
|
---|
754 | fd_set set;
|
---|
755 | struct timeval timeout;
|
---|
756 | FrameworkManager_impl *caller = (FrameworkManager_impl *)arg;
|
---|
757 | int rv;
|
---|
758 |
|
---|
759 | vector<log_desc_t> logs;
|
---|
760 |
|
---|
761 | #ifdef __XENO__
|
---|
762 | filename = NRT_PIPE_PATH + caller->self->ObjectName() + "-log_cmd";
|
---|
763 | while (cmd_pipe < 0) {
|
---|
764 | cmd_pipe = open(filename.c_str(), O_RDWR);
|
---|
765 | if (cmd_pipe < 0 && errno != ENOENT)
|
---|
766 | caller->self->Err("open rt_pipe error: %s %i\n", filename.c_str(), errno);
|
---|
767 | usleep(1000);
|
---|
768 | }
|
---|
769 | filename = NRT_PIPE_PATH + caller->self->ObjectName() + "-log_data";
|
---|
770 | while (data_pipe < 0) {
|
---|
771 | data_pipe = open(filename.c_str(), O_RDWR);
|
---|
772 | if (data_pipe < 0 && errno != ENOENT)
|
---|
773 | caller->self->Err("open rt_pipe error: %s %i\n", filename.c_str(), errno);
|
---|
774 | usleep(1000);
|
---|
775 | }
|
---|
776 | #else
|
---|
777 | cmd_pipe = caller->cmd_pipe[0];
|
---|
778 | data_pipe = caller->data_pipe[0];
|
---|
779 | #endif
|
---|
780 |
|
---|
781 | while (caller->continuer == true) {
|
---|
782 | FD_ZERO(&set);
|
---|
783 | FD_SET(cmd_pipe, &set);
|
---|
784 | FD_SET(data_pipe, &set);
|
---|
785 |
|
---|
786 | timeout.tv_sec = 0;
|
---|
787 | timeout.tv_usec = SELECT_TIMEOUT_MS * 1000;
|
---|
788 | rv = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
|
---|
789 |
|
---|
790 | if (rv == -1) {
|
---|
791 | caller->Err("select error\n"); // an error accured
|
---|
792 | } else if (rv == 0) {
|
---|
793 | // printf("timeout write_log_user %s\n",caller->ObjectName().c_str()); //
|
---|
794 | // a timeout occured
|
---|
795 | } else {
|
---|
796 | if (FD_ISSET(cmd_pipe, &set)) {
|
---|
797 | log_desc_t tmp;
|
---|
798 | read(cmd_pipe, &tmp, sizeof(log_desc_t));
|
---|
799 |
|
---|
800 | if (tmp.running == true) // start logging
|
---|
801 | {
|
---|
802 | filename =
|
---|
803 | caller->log_path + "/" + caller->FileName(tmp.device) + ".dbt";
|
---|
804 | printf("Creating log file %s (log size %i)\n", filename.c_str(),
|
---|
805 | (int)tmp.size);
|
---|
806 | tmp.dbtFile = inithdFile((char *)filename.c_str(), UAV, tmp.size);
|
---|
807 | logs.push_back(tmp);
|
---|
808 |
|
---|
809 | if (logs.size() == 1) {
|
---|
810 | filename = caller->log_path + "/setup.xml";
|
---|
811 | xmlSaveFile(filename.c_str(), caller->file_doc);
|
---|
812 | }
|
---|
813 | } else // stop logging
|
---|
814 | {
|
---|
815 | for (size_t i = 0; i < logs.size(); i++) {
|
---|
816 | if (logs.at(i).dbtFile != NULL) {
|
---|
817 | close_hdfile(logs.at(i).dbtFile);
|
---|
818 |
|
---|
819 | filename = caller->FileName(logs.at(i).device) + ".dbt";
|
---|
820 | caller->SendFile(caller->log_path, filename);
|
---|
821 |
|
---|
822 | fstream txt_file;
|
---|
823 | filename = caller->FileName(logs.at(i).device) + ".txt";
|
---|
824 | txt_file.open((caller->log_path + "/" + filename).c_str(),
|
---|
825 | fstream::out);
|
---|
826 | txt_file << "1: time (us)\n2: time (ns)\n";
|
---|
827 | int index = 3;
|
---|
828 | logs.at(i).device->pimpl_->WriteLogsDescriptors(txt_file, &index);
|
---|
829 | txt_file.close();
|
---|
830 |
|
---|
831 | caller->SendFile(caller->log_path, filename);
|
---|
832 | }
|
---|
833 | }
|
---|
834 | // a revoir celui ci est le xml enregistré et pas forcement l'actuel
|
---|
835 | // if(caller->xml_file!="") caller->SendFile(caller->xml_file);
|
---|
836 | caller->FinishSending();
|
---|
837 |
|
---|
838 | logs.clear();
|
---|
839 | }
|
---|
840 | }
|
---|
841 |
|
---|
842 | if (FD_ISSET(data_pipe, &set)) {
|
---|
843 | log_header_t header;
|
---|
844 | read(data_pipe, &header, sizeof(log_header_t));
|
---|
845 |
|
---|
846 | for (size_t i = 0; i < logs.size(); i++) {
|
---|
847 | if (logs.at(i).device == header.device) {
|
---|
848 | char *buf = (char *)malloc(header.size);
|
---|
849 | read(data_pipe, buf, header.size);
|
---|
850 | // printf("%s \n",header.device->ObjectName().c_str());
|
---|
851 | // for(int i=0;i<header.size;i++) printf("%x ",buf[i]);
|
---|
852 | // printf("\n");
|
---|
853 | if (logs.at(i).size == header.size) {
|
---|
854 | if (logs.at(i).dbtFile != NULL) {
|
---|
855 | write_hdfile(
|
---|
856 | logs.at(i).dbtFile, buf, (road_time_t)(header.time / 1000),
|
---|
857 | (road_timerange_t)(header.time % 1000), header.size);
|
---|
858 | } else {
|
---|
859 | printf("err\n");
|
---|
860 | }
|
---|
861 | } else {
|
---|
862 | caller->self->Err("%s log size is not correct %i/%i\n",
|
---|
863 | header.device->ObjectName().c_str(),
|
---|
864 | header.size, logs.at(i).size);
|
---|
865 | }
|
---|
866 | free(buf);
|
---|
867 | }
|
---|
868 | }
|
---|
869 | }
|
---|
870 | }
|
---|
871 | }
|
---|
872 |
|
---|
873 | #ifdef __XENO__
|
---|
874 | close(cmd_pipe);
|
---|
875 | close(data_pipe);
|
---|
876 | #endif
|
---|
877 |
|
---|
878 | pthread_exit(0);
|
---|
879 | }
|
---|
880 |
|
---|
881 | string FrameworkManager_impl::FileName(IODevice *device) {
|
---|
882 | return getFrameworkManager()->ObjectName() + "_" + device->ObjectName();
|
---|
883 | }
|
---|
884 |
|
---|
885 | void FrameworkManager_impl::PrintXml(void) const {
|
---|
886 | xmlChar *xmlbuff;
|
---|
887 | int buffersize;
|
---|
888 | xmlDocDumpFormatMemory(file_doc, &xmlbuff, &buffersize, 1);
|
---|
889 | Printf("xml:\n%s\n", xmlbuff);
|
---|
890 | xmlFree(xmlbuff);
|
---|
891 | }
|
---|