source: flair-src/trunk/lib/FlairCore/src/FrameworkManager_impl.cpp@ 233

Last change on this file since 233 was 214, checked in by Sanahuja Guillaume, 6 years ago

matrix

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