syslog.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifdef PC_BUILD
  2. #include "../gps-test-tool/main.h"
  3. #else
  4. #include "main.h"
  5. #endif
  6. #ifndef FW_VERSION
  7. #define FW_VERSION "unknown"
  8. #endif
  9. __flash const char reject_reason_kalman[] = "kalman";
  10. __flash const char reject_reason_distdiff[] = "distdiff";
  11. __flash const char reject_reason_mindist[] = "mindist";
  12. __flash const char *reject_reason_names[] = {
  13. [REJECT_REASON_KALMAN] = reject_reason_kalman,
  14. [REJECT_REASON_DISTDIFF] = reject_reason_distdiff,
  15. [REJECT_REASON_MINDIST] = reject_reason_mindist,
  16. };
  17. __flash const char status_no_power[] = "no power";
  18. __flash const char status_no_disk[] = "no card";
  19. __flash const char status_no_gps[] = "waiting for GPS";
  20. __flash const char status_ok[] = "ok";
  21. __flash const char status_disk_error[] = "card mount failed";
  22. __flash const char status_file_write_error[] = "write failed";
  23. __flash const char status_file_sync_error[] = "sync failed";
  24. __flash const char status_file_close_error[] = "close failed";
  25. __flash const char status_file_open_error[] = "open failed";
  26. __flash const char status_unknown[] = "unknown";
  27. __flash const char *status_names[] = {
  28. status_no_power,
  29. status_no_disk,
  30. status_no_gps,
  31. status_ok,
  32. status_disk_error,
  33. status_file_write_error,
  34. status_file_sync_error,
  35. status_file_close_error,
  36. status_file_open_error,
  37. };
  38. static struct {
  39. unsigned char reason;
  40. unsigned char count;
  41. unsigned int hdop_x100;
  42. unsigned char sats;
  43. unsigned char fix_type;
  44. } reject_coalesce;
  45. static unsigned char fix_had_first;
  46. static unsigned long int gps_on_uptime;
  47. void log_prefix(void) {
  48. if (utc)
  49. xprintf(PSTR("%s up=%lu "), get_iso_time(utc, 0), get_uptime_ms());
  50. else
  51. xprintf(PSTR("NOTIME up=%lu "), get_uptime_ms());
  52. }
  53. void log_config(void) {
  54. log_prefix();
  55. xputs_P(PSTR("CFG filt="));
  56. xputs_P(get_flag(CONFFLAG_DISABLE_FILTERS) ? PSTR("off") : PSTR("on"));
  57. xprintf(PSTR(" skip=%u gate_sats=%u pause_t=%us pause_d=%um pause_v=%ukmh sbas_search="),
  58. (unsigned int)System.conf.skip_points, (unsigned int)System.conf.min_sats,
  59. (unsigned int)System.conf.auto_pause_time, (unsigned int)System.conf.auto_pause_dist,
  60. (unsigned int)System.conf.auto_pause_speed);
  61. xputs_P(get_flag(CONFFLAG_ENABLE_SBAS) ? PSTR("on") : PSTR("off"));
  62. xputs_P(PSTR(" auto_pause="));
  63. xputs_P(get_flag(CONFFLAG_AUTO_PAUSE) ? PSTR("on") : PSTR("off"));
  64. xputs_P(PSTR(" gnss="));
  65. xputs_P(gnss_names[System.conf.gnss_mode]);
  66. xputs_P(PSTR(" uart_log="));
  67. xputs_P(uart_log_mode_names[System.conf.uart_log_mode]);
  68. xputs_P(PSTR(" verbose="));
  69. xputs_P(get_flag(CONFFLAG_VERBOSE_LOG) ? PSTR("on") : PSTR("off"));
  70. xputs_P(PSTR(" reset_new_file="));
  71. xputs_P(get_flag(CONFFLAG_RESET_ON_NEW_FILE) ? PSTR("on") : PSTR("off"));
  72. xputs_P(PSTR("\r\n"));
  73. }
  74. void log_boot_record(void) {
  75. log_prefix();
  76. xputs_P(PSTR("BOOT fw="));
  77. xputs_P(PSTR(FW_VERSION));
  78. xputs_P(PSTR(" reset="));
  79. if (reset_cause & _BV(WDRF))
  80. xputs_P(PSTR("WDT"));
  81. else if (reset_cause & _BV(BORF))
  82. xputs_P(PSTR("BOR"));
  83. else if (reset_cause & _BV(EXTRF))
  84. xputs_P(PSTR("EXT"));
  85. else if (reset_cause & _BV(PORF))
  86. xputs_P(PSTR("POR"));
  87. else
  88. xputs_P(PSTR("UNKNOWN"));
  89. xputs_P(PSTR(" rx="));
  90. xputs_P(gnss_names[System.conf.gnss_mode]);
  91. xputs_P(PSTR("\r\n"));
  92. log_config();
  93. }
  94. void log_loop_status(unsigned char status) {
  95. log_prefix();
  96. xprintf(PSTR("LOOP err=%u ("), (unsigned int)status);
  97. if (status < sizeof(status_names)/sizeof(status_names[0]))
  98. xputs_P(status_names[status]);
  99. else
  100. xputs_P(status_unknown);
  101. xputs_P(PSTR(")\r\n"));
  102. }
  103. void gps_powered_on(void) {
  104. gps_on_uptime = get_uptime_ms();
  105. fix_had_first = 0;
  106. }
  107. void log_fix_event(void) {
  108. log_prefix();
  109. xprintf(PSTR("FIX %uD"), (unsigned int)System.fix_type);
  110. xputs_P(System.sbas ? PSTR("/D") : PSTR(""));
  111. if (!fix_had_first) {
  112. unsigned long int ttff = get_uptime_ms() - gps_on_uptime;
  113. xprintf(PSTR(" ttff=%lu.%01lus"), ttff/1000, (ttff%1000)/100);
  114. fix_had_first = 1;
  115. } else {
  116. xputs_P(PSTR(" regained"));
  117. }
  118. xprintf(PSTR(" sats=%u hdop=%.2f\r\n"), (unsigned int)System.satellites_used, (double)System.hdop_x100/100.0);
  119. }
  120. void log_fix_lost(void) {
  121. log_prefix();
  122. xprintf(PSTR("FIX LOST sats=%u\r\n"), (unsigned int)System.satellites_used);
  123. }
  124. void log_sbas_transition(void) {
  125. log_prefix();
  126. xputs_P(PSTR("SBAS "));
  127. xputs_P(System.sbas ? PSTR("on") : PSTR("off"));
  128. xputs_P(PSTR("\r\n"));
  129. }
  130. void log_pause_event(unsigned char started) {
  131. log_prefix();
  132. xputs_P(started ? PSTR("PAUSE\r\n") : PSTR("RESUME\r\n"));
  133. }
  134. void log_reject_flush(void) {
  135. if (!reject_coalesce.count)
  136. return;
  137. log_prefix();
  138. xputs_P(PSTR("REJ reason="));
  139. xputs_P(reject_reason_names[reject_coalesce.reason]);
  140. xprintf(PSTR(" hdop=%.2f sats=%u fix=%uD"), (double)reject_coalesce.hdop_x100/100.0,
  141. (unsigned int)reject_coalesce.sats, (unsigned int)reject_coalesce.fix_type);
  142. if (reject_coalesce.count > 1)
  143. xprintf(PSTR(" x%u"), (unsigned int)reject_coalesce.count);
  144. xputs_P(PSTR("\r\n"));
  145. reject_coalesce.count = 0;
  146. }
  147. void log_reject(unsigned char reason) {
  148. System.rejected_count++;
  149. if (reject_coalesce.count && reject_coalesce.reason == reason && reject_coalesce.count < 255) {
  150. reject_coalesce.count++;
  151. reject_coalesce.hdop_x100 = System.hdop_x100;
  152. reject_coalesce.sats = System.satellites_used;
  153. reject_coalesce.fix_type = System.fix_type;
  154. return;
  155. }
  156. log_reject_flush();
  157. reject_coalesce.reason = reason;
  158. reject_coalesce.count = 1;
  159. reject_coalesce.hdop_x100 = System.hdop_x100;
  160. reject_coalesce.sats = System.satellites_used;
  161. reject_coalesce.fix_type = System.fix_type;
  162. }
  163. void log_jump(unsigned char is_alt, float meters) {
  164. log_prefix();
  165. xputs_P(PSTR("JUMP "));
  166. xputs_P(is_alt ? PSTR("alt=") : PSTR("pos="));
  167. xprintf(PSTR("%.1fm\r\n"), (double)meters);
  168. }
  169. void log_status_line(void) {
  170. signed int batpct = (signed int)((System.bat_volt - 3.3) / (4.2 - 3.3) * 100.0 + 0.5);
  171. if (batpct < 0)
  172. batpct = 0;
  173. if (batpct > 100)
  174. batpct = 100;
  175. log_reject_flush();
  176. log_prefix();
  177. xprintf(PSTR("ST v=%.3f bat%%=%d"), (double)System.bat_volt, batpct);
  178. if (System.temperature_ok)
  179. xprintf(PSTR(" t=%.1f"), (double)System.temperature);
  180. xprintf(PSTR(" sats=%u hdop=%.2f fix=%u"), (unsigned int)System.satellites_used,
  181. (double)System.hdop_x100/100.0, (unsigned int)System.fix_type);
  182. xputs_P(System.sbas ? PSTR("D") : PSTR(""));
  183. xprintf(PSTR(" dist=%.2f gain=%.1f/raw%.1f loss=%.1f/raw%.1f wr=%lu rej=%lu"),
  184. (double)System.distance/100.0, (double)System.elevation_gain/10.0, (double)System.elevation_gain_raw/10.0,
  185. (double)System.elevation_loss/10.0, (double)System.elevation_loss_raw/10.0,
  186. System.points_written, System.rejected_count);
  187. xprintf(PSTR(" iomax=%ums stack=%u"), io_get_and_reset_max(), get_stack_free_bytes());
  188. if (utc)
  189. xprintf(PSTR(" loc=%s"), get_iso_time(utc, 1));
  190. xputs_P(PSTR("\r\n"));
  191. }
  192. void log_session_summary(unsigned char final) {
  193. unsigned int moving = get_logging_time();
  194. unsigned long int stopped = get_pause_time();
  195. unsigned int h, m;
  196. log_reject_flush();
  197. log_prefix();
  198. xputs_P(final ? PSTR("SUMMARY final ") : PSTR("SUMMARY "));
  199. xprintf(PSTR("dist=%.2f gain=%.1f loss=%.1f moving="), (double)System.distance/100.0,
  200. (double)System.elevation_gain/10.0, (double)System.elevation_loss/10.0);
  201. h = moving/3600; m = (moving/60)%60;
  202. xprintf(PSTR("%uh%02um stopped="), h, m);
  203. h = stopped/3600; m = (stopped/60)%60;
  204. xprintf(PSTR("%uh%02um altmax=%.1f vmin=%.3f rej=%lu"), h, m,
  205. (double)System.alt_max/10.0, (double)System.bat_volt_min, System.rejected_count);
  206. /* Both averages should track each other; a growing gap is worth noticing. */
  207. xprintf(PSTR(" avg_speed=%.2f avg_dist=%.2f\r\n"),
  208. (double)get_avg_speed_x100()/100.0, (double)get_dist_avg_speed_x100()/100.0);
  209. }