SELKIELogger  1.0.0
DumpMessages.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 "SELKIELoggerMP.h"
29 
30 #include "version.h"
31 
48 int main(int argc, char *argv[]) {
49  program_state state = {0};
50  state.verbose = 1;
51 
52  char *usage = "Usage: %1$s [-v] datfile\n"
53  "\nVersion: " GIT_VERSION_STRING "\n";
54 
55  opterr = 0; // Handle errors ourselves
56  int go = 0;
57  bool doUsage = false;
58  while ((go = getopt(argc, argv, "v")) != -1) {
59  switch (go) {
60  case 'v':
61  state.verbose++;
62  break;
63  case '?':
64  log_error(&state, "Unknown option `-%c'", optopt);
65  doUsage = true;
66  }
67  }
68 
69  // Should be 1 spare arguments: The file to convert
70  if (argc - optind != 1) {
71  log_error(&state, "Invalid arguments");
72  doUsage = true;
73  }
74 
75  if (doUsage) {
76  fprintf(stderr, usage, argv[0]);
77  destroy_program_state(&state);
78  return -1;
79  }
80 
81  char *inFileName = strdup(argv[optind]);
82  FILE *inFile = fopen(inFileName, "rb");
83  if (inFile == NULL) {
84  log_error(&state, "Unable to open input file");
85  free(inFileName);
86  destroy_program_state(&state);
87  return -1;
88  }
89 
90  state.started = 1;
91  int msgCount = 0;
92  while (!(feof(inFile))) {
93  // Read message from data file
94  msg_t tmp = {0};
95  if (!mp_readMessage(fileno(inFile), &tmp)) {
96  if (tmp.data.value == 0xAA || tmp.data.value == 0xEE) {
97  log_error(&state,
98  "Error reading messages from file (Code: 0x%52x)\n",
99  (uint8_t)tmp.data.value);
100  }
101  if (tmp.data.value == 0xFD) {
102  // No more data, exit cleanly
103  log_info(&state, 1, "End of file reached");
104  }
105  break;
106  }
107 
108  msgCount++;
109  if (tmp.type >= 0x03 || state.verbose > 1) {
110  char *msgstring = msg_to_string(&tmp);
111  if (msgstring) { fprintf(stdout, "%s\n", msgstring); }
112  free(msgstring);
113  }
114  msg_destroy(&tmp);
115  }
116 
117  log_info(&state, 1, "%d messages processed", msgCount);
118  free(inFileName);
119  fclose(inFile);
120  return 0;
121 }
int main(int argc, char *argv[])
Definition: DumpMessages.c:48
int msgCount[PGN_MAX]
Definition: N2KClassify.c:51
char * msg_to_string(const msg_t *msg)
Generate string representation of message.
Definition: messages.c:201
void msg_destroy(msg_t *msg)
Destroy a message.
Definition: messages.c:349
bool mp_readMessage(int handle, msg_t *out)
Static wrapper around mp_readMessage_buf.
Definition: MPSerial.c:66
void destroy_program_state(program_state *s)
Cleanly destroy program state.
Definition: logging.c:207
void log_info(const program_state *s, const int level, const char *format,...)
Output formatted information message at a given level.
Definition: logging.c:125
void log_error(const program_state *s, const char *format,...)
Output formatted error message.
Definition: logging.c:47
Queuable message.
Definition: messages.h:71
msg_data_t data
Embedded data.
Definition: messages.h:76
uint8_t type
Message type. Common types to be documented.
Definition: messages.h:73
Program state and logging information.
Definition: logging.h:40
int verbose
Current log verbosity (console output)
Definition: logging.h:43
bool started
Indicates startup completed.
Definition: logging.h:41
float value
Generic numerical data.
Definition: messages.h:46
#define GIT_VERSION_STRING
Git version description.
Definition: version.h.in:13