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