fpvstat.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # include <stdio.h>
  2. # include <stdint.h>
  3. # include <stdlib.h>
  4. # include <unistd.h>
  5. # include <string.h>
  6. # include <errno.h>
  7. # include <sys/types.h>
  8. # include <sys/stat.h>
  9. # include <fcntl.h>
  10. # include <arpa/inet.h>
  11. # include <getopt.h>
  12. struct fpv
  13. {
  14. uint32_t len;
  15. uint8_t type;
  16. uint32_t ms;
  17. uint8_t pad[8];
  18. } __attribute__((packed));
  19. struct cpu
  20. {
  21. uint8_t load;
  22. uint8_t temp;
  23. } __attribute__((packed));
  24. struct air
  25. {
  26. struct
  27. {
  28. int8_t rssi;
  29. uint32_t lost;
  30. } __attribute__((packed)) uplink;
  31. struct
  32. {
  33. int8_t rssi;
  34. uint32_t lost;
  35. } __attribute__((packed)) rc;
  36. struct cpu cpu;
  37. uint32_t blocks;
  38. uint32_t skipped;
  39. uint32_t failed;
  40. long long time;
  41. uint16_t bitrate;
  42. uint16_t measured_bitrate;
  43. uint8_t cts;
  44. uint8_t undervoltage;
  45. } __attribute__((packed));
  46. struct gnd
  47. {
  48. uint32_t broken;
  49. uint32_t lost;
  50. uint32_t skipped;
  51. uint32_t received;
  52. uint32_t real_bitrate;
  53. uint32_t measured_bitrate;
  54. uint32_t bitrate;
  55. uint32_t uplink_lost;
  56. uint32_t downlink_lost;
  57. uint32_t unused[3];
  58. int8_t air_rssi;
  59. int8_t joystick;
  60. struct
  61. {
  62. struct cpu gnd;
  63. struct cpu air;
  64. } __attribute__((packed)) cpu;
  65. uint32_t nics;
  66. struct
  67. {
  68. uint32_t packets;
  69. int8_t rssi;
  70. int8_t type;
  71. } __attribute__((packed)) nic[6];
  72. } __attribute__((packed));
  73. static const char short_options[] = "s:e:";
  74. static const struct option long_options[] =
  75. {
  76. { "start", required_argument, NULL, 's' },
  77. { "end", required_argument, NULL, 'e' },
  78. { 0, 0, NULL, 0 }
  79. };
  80. static int opt_s = 0;
  81. static int opt_e = 0;
  82. size_t in(int fd, uint8_t *data, size_t len)
  83. {
  84. int res = 0;
  85. while (len > 0) {
  86. size_t n = read(fd, data, len);
  87. if (n > 0) {
  88. len -= n;
  89. data += n;
  90. res += n;
  91. } else {
  92. if (n < 0) {
  93. if (errno != EAGAIN) {
  94. printf("read: %s\n", strerror(errno));
  95. } else {
  96. continue;
  97. }
  98. }
  99. break;
  100. }
  101. }
  102. return res;
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. struct fpv fpv;
  107. static uint8_t data[8 * 1024 * 1024];
  108. int i;
  109. for (;;) {
  110. int c;
  111. if ((c = getopt_long_only(argc, argv, short_options, long_options, &i)) == -1) {
  112. break;
  113. }
  114. switch (c) {
  115. case 's':
  116. opt_s = atoi(optarg);
  117. break;
  118. case 'e':
  119. opt_e = atoi(optarg);
  120. break;
  121. case 0:
  122. break;
  123. default:
  124. return 1;
  125. }
  126. }
  127. if ((argc - optind) < 1) {
  128. return 1;
  129. }
  130. for (i = 0; (optind + i) < argc; i++) {
  131. int fd = open(argv[optind + i], O_RDONLY);
  132. if (fd >= 0) {
  133. while (1) {
  134. if (in(fd, (uint8_t*)&fpv, sizeof(fpv)) > 0) {
  135. if (in(fd, data, fpv.len) > 0) {
  136. switch (fpv.type) {
  137. case 5:
  138. {
  139. struct gnd *gnd = (struct gnd *)data;
  140. for (i = 0; i < gnd->nics; i++) {
  141. printf(" %5d", gnd->nic[i].rssi);
  142. }
  143. printf(" %5d", gnd->lost);
  144. printf(" %5d", gnd->broken);
  145. printf(" %5d", gnd->air_rssi);
  146. printf(" %5d", gnd->uplink_lost);
  147. printf(" %5d", gnd->uplink_lost);
  148. printf(" %5d", gnd->cpu.air.load);
  149. printf(" %5d", gnd->cpu.air.temp);
  150. printf(" %5d", gnd->cpu.gnd.load);
  151. printf(" %5d", gnd->cpu.gnd.temp);
  152. printf("\n");
  153. continue;
  154. }
  155. break;
  156. default:
  157. continue;
  158. }
  159. }
  160. }
  161. break;
  162. }
  163. close(fd);
  164. }
  165. }
  166. return 0;
  167. }