main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. System.conf.auto_pause_dist = 100;
  64. System.conf.min_sats = 4; /* Require at least 4 satellites */
  65. System.tracking_paused = 0;
  66. System.tracking_auto_paused = 0;
  67. System.bat_volt = 4.0f; /* no real ADC here; keeps the status line's v=/bat%= fields sane */
  68. System.bat_volt_min = 4.0f;
  69. utc = 0;
  70. }
  71. /* Process NMEA file with specified filter setting */
  72. int process_nmea_file(const char *input_file, const char *output_gpx,
  73. const char *debug_file, int filters_enabled) {
  74. FILE *input;
  75. FIL gpx_file;
  76. char line[256];
  77. int point_count = 0;
  78. /* Open input file */
  79. input = fopen(input_file, "r");
  80. if (!input) {
  81. fprintf(stderr, "Error: Cannot open input file '%s'\n", input_file);
  82. return 1;
  83. }
  84. /* Open debug output file */
  85. debug_output = fopen(debug_file, "w");
  86. if (!debug_output) {
  87. fprintf(stderr, "Error: Cannot open debug file '%s'\n", debug_file);
  88. fclose(input);
  89. return 1;
  90. }
  91. /* Configure filters */
  92. config_flags = filters_enabled ? 0 : CONFFLAG_DISABLE_FILTERS;
  93. /* Reset system state */
  94. reset_system();
  95. gps_powered_on();
  96. log_boot_record();
  97. /* Open GPX output file */
  98. if (f_open(&gpx_file, output_gpx, FA_WRITE | FA_OPEN_ALWAYS) != 0) {
  99. fprintf(stderr, "Error: Cannot open GPX file '%s'\n", output_gpx);
  100. fclose(input);
  101. fclose(debug_output);
  102. return 1;
  103. }
  104. /* Initialize GPX file */
  105. if (gpx_init(&gpx_file) != 0) {
  106. fprintf(stderr, "Error: GPX initialization failed\n");
  107. fclose(input);
  108. fclose(debug_output);
  109. f_close(&gpx_file);
  110. return 1;
  111. }
  112. fprintf(debug_output, "=== GPS Test Tool - %s ===\n",
  113. filters_enabled ? "FILTERS ENABLED" : "FILTERS DISABLED");
  114. fprintf(debug_output, "Input: %s\n", input_file);
  115. fprintf(debug_output, "Output: %s\n\n", output_gpx);
  116. /* Process NMEA file line by line */
  117. unsigned char fix_ok_prev = 0;
  118. unsigned char sbas_prev = 0xFF; /* 0xFF: not yet known, suppress the first (non-)transition */
  119. time_t next_status_utc = 0;
  120. time_t next_summary_utc = 0;
  121. while (fgets(line, sizeof(line), input)) {
  122. size_t len = strlen(line);
  123. /* Skip empty lines */
  124. if (len == 0) continue;
  125. /* Ensure line ends with \r\n as expected by gps_parse */
  126. if (len > 0 && line[len-1] == '\n') {
  127. line[len-1] = '\0';
  128. len--;
  129. }
  130. if (len > 0 && line[len-1] == '\r') {
  131. line[len-1] = '\0';
  132. len--;
  133. }
  134. /* Add back \r\n for gps_parse */
  135. strcat(line, "\r\n");
  136. /* Parse NMEA sentence */
  137. if (gps_parse(line) != 0) {
  138. /* Valid location data received (RMC parsed) */
  139. /* Only process if we have valid coordinates from GGA */
  140. if (System.location_valid == LOC_VALID_NEW || System.location_valid == LOC_VALID) {
  141. gpx_process_point(&location, &gpx_file);
  142. point_count++;
  143. }
  144. }
  145. /* Mirror soft/main.c's system-log event/status triggers, so this
  146. * tool exercises and lets you review the same log output the
  147. * firmware produces. There's no real clock here, so pace the
  148. * periodic status line/session summary off the NMEA timestamps
  149. * instead of uptime. */
  150. if (utc) {
  151. unsigned char fix_ok_now = (FLAGS & F_GPSOK) && !System.sat_count_low;
  152. if (fix_ok_now != fix_ok_prev) {
  153. if (fix_ok_now)
  154. log_fix_event();
  155. else
  156. log_fix_lost();
  157. fix_ok_prev = fix_ok_now;
  158. }
  159. if (System.sbas != sbas_prev) {
  160. if (sbas_prev != 0xFF)
  161. log_sbas_transition();
  162. sbas_prev = System.sbas;
  163. }
  164. if (!next_status_utc)
  165. next_status_utc = utc + 60;
  166. if (utc >= next_status_utc) {
  167. log_status_line();
  168. next_status_utc = utc + 60;
  169. }
  170. if (!next_summary_utc)
  171. next_summary_utc = utc + 3600;
  172. if (utc >= next_summary_utc) {
  173. log_session_summary(0);
  174. next_summary_utc = utc + 3600;
  175. }
  176. }
  177. }
  178. /* Close GPX file */
  179. gpx_close(&gpx_file);
  180. log_session_summary(1);
  181. /* Print summary */
  182. fprintf(debug_output, "\n=== Summary ===\n");
  183. fprintf(debug_output, "Points processed: %d\n", point_count);
  184. fprintf(debug_output, "Total distance: %.2f km\n", System.distance / 100000.0);
  185. fprintf(debug_output, "Elevation gain: %.1f m\n", System.elevation_gain / 10.0);
  186. fprintf(debug_output, "Elevation loss: %.1f m\n", System.elevation_loss / 10.0);
  187. /* Close files */
  188. fclose(input);
  189. fclose(debug_output);
  190. debug_output = NULL;
  191. printf("Processed %d points -> %s (filters %s)\n",
  192. point_count, output_gpx, filters_enabled ? "ON" : "OFF");
  193. return 0;
  194. }
  195. int main(int argc, char *argv[]) {
  196. int ret;
  197. printf("GPS Test Tool v1.0\n");
  198. printf("==================\n\n");
  199. if (argc != 2) {
  200. fprintf(stderr, "Usage: %s <input.nmea>\n", argv[0]);
  201. fprintf(stderr, "\nGenerates:\n");
  202. fprintf(stderr, " - output_filtered.gpx\n");
  203. fprintf(stderr, " - output_unfiltered.gpx\n");
  204. fprintf(stderr, " - debug_filtered.txt\n");
  205. fprintf(stderr, " - debug_unfiltered.txt\n");
  206. return 1;
  207. }
  208. /* Process with filters enabled */
  209. printf("Processing with filters ENABLED...\n");
  210. ret = process_nmea_file(argv[1], "output_filtered.gpx",
  211. "debug_filtered.txt", 1);
  212. if (ret != 0) {
  213. fprintf(stderr, "Failed to process with filters enabled\n");
  214. return ret;
  215. }
  216. /* Process with filters disabled */
  217. printf("Processing with filters DISABLED...\n");
  218. ret = process_nmea_file(argv[1], "output_unfiltered.gpx",
  219. "debug_unfiltered.txt", 0);
  220. if (ret != 0) {
  221. fprintf(stderr, "Failed to process with filters disabled\n");
  222. return ret;
  223. }
  224. printf("\nProcessing complete!\n");
  225. printf("Generated files:\n");
  226. printf(" - output_filtered.gpx (filters ON)\n");
  227. printf(" - output_unfiltered.gpx (filters OFF)\n");
  228. printf(" - debug_filtered.txt (debug output with filters)\n");
  229. printf(" - debug_unfiltered.txt (debug output without filters)\n");
  230. return 0;
  231. }