#include "sensor_aq_mbedtls_hs256.h"
// context to sign data, this object is instantiated below
sensor_aq_signing_ctx_t signing_ctx;
// use HMAC-SHA256 signatures, signed with Mbed TLS
sensor_aq_mbedtls_hs256_ctx_t hs_ctx;
// initialize the Mbed TLS context which also instantiates signing_ctx
sensor_aq_init_mbedtls_hs256_context(&signing_ctx, &hs_ctx, "my-hmac-sha256-key");
// set up the sensor acquisition context
// the SDK requires a single buffer, and does not do any dynamic allocation
{ (unsigned char*)malloc(1024), 1024 },
// pass in the signing context
// pointers to fwrite and fseek - note that these are pluggable so you
// can work with them on non-POSIX systems too. See the Porting Guide below.
// if you set the time function this will add the 'iat' (issued at) field to the header
// you can set this pointer to NULL for device that don't have an accurate clock (not recommended)
sensor_aq_payload_info payload = {
// unique device ID (optional),
// set this to e.g. MAC address or device EUI **if** your device has one
// device type (required), use the same device type for similar devices
// how often new data is sampled in ms. (100Hz = every 10 ms.)
// (note: this is a float)
// the axes which you'll use.
// the units field needs to comply to SenML units
// (see https://www.iana.org/assignments/senml/senml.xhtml)
{ { "accX", "m/s2" }, { "accY", "m/s2" }, { "accZ", "m/s2" } }
// place to write our data
FILE *file = fopen("encoded.cbor", "w+");
// initialize the context, this verifies that all requirements are present.
// it also writes the initial CBOR structure.
res = sensor_aq_init(&ctx, &payload, file);
printf("sensor_aq_init failed (%d)\n", res);
// Periodically call `sensor_aq_add_data` to append data
// (according to the frequency in the payload header)
for (size_t ix = 0; ix < sizeof(values) / sizeof(values[0]); ix++) {
res = sensor_aq_add_data(&ctx, values[ix], 3);
printf("sensor_aq_add_data failed (%d)\n", res);
// when you're done call `sensor_aq_finish`
// this will calculate the finalized signature
// and close the CBOR file
res = sensor_aq_finish(&ctx);
printf("sensor_aq_finish failed (%d)\n", res);
// this would be a good moment to upload 'encoded.cbor'
// to the Ingestion Service using your HTTP library of choice
// for convenience we'll print the encoded file.
// you can paste the output in http://cbor.me to decode
printf("Encoded file:\n");
// Print the content of the file here:
fseek(file, 0, SEEK_END);
size_t len = ftell(file);
uint8_t *buffer = (uint8_t*)malloc(len);
fseek(file, 0, SEEK_SET);
fread(buffer, len, 1, file);
for (size_t ix = 0; ix < len; ix++) {
printf("%02x ", buffer[ix]);