cosc 0.1.0
OSC library for C99 or C++11.
Loading...
Searching...
No Matches
serial.c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "cosc.h"
int main(int argc, char *argv[])
{
char buffer[1024] = {0};
struct cosc_level levels[4];
struct cosc_serial serial;
// Setup the writer.
&serial,
buffer, sizeof(buffer), // Store to this buffer.
levels, 4, // Provide 4 levels, 1 is minimum.
0 // Zero flags.
);
// Timetag to 3.5 seconds.
cosc_uint64 timetag = cosc_time_to_timetag(3, 500000000);
// Start with a bundle (we could start with a message directly too).
ret = cosc_writer_start_bundle(&serial, timetag);
if (ret < 0)
{
printf("Uh oh, couldn't start writing bundle: %d\n", ret);
return 1;
}
// We could use cosc_writer_message(&serial, &message, NULL) to write
// an entire message, but we want to nest things in the blob so
// let's go for an start message.
ret = cosc_writer_start_message(&serial, "/address", 1024, ",ibf", 1024);
if (ret < 0)
{
printf("Uh oh, couldn't start writing message: %d\n", ret);
return 1;
}
// The first value is 'i'.
ret = cosc_writer_int32(&serial, 150);
if (ret < 0)
{
printf("Uh oh, couldn't write 'i': %d\n", ret);
return 1;
}
// We could write the entire blob like this:
// ret = cosc_writer_blob(&serial, blob_data, blob_size);
// But we want to nest another message into the blob.
ret = cosc_writer_start_blob(&serial);
if (ret < 0)
{
printf("Uh oh, couldn't start writing blob: %d\n", ret);
return 1;
}
// With the blob start all other writes will be stored to the blob,
// but we want a message. We could do it like this:
// cosc_writer_message(&serial, &message, NULL);
// But let's do it with an start message instead.
ret = cosc_writer_start_message(&serial, "/nested", 1024, ",i", 1024);
if (ret < 0)
{
printf("Uh oh, couldn't start writing nested message: %d\n", ret);
return 1;
}
// The first value is 'i'.
ret = cosc_writer_int32(&serial, 12345678);
if (ret < 0)
{
printf("Uh oh, couldn't write 'i': %d\n", ret);
return 1;
}
// Let's end the message nested message.
ret = cosc_writer_end_message(&serial);
if (ret < 0)
{
printf("Uh oh, couldn't end writing the nested message: %d\n", ret);
return 1;
}
// Let's end the blob.
ret = cosc_writer_end_blob(&serial);
if (ret < 0)
{
printf("Uh oh, couldn't end writing the blob: %d\n", ret);
return 1;
}
// Now we're back to the first message, let's wrap it up with
// it's third and final value of type 'f'.
ret = cosc_writer_float32(&serial, 1.5);
if (ret < 0)
{
printf("Uh oh, couldn't write 'f': %d\n", ret);
return 1;
}
// Done, let's end the message.
ret = cosc_writer_end_message(&serial);
if (ret < 0)
{
printf("Uh oh, couldn't end writing the message: %d\n", ret);
return 1;
}
// Done, let's end the bundle.
ret = cosc_writer_end_bundle(&serial);
if (ret < 0)
{
printf("Uh oh, couldn't end writing the bundle: %d\n", ret);
return 1;
}
// How big was it?
printf("Wrote %d bytes of OSC data.\n", cosc_serial_get_size(&serial));
// We could dump the buffer to see what it looks like.
for (int i = 0; i < cosc_serial_get_size(&serial); i++)
{
printf("%03d: 0x%02x '%c'\n", i, (unsigned char)buffer[i], buffer[i] > 31 ? buffer[i] : ' ');
}
// Now to read it all.
&serial,
buffer, cosc_serial_get_size(&serial), // Load from this buffer using the written size.
levels, 4, // Levels.
0 // No flags.
);
// Let's just make sure we have a bundle
if (cosc_reader_peek_bundle(&serial, NULL, NULL) < 0)
{
printf("Uh oh, not a bundle? %d\n", ret);
return 1;
}
// The bundle.
timetag = 0; // Just to make sure we actuall did read it.
ret = cosc_reader_start_bundle(&serial, &timetag);
if (ret < 0)
{
printf("Uh oh, couldn't start bundle: %d\n", ret);
return 1;
}
cosc_uint32 nanos;
cosc_uint32 seconds = cosc_timetag_to_time(timetag, &nanos);
printf("Timetag is %f.\n", (double)seconds + nanos / 1000000000.0);
// Start the message.
const char *address, *typetag;
cosc_int32 address_len, typetag_len;
&serial,
&address, &address_len,
&typetag, &typetag_len
);
if (ret < 0)
{
printf("Uh oh, couldn't start message: %d\n", ret);
return 1;
}
printf("Address '%s' and typetag '%s'\n", address, typetag);
// First value
cosc_int32 value_i;
ret = cosc_reader_int32(&serial, &value_i);
if (ret < 0)
{
printf("Uh oh, couldn't read 'i': %d\n", ret);
return 1;
}
printf("Value 'i': %d\n", value_i);
// Then the blob.
cosc_int32 blob_size;
ret = cosc_reader_start_blob(&serial, &blob_size);
if (ret < 0)
{
printf("Uh oh, couldn't start blob: %d\n", ret);
return 1;
}
printf("Blob is %d bytes.\n", blob_size);
// We know we put a message in here, let's do the shorthand
// function this time. We need to store the values which
// is only 1 'i' value.
union cosc_value values[1];
struct cosc_message message = {
.values = {.read = values},
.values_n = 1
};
cosc_int32 value_count; // We can see the actual number of values here.
ret = cosc_reader_message(&serial, &message, &value_count, true);
if (ret < 0)
{
printf("Uh oh, couldn't read nested message: %d\n", ret);
return 1;
}
printf("Address='%s', Typetag='%s'\n", message.address, message.typetag);
printf("Nested message has %d values.\n", value_count);
printf("Value 'i': %d\n", values[0].i);
// Close the blob.
ret = cosc_reader_end_blob(&serial);
if (ret < 0)
{
printf("Uh oh, couldn't close the blob: %d\n", ret);
return 1;
}
// Final value of the top message.
cosc_float32 value_f;
ret = cosc_reader_float32(&serial, &value_f);
if (ret < 0)
{
printf("Uh oh, couldn't read 'f': %d\n", ret);
return 1;
}
printf("Final float is %f\n", value_f);
// We need to end the top message.
ret = cosc_reader_end_message(&serial, true);
if (ret < 0)
{
printf("Uh oh, couldn't end the message: %d\n", ret);
return 1;
}
// We need to end the bundle.
ret = cosc_reader_end_bundle(&serial);
if (ret < 0)
{
printf("Uh oh, couldn't end the message: %d\n", ret);
return 1;
}
// How big was it?
printf("Read %d bytes of OSC data.\n", cosc_serial_get_size(&serial));
return 0;
}
OSC library for C99 or C++11.
COSC_TYPE_INT32 cosc_int32
Signed 32-bit integer.
Definition cosc.h:253
COSC_API cosc_int32 cosc_reader_end_bundle(struct cosc_serial *serial)
End a bundle level.
Definition cosc.c:2907
COSC_API cosc_int32 cosc_reader_message(struct cosc_serial *serial, struct cosc_message *message, cosc_int32 *value_count, cosc_int32 exit_early)
Read an OSC message.
Definition cosc.c:3237
COSC_API cosc_int32 cosc_reader_int32(struct cosc_serial *serial, cosc_int32 *value)
Write a signed 32-bit integer.
Definition cosc.c:3064
COSC_API cosc_int32 cosc_reader_peek_bundle(struct cosc_serial *serial, cosc_uint64 *timetag, cosc_int32 *psize)
Check if the buffer has a bundle at the current read size.
Definition cosc.c:2858
COSC_API void cosc_writer_setup(struct cosc_serial *serial, void *buffer, cosc_int32 buffer_size, struct cosc_level *levels, cosc_int32 level_max, cosc_uint32 flags)
Setup a serial for writing.
Definition cosc.c:2408
COSC_TYPE_UINT64 cosc_uint64
Unsigned 64-bit integer.
Definition cosc.h:345
COSC_API cosc_int32 cosc_reader_start_message(struct cosc_serial *serial, const char **address, cosc_int32 *address_n, const char **typetag, cosc_int32 *typetag_n)
Start a new message level.
Definition cosc.c:2921
COSC_API cosc_int32 cosc_writer_start_blob(struct cosc_serial *serial)
Start a new blob level.
Definition cosc.c:2525
COSC_API cosc_int32 cosc_reader_end_message(struct cosc_serial *serial, cosc_int32 exit_early)
End a message level.
Definition cosc.c:2973
COSC_API cosc_int32 cosc_writer_float32(struct cosc_serial *serial, cosc_float32 value)
Write a 32-bit float.
Definition cosc.c:2590
COSC_API cosc_int32 cosc_reader_float32(struct cosc_serial *serial, cosc_float32 *value)
Write a 32-bit float.
Definition cosc.c:3077
COSC_API cosc_int32 cosc_reader_start_bundle(struct cosc_serial *serial, cosc_uint64 *timetag)
Start a new bundle level.
Definition cosc.c:2880
COSC_API cosc_uint32 cosc_timetag_to_time(cosc_uint64 timetag, cosc_uint32 *nanos)
Convert a timetag to seconds and nanoseconds.
Definition cosc.c:970
COSC_API cosc_uint64 cosc_time_to_timetag(cosc_uint32 seconds, cosc_uint32 nanos)
Convert time to a timetag.
Definition cosc.c:996
COSC_API cosc_int32 cosc_writer_end_bundle(struct cosc_serial *serial)
End a bundle level.
Definition cosc.c:2442
COSC_API cosc_int32 cosc_reader_start_blob(struct cosc_serial *serial, cosc_int32 *value_size)
Start a new blob level.
Definition cosc.c:3006
COSC_API cosc_int32 cosc_serial_get_size(const struct cosc_serial *serial)
Get the number of written or read bytes.
Definition cosc.c:2371
COSC_API cosc_int32 cosc_writer_int32(struct cosc_serial *serial, cosc_int32 value)
Write a signed 32-bit integer.
Definition cosc.c:2577
COSC_API cosc_int32 cosc_writer_end_blob(struct cosc_serial *serial)
End a blob level.
Definition cosc.c:2543
COSC_API void cosc_reader_setup(struct cosc_serial *serial, const void *buffer, cosc_int32 buffer_size, struct cosc_level *levels, cosc_int32 level_max, cosc_uint32 flags)
Setup a serial for reading.
Definition cosc.c:2846
COSC_API cosc_int32 cosc_reader_end_blob(struct cosc_serial *serial)
End a blob level.
Definition cosc.c:3032
COSC_API cosc_int32 cosc_writer_start_message(struct cosc_serial *serial, const char *address, cosc_int32 address_n, const char *typetag, cosc_int32 typetag_n)
Start a new message level.
Definition cosc.c:2456
COSC_TYPE_UINT32 cosc_uint32
Unsigned 32-bit integer.
Definition cosc.h:248
COSC_API cosc_int32 cosc_writer_end_message(struct cosc_serial *serial)
End a message level.
Definition cosc.c:2500
COSC_TYPE_FLOAT32 cosc_float32
32-bit floating point.
Definition cosc.h:350
COSC_API cosc_int32 cosc_writer_start_bundle(struct cosc_serial *serial, cosc_uint64 timetag)
Start a new bundle level.
Definition cosc.c:2420
Used to manage the levels of a serial.
Definition cosc.h:603
A message.
Definition cosc.h:536
union cosc_message::@050073337162334015163030171201070055033256045255 values
Const safety.
const char * typetag
The typetag.
Definition cosc.h:552
const char * address
The address.
Definition cosc.h:541
A serial.
Definition cosc.h:653
struct cosc_level * levels
A pointer to an array of levels.
Definition cosc.h:673
A value.
Definition cosc.h:447