SELKIELogger  1.0.0
NMEAMessagesFromFile.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 <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "SELKIELoggerBase.h"
28 #include "SELKIELoggerNMEA.h"
29 
49 int main(int argc, char *argv[]) {
50  //LCOV_EXCL_START
51  if (argc < 2) {
52  fprintf(stderr, "Usage: %s <file>\n", argv[0]);
53  return -2;
54  }
55 
56  errno = 0;
57  FILE *testFile = fopen(argv[1], "r");
58  if ((testFile == NULL) || errno) {
59  fprintf(stderr, "Unable to open test file %s\n", argv[1]);
60  perror("open");
61  return -2;
62  }
63  //LCOV_EXCL_STOP
64 
65  int count = 0;
66  int exit = 0;
67  while (!(feof(testFile) || exit == 1)) {
68  nmea_msg_t tmp = {0};
69  if (nmea_readMessage(fileno(testFile), &tmp)) {
70  // Successfully read message
71  count++;
72  strarray *f = nmea_parse_fields(&tmp);
73  if (f == NULL) { return -1; }
74  if (strncmp(tmp.message, "ZDA", 3) == 0) {
75  struct tm *t = nmea_parse_zda(&tmp);
76  //LCOV_EXCL_START
77  if (t == NULL) {
78  sa_destroy(f);
79  free(f);
80  return -1;
81  }
82  //LCOV_EXCL_STOP
83  fprintf(stdout, "%s", asctime(t));
84  free(t);
85  }
86  sa_destroy(f);
87  free(f);
88  } else {
89  switch (tmp.raw[0]) {
90  //LCOV_EXCL_START
91  case 0xAA:
92  fclose(testFile);
93  return -2;
94  break;
95  case 0xEE:
96  fclose(testFile);
97  return -1;
98  break;
99  //LCOV_EXCL_STOP
100  case 0xFD:
101  exit = 1;
102  break;
103  case 0xFF:
104  default:
105  break;
106  }
107  }
108  }
109  fprintf(stdout, "%d messages read\n", count);
110  fclose(testFile);
111  return 0;
112 }
int main(int argc, char *argv[])
bool nmea_readMessage(int handle, nmea_msg_t *out)
Static wrapper around mp_readMessage_buf.
Definition: NMEASerial.c:66
struct tm * nmea_parse_zda(const nmea_msg_t *msg)
Get date/time from NMEA ZDA message.
Definition: NMEAMessages.c:240
strarray * nmea_parse_fields(const nmea_msg_t *nmsg)
Parse raw data into fields.
Definition: NMEAMessages.c:203
void sa_destroy(strarray *sa)
Destroy array and contents.
Definition: strarray.c:182
Generic NMEA message structure.
Definition: NMEATypes.h:78
uint8_t raw[80]
Definition: NMEATypes.h:83
char message[3]
Message ID.
Definition: NMEATypes.h:81
Array of strings.
Definition: strarray.h:43