main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * GPS Test Tool - PC Application
  3. *
  4. * Tests GPS filtering and GPX generation using actual embedded code
  5. * Usage: ./gps-test-tool <input.nmea>
  6. *
  7. * Generates:
  8. * - output_filtered.gpx
  9. * - output_unfiltered.gpx
  10. * - debug_filtered.txt
  11. * - debug_unfiltered.txt
  12. */
  13. #define PC_BUILD
  14. #include "main.h"
  15. /* Global variables expected by project code */
  16. volatile struct system_s System;
  17. struct location_s location;
  18. time_t utc;
  19. unsigned char FLAGS = 0;
  20. volatile unsigned char reset_cause = 0; /* no real MCU reset happened; log_boot_record() reports "UNKNOWN" */
  21. /* File handle for debug output redirection */
  22. FILE *debug_output = NULL;
  23. /* soft/syslog.c's log_config()/log_boot_record() print these; not part of
  24. * this PC build otherwise (they're normally in soft/settings.c). */
  25. __flash const char gnss_name_stub[] = "GPS+GLONASS+GALILEO/etc (PC test tool - not tracked per mode)";
  26. __flash const char *gnss_names[] = {
  27. gnss_name_stub, gnss_name_stub, gnss_name_stub,
  28. gnss_name_stub, gnss_name_stub, gnss_name_stub,
  29. };
  30. __flash const char uart_log_mode_stub[] = "n/a (PC test tool)";
  31. __flash const char *uart_log_mode_names[] = {
  32. uart_log_mode_stub, uart_log_mode_stub, uart_log_mode_stub,
  33. };
  34. /* Function prototypes from project files */
  35. unsigned char gpx_init(FIL *file);
  36. unsigned char gpx_close(FIL *file);
  37. void gpx_process_point(struct location_s *loc, FIL *file);
  38. void gpx_reset_gap(void);
  39. time_t gps_parse(const char *nmea);
  40. /* Time function implementation */
  41. char *get_iso_time(time_t time, unsigned char local) {
  42. static char output[32];
  43. struct tm *ct = gmtime(&time);
  44. sprintf(output, "%04d-%02d-%02dT%02d:%02d:%02d.000Z",
  45. ct->tm_year + 1900, ct->tm_mon + 1, ct->tm_mday,
  46. ct->tm_hour, ct->tm_min, ct->tm_sec);
  47. return output;
  48. }
  49. void iso_time_to_filename(char *time) {
  50. while (*time) {
  51. switch (*time) {
  52. case ':': *time = '-'; break;
  53. case '+': *time = 'p'; break;
  54. }
  55. time++;
  56. }
  57. }
  58. /* Reset system state */
  59. void reset_system(void) {
  60. memset((void*)&System, 0, sizeof(System));
  61. memset(&location, 0, sizeof(location));
  62. System.conf.skip_points = 0; /* Don't skip points for testing */
  63. /* Match soft/settings.c's defaults_u8, so this tool's auto-pause behavior
  64. * (enabled below) matches an out-of-the-box device. */
  65. System.conf.auto_pause_time = 30;
  66. System.conf.auto_pause_dist = 10;
  67. System.conf.auto_pause_speed = 3;
  68. System.conf.min_sats = 4; /* Require at least 4 satellites */
  69. System.tracking_paused = 0;
  70. System.tracking_auto_paused = 0;
  71. System.bat_volt = 4.0f; /* no real ADC here; keeps the status line's v=/bat%= fields sane */
  72. System.bat_volt_min = 4.0f;
  73. utc = 0;
  74. }
  75. /* Process NMEA file with specified filter setting */
  76. int process_nmea_file(const char *input_file, const char *output_gpx,
  77. const char *debug_file, int filters_enabled) {
  78. FILE *input;
  79. FIL gpx_file;
  80. char line[256];
  81. int point_count = 0;
  82. /* Open input file */
  83. input = fopen(input_file, "r");
  84. if (!input) {
  85. fprintf(stderr, "Error: Cannot open input file '%s'\n", input_file);
  86. return 1;
  87. }
  88. /* Open debug output file */
  89. debug_output = fopen(debug_file, "w");
  90. if (!debug_output) {
  91. fprintf(stderr, "Error: Cannot open debug file '%s'\n", debug_file);
  92. fclose(input);
  93. return 1;
  94. }
  95. /* Reset system state */
  96. reset_system();
  97. /* Configure filters */
  98. set_flag(CONFFLAG_DISABLE_FILTERS, !filters_enabled);
  99. set_flag(CONFFLAG_AUTO_PAUSE, 1);
  100. gps_powered_on();
  101. log_boot_record();
  102. /* Open GPX output file */
  103. if (f_open(&gpx_file, output_gpx, FA_WRITE | FA_OPEN_ALWAYS) != 0) {
  104. fprintf(stderr, "Error: Cannot open GPX file '%s'\n", output_gpx);
  105. fclose(input);
  106. fclose(debug_output);
  107. return 1;
  108. }
  109. /* Initialize GPX file */
  110. if (gpx_init(&gpx_file) != 0) {
  111. fprintf(stderr, "Error: GPX initialization failed\n");
  112. fclose(input);
  113. fclose(debug_output);
  114. f_close(&gpx_file);
  115. return 1;
  116. }
  117. fprintf(debug_output, "=== GPS Test Tool - %s ===\n",
  118. filters_enabled ? "FILTERS ENABLED" : "FILTERS DISABLED");
  119. fprintf(debug_output, "Input: %s\n", input_file);
  120. fprintf(debug_output, "Output: %s\n\n", output_gpx);
  121. /* Process NMEA file line by line */
  122. /* Kept in sync with soft/main.c's FIX_HOLD_EPOCHS. */
  123. #define FIX_HOLD_EPOCHS 5
  124. unsigned char fix_ok_prev = 0;
  125. unsigned char fix_hold_counter = 0;
  126. unsigned char dgps_prev = 0xFF; /* 0xFF: not yet known, suppress the first (non-)transition */
  127. time_t next_status_utc = 0;
  128. time_t next_summary_utc = 0;
  129. while (fgets(line, sizeof(line), input)) {
  130. size_t len = strlen(line);
  131. /* Skip empty lines */
  132. if (len == 0) continue;
  133. /* Ensure line ends with \r\n as expected by gps_parse */
  134. if (len > 0 && line[len-1] == '\n') {
  135. line[len-1] = '\0';
  136. len--;
  137. }
  138. if (len > 0 && line[len-1] == '\r') {
  139. line[len-1] = '\0';
  140. len--;
  141. }
  142. /* Add back \r\n for gps_parse, bounded: fgets can fill line[] to
  143. * capacity with no trailing newline, leaving no room for it. */
  144. if (len + 2 >= sizeof(line))
  145. len = sizeof(line) - 3;
  146. line[len] = '\0';
  147. strcat(line, "\r\n");
  148. /* Parse NMEA sentence */
  149. if (gps_parse(line) != 0) {
  150. /* Valid location data received (RMC parsed) */
  151. /* Only process if we have valid coordinates from GGA */
  152. if (System.location_valid == LOC_VALID_NEW || System.location_valid == LOC_VALID) {
  153. if (fix_gap_detected(location.time)) {
  154. auto_pause_reset_gap();
  155. gpx_reset_gap();
  156. }
  157. auto_pause_process();
  158. gpx_process_point(&location, &gpx_file);
  159. point_count++;
  160. }
  161. }
  162. /* Mirror soft/main.c's system-log event/status triggers, so this
  163. * tool exercises and lets you review the same log output the
  164. * firmware produces. There's no real clock here, so pace the
  165. * periodic status line/session summary off the NMEA timestamps
  166. * instead of uptime. */
  167. if (utc) {
  168. if (nmea_epoch_complete()) {
  169. unsigned char fix_ok_now = (FLAGS & F_GPSOK) && !System.sat_count_low;
  170. if (fix_ok_now == fix_ok_prev) {
  171. fix_hold_counter = 0;
  172. } else if (++fix_hold_counter >= FIX_HOLD_EPOCHS) {
  173. fix_hold_counter = 0;
  174. fix_ok_prev = fix_ok_now;
  175. if (fix_ok_now)
  176. log_fix_event();
  177. else
  178. log_fix_lost();
  179. }
  180. }
  181. if (System.dgps != dgps_prev) {
  182. if (dgps_prev != 0xFF)
  183. log_dgps_transition();
  184. dgps_prev = System.dgps;
  185. }
  186. if (!next_status_utc)
  187. next_status_utc = utc + 60;
  188. if (utc >= next_status_utc) {
  189. log_status_line();
  190. next_status_utc = utc + 60;
  191. }
  192. if (!next_summary_utc)
  193. next_summary_utc = utc + 3600;
  194. if (utc >= next_summary_utc) {
  195. log_session_summary(0);
  196. next_summary_utc = utc + 3600;
  197. }
  198. }
  199. }
  200. /* Close GPX file */
  201. gpx_close(&gpx_file);
  202. log_session_summary(1);
  203. /* Print summary */
  204. {
  205. unsigned int moving = get_logging_time();
  206. unsigned long int stopped = get_pause_time();
  207. /* points_accepted counts every point past all reject checks, at
  208. * AVG_COUNT=3 samples per written trkpt with filters enabled (1:1
  209. * with filters disabled, so this is 0 in that mode). */
  210. unsigned long int averaged_out = System.points_accepted - System.points_written *
  211. (filters_enabled ? 3UL : 1UL);
  212. fprintf(debug_output, "\n=== Summary ===\n");
  213. fprintf(debug_output, "Points processed: %d\n", point_count);
  214. fprintf(debug_output, "Points during pauses: %lu\n", System.points_paused);
  215. fprintf(debug_output, "Points skipped (warm-up): %lu\n", System.points_skipped);
  216. fprintf(debug_output, "Points rejected: %lu\n", System.rejected_count);
  217. fprintf(debug_output, "Points averaged out: %lu\n", averaged_out);
  218. fprintf(debug_output, "Points saved: %lu\n", System.points_written);
  219. fprintf(debug_output, "Total distance: %.2f km\n", System.distance / 100000.0);
  220. fprintf(debug_output, "Elevation gain: %.1f m\n", System.elevation_gain / 10.0);
  221. fprintf(debug_output, "Elevation loss: %.1f m\n", System.elevation_loss / 10.0);
  222. fprintf(debug_output, "Moving time: %uh%02um\n", moving/3600, (moving/60)%60);
  223. fprintf(debug_output, "Stopped time: %luh%02lum\n", stopped/3600, (stopped/60)%60);
  224. fprintf(debug_output, "Average speed: %.2f km/h\n", get_avg_speed_x100()/100.0);
  225. fprintf(debug_output, "Average speed (dist/time): %.2f km/h\n", get_dist_avg_speed_x100()/100.0);
  226. }
  227. /* Close files */
  228. fclose(input);
  229. fclose(debug_output);
  230. debug_output = NULL;
  231. printf("Processed %d points -> %s (filters %s)\n",
  232. point_count, output_gpx, filters_enabled ? "ON" : "OFF");
  233. return 0;
  234. }
  235. int main(int argc, char *argv[]) {
  236. int ret;
  237. printf("GPS Test Tool v1.0\n");
  238. printf("==================\n\n");
  239. if (argc != 2) {
  240. fprintf(stderr, "Usage: %s <input.nmea>\n", argv[0]);
  241. fprintf(stderr, "\nGenerates:\n");
  242. fprintf(stderr, " - output_filtered.gpx\n");
  243. fprintf(stderr, " - output_unfiltered.gpx\n");
  244. fprintf(stderr, " - debug_filtered.txt\n");
  245. fprintf(stderr, " - debug_unfiltered.txt\n");
  246. return 1;
  247. }
  248. /* Process with filters enabled */
  249. printf("Processing with filters ENABLED...\n");
  250. ret = process_nmea_file(argv[1], "output_filtered.gpx",
  251. "debug_filtered.txt", 1);
  252. if (ret != 0) {
  253. fprintf(stderr, "Failed to process with filters enabled\n");
  254. return ret;
  255. }
  256. /* Process with filters disabled */
  257. printf("Processing with filters DISABLED...\n");
  258. ret = process_nmea_file(argv[1], "output_unfiltered.gpx",
  259. "debug_unfiltered.txt", 0);
  260. if (ret != 0) {
  261. fprintf(stderr, "Failed to process with filters disabled\n");
  262. return ret;
  263. }
  264. printf("\nProcessing complete!\n");
  265. printf("Generated files:\n");
  266. printf(" - output_filtered.gpx (filters ON)\n");
  267. printf(" - output_unfiltered.gpx (filters OFF)\n");
  268. printf(" - debug_filtered.txt (debug output with filters)\n");
  269. printf(" - debug_unfiltered.txt (debug output without filters)\n");
  270. return 0;
  271. }