logging loop, needs to be static to allow for re-alloc of GPS backends
| 489 | |
| 490 | // logging loop, needs to be static to allow for re-alloc of GPS backends |
| 491 | void AP_GPS_Backend::logging_loop(void) |
| 492 | { |
| 493 | while (true) { |
| 494 | hal.scheduler->delay(10); |
| 495 | static uint16_t lognum; |
| 496 | for (uint8_t instance=0; instance<2; instance++) { |
| 497 | if (logging[instance].fd == -1 && logging[instance].buf.available()) { |
| 498 | char fname[] = "gpsN_XXX.log"; |
| 499 | fname[3] = '1' + instance; |
| 500 | if (lognum == 0) { |
| 501 | for (lognum=1; lognum<1000; lognum++) { |
| 502 | struct stat st; |
| 503 | hal.util->snprintf(&fname[5], 8, "%03u.log", lognum); |
| 504 | if (AP::FS().stat(fname, &st) != 0) { |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | hal.util->snprintf(&fname[5], 8, "%03u.log", lognum); |
| 510 | logging[instance].fd = AP::FS().open(fname, O_WRONLY|O_CREAT|O_APPEND); |
| 511 | } |
| 512 | if (logging[instance].fd != -1) { |
| 513 | uint32_t n = 0; |
| 514 | const uint8_t *p; |
| 515 | while ((p = logging[instance].buf.readptr(n)) != nullptr && n != 0) { |
| 516 | struct { |
| 517 | uint32_t magic = 0x7fe53b04U; |
| 518 | uint32_t time_ms; |
| 519 | uint32_t n; |
| 520 | } header; |
| 521 | header.n = n; |
| 522 | header.time_ms = AP_HAL::millis(); |
| 523 | // short writes are unlikely and are ignored (only FS full errors) |
| 524 | AP::FS().write(logging[instance].fd, (const uint8_t *)&header, sizeof(header)); |
| 525 | AP::FS().write(logging[instance].fd, p, n); |
| 526 | logging[instance].buf.advance(n); |
| 527 | AP::FS().fsync(logging[instance].fd); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // logging thread start, needs to be non-static for thread_create |
| 535 | void AP_GPS_Backend::logging_start(void) |