SELKIELogger  1.0.0
UBXChecksumTest.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 "SELKIELoggerGPS.h"
26 
43 int main(void) {
44  bool passed = true;
45 
46  ubx_message validCS = {0xB5, 0x62, 0x06, 0x02, 0x0001, {0x01}, 0x0A, 0x2A, 0x00};
47  ubx_message invalidCS = {0xB5, 0x62, 0x06, 0x02, 0x0001, {0x01}, 0xFF, 0xFF, 0x00};
48 
49  char *hex = ubx_string_hex(&validCS);
50  if (ubx_check_checksum(&validCS) == false) {
51  // LCOV_EXCL_START
52  fprintf(stderr, "[Failed] %s\n", hex);
53  fprintf(stderr, "[Error] Valid checksum failed test\n");
54  passed = false;
55  // LCOV_EXCL_STOP
56  } else {
57  printf("[Pass] Valid checksum: %s\n", hex);
58  }
59  free(hex);
60  hex = NULL;
61 
62  hex = ubx_string_hex(&invalidCS);
63  if (ubx_check_checksum(&invalidCS) == true) {
64  // LCOV_EXCL_START
65  fprintf(stderr, "[Failed] %s\n", hex);
66  fprintf(stderr, "[Error] Invalid checksum passed test\n");
67  passed = false;
68  // LCOV_EXCL_STOP
69  } else {
70  printf("[Pass] Invalid checksum: %s\n", hex);
71  }
72  free(hex);
73  hex = NULL;
74 
75  ubx_set_checksum(&invalidCS);
76 
77  hex = ubx_string_hex(&invalidCS);
78  if (ubx_check_checksum(&invalidCS) == false) {
79  // LCOV_EXCL_START
80  fprintf(stderr, "[Failed] %s\n", hex);
81  fprintf(stderr, "[Error] Corrected checksum failed test\n");
82  passed = false;
83  // LCOV_EXCL_STOP
84  } else {
85  printf("[Pass] Corrected checksum: %s\n", hex);
86  }
87  free(hex);
88  hex = NULL;
89 
90  if (passed) { return 0; }
91 
92  return -1;
93 }
int main(void)
bool ubx_check_checksum(const ubx_message *msg)
Verify checksum bytes of UBX message.
Definition: GPSMessages.c:82
void ubx_set_checksum(ubx_message *msg)
Set checksum bytes for UBX message.
Definition: GPSMessages.c:71
char * ubx_string_hex(const ubx_message *msg)
Return UBX message as string of hexadecimal pairs.
Definition: GPSMessages.c:134
Internal representation of a UBX message.
Definition: GPSTypes.h:80