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

Last change on this file since 68 was 67, checked in by Sanahuja Guillaume, 8 years ago

corrections bugs checkpoint map

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