SELKIELogger  1.0.0
messages.h
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 #ifndef SELKIELoggerBase_Messages
22 #define SELKIELoggerBase_Messages
23 #include "strarray.h"
24 #include <stdint.h>
25 
45 typedef union {
46  float value;
47  uint32_t timestamp;
48  uint8_t *bytes;
49  string string;
51  float *farray;
52 } msg_data_t;
53 
55 typedef enum {
56  MSG_ERROR = -1,
57  MSG_UNDEF = 0,
64 } msg_dtype_t;
65 
67 
71 typedef struct {
72  uint8_t source;
73  uint8_t type;
74  size_t length;
77 } msg_t;
78 
80 msg_t *msg_new_float(const uint8_t source, const uint8_t type, const float val);
81 
83 msg_t *msg_new_timestamp(const uint8_t source, const uint8_t type, const uint32_t ts);
84 
86 msg_t *msg_new_string(const uint8_t source, const uint8_t type, const size_t len, const char *str);
87 
89 msg_t *msg_new_string_array(const uint8_t source, const uint8_t type, const strarray *array);
90 
92 msg_t *msg_new_bytes(const uint8_t source, const uint8_t type, const size_t len, const uint8_t *bytes);
93 
95 msg_t *msg_new_float_array(const uint8_t source, const uint8_t type, const size_t entries, const float *array);
96 
98 char *msg_to_string(const msg_t *msg);
99 
101 char *msg_data_to_string(const msg_t *msg);
102 
104 char *msg_data_narr_to_string(const msg_t *msg);
105 
107 char *msg_data_sarr_to_string(const msg_t *msg);
108 
110 void msg_destroy(msg_t *msg);
112 #endif
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_data_to_string(const msg_t *msg)
Generate string representation of message data.
Definition: messages.c:222
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_dtype_t
Each data type should map to an entry in the msg_data_t union.
Definition: messages.h:55
char * msg_data_sarr_to_string(const msg_t *msg)
Represent string array as single string.
Definition: messages.c:310
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
char * msg_data_narr_to_string(const msg_t *msg)
Convert numerical array to string.
Definition: messages.c:280
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_NUMARRAY
Array of floating point values.
Definition: messages.h:63
@ MSG_ERROR
An error code is returned in data.value.
Definition: messages.h:56
@ MSG_FLOAT
Generic numerical data.
Definition: messages.h:58
@ MSG_BYTES
Raw binary data.
Definition: messages.h:60
@ MSG_STRING
Single string.
Definition: messages.h:61
@ MSG_UNDEF
Undefined/Uninitialised message.
Definition: messages.h:57
@ MSG_STRARRAY
Array of strings.
Definition: messages.h:62
@ MSG_TIMESTAMP
Timestamp (milliseconds since defined epoch/event)
Definition: messages.h:59
Queuable message.
Definition: messages.h:71
size_t length
Data type dependent, see the msg_new functions.
Definition: messages.h:74
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
msg_dtype_t dtype
Embedded data type.
Definition: messages.h:75
Array of strings.
Definition: strarray.h:43
float * farray
Array of floats.
Definition: messages.h:51
string string
Single character array with length.
Definition: messages.h:49
uint32_t timestamp
Intended to represent millisecond level clock.
Definition: messages.h:47
float value
Generic numerical data.
Definition: messages.h:46
strarray names
Array of strings, intended for use to provide channel names.
Definition: messages.h:50
uint8_t * bytes
Our "raw" binary type.
Definition: messages.h:48