Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

C quickstart

The C ABI is a static library plus the generated header crates/ffi/include/predsync.h. Link against libpredsync_ffi.a; every entry point returns a PredsyncStatus.

#include "predsync.h"
#include <stdint.h>
#include <string.h>

static const char SCHEMA[] =
    "name: telemetry\n"
    "sync: { refresh: 8, reset: 32 }\n"
    "fields:\n"
    "  - name: x\n"
    "    type: f64\n"
    "    quantise: 0.01\n"
    "    range: [-100.0, 100.0]\n"
    "    predictor: { type: linear, window: 4 }\n"
    "    tolerance: { abs: 0.01 }\n";

int send_one_frame(void) {
    PredsyncSet *set = NULL;
    PredsyncEncoder *encoder = NULL;
    uint64_t bits = 0;
    double value = 1.25;
    uint8_t frame[256];
    uintptr_t frame_len = 0;
    PredsyncEncodeInfo info;
    uint32_t error = 0;
    int result = 1;

    memcpy(&bits, &value, sizeof bits);
    PredsyncValue values[1] = {{
        Scalar, F64, bits, NULL, 0, 0
    }};

    if (predsync_set_from_yaml(SCHEMA, strlen(SCHEMA), &set) != Ok) {
        return 1;
    }
    if (predsync_encoder_new(set, 0, 1000000ULL, 0xffff, &encoder) != Ok) {
        goto done;
    }
    if (predsync_encode(encoder, values, 1, 0, frame, sizeof frame,
                        &frame_len, &info, &error) == Ok) {
        result = 0; /* pass frame, frame_len to your transport */
    }

done:
    if (encoder != NULL) {
        predsync_encoder_free(encoder);
    }
    predsync_set_free(set);
    return result;
}

The library never owns a caller buffer. Strings and lists are borrowed on encode; decoded strings and lists are copied into caller-provided capacity. See the C API reference and the complete C quickstart example.