SELKIELogger  1.0.0
N2KClassify.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 <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "SELKIELoggerBase.h"
29 
30 #include "SELKIELoggerN2K.h"
31 
32 #include "version.h"
40 #define PGN_MAX 16777216
41 
43 #define BUFSIZE 1024
44 
52 
61 int main(int argc, char *argv[]) {
62  program_state state = {0};
63  state.verbose = 1;
64 
65  char *usage = "Usage: %1$s datfile\n"
66  "\nVersion: " GIT_VERSION_STRING "\n";
67 
68  opterr = 0; // Handle errors ourselves
69  int go = 0;
70  bool doUsage = false;
71  while ((go = getopt(argc, argv, "")) != -1) {
72  switch (go) {
73  case '?':
74  log_error(&state, "Unknown option `-%c'", optopt);
75  doUsage = true;
76  }
77  }
78 
79  // Should be 1 spare arguments: The file to convert
80  if (argc - optind != 1) {
81  log_error(&state, "Invalid arguments");
82  doUsage = true;
83  }
84 
85  if (doUsage) {
86  fprintf(stderr, usage, argv[0]);
87  return -1;
88  }
89 
90  FILE *nf = fopen(argv[optind], "r");
91 
92  if (nf == NULL) {
93  log_error(&state, "Unable to open input file \"%s\"", argv[optind]);
94  return -1;
95  }
96 
97  state.started = true;
98  bool processing = true;
99 
100  uint8_t buf[BUFSIZE] = {0};
101  size_t hw = 0;
102  int count = 0;
103  while (processing || hw > 18) {
104  if (processing && (hw < BUFSIZE)) {
105  ssize_t ret = fread(&(buf[hw]), sizeof(uint8_t), BUFSIZE - hw, nf);
106  if (ret < 0) {
107  log_error(&state, "Unable to read data from input");
108  processing = false;
109  } else {
110  hw += ret;
111  }
112  }
113  if (feof(nf)) {
114  log_info(&state, 2, "End of file reached");
115  processing = false;
116  }
117  n2k_act_message *nm = NULL;
118  size_t end = 0;
119  bool r = n2k_act_from_bytes(buf, hw, &nm, &end, (state.verbose > 2));
120  if (r) {
121  msgCount[nm->PGN]++;
122  count++;
123  } else {
124  if (!processing && end == 0) {
125  // If we're not making progress and we're at the end of file then
126  // this must be a bad message Force the process to move onwards
127  end = 1;
128  }
129  }
130 
131  if (nm) {
132  if (nm->data) { free(nm->data); }
133  free(nm);
134  nm = NULL;
135  }
136 
137  if ((hw - end) > 0) {
138  memmove(buf, &(buf[end]), hw - end);
139  hw -= end;
140  } else {
141  hw = 0;
142  end = 0;
143  }
144 
145  memset(&(buf[hw]), 0, BUFSIZE - hw);
146  }
147  log_info(&state, 0, "%d messages successfully read from file", count);
148  fprintf(stdout, "\nPGN \tCount\n");
149  for (int i = 0; i < PGN_MAX; i++) {
150  if (msgCount[i] > 0) { fprintf(stdout, "%6d\t%6d\n", i, msgCount[i]); }
151  }
152  fclose(nf);
153  return 0;
154 }
int main(int argc, char *argv[])
Definition: N2KClassify.c:61
#define PGN_MAX
Largest possible PGN value.
Definition: N2KClassify.c:40
int msgCount[PGN_MAX]
Definition: N2KClassify.c:51
#define BUFSIZE
Allocated read buffer size.
Definition: N2KClassify.c:43
bool n2k_act_from_bytes(const uint8_t *in, const size_t len, n2k_act_message **msg, size_t *pos, bool debug)
Convert a series of recieved bytes from ACT gateway devices into a message representation.
Definition: N2KTypes.c:98
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
uint32_t PGN
24 bit PGN identifier
Definition: N2KTypes.h:84
uint8_t * data
Message payload.
Definition: N2KTypes.h:89
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
#define GIT_VERSION_STRING
Git version description.
Definition: version.h.in:13