SELKIELogger  1.0.0
MPTests.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 <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include <sys/time.h>
26 
27 #include "SELKIELoggerBase.h"
28 #include "SELKIELoggerMP.h"
29 
44 int main(void) {
45  msg_t *msn = msg_new_string(SLSOURCE_TEST1, SLCHAN_NAME, 22, "Software Test Source 1");
46  assert(msn);
47 
48  // Generate a channel map for this "source"
49  strarray *SA = sa_new(6);
50 
51  assert(SA);
52  assert(sa_create_entry(SA, 0, 4, "Name"));
53  assert(sa_create_entry(SA, 1, 8, "Channels"));
54  assert(sa_create_entry(SA, 2, 9, "Timestamp"));
55  assert(sa_create_entry(SA, 3, 16, "SA Entry 1 - One"));
56  assert(sa_create_entry(SA, 4, 16, "SA Entry 2 - Two"));
57  assert(sa_create_entry(SA, 5, 18, "SA Entry 3 - Three"));
58 
60  assert(msa);
61 
62  // Generate a timestamp message
63  // Use a constant so that output can be compared
64  uint32_t tstamp = 3503357677;
66  assert(mts);
67 
68  msg_t *mdf = msg_new_float(SLSOURCE_TEST1, 3, 3.14159);
69  assert(mdf);
70 
71  uint8_t bytes[20] = {0x01, 0x05, 0xFD, 0xFC, 0x03, 0x02, 0x06, 0xFE, 0xFD, 0x04,
72  0x03, 0x06, 0xFF, 0xFE, 0x05, 0x00, 0x04, 0xFC, 0xFB, 0x02};
73  msg_t *mdb = msg_new_bytes(SLSOURCE_TEST1, 4, 20, bytes);
74  assert(mdb);
75 
76  float f[3] = {1.1, 2.2, 3.14159};
77  msg_t *mfa = msg_new_float_array(SLSOURCE_TEST1, 5, 3, f);
78 
79  assert(mp_writeMessage(fileno(stdout), msn));
80  assert(mp_writeMessage(fileno(stdout), msa));
81  assert(mp_writeMessage(fileno(stdout), mts));
82  assert(mp_writeMessage(fileno(stdout), mdf));
83  assert(mp_writeMessage(fileno(stdout), mdb));
84  assert(mp_writeMessage(fileno(stdout), mfa));
85 
86  FILE *tmp = tmpfile();
87  int tmpfd = fileno(tmp);
88  assert(mp_writeMessage(tmpfd, msn));
89  assert(mp_writeMessage(tmpfd, msa));
90  assert(mp_writeMessage(tmpfd, mts));
91  assert(mp_writeMessage(tmpfd, mdf));
92  assert(mp_writeMessage(tmpfd, mdb));
93  assert(mp_writeMessage(tmpfd, mfa));
94 
95  msg_destroy(msn);
96  free(msn);
97  msg_destroy(msa);
98  free(msa);
99  msg_destroy(mts);
100  free(mts);
101  msg_destroy(mdf);
102  free(mdf);
103  msg_destroy(mdb);
104  free(mdb);
105  msg_destroy(mfa);
106  free(mfa);
107 
108  sa_destroy(SA);
109  free(SA);
110 
111  fflush(tmp);
112  rewind(tmp);
113 
114  int count = 0;
115  int exit = 0;
116  while (!(feof(tmp) || exit == 1)) {
117  msg_t tMessage = {0};
118  if (mp_readMessage(fileno(tmp), &tMessage)) {
119  // Successfully read message
120  count++;
121  char *str = msg_to_string(&tMessage);
122  if (str) {
123  fprintf(stdout, "%s\n", str);
124  free(str);
125  str = NULL;
126  }
127  } else {
128  assert(tMessage.dtype == MSG_ERROR);
129  switch ((uint8_t)tMessage.data.value) {
130  // LCOV_EXCL_START
131  case 0xAA:
132  msg_destroy(&tMessage);
133  fclose(tmp);
134  return -2;
135  case 0xEE:
136  msg_destroy(&tMessage);
137  fclose(tmp);
138  return -1;
139  // LCOV_EXCL_STOP
140  case 0xFD:
141  exit = 1;
142  break;
143  case 0xFF:
144  default:
145  break;
146  }
147  }
148  msg_destroy(&tMessage);
149  }
150  assert(count == 6);
151 
152  fclose(tmp);
153  return EXIT_SUCCESS;
154 }
int main(void)
Definition: MPTests.c:44
msg_t * msg_new_string(const uint8_t source, const uint8_t type, const size_t len, const char *str)
Create a new message with a single string embedded.
Definition: messages.c:84
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
msg_t * msg_new_string_array(const uint8_t source, const uint8_t type, const strarray *array)
Create a new message containing an array of strings.
Definition: messages.c:116
msg_t * msg_new_bytes(const uint8_t source, const uint8_t type, const size_t len, const uint8_t *bytes)
Create a new message containing raw binary data.
Definition: messages.c:147
msg_t * msg_new_timestamp(const uint8_t source, const uint8_t type, const uint32_t ts)
Create a timestamp message.
Definition: messages.c:57
msg_t * msg_new_float_array(const uint8_t source, const uint8_t type, const size_t entries, const float *array)
Create a new message containing an array of floating point data.
Definition: messages.c:176
msg_t * msg_new_float(const uint8_t source, const uint8_t type, const float val)
Create new message with a single numeric value.
Definition: messages.c:38
@ MSG_ERROR
An error code is returned in data.value.
Definition: messages.h:56
bool mp_readMessage(int handle, msg_t *out)
Static wrapper around mp_readMessage_buf.
Definition: MPSerial.c:66
bool mp_writeMessage(int handle, const msg_t *out)
Send message to attached device.
Definition: MPSerial.c:382
#define SLCHAN_TSTAMP
Source timestamp (milliseconds, arbitrary epoch)
Definition: sources.h:99
#define SLCHAN_MAP
Channel name map (excludes log channels)
Definition: sources.h:98
#define SLCHAN_NAME
Name of source device.
Definition: sources.h:97
#define SLSOURCE_TEST1
Test data source ID (1/3)
Definition: sources.h:58
void sa_destroy(strarray *sa)
Destroy array and contents.
Definition: strarray.c:182
strarray * sa_new(int entries)
Allocate storage for a new array.
Definition: strarray.c:37
bool sa_create_entry(strarray *array, const int index, const size_t len, const char *src)
Create an string in a given position from a character array and length.
Definition: strarray.c:149
Queuable message.
Definition: messages.h:71
msg_data_t data
Embedded data.
Definition: messages.h:76
msg_dtype_t dtype
Embedded data type.
Definition: messages.h:75
Array of strings.
Definition: strarray.h:43
float value
Generic numerical data.
Definition: messages.h:46