2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// C API for libsds, built on the nim-ffi framework (v0.2.0+).
|
2026-06-03 12:14:29 +02:00
|
|
|
//
|
2026-06-03 13:22:11 +02:00
|
|
|
// Requests, responses and events are marshalled as CBOR. Request payloads are
|
|
|
|
|
// passed as a (reqCbor, reqCborLen) byte buffer; results and events are
|
|
|
|
|
// delivered to the callback as a CBOR buffer (msg, len). Each request/response
|
|
|
|
|
// struct and event payload is defined in library/libsds.nim. Events are
|
|
|
|
|
// wrapped in a CBOR envelope { eventType: <wire name>, payload: <struct> }.
|
2025-05-29 16:48:53 +05:30
|
|
|
#ifndef __libsds__
|
|
|
|
|
#define __libsds__
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
// The possible returned values for the functions that return int
|
|
|
|
|
#define RET_OK 0
|
|
|
|
|
#define RET_ERR 1
|
|
|
|
|
#define RET_MISSING_CALLBACK 2
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// Result/event callback. `msg` is the CBOR payload of length `len`.
|
2026-06-03 12:14:29 +02:00
|
|
|
// callerRet is one of the RET_* codes above.
|
2025-05-29 16:48:53 +05:30
|
|
|
typedef void (*SdsCallBack) (int callerRet, const char* msg, size_t len, void* userData);
|
|
|
|
|
|
2026-06-03 12:14:29 +02:00
|
|
|
// Synchronous provider invoked by SDS-R to fetch a retrieval hint for a
|
|
|
|
|
// message id. The implementation allocates `*hint` (and sets `*hintLen`); the
|
2026-06-03 13:22:11 +02:00
|
|
|
// library takes ownership and frees it with deallocShared. Registered via
|
|
|
|
|
// sds_set_retrieval_hint_provider (see below).
|
2026-01-29 15:22:40 +05:30
|
|
|
typedef void (*SdsRetrievalHintProvider) (const char* messageId, char** hint, size_t* hintLen, void* userData);
|
|
|
|
|
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// --- Lifecycle -------------------------------------------------------------
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// Create a context + ReliabilityManager. reqCbor encodes SdsConfig
|
|
|
|
|
// { participantId: tstr } (empty participantId disables SDS-R). Returns the
|
|
|
|
|
// context handle, or NULL on failure; the callback also fires on completion.
|
|
|
|
|
void* sds_create(const uint8_t* reqCbor, size_t reqCborLen, SdsCallBack callback, void* userData);
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// Tear down the context created by sds_create. Blocks until the worker and
|
|
|
|
|
// watchdog threads have joined.
|
|
|
|
|
int sds_destroy(void* ctx);
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-01-29 15:22:40 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// --- Events ----------------------------------------------------------------
|
|
|
|
|
// Subscribe `callback` to an event by wire name and receive a stable listener
|
|
|
|
|
// id (non-zero). Event wire names: "message_ready", "message_sent",
|
|
|
|
|
// "missing_dependencies", "periodic_sync", "repair_ready". Subscribe to each
|
|
|
|
|
// event separately. Payloads arrive as CBOR { eventType, payload }.
|
|
|
|
|
uint64_t sds_add_event_listener(void* ctx, const char* eventName, SdsCallBack callback, void* userData);
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// Remove a listener by id. Returns 0 on success, non-zero if not found.
|
|
|
|
|
int sds_remove_event_listener(void* ctx, uint64_t listenerId);
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// Register the SDS-R retrieval-hint provider. reqCbor encodes
|
|
|
|
|
// SdsHintProviderRequest { callbackAddr: uint, userDataAddr: uint } — the
|
|
|
|
|
// SdsRetrievalHintProvider function pointer and its user-data as integer
|
|
|
|
|
// addresses.
|
|
|
|
|
int sds_set_retrieval_hint_provider(void* ctx, SdsCallBack callback, void* userData, const uint8_t* reqCbor, size_t reqCborLen);
|
2025-05-29 16:48:53 +05:30
|
|
|
|
|
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// --- Core API Functions ----------------------------------------------------
|
|
|
|
|
// Each takes a CBOR-encoded request buffer; the result is delivered to
|
|
|
|
|
// `callback` as CBOR.
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// reqCbor: SdsWrapRequest { message: bytes, messageId: tstr, channelId: tstr }
|
|
|
|
|
// result: SdsWrapResponse { message: bytes }
|
|
|
|
|
int sds_wrap_outgoing_message(void* ctx, SdsCallBack callback, void* userData, const uint8_t* reqCbor, size_t reqCborLen);
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2026-06-03 13:22:11 +02:00
|
|
|
// reqCbor: SdsUnwrapRequest { message: bytes }
|
|
|
|
|
// result: SdsUnwrapResponse { message: bytes, channelId: tstr,
|
|
|
|
|
// missingDeps: [{ messageId: tstr, retrievalHint: bytes }] }
|
|
|
|
|
int sds_unwrap_received_message(void* ctx, SdsCallBack callback, void* userData, const uint8_t* reqCbor, size_t reqCborLen);
|
|
|
|
|
|
|
|
|
|
// reqCbor: SdsMarkDependenciesRequest { messageIds: [tstr], channelId: tstr }
|
|
|
|
|
int sds_mark_dependencies_met(void* ctx, SdsCallBack callback, void* userData, const uint8_t* reqCbor, size_t reqCborLen);
|
|
|
|
|
|
|
|
|
|
// reqCbor: empty/unit payload (no fields).
|
|
|
|
|
int sds_reset(void* ctx, SdsCallBack callback, void* userData, const uint8_t* reqCbor, size_t reqCborLen);
|
|
|
|
|
|
|
|
|
|
// reqCbor: empty/unit payload (no fields).
|
|
|
|
|
int sds_start_periodic_tasks(void* ctx, SdsCallBack callback, void* userData, const uint8_t* reqCbor, size_t reqCborLen);
|
2025-05-29 16:48:53 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-06-03 12:14:29 +02:00
|
|
|
#endif /* __libsds__ */
|