#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sensor_aq.h"
#include "sensor_aq_mbedtls_hs256.h"
int main() {
// 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
sensor_aq_ctx ctx = {
// the SDK requires a single buffer, and does not do any dynamic allocation
{ (unsigned char*)malloc(1024), 1024 },
// pass in the signing context
&signing_ctx,
// 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.
&fwrite,
&fseek,
// 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)
&time
};
// payload header
sensor_aq_payload_info payload = {
// unique device ID (optional),
// set this to e.g. MAC address or device EUI **if** your device has one
"ac:87:a3:0a:2d:1b",
// device type (required), use the same device type for similar devices
"DISCO-L475VG-IOT01A",
// how often new data is sampled in ms. (100Hz = every 10 ms.)
// (note: this is a float)
10,
// 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.
int res;
res = sensor_aq_init(&ctx, &payload, file);
if (res != AQ_OK) {
printf("sensor_aq_init failed (%d)\n", res);
return 1;
}
// Periodically call `sensor_aq_add_data` to append data
// (according to the frequency in the payload header)
float values[][3] = {
{ -9.81, 0.03, 1.21 },
{ -9.83, 0.04, 1.28 },
{ -9.12, 0.03, 1.23 },
{ -9.14, 0.01, 1.25 }
};
for (size_t ix = 0; ix < sizeof(values) / sizeof(values[0]); ix++) {
res = sensor_aq_add_data(&ctx, values[ix], 3);
if (res != AQ_OK) {
printf("sensor_aq_add_data failed (%d)\n", res);
return 1;
}
}
// when you're done call `sensor_aq_finish`
// this will calculate the finalized signature
// and close the CBOR file
res = sensor_aq_finish(&ctx);
if (res != AQ_OK) {
printf("sensor_aq_finish failed (%d)\n", res);
return 1;
}
// 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]);
}
printf("\n");
}