147 msg_t *
msg_new_bytes(
const uint8_t source,
const uint8_t type,
const size_t len,
const uint8_t *bytes) {
153 newmsg->
data.
bytes = calloc(len,
sizeof(uint8_t));
182 newmsg->
data.
farray = calloc(entries,
sizeof(
float));
184 memcpy(newmsg->
data.
farray, array, entries *
sizeof(
float));
202 if (msg == NULL) {
return NULL; }
206 int ar = asprintf(&out,
"0x%02x:0x%02x %s", msg->
source, msg->
type, dstr);
209 if (ar >= 0) {
return out; }
223 if (msg == NULL) {
return NULL; }
227 switch (msg->
dtype) {
229 rv = asprintf(&out,
"%.6f", msg->
data.
value);
239 if (out == NULL) { out = strdup(
"[String array - error converting to string]"); }
242 rv = asprintf(&out,
"[Binary data, %zd bytes]", msg->
length);
246 if (out == NULL) { out = strdup(
"[Numeric array - error converting to string]"); }
250 out = strdup(
"[Message flagged as error]");
253 out = strdup(
"[Message flagged as uninitialised]");
256 out = strdup(
"[Message type unknown]");
262 if (out) { free(out); }
281 const size_t alen = 250;
282 char *out = calloc(alen,
sizeof(
char));
283 if (out == NULL) {
return NULL; }
285 for (
unsigned int i = 0; i < msg->
length; i++) {
286 int l = snprintf(&(out[pos]), (alen - pos),
"%.4f/", msg->
data.
farray[i]);
322 char *out = calloc(len,
sizeof(
char));
327 l = snprintf(&(out[pos]), (len - pos),
"-/");
350 if (msg == NULL) {
return; }
352 switch (msg->
dtype) {
373 fprintf(stderr,
"Unhandled message type in msg_destroy!\n");
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.
char * msg_data_to_string(const msg_t *msg)
Generate string representation of message data.
char * msg_to_string(const msg_t *msg)
Generate string representation of message.
void msg_destroy(msg_t *msg)
Destroy a message.
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.
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.
char * msg_data_sarr_to_string(const msg_t *msg)
Represent string array as single string.
msg_t * msg_new_timestamp(const uint8_t source, const uint8_t type, const uint32_t ts)
Create a timestamp message.
char * msg_data_narr_to_string(const msg_t *msg)
Convert numerical array to string.
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.
msg_t * msg_new_float(const uint8_t source, const uint8_t type, const float val)
Create new message with a single numeric value.
@ MSG_NUMARRAY
Array of floating point values.
@ MSG_ERROR
An error code is returned in data.value.
@ MSG_FLOAT
Generic numerical data.
@ MSG_BYTES
Raw binary data.
@ MSG_STRING
Single string.
@ MSG_UNDEF
Undefined/Uninitialised message.
@ MSG_STRARRAY
Array of strings.
@ MSG_TIMESTAMP
Timestamp (milliseconds since defined epoch/event)
void sa_destroy(strarray *sa)
Destroy array and contents.
bool sa_copy(strarray *dst, const strarray *src)
Copy an array of strings from src to dst.
bool str_update(string *str, const size_t len, const char *src)
Update an existing string from a character array of given length.
void str_destroy(string *str)
Destroy a string and free its contents.
size_t length
Data type dependent, see the msg_new functions.
msg_data_t data
Embedded data.
uint8_t type
Message type. Common types to be documented.
uint8_t source
Maps to a specific sensor unit or data source.
msg_dtype_t dtype
Embedded data type.
int entries
Maximum number of strings in array, set when calling sa_new()
string * strings
Simple array of string structures.
char * data
Character array, should be null terminated.
size_t length
This should include a terminating null byte where possible.
float * farray
Array of floats.
string string
Single character array with length.
uint32_t timestamp
Intended to represent millisecond level clock.
float value
Generic numerical data.
strarray names
Array of strings, intended for use to provide channel names.
uint8_t * bytes
Our "raw" binary type.