Skip to content

Commit

Permalink
Add option to disable CRC calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Feb 28, 2021
1 parent adce7a2 commit 6fbcf34
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lwgps/src/include/lwgps/lwgps.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ typedef struct {

uint8_t star; /*!< Star detected flag */

#if LWGPS_CFG_CRC
uint8_t crc_calc; /*!< Calculated CRC string */
#endif /* LWGPS_CFG_CRC */

union {
uint8_t dummy; /*!< Dummy byte */
Expand Down
9 changes: 9 additions & 0 deletions lwgps/src/include/lwgps/lwgps_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ extern "C" {
#define LWGPS_CFG_STATEMENT_PUBX_TIME 0
#endif

/**
* \brief Enables `1` or disables `0` CRC calculation and check
*
* \note When not enabled, CRC check is ignored
*/
#ifndef LWGPS_CFG_CRC
#define LWGPS_CFG_CRC 1
#endif

/* Guard against accidental parser breakage */
#if LWGPS_CFG_STATEMENT_PUBX_TIME && !LWGPS_CFG_STATEMENT_PUBX
#error LWGPS_CFG_STATEMENT_PUBX must be enabled when enabling LWGPS_CFG_STATEMENT_PUBX_TIME
Expand Down
8 changes: 8 additions & 0 deletions lwgps/src/lwgps/lwgps.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
#define R2D(x) FLT(FLT(x) * FLT(57.29577951308232))/*!< Radians to degrees */
#define EARTH_RADIUS FLT(6371.0) /*!< Earth radius in units of kilometers */

#if LWGPS_CFG_CRC
#define CRC_ADD(_gh, ch) (_gh)->p.crc_calc ^= (uint8_t)(ch)
#else
#define CRC_ADD(_gh, ch)
#endif /* LWGPS_CFG_CRC */
#define TERM_ADD(_gh, ch) do { \
if ((_gh)->p.term_pos < (sizeof((_gh)->p.term_str) - 1)) { \
(_gh)->p.term_str[(_gh)->p.term_pos] = (ch);\
Expand Down Expand Up @@ -346,6 +350,7 @@ prv_parse_term(lwgps_t* gh) {
return 1;
}

#if LWGPS_CFG_CRC
/**
* \brief Compare calculated CRC with received CRC
* \param[in] gh: GPS handle
Expand All @@ -357,6 +362,9 @@ prv_check_crc(lwgps_t* gh) {
crc = (uint8_t)((CHTN(gh->p.term_str[0]) & 0x0F) << 0x04) | (CHTN(gh->p.term_str[1]) & 0x0F); /* Convert received CRC from string (hex) to number */
return gh->p.crc_calc == crc; /* They must match! */
}
#else
#define prv_check_crc(_gh) (1)
#endif /* LWGPS_CFG_CRC */

/**
* \brief Copy temporary memory to user memory
Expand Down

0 comments on commit 6fbcf34

Please sign in to comment.