SELKIELogger  1.0.0
NMEAChecksumTest.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "SELKIELoggerNMEA.h"
26 
43 int main(void) {
44  bool passed = true;
45 
46  //$IIXDR,C,,C,ENV_WATER_T,C,16.24,C,ENV_OUTAIR_T,P,101800,P,ENV_ATMOS_P*61
47  nmea_msg_t validCS = {.encapsulated = false,
48  .talker = {'I', 'I'},
49  .message = {'X', 'D', 'R'},
50  .rawlen = 62,
51  .raw = {0x43, 0x2c, 0x2c, 0x43, 0x2c, 0x45, 0x4e, 0x56, 0x5f,
52  0x57, 0x41, 0x54, 0x45, 0x52, 0x5f, 0x54, 0x2c, 0x43,
53  0x2c, 0x31, 0x36, 0x2e, 0x32, 0x34, 0x2c, 0x43, 0x2c,
54  0x45, 0x4e, 0x56, 0x5f, 0x4f, 0x55, 0x54, 0x41, 0x49,
55  0x52, 0x5f, 0x54, 0x2c, 0x50, 0x2c, 0x31, 0x30, 0x31,
56  0x38, 0x30, 0x30, 0x2c, 0x50, 0x2c, 0x45, 0x4e, 0x56,
57  0x5f, 0x41, 0x54, 0x4d, 0x4f, 0x53, 0x5f, 0x50}};
58  validCS.checksum = 0x61;
59 
60  nmea_msg_t invalidCS = {.encapsulated = false,
61  .talker = "II",
62  .message = "XDR",
63  .rawlen = 62,
64  .raw = {0x43, 0x2c, 0x2c, 0x43, 0x2c, 0x45, 0x4e, 0x56, 0x5f,
65  0x57, 0x41, 0x54, 0x45, 0x52, 0x5f, 0x54, 0x2c, 0x43,
66  0x2c, 0x31, 0x36, 0x2e, 0x32, 0x34, 0x2c, 0x43, 0x2c,
67  0x45, 0x4e, 0x56, 0x5f, 0x4f, 0x55, 0x54, 0x41, 0x49,
68  0x52, 0x5f, 0x54, 0x2c, 0x50, 0x2c, 0x31, 0x30, 0x31,
69  0x38, 0x30, 0x30, 0x2c, 0x50, 0x2c, 0x45, 0x4e, 0x56,
70  0x5f, 0x41, 0x54, 0x4d, 0x4f, 0x53, 0x5f, 0x50}};
71  invalidCS.checksum = 0xFF;
72 
73  char *hex = nmea_string_hex(&validCS);
74  if (nmea_check_checksum(&validCS) == false) {
75  // LCOV_EXCL_START
76  fprintf(stderr, "[Failed] %s\n", hex);
77  fprintf(stderr, "[Error] Valid checksum failed test\n");
78  passed = false;
79  // LCOV_EXCL_STOP
80  } else {
81  printf("[Pass] Valid checksum: %s\n", hex);
82  }
83  free(hex);
84  hex = NULL;
85 
86  hex = nmea_string_hex(&invalidCS);
87  if (nmea_check_checksum(&invalidCS) == true) {
88  // LCOV_EXCL_START
89  fprintf(stderr, "[Failed] %s\n", hex);
90  fprintf(stderr, "[Error] Invalid checksum passed test\n");
91  passed = false;
92  // LCOV_EXCL_STOP
93  } else {
94  printf("[Pass] Invalid checksum: %s\n", hex);
95  }
96  free(hex);
97  hex = NULL;
98 
99  nmea_set_checksum(&invalidCS);
100 
101  hex = nmea_string_hex(&invalidCS);
102  if (nmea_check_checksum(&invalidCS) == false) {
103  // LCOV_EXCL_START
104  fprintf(stderr, "[Failed] %s\n", hex);
105  fprintf(stderr, "[Error] Corrected checksum failed test\n");
106  passed = false;
107  // LCOV_EXCL_STOP
108  } else {
109  printf("[Pass] Corrected checksum: %s\n", hex);
110  }
111  free(hex);
112  hex = NULL;
113 
114  if (passed) { return 0; }
115 
116  return -1;
117 }
int main(void)
char * nmea_string_hex(const nmea_msg_t *msg)
Return NMEA message as string.
Definition: NMEAMessages.c:174
void nmea_set_checksum(nmea_msg_t *msg)
Set checksum bytes for NMEA message.
Definition: NMEAMessages.c:74
bool nmea_check_checksum(const nmea_msg_t *msg)
Verify checksum bytes of NMEA message.
Definition: NMEAMessages.c:85
Generic NMEA message structure.
Definition: NMEATypes.h:78
bool encapsulated
Encapsulated message (all data in raw)
Definition: NMEATypes.h:79
uint8_t checksum
Message Checksum.
Definition: NMEATypes.h:86