Robert Adams commited on
Commit ·
636796a
unverified ·
0
Parent(s):
Initial: WIT sensor contract and addressing scheme
Browse files- README.md +22 -0
- sensor.wit +197 -0
README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Synapse Agriculture — Specifications
|
| 2 |
+
|
| 3 |
+
Public specifications and contracts for the Synapse farm infrastructure platform.
|
| 4 |
+
|
| 5 |
+
## Contents
|
| 6 |
+
|
| 7 |
+
- `sensor.wit` — WebAssembly Interface Types definition for the sensor module API. This is the immortal contract between MCU sensor firmware (WASM), field gateways, Houston services, and the browser frontend.
|
| 8 |
+
|
| 9 |
+
## Addressing Scheme
|
| 10 |
+
|
| 11 |
+
Synapse uses a numeric addressing scheme: `{realm}.{layer}.{sublayer}.{instance}`
|
| 12 |
+
|
| 13 |
+
- Realm 0: Meta (protocols, contracts)
|
| 14 |
+
- Realm 1: Farm (Build a Farm layers 0-10, field assets)
|
| 15 |
+
- Realm 2: Governance (Board personas, decision-making)
|
| 16 |
+
- Realm 3: Infrastructure (services, databases, proxies)
|
| 17 |
+
|
| 18 |
+
Alternate namespace: B (Board), S (Service), F (Field), P (Protocol)
|
| 19 |
+
|
| 20 |
+
## License
|
| 21 |
+
|
| 22 |
+
AGPL-3.0-or-later
|
sensor.wit
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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is the IMMORTAL CONTRACT between:
|
| 3 |
+
// - Guest: sensor WASM modules (runs on MCU/gateway/host)
|
| 4 |
+
// - Host: wasm3 (MCU), wasmtime (gateway/host), browser VM
|
| 5 |
+
//
|
| 6 |
+
// Adding a new sensor type = add to this file, regen bindings,
|
| 7 |
+
// type-check catches every integration point at compile time.
|
| 8 |
+
//
|
| 9 |
+
// Changing this file is a BREAKING CHANGE across the entire stack.
|
| 10 |
+
// Treat it like a database migration — versioned, reviewed, irreversible.
|
| 11 |
+
|
| 12 |
+
package synapse:sensor@0.1.0;
|
| 13 |
+
|
| 14 |
+
/// Types shared across all sensor modules and host runtimes.
|
| 15 |
+
/// These compile into synapse-core and are used everywhere.
|
| 16 |
+
interface types {
|
| 17 |
+
/// Sensor reading with metadata for provenance tracking
|
| 18 |
+
record reading {
|
| 19 |
+
/// Unix timestamp in milliseconds (from host clock or RTC)
|
| 20 |
+
timestamp-ms: u64,
|
| 21 |
+
/// Sensor channel identifier (maps to physical probe)
|
| 22 |
+
channel: u8,
|
| 23 |
+
/// Raw ADC or digital value before calibration
|
| 24 |
+
raw-value: s32,
|
| 25 |
+
/// Calibrated value as fixed-point (value * 1000)
|
| 26 |
+
/// Using s32 instead of f32 because wasm3 soft-float
|
| 27 |
+
/// on Cortex-M is slow and we don't need the precision
|
| 28 |
+
calibrated-value: s32,
|
| 29 |
+
/// Unit of measurement after calibration
|
| 30 |
+
unit: measurement-unit,
|
| 31 |
+
/// Quality/confidence flag from self-diagnostics
|
| 32 |
+
quality: reading-quality,
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
/// Fixed-point calibration coefficients for linear cal:
|
| 36 |
+
/// calibrated = (raw * slope / 1000) + (offset / 1000)
|
| 37 |
+
/// Two-point cal: derive slope/offset from known standards
|
| 38 |
+
record calibration {
|
| 39 |
+
slope: s32, // multiplied by 1000
|
| 40 |
+
offset: s32, // multiplied by 1000
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/// What physical quantity this reading represents
|
| 44 |
+
enum measurement-unit {
|
| 45 |
+
/// Water chemistry
|
| 46 |
+
ph, // pH units (0-14)
|
| 47 |
+
ec, // electrical conductivity, µS/cm
|
| 48 |
+
dissolved-oxygen, // mg/L
|
| 49 |
+
orp, // mV
|
| 50 |
+
temperature-water, // °C * 1000
|
| 51 |
+
|
| 52 |
+
/// Soil
|
| 53 |
+
moisture-vwc, // volumetric water content, % * 1000
|
| 54 |
+
temperature-soil, // °C * 1000
|
| 55 |
+
|
| 56 |
+
/// Atmosphere
|
| 57 |
+
temperature-air, // °C * 1000
|
| 58 |
+
humidity, // % * 1000
|
| 59 |
+
pressure, // hPa * 1000
|
| 60 |
+
light-lux, // lux
|
| 61 |
+
light-par, // µmol/m²/s (photosynthetically active)
|
| 62 |
+
|
| 63 |
+
/// Power (Layer 7)
|
| 64 |
+
voltage, // mV
|
| 65 |
+
current, // mA
|
| 66 |
+
power, // mW
|
| 67 |
+
battery-soc, // state of charge, % * 10
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/// Self-diagnostic quality assessment
|
| 71 |
+
enum reading-quality {
|
| 72 |
+
good,
|
| 73 |
+
degraded, // reading taken but outside expected range
|
| 74 |
+
cal-needed, // calibration overdue or drift detected
|
| 75 |
+
fault, // sensor not responding or shorted
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/// Configuration pushed from gateway to node
|
| 79 |
+
record sensor-config {
|
| 80 |
+
/// Sampling interval in seconds
|
| 81 |
+
sample-interval-secs: u32,
|
| 82 |
+
/// Which channels to read (bitmask, up to 8 channels)
|
| 83 |
+
active-channels: u8,
|
| 84 |
+
/// Per-channel calibration (indexed by channel number)
|
| 85 |
+
calibrations: list<calibration>,
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/// Compact transmission payload for LoRa
|
| 89 |
+
/// Designed to fit in a single LoRa packet (<= 242 bytes at SF7)
|
| 90 |
+
record transmission-payload {
|
| 91 |
+
/// Node identifier (unique per site)
|
| 92 |
+
node-id: u16,
|
| 93 |
+
/// Sequence number for dedup and gap detection
|
| 94 |
+
sequence: u16,
|
| 95 |
+
/// Battery voltage in mV (for power monitoring)
|
| 96 |
+
battery-mv: u16,
|
| 97 |
+
/// All readings from this sample cycle
|
| 98 |
+
readings: list<reading>,
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/// Host functions provided TO the sensor module BY the runtime.
|
| 103 |
+
/// The MCU firmware implements these against real hardware.
|
| 104 |
+
/// The test harness implements them as mocks.
|
| 105 |
+
/// The browser implements them as no-ops or simulations.
|
| 106 |
+
interface host {
|
| 107 |
+
use types.{reading, calibration};
|
| 108 |
+
|
| 109 |
+
/// Read raw value from I2C sensor
|
| 110 |
+
/// address: 7-bit I2C device address (e.g., 0x63 for Atlas pH)
|
| 111 |
+
/// register: register to read from
|
| 112 |
+
/// length: bytes to read (max 32)
|
| 113 |
+
/// Returns: raw bytes from device, or error
|
| 114 |
+
read-i2c: func(address: u8, register: u8, length: u8) -> result<list<u8>, sensor-error>;
|
| 115 |
+
|
| 116 |
+
/// Read ADC channel (for analog sensors)
|
| 117 |
+
/// channel: ADC channel number (0-7 on RP2350)
|
| 118 |
+
/// Returns: raw 12-bit ADC value (0-4095)
|
| 119 |
+
read-adc: func(channel: u8) -> result<u16, sensor-error>;
|
| 120 |
+
|
| 121 |
+
/// Get current timestamp from RTC or host clock
|
| 122 |
+
get-timestamp-ms: func() -> u64;
|
| 123 |
+
|
| 124 |
+
/// Queue a LoRa transmission
|
| 125 |
+
/// payload: CBOR-encoded bytes to transmit
|
| 126 |
+
/// Returns: number of bytes queued, or error
|
| 127 |
+
transmit: func(payload: list<u8>) -> result<u32, sensor-error>;
|
| 128 |
+
|
| 129 |
+
/// Enter low-power sleep for specified duration
|
| 130 |
+
/// The WASM module yields execution here; host handles
|
| 131 |
+
/// actual MCU sleep modes (DORMANT on RP2350)
|
| 132 |
+
sleep-ms: func(duration-ms: u32);
|
| 133 |
+
|
| 134 |
+
/// Log a diagnostic message (forwarded to gateway if possible)
|
| 135 |
+
/// Compiled out / no-op on MCU builds via feature flag
|
| 136 |
+
log: func(level: log-level, message: string);
|
| 137 |
+
|
| 138 |
+
enum sensor-error {
|
| 139 |
+
/// Device not responding on bus
|
| 140 |
+
not-found,
|
| 141 |
+
/// Bus arbitration failure
|
| 142 |
+
bus-error,
|
| 143 |
+
/// Device returned NAK
|
| 144 |
+
nak,
|
| 145 |
+
/// Read timed out
|
| 146 |
+
timeout,
|
| 147 |
+
/// Transmission queue full
|
| 148 |
+
queue-full,
|
| 149 |
+
/// Generic / unclassified
|
| 150 |
+
other,
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
enum log-level {
|
| 154 |
+
debug,
|
| 155 |
+
info,
|
| 156 |
+
warn,
|
| 157 |
+
error,
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
/// The interface that every sensor module MUST implement.
|
| 162 |
+
/// This is the guest-side contract — the "main" of the module.
|
| 163 |
+
interface guest {
|
| 164 |
+
use types.{reading, sensor-config, transmission-payload};
|
| 165 |
+
|
| 166 |
+
/// Called once at boot. Host passes stored config.
|
| 167 |
+
/// Module initializes internal state, validates config.
|
| 168 |
+
/// Returns: true if init succeeded, false to signal fault.
|
| 169 |
+
init: func(config: sensor-config) -> bool;
|
| 170 |
+
|
| 171 |
+
/// Called each sample cycle by the host's main loop.
|
| 172 |
+
/// Module reads sensors (via host.read-i2c / host.read-adc),
|
| 173 |
+
/// applies calibration, builds readings list.
|
| 174 |
+
/// Returns: payload ready for LoRa transmission.
|
| 175 |
+
sample: func() -> transmission-payload;
|
| 176 |
+
|
| 177 |
+
/// Called when gateway pushes new config (e.g., new cal values).
|
| 178 |
+
/// Module validates and applies, returns success/failure.
|
| 179 |
+
reconfigure: func(config: sensor-config) -> bool;
|
| 180 |
+
|
| 181 |
+
/// Self-diagnostic. Module checks sensor responsiveness,
|
| 182 |
+
/// validates readings against expected ranges, reports health.
|
| 183 |
+
/// Returns: list of (channel, quality) pairs.
|
| 184 |
+
diagnose: func() -> list<tuple<u8, reading-quality>>;
|
| 185 |
+
|
| 186 |
+
use host.{sensor-error};
|
| 187 |
+
|
| 188 |
+
/// Redeclare quality enum access for diagnose return
|
| 189 |
+
use types.{reading-quality};
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
/// The complete world — wires guest to host.
|
| 193 |
+
/// cargo-component uses this to generate the full bindings.
|
| 194 |
+
world sensor-node {
|
| 195 |
+
import host;
|
| 196 |
+
export guest;
|
| 197 |
+
}
|