main.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * main.h - PC Compatible Version
  3. * Central header for GPS Test Tool - handles PC/embedded switches
  4. */
  5. #pragma once
  6. #ifdef PC_BUILD
  7. /* ========== PC BUILD CONFIGURATION ========== */
  8. /* Standard C headers - all includes centralized here */
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <stddef.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <math.h>
  16. #include <stdarg.h>
  17. #include <ctype.h>
  18. /* Prevent AVR headers from being included */
  19. #define _AVR_PGMSPACE_H_ 1
  20. #define _AVR_IO_H_ 1
  21. #define _AVR_WDT_H_ 1
  22. #define _AVR_SLEEP_H_ 1
  23. #define _AVR_INTERRUPT_H_ 1
  24. #define _AVR_EEPROM_H_ 1
  25. /* PROGMEM/Flash support - no-op on PC */
  26. #define PROGMEM
  27. #define __flash
  28. #define PSTR(s) (s)
  29. #define strcpy_P strcpy
  30. #define strcat_P strcat
  31. #define strcmp_P strcmp
  32. #define pgm_read_byte(addr) (*(addr))
  33. /* Stub LCD type to bypass error */
  34. #define LCD_ALNUM
  35. /* Interrupt stubs */
  36. #define cli()
  37. #define sei()
  38. /* FatFS types */
  39. typedef struct {
  40. FILE *fp;
  41. char filename[256];
  42. } FIL;
  43. typedef uint32_t DWORD;
  44. typedef unsigned int UINT;
  45. typedef unsigned char BYTE;
  46. typedef char TCHAR;
  47. typedef unsigned char FRESULT;
  48. /* FatFS functions */
  49. #define FA_WRITE 0x02
  50. #define FA_OPEN_ALWAYS 0x10
  51. static inline FRESULT f_open(FIL *file, const TCHAR *path, BYTE mode) {
  52. (void)mode;
  53. file->fp = fopen(path, "w");
  54. return file->fp ? 0 : 1;
  55. }
  56. static inline FRESULT f_write(FIL *file, const void *buf, UINT len, UINT *written) {
  57. if (!file->fp) return 1;
  58. *written = fwrite(buf, 1, len, file->fp);
  59. return (*written == len) ? 0 : 1;
  60. }
  61. static inline FRESULT f_close(FIL *file) {
  62. if (file->fp) {
  63. fclose(file->fp);
  64. file->fp = NULL;
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. /* xprintf functions */
  70. extern FILE *debug_output;
  71. static inline void xputs(const char* str) {
  72. FILE *out = debug_output ? debug_output : stdout;
  73. fputs(str, out);
  74. }
  75. static inline void xputs_P(const char* str) {
  76. FILE *out = debug_output ? debug_output : stdout;
  77. fputs(str, out);
  78. }
  79. static inline void xprintf(const char* fmt, ...) {
  80. FILE *out = debug_output ? debug_output : stdout;
  81. va_list ap;
  82. va_start(ap, fmt);
  83. vfprintf(out, fmt, ap);
  84. va_end(ap);
  85. }
  86. static inline void xsprintf(char* buff, const char* fmt, ...) {
  87. va_list ap;
  88. va_start(ap, fmt);
  89. vsprintf(buff, fmt, ap);
  90. va_end(ap);
  91. }
  92. static inline void xfprintf(void (*put)(int), const char* fmt, ...) {
  93. /* Stub for PC - just ignore output to UART */
  94. (void)put;
  95. (void)fmt;
  96. }
  97. /* xatof - Parse float from string (used by NMEA parser) */
  98. static inline void xatof(const char **str, double *result) {
  99. char *end;
  100. *result = strtod(*str, &end);
  101. *str = end;
  102. }
  103. /* Math constants */
  104. #ifndef M_PI
  105. #define M_PI 3.14159265358979323846
  106. #endif
  107. /* NMEA time fields are UTC. The embedded build's stime.S mktime() is a
  108. * UTC-only conversion (no TZ/DST concept), but libc's mktime() treats
  109. * struct tm as local time and applies TZ - redirect to timegm() (POSIX/
  110. * glibc/BSD extension, ignores TZ) so the PC build matches. */
  111. #define mktime timegm
  112. #else
  113. /* ========== EMBEDDED BUILD CONFIGURATION ========== */
  114. #define __PROG_TYPES_COMPAT__
  115. #include <inttypes.h>
  116. #include <avr/pgmspace.h>
  117. #include <avr/interrupt.h>
  118. #include "stime.h"
  119. #include "expander.h"
  120. #include "settings.h"
  121. #if defined(LCD_GRAPHIC)
  122. #include "UC1601S-I2C.h"
  123. #elif defined(LCD_ALNUM)
  124. #include "HD44780-I2C.h"
  125. #else
  126. #error No LCD type defined
  127. #endif
  128. #endif /* PC_BUILD */
  129. /* ========== COMMON DEFINITIONS (both PC and embedded) ========== */
  130. #define IVT_SYNC 180
  131. #define POWER_SW_TIME 300
  132. #define BACKLIGHT_TIME 10000
  133. #define VI_LVL 3.1
  134. #define VI_LVH 3.4
  135. #define VI_MULT (3.3 / 6.6 / 2.495 * 1024)
  136. /* GPS initialization states (PC-build value only - GPS_INIT_INITIALIZED
  137. * doesn't exist here, this tool has no real GPS module init sequence) */
  138. #define GPS_INIT_NOT_INITIALIZED 0
  139. #define GPS_INIT_QUERY_SENT 1
  140. /* struct system_s/location_s/config_s/auto_pause_s and the value constants
  141. * their fields hold are in soft/common.h - shared byte-for-byte with the
  142. * embedded build, since gpx.c/nmea.c/syslog.c/autopause.c (soft/) compile
  143. * unmodified into this tool too (see the *_wrapper.c files). */
  144. #include "../soft/common.h"
  145. extern struct auto_pause_s auto_pause;
  146. /* Global variables */
  147. extern volatile struct system_s System;
  148. extern struct location_s location;
  149. extern time_t utc;
  150. /* Kept in sync with soft/settings.c's get_flag()/set_flag(): System.conf.flags
  151. * is part of struct config_s in common.h, and CONFFLAG_* there is a bit
  152. * index (not a pre-shifted mask), so this must index the same way - not the
  153. * config_flags-bitmask scheme this used to have, which numbered its flags
  154. * differently (and didn't have all of them), a silent mismatch from
  155. * embedded's real indices that a shared header can't paper over unless both
  156. * sides actually agree on what a CONFFLAG_* value means. */
  157. static inline unsigned char get_flag(unsigned char index) {
  158. return System.conf.flags[index/8] & (1 << (index % 8));
  159. }
  160. static inline void set_flag(unsigned char index, unsigned char val) {
  161. if (val)
  162. System.conf.flags[index/8] |= (1 << (index % 8));
  163. else
  164. System.conf.flags[index/8] &= ~(1 << (index % 8));
  165. }
  166. /* soft/main.c's helpers, reimplemented here: soft/main.c itself isn't part
  167. * of this PC build (this test tool has its own main()), but soft/syslog.c
  168. * calls into them. */
  169. unsigned char is_paused(void); /* soft/gpx.c, via gpx_wrapper.c */
  170. static inline unsigned char tracking_is_paused(void) {
  171. return System.tracking_paused || System.tracking_auto_paused;
  172. }
  173. /* Kept in sync with soft/main.c's pause_time_start()/pause_time_end()/
  174. * get_pause_time(): call the start/end function exactly once, at the moment
  175. * a pause actually begins/ends (not deferred to the next accepted point,
  176. * which may never come before the pause ends, or may come from a much
  177. * later, unrelated pause) - see soft/main.c for the full rationale. */
  178. static inline void pause_time_start(void) {
  179. System.current_pause_start = utc;
  180. }
  181. static inline void pause_time_end(void) {
  182. if (System.current_pause_start && System.current_pause_start >= System.time_start)
  183. System.pause_time += utc - System.current_pause_start;
  184. System.current_pause_start = 0;
  185. }
  186. /* Kept in sync with soft/main.c's fix_gap_detected() - see there for the
  187. * full rationale. */
  188. static inline unsigned char fix_gap_detected(time_t point_time) {
  189. static time_t last_point_time = 0;
  190. unsigned char gap = last_point_time && point_time - last_point_time > 3;
  191. last_point_time = point_time;
  192. return gap;
  193. }
  194. static inline time_t get_pause_time(void) {
  195. time_t res = System.pause_time;
  196. if (is_paused() && System.current_pause_start && System.current_pause_start >= System.time_start)
  197. res += utc - System.current_pause_start;
  198. return res;
  199. }
  200. static inline unsigned int get_logging_time(void) {
  201. if (!utc || !System.time_start)
  202. return 0;
  203. return utc - System.time_start - get_pause_time();
  204. }
  205. static inline unsigned int get_avg_speed_x100(void) {
  206. if (!System.speed_sample_count)
  207. return 0;
  208. return System.speed_accum_x100 / System.speed_sample_count;
  209. }
  210. static inline unsigned int get_dist_avg_speed_x100(void) {
  211. unsigned int moving = get_logging_time();
  212. if (!moving)
  213. return 0;
  214. return (unsigned int)((unsigned long int)System.distance * 36UL / (10UL * moving));
  215. }
  216. /* Time functions */
  217. char *get_iso_time(time_t time, unsigned char local);
  218. void iso_time_to_filename(char *time);
  219. /* GPS and distance functions */
  220. void gps_initialize(void);
  221. void add_distance(float dist);
  222. void add_elevation(float ele_change);
  223. float distance(struct location_s *pos1, struct location_s *pos2);
  224. unsigned char nmea_epoch_complete(void);
  225. /* The structured system log (soft/syslog.c) is compiled into this test tool
  226. * too (see syslog_wrapper.c), so its real log_reject()/log_jump()/log_prefix()
  227. * etc. are used here, not stubs - see gnss_names/uart_log_mode_names/
  228. * reset_cause below for what it needs that isn't otherwise part of this
  229. * PC build. */
  230. void log_prefix(void);
  231. void log_reject(unsigned char reason);
  232. void log_reject_flush(void);
  233. void log_jump(unsigned char is_alt, float meters);
  234. void log_boot_record(void);
  235. void log_status_line(void);
  236. void log_fix_event(void);
  237. void log_fix_lost(void);
  238. void log_dgps_transition(void);
  239. void log_session_summary(unsigned char final);
  240. void log_pause_event(unsigned char started);
  241. void gps_powered_on(void);
  242. /* Auto-pause is real logic (soft/autopause.c), not stubbed - it directly
  243. * gates System.distance/elevation_gain/elevation_loss, which is a major part
  244. * of what this tool's output looks like. It calls beep(), which is
  245. * meaningless on PC and stubbed out here. */
  246. void auto_pause_process(void);
  247. void auto_pause_reset_gap(void);
  248. unsigned char gps_fix_trustworthy(void);
  249. static inline void beep(unsigned int len, unsigned char cnt) { (void)len; (void)cnt; }
  250. extern volatile unsigned char reset_cause;
  251. extern __flash const char *gnss_names[];
  252. extern __flash const char *uart_log_mode_names[];
  253. /* MCUSR reset-cause bits and _BV(): meaningless on PC (reset_cause is always
  254. * 0 here, so log_boot_record() just reports "UNKNOWN"), but syslog.c
  255. * references them unconditionally. */
  256. #define _BV(x) (1 << (x))
  257. #define WDRF 3
  258. #define BORF 2
  259. #define EXTRF 1
  260. #define PORF 0
  261. /* soft/main.c's SD I/O timing and stack-headroom instrumentation isn't
  262. * meaningful for the PC build's stdio-based file stubs; stub it out. */
  263. static inline unsigned long int get_uptime_ms(void) { return 0; }
  264. static inline void io_mark(unsigned long int start_ms) { (void)start_ms; }
  265. static inline unsigned int io_get_and_reset_max(void) { return 0; }
  266. static inline unsigned int get_stack_free_bytes(void) { return 0; }
  267. /* Stub functions for PC build */
  268. #ifdef PC_BUILD
  269. static inline void set_timer(int timer, int val) { (void)timer; (void)val; }
  270. static inline int timer_expired(int timer) { (void)timer; return 0; }
  271. static inline void wdt_reset(void) {}
  272. static inline void sleep(void) {}
  273. static inline int uart0_test(void) { return 0; }
  274. static inline unsigned char uart0_get(void) { return 0; }
  275. static inline void uart0_put(char c) { (void)c; }
  276. static inline void uart1_put(char c) { (void)c; }
  277. /* Must have external linkage (defined once, in main.c/test_malformed.c):
  278. * nmea.c (compiled via nmea_actual_wrapper.c, a separate translation unit)
  279. * sets F_GPSOK here, and the driver loop needs to see that same flag. A
  280. * `static` FLAGS in this shared header would give every .c file its own
  281. * private copy, silently discarding those updates. */
  282. extern unsigned char FLAGS;
  283. #define F_LVD 0x01
  284. #define F_POWEROFF 0x02
  285. #define F_GPSOK 0x04
  286. #define recv_timeout 0
  287. #endif /* PC_BUILD stubs */