main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 config_flags = 0;
  20. unsigned char FLAGS = 0;
  21. volatile unsigned char reset_cause = 0; /* no real MCU reset happened; log_boot_record() reports "UNKNOWN" */
  22. /* File handle for debug output redirection */
  23. FILE *debug_output = NULL;
  24. /* soft/syslog.c's log_config()/log_boot_record() print these; not part of
  25. * this PC build otherwise (they're normally in soft/settings.c). */
  26. __flash const char gnss_name_stub[] = "GPS+GLONASS+GALILEO/etc (PC test tool - not tracked per mode)";
  27. __flash const char *gnss_names[] = {
  28. gnss_name_stub, gnss_name_stub, gnss_name_stub,
  29. gnss_name_stub, gnss_name_stub, gnss_name_stub,
  30. };
  31. __flash const char uart_log_mode_stub[] = "n/a (PC test tool)";
  32. __flash const char *uart_log_mode_names[] = {
  33. uart_log_mode_stub, uart_log_mode_stub, uart_log_mode_stub,
  34. };
  35. /* Function prototypes from project files */
  36. unsigned char gpx_init(FIL *file);
  37. unsigned char gpx_close(FIL *file);
  38. void gpx_process_point(struct location_s *loc, FIL *file);
  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. /* Configure filters */
  96. config_flags = (filters_enabled ? 0 : CONFFLAG_DISABLE_FILTERS) | CONFFLAG_AUTO_PAUSE;
  97. /* Reset system state */
  98. reset_system();
  99. gps_powered_on();
  100. log_boot_record();
  101. /* Open GPX output file */
  102. if (f_open(&gpx_file, output_gpx, FA_WRITE | FA_OPEN_ALWAYS) != 0) {
  103. fprintf(stderr, "Error: Cannot open GPX file '%s'\n", output_gpx);
  104. fclose(input);
  105. fclose(debug_output);
  106. return 1;
  107. }
  108. /* Initialize GPX file */
  109. if (gpx_init(&gpx_file) != 0) {
  110. fprintf(stderr, "Error: GPX initialization failed\n");
  111. fclose(input);
  112. fclose(debug_output);
  113. f_close(&gpx_file);
  114. return 1;
  115. }
  116. fprintf(debug_output, "=== GPS Test Tool - %s ===\n",
  117. filters_enabled ? "FILTERS ENABLED" : "FILTERS DISABLED");
  118. fprintf(debug_output, "Input: %s\n", input_file);
  119. fprintf(debug_output, "Output: %s\n\n", output_gpx);
  120. /* Process NMEA file line by line */
  121. unsigned char fix_ok_prev = 0;
  122. unsigned char sbas_prev = 0xFF; /* 0xFF: not yet known, suppress the first (non-)transition */
  123. time_t next_status_utc = 0;
  124. time_t next_summary_utc = 0;
  125. while (fgets(line, sizeof(line), input)) {
  126. size_t len = strlen(line);
  127. /* Skip empty lines */
  128. if (len == 0) continue;
  129. /* Ensure line ends with \r\n as expected by gps_parse */
  130. if (len > 0 && line[len-1] == '\n') {
  131. line[len-1] = '\0';
  132. len--;
  133. }
  134. if (len > 0 && line[len-1] == '\r') {
  135. line[len-1] = '\0';
  136. len--;
  137. }
  138. /* Add back \r\n for gps_parse */
  139. strcat(line, "\r\n");
  140. /* Parse NMEA sentence */
  141. if (gps_parse(line) != 0) {
  142. /* Valid location data received (RMC parsed) */
  143. /* Only process if we have valid coordinates from GGA */
  144. if (System.location_valid == LOC_VALID_NEW || System.location_valid == LOC_VALID) {
  145. gpx_process_point(&location, &gpx_file);
  146. auto_pause_process();
  147. point_count++;
  148. }
  149. }
  150. /* Mirror soft/main.c's system-log event/status triggers, so this
  151. * tool exercises and lets you review the same log output the
  152. * firmware produces. There's no real clock here, so pace the
  153. * periodic status line/session summary off the NMEA timestamps
  154. * instead of uptime. */
  155. if (utc) {
  156. unsigned char fix_ok_now = (FLAGS & F_GPSOK) && !System.sat_count_low;
  157. if (fix_ok_now != fix_ok_prev) {
  158. if (fix_ok_now)
  159. log_fix_event();
  160. else
  161. log_fix_lost();
  162. fix_ok_prev = fix_ok_now;
  163. }
  164. if (System.sbas != sbas_prev) {
  165. if (sbas_prev != 0xFF)
  166. log_sbas_transition();
  167. sbas_prev = System.sbas;
  168. }
  169. if (!next_status_utc)
  170. next_status_utc = utc + 60;
  171. if (utc >= next_status_utc) {
  172. log_status_line();
  173. next_status_utc = utc + 60;
  174. }
  175. if (!next_summary_utc)
  176. next_summary_utc = utc + 3600;
  177. if (utc >= next_summary_utc) {
  178. log_session_summary(0);
  179. next_summary_utc = utc + 3600;
  180. }
  181. }
  182. }
  183. /* Close GPX file */
  184. gpx_close(&gpx_file);
  185. log_session_summary(1);
  186. /* Print summary */
  187. fprintf(debug_output, "\n=== Summary ===\n");
  188. fprintf(debug_output, "Points processed: %d\n", point_count);
  189. fprintf(debug_output, "Total distance: %.2f km\n", System.distance / 100000.0);
  190. fprintf(debug_output, "Elevation gain: %.1f m\n", System.elevation_gain / 10.0);
  191. fprintf(debug_output, "Elevation loss: %.1f m\n", System.elevation_loss / 10.0);
  192. /* Close files */
  193. fclose(input);
  194. fclose(debug_output);
  195. debug_output = NULL;
  196. printf("Processed %d points -> %s (filters %s)\n",
  197. point_count, output_gpx, filters_enabled ? "ON" : "OFF");
  198. return 0;
  199. }
  200. int main(int argc, char *argv[]) {
  201. int ret;
  202. printf("GPS Test Tool v1.0\n");
  203. printf("==================\n\n");
  204. if (argc != 2) {
  205. fprintf(stderr, "Usage: %s <input.nmea>\n", argv[0]);
  206. fprintf(stderr, "\nGenerates:\n");
  207. fprintf(stderr, " - output_filtered.gpx\n");
  208. fprintf(stderr, " - output_unfiltered.gpx\n");
  209. fprintf(stderr, " - debug_filtered.txt\n");
  210. fprintf(stderr, " - debug_unfiltered.txt\n");
  211. return 1;
  212. }
  213. /* Process with filters enabled */
  214. printf("Processing with filters ENABLED...\n");
  215. ret = process_nmea_file(argv[1], "output_filtered.gpx",
  216. "debug_filtered.txt", 1);
  217. if (ret != 0) {
  218. fprintf(stderr, "Failed to process with filters enabled\n");
  219. return ret;
  220. }
  221. /* Process with filters disabled */
  222. printf("Processing with filters DISABLED...\n");
  223. ret = process_nmea_file(argv[1], "output_unfiltered.gpx",
  224. "debug_unfiltered.txt", 0);
  225. if (ret != 0) {
  226. fprintf(stderr, "Failed to process with filters disabled\n");
  227. return ret;
  228. }
  229. printf("\nProcessing complete!\n");
  230. printf("Generated files:\n");
  231. printf(" - output_filtered.gpx (filters ON)\n");
  232. printf(" - output_unfiltered.gpx (filters OFF)\n");
  233. printf(" - debug_filtered.txt (debug output with filters)\n");
  234. printf(" - debug_unfiltered.txt (debug output without filters)\n");
  235. return 0;
  236. }