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

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

added compile info

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