|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +// SPDX-FileCopyrightText: 2025 Hyperpolymath Contributors |
| 3 | +module bebop_bridge |
| 4 | + |
| 5 | +// This module binds to the C ABI defined in include/bebop_v_ffi.h. |
| 6 | +// It intentionally uses ptr+len (VBytes) and does not assume NUL-terminated payload strings. |
| 7 | + |
| 8 | +[typedef] |
| 9 | +pub struct C.BebopCtx {} |
| 10 | + |
| 11 | +[typedef] |
| 12 | +pub struct C.VBytes { |
| 13 | + ptr &u8 |
| 14 | + len usize |
| 15 | +} |
| 16 | + |
| 17 | +[typedef] |
| 18 | +pub struct C.VSensorReading { |
| 19 | + timestamp u64 |
| 20 | + sensor_id C.VBytes |
| 21 | + sensor_type u16 |
| 22 | + value f64 |
| 23 | + unit C.VBytes |
| 24 | + location C.VBytes |
| 25 | + metadata_count usize |
| 26 | + metadata_keys &C.VBytes |
| 27 | + metadata_values &C.VBytes |
| 28 | + error_code int |
| 29 | + error_message &char |
| 30 | +} |
| 31 | + |
| 32 | +// C ABI functions |
| 33 | +fn C.bebop_ctx_new() &C.BebopCtx |
| 34 | +fn C.bebop_ctx_free(ctx &C.BebopCtx) |
| 35 | +fn C.bebop_ctx_reset(ctx &C.BebopCtx) |
| 36 | + |
| 37 | +fn C.bebop_decode_sensor_reading(ctx &C.BebopCtx, data &u8, len usize, out &C.VSensorReading) int |
| 38 | +fn C.bebop_free_sensor_reading(ctx &C.BebopCtx, reading &C.VSensorReading) |
| 39 | +fn C.bebop_encode_batch_readings(ctx &C.BebopCtx, readings &C.VSensorReading, count usize, out_buf &u8, out_len usize) usize |
| 40 | + |
| 41 | +// Helper: bytes -> V string (copies) |
| 42 | +[inline] |
| 43 | +fn bytes_to_string(b C.VBytes) string { |
| 44 | + if isnil(b.ptr) || b.len == 0 { return '' } |
| 45 | + return unsafe { b.ptr.vstring_with_len(int(b.len)) } |
| 46 | +} |
| 47 | + |
| 48 | +// Small safe wrapper type |
| 49 | +pub struct BebopCtx { |
| 50 | + ctx &C.BebopCtx |
| 51 | +} |
| 52 | + |
| 53 | +pub fn new_ctx() BebopCtx { |
| 54 | + return BebopCtx{ ctx: C.bebop_ctx_new() } |
| 55 | +} |
| 56 | + |
| 57 | +pub fn (mut c BebopCtx) free() { |
| 58 | + if !isnil(c.ctx) { |
| 59 | + C.bebop_ctx_free(c.ctx) |
| 60 | + c.ctx = unsafe { nil } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub fn (c &BebopCtx) reset() { |
| 65 | + if !isnil(c.ctx) { C.bebop_ctx_reset(c.ctx) } |
| 66 | +} |
| 67 | + |
| 68 | +pub struct SensorReading { |
| 69 | + pub: |
| 70 | + timestamp u64 |
| 71 | + sensor_id string |
| 72 | + sensor_type u16 |
| 73 | + value f64 |
| 74 | + unit string |
| 75 | + location string |
| 76 | + metadata map[string]string |
| 77 | +} |
| 78 | + |
| 79 | +pub fn (c &BebopCtx) decode_sensor_reading(data []u8) ?SensorReading { |
| 80 | + mut out := C.VSensorReading{} |
| 81 | + rc := C.bebop_decode_sensor_reading(c.ctx, data.data, data.len, &out) |
| 82 | + if rc != 0 || out.error_code != 0 { |
| 83 | + return none |
| 84 | + } |
| 85 | + |
| 86 | + mut md := map[string]string{} |
| 87 | + for i := 0; i < out.metadata_count; i++ { |
| 88 | + k := unsafe { out.metadata_keys[i] } |
| 89 | + v := unsafe { out.metadata_values[i] } |
| 90 | + md[bytes_to_string(k)] = bytes_to_string(v) |
| 91 | + } |
| 92 | + |
| 93 | + // If the implementation needs explicit free per reading, call it here. |
| 94 | + // For pure arena allocation, ctx.reset() will reclaim. |
| 95 | + C.bebop_free_sensor_reading(c.ctx, &out) |
| 96 | + |
| 97 | + return SensorReading{ |
| 98 | + timestamp: out.timestamp |
| 99 | + sensor_id: bytes_to_string(out.sensor_id) |
| 100 | + sensor_type: out.sensor_type |
| 101 | + value: out.value |
| 102 | + unit: bytes_to_string(out.unit) |
| 103 | + location: bytes_to_string(out.location) |
| 104 | + metadata: md |
| 105 | + } |
| 106 | +} |
0 commit comments