SELKIELogger  1.0.0
MPMessagesFromFile.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 <string.h>
24 #include <unistd.h>
25 
26 #include "SELKIELoggerMP.h"
27 
47 int main(int argc, char *argv[]) {
48  //LCOV_EXCL_START
49  if (argc < 2) {
50  fprintf(stderr, "Usage: %s <file>\n", argv[0]);
51  return -2;
52  }
53 
54  errno = 0;
55  FILE *testFile = fopen(argv[1], "r");
56  if ((testFile == NULL) || errno) {
57  fprintf(stderr, "Unable to open test file %s\n", argv[1]);
58  perror("open");
59  return -2;
60  }
61  //LCOV_EXCL_STOP
62 
63  int count = 0;
64  int exit = 0;
65  uint8_t sources[128] = {0};
66  uint8_t types[128] = {0};
67 
68  while (!(feof(testFile) || exit == 1)) {
69  msg_t tmp = {0};
70  if (mp_readMessage(fileno(testFile), &tmp)) {
71  // Successfully read message
72  count++;
73  sources[tmp.source]++;
74  types[tmp.type]++;
75  msg_destroy(&tmp);
76  } else {
77  switch ((uint8_t)tmp.data.value) {
78  //LCOV_EXCL_START
79  // Exclude error cases from test coverage
80  case 0xAA:
81  fclose(testFile);
82  return -2;
83  break;
84  case 0xEE:
85  fclose(testFile);
86  return -1;
87  break;
88  //LCOV_EXCL_STOP
89  case 0xFD:
90  exit = 1;
91  break;
92  case 0xFF:
93  default:
94  break;
95  }
96  }
97  }
98  fprintf(stdout, "%d messages read\n", count);
99  for (int i = 0; i < 128; i++) {
100  if (sources[i] > 0) {
101  fprintf(stdout, "%d messages read from source 0x%02x\n", sources[i], i);
102  }
103  }
104  for (int i = 0; i < 128; i++) {
105  if (types[i] > 0) {
106  fprintf(stdout, "%d messages read were of type 0x%02x\n", types[i], i);
107  }
108  }
109  fclose(testFile);
110  return 0;
111 }
int main(int argc, char *argv[])
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
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
uint8_t source
Maps to a specific sensor unit or data source.
Definition: messages.h:72
float value
Generic numerical data.
Definition: messages.h:46