// Shared helpers for the UHPPOTE hardware test scripts. // Run directly against the device (independent of the HTTP server). // // node apps/server/scripts/uhppote-listen.mjs // node apps/server/scripts/uhppote-relay.mjs // // Env overrides: // UHPPOTE_SERIAL controller serial (default 225088491) // UHPPOTE_HOST controller IP (default 10.0.10.3) // UHPPOTE_BCAST Config broadcast (default derived from HOST subnet) // HOST_IP this host's IP the controller pushes events to // (default: auto-detected interface on the controller's subnet) import { networkInterfaces } from "node:os"; import { createRequire } from "node:module"; const require = createRequire(import.meta.url); const uhppoted = require("uhppoted"); export const SERIAL = Number(process.env.UHPPOTE_SERIAL ?? 225088491); export const HOST = process.env.UHPPOTE_HOST ?? "10.0.10.3"; /** Subnet-directed broadcast for the interface that owns `ip`. */ function broadcastForHost(ip) { const o = ip.split(".").map(Number); for (const ifaces of Object.values(networkInterfaces())) { for (const i of ifaces ?? []) { if (i.family !== "IPv4" || i.internal) continue; const a = i.address.split(".").map(Number); const m = i.netmask.split(".").map(Number); if (o.every((x, k) => (x & m[k]) === (a[k] & m[k]))) { return a.map((x, k) => (x & m[k]) | (~m[k] & 0xff)).join("."); } } } return "255.255.255.255"; } /** This host's own IP on the controller's subnet (where it should push events). */ export function hostIpOnControllerSubnet(ip = HOST) { if (process.env.HOST_IP) return process.env.HOST_IP; const o = ip.split(".").map(Number); for (const ifaces of Object.values(networkInterfaces())) { for (const i of ifaces ?? []) { if (i.family !== "IPv4" || i.internal) continue; const a = i.address.split(".").map(Number); const m = i.netmask.split(".").map(Number); if (o.every((x, k) => (x & m[k]) === (a[k] & m[k]))) return i.address; } } return null; } export const BCAST = process.env.UHPPOTE_BCAST ?? broadcastForHost(HOST); export function makeCtx(timeoutMs = 5000) { return { config: new uhppoted.Config( "parking", "0.0.0.0", `${BCAST}:60000`, "0.0.0.0:60001", timeoutMs, [], false, ), locale: "en-US", }; } export const controller = { id: SERIAL, address: HOST, protocol: "udp" }; export { uhppoted };