Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/server/antenna_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,14 @@ func (c *antennaClaimService) ownerHeightAt(ctx context.Context, pubkey string,
return &v
}

// attachOwnerHeights sets owner on the node endpoints that have one.
// attachOwnerHeights sets owner (the approved height) and radio (the
// approved station radio, #1017 M4) on the node endpoints that have them.
func (s *Server) attachOwnerHeights(ctx context.Context, ends *[2]terrainEnd) {
for i := range ends {
if ends[i].kind == "node" && ends[i].pubkey != "" {
ends[i].owner = s.antennaClaims().ownerHeightAt(ctx, ends[i].pubkey, ends[i].pt)
c := s.antennaClaims()
ends[i].owner = c.ownerHeightAt(ctx, ends[i].pubkey, ends[i].pt)
ends[i].radio = c.ownerRadioAt(ctx, ends[i].pubkey, ends[i].pt)
}
}
}
Expand Down
504 changes: 504 additions & 0 deletions cmd/server/station_models_1017_m4_test.go

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions cmd/server/station_radio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package main

// #1017 M4 — the owner's station values in the models.
//
// An approved, in-force, non-stale station-details claim (#898, #1017 M3)
// may state the radio at the antenna port (TX power, cable loss), the
// antenna gain, and for a yagi or a panel its direction and beamwidth.
// This file turns those values into what the predicted reach and the
// terrain profile use:
//
// - ownerRadio: the radio part of the details, nil when they state none;
// a missing beamwidth takes the antenna type's default and is marked
// assumed (never stored, M0 §6).
// - stationPatternDb: the directional antenna's horizontal pattern,
// A(φ) = −min(12·(φ/θ3dB)², Am) dB relative to boresight (M0 §6). The
// browser applies the same function to the reach raster and list
// (public/coverage.js PATTERN, pinned by
// TestStationPattern1017M4_MatchesTheClient); the server applies it to
// the reach list's counts (owner decision 8, corrected 2026-11-29) and
// to the terrain profile's margins.
// - initialBearingDeg: the great-circle bearing the pattern is read at.
//
// Am and the default beamwidths are operator constants for now; the
// customizer may expose them later (AGENTS.md rule 8).
//
// Everything here reads the claim snapshot the server already keeps
// (antennaClaimService.ownerStations); nothing is written.

import (
"context"
"math"
"strings"

"github.com/meshcore-analyzer/channelregistry/antennaclaim"
"github.com/meshcore-analyzer/dhm"
)

const (
// stationPatternFloorDb is Am: the deepest the pattern goes, the
// front-to-back ratio, in dB below boresight (M0 §6, ASSUMED typical).
stationPatternFloorDb = 20.0
)

// stationDefaultBeamwidthDeg is the horizontal −3 dB beamwidth a
// directional antenna without a stated one is modelled with (M0 §6,
// owner decision 7: yagi 60°, panel 90°, shown as assumed).
var stationDefaultBeamwidthDeg = map[string]int{"yagi": 60, "panel": 90}

// stationPatternDb is the gain, relative to boresight, offDeg degrees off
// the antenna's azimuth: 0 on boresight, −3 dB at ±beamwidth/2, never
// below −stationPatternFloorDb. offDeg may be any angle; it is wrapped to
// [−180°, 180°].
func stationPatternDb(offDeg, beamwidthDeg float64) float64 {
phi := math.Mod(offDeg, 360)
if phi > 180 {
phi -= 360
} else if phi < -180 {
phi += 360
}
r := phi / beamwidthDeg
return -math.Min(12*r*r, stationPatternFloorDb)
}

// initialBearingDeg is the initial great-circle bearing from a to b in
// degrees clockwise from true north, in [0, 360).
func initialBearingDeg(a, b dhm.LatLon) float64 {
const rad = math.Pi / 180
la1, la2 := a.Lat*rad, b.Lat*rad
dl := (b.Lon - a.Lon) * rad
y := math.Sin(dl) * math.Cos(la2)
x := math.Cos(la1)*math.Sin(la2) - math.Sin(la1)*math.Cos(la2)*math.Cos(dl)
deg := math.Atan2(y, x) / rad
if deg < 0 {
deg += 360
}
if deg >= 360 {
deg -= 360
}
return deg
}

// ownerRadio is the radio part of a node's approved station details. A
// nil pointer field is a value the owner did not state.
type ownerRadio struct {
txPowerDbm *float64
gainDbi *float64
cableLossDb *float64
azimuthDeg *int
// beamwidthDeg is the owner's beamwidth, else the antenna type's
// default (beamwidthAssumed); 0 without a direction.
beamwidthDeg int
beamwidthAssumed bool
}

// ownerRadioOf is the radio part of d; nil when d states none of TX
// power, gain, cable loss or direction.
func ownerRadioOf(d antennaclaim.StationDetails) *ownerRadio {
o := &ownerRadio{txPowerDbm: d.TxPowerDbm, gainDbi: d.GainDBi, cableLossDb: d.CableLossDb}
if d.AzimuthDeg != nil {
switch {
case d.BeamwidthDeg != nil:
o.azimuthDeg, o.beamwidthDeg = d.AzimuthDeg, *d.BeamwidthDeg
case stationDefaultBeamwidthDeg[d.AntennaType] > 0:
o.azimuthDeg, o.beamwidthDeg, o.beamwidthAssumed = d.AzimuthDeg, stationDefaultBeamwidthDeg[d.AntennaType], true
}
}
if o.txPowerDbm == nil && o.gainDbi == nil && o.cableLossDb == nil && o.azimuthDeg == nil {
return nil
}
return o
}

// directional reports whether the owner's antenna has a direction.
func (o *ownerRadio) directional() bool {
return o != nil && o.azimuthDeg != nil && o.beamwidthDeg > 0
}

// patternToward is the pattern's gain toward bearingDeg relative to
// boresight; 0 for an omni (or unknown) antenna.
func (o *ownerRadio) patternToward(bearingDeg float64) float64 {
if !o.directional() {
return 0
}
return stationPatternDb(bearingDeg-float64(*o.azimuthDeg), float64(o.beamwidthDeg))
}

// over is r with the owner's stated values in place of the operator's
// (precedence owner > operator; the viewer's sliders come first in the
// browser). The receiver's gain and the sensitivity stay the operator's.
func (r CoverageRadio) over(o *ownerRadio) CoverageRadio {
if o == nil {
return r
}
if o.txPowerDbm != nil {
r.TxPowerDbm = *o.txPowerDbm
}
if o.gainDbi != nil {
r.AntennaGainDbi = *o.gainDbi
}
if o.cableLossDb != nil {
r.LossDb = *o.cableLossDb
}
return r
}

// CoverageStationRadio is the coverage response's `station` block: the
// owner's stated radio for the centre node, added at serve time from the
// claim snapshot and never cached with the result. Only stated values are
// present; the panel starts from them (viewer sliders > owner > operator).
type CoverageStationRadio struct {
TxPowerDbm *float64 `json:"txPowerDbm,omitempty"`
AntennaGainDbi *float64 `json:"antennaGainDbi,omitempty"`
LossDb *float64 `json:"lossDb,omitempty"` // the owner's cable loss
AzimuthDeg *int `json:"azimuthDeg,omitempty"`
// BeamwidthDeg is set with AzimuthDeg: the owner's, else the antenna
// type's default, then BeamwidthAssumed.
BeamwidthDeg *int `json:"beamwidthDeg,omitempty"`
BeamwidthAssumed bool `json:"beamwidthAssumed,omitempty"`
}

// stationBlock is o as the coverage response's station block; nil for nil.
func (o *ownerRadio) stationBlock() *CoverageStationRadio {
if o == nil {
return nil
}
out := &CoverageStationRadio{TxPowerDbm: o.txPowerDbm, AntennaGainDbi: o.gainDbi, LossDb: o.cableLossDb}
if o.directional() {
bw := o.beamwidthDeg
out.AzimuthDeg, out.BeamwidthDeg, out.BeamwidthAssumed = o.azimuthDeg, &bw, o.beamwidthAssumed
}
return out
}

// ownerRadioAt is the radio of node pubkey's approved station details
// while the node stands where they were proven; nil when details are off,
// there are none, they state no radio, or the node moved (stale, as
// /station). The caller has already refused hidden nodes.
func (c *antennaClaimService) ownerRadioAt(ctx context.Context, pubkey string, pt dhm.LatLon) *ownerRadio {
if c == nil || !c.detailsEnabled {
return nil
}
st, ok := c.ownerStations(ctx)[strings.ToLower(pubkey)]
if !ok || c.limits.Stale(dhm.GroundDistance(pt, dhm.LatLon{Lat: st.lat, Lon: st.lon})) {
return nil
}
return ownerRadioOf(st.details)
}

// endpointRadio is an endpoint's radio in a terrain profile: the owner's
// values where stated (named in OwnerStated), the operator's link budget
// otherwise, and the pattern toward the other end (bearingDeg) for a
// directional antenna.
func endpointRadio(op CoverageRadio, o *ownerRadio, bearingDeg float64) *EndpointRadio {
r := op.over(o)
out := &EndpointRadio{TxPowerDbm: r.TxPowerDbm, GainDBi: r.AntennaGainDbi, CableLossDb: r.LossDb, Source: "operator"}
if o != nil {
out.Source = "owner"
for _, f := range []struct {
name string
stated bool
}{{"txPowerDbm", o.txPowerDbm != nil}, {"gainDbi", o.gainDbi != nil}, {"cableLossDb", o.cableLossDb != nil}} {
if f.stated {
out.OwnerStated = append(out.OwnerStated, f.name)
}
}
}
if o.directional() {
bw, p := o.beamwidthDeg, round2(o.patternToward(bearingDeg))
out.AzimuthDeg, out.BeamwidthDeg, out.BeamwidthAssumed, out.PatternDb = o.azimuthDeg, &bw, o.beamwidthAssumed, &p
}
return out
}

// linkMarginDb is the margin of a link from tx to rx over lossDb, at the
// operator's sensitivity and environment loss: the reach panel's budget
// (public/coverage.js budgetOf) with each end's own radio and pattern.
func linkMarginDb(tx, rx *EndpointRadio, lossDb, sensitivityDbm, envLossDb float64) float64 {
gain := func(e *EndpointRadio) float64 {
g := e.GainDBi - e.CableLossDb
if e.PatternDb != nil {
g += *e.PatternDb
}
return g
}
return tx.TxPowerDbm + gain(tx) + gain(rx) - lossDb - sensitivityDbm - envLossDb
}

// terrainLinkSettings is what the terrain profile's link budget uses: the
// operator's radio block, the predicted reach's model and environment
// loss (map.coverage, resolved as the reach resolves them, even while the
// reach itself is off).
type terrainLinkSettings struct {
radio CoverageRadio
model dhm.PathModel
envLossDb float64
}

// resolveTerrainLinkSettings resolves rc and cc quietly: the reach and the
// radio block already report their own misconfigurations.
func resolveTerrainLinkSettings(rc *RadioConfig, cc *CoverageConfig) terrainLinkSettings {
if cc == nil {
cc = &CoverageConfig{}
}
set := resolveCoverageSettings(*cc, quietLogf)
return terrainLinkSettings{radio: resolveCoverageRadio(rc, quietLogf), model: set.model, envLossDb: set.envLossDb}
}

// budget is the profile's link: the path loss of link over surface under
// the reach's model (dhm.Link.PathLoss, the function every node in reach
// uses), and the margin each way with the ends' radios. O(samples), from
// samples the profile already holds: no upstream call. nil while a sample
// or an antenna height is missing.
func (s terrainLinkSettings) budget(link dhm.Link, surface []float32, a, b *EndpointRadio) *TerrainLink {
if a == nil || b == nil {
return nil
}
loss, ok := link.PathLoss(surface, s.model)
if !ok || math.IsNaN(loss) || math.IsInf(loss, 0) {
return nil
}
loss = math.Round(loss*10) / 10
return &TerrainLink{LossDb: loss, Model: s.model.ID(), SensitivityDbm: s.radio.SensitivityDbm, EnvironmentLossDb: s.envLossDb,
MarginABDb: math.Round(linkMarginDb(a, b, loss, s.radio.SensitivityDbm, s.envLossDb)*10) / 10,
MarginBADb: math.Round(linkMarginDb(b, a, loss, s.radio.SensitivityDbm, s.envLossDb)*10) / 10}
}
6 changes: 6 additions & 0 deletions cmd/server/terrain_antenna_estimate_789_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -716,5 +717,10 @@ func sameEnd789(a, b TerrainEndpoint) bool {
return false
}
a.GroundM, b.GroundM = nil, nil
// The endpoint radio (#1017 M4) is a pointer too: compared by value.
if !reflect.DeepEqual(a.Radio, b.Radio) {
return false
}
a.Radio, b.Radio = nil, nil
return a == b
}
9 changes: 8 additions & 1 deletion cmd/server/terrain_coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ func (s *Server) handleTerrainCoverage(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("X-CoreScope-Cache", state)
resp := c.buildResponse(req, entry)
resp.Reach = s.coverageReachView(r.Context(), c, entry.meta.Reach)
// The owner's station radio (#1017 M4) is the snapshot's, at serve
// time: never part of the cached result.
resp.Station = req.end.radio.stationBlock()
resp.Reach = s.coverageReachViewFrom(r.Context(), c, entry.meta.Reach, &reachOrigin{centre: req.center, radio: req.end.radio})
writeJSON(w, resp)
}

Expand Down Expand Up @@ -1135,6 +1138,10 @@ type CoverageResponse struct {
// Reach is the nodes in reach (#941 M2); null when they could not be
// evaluated.
Reach *CoverageReach `json:"reach"`
// Station is the centre node owner's approved radio (#1017 M4): the
// panel's starting values over the operator's, and the direction the
// panel and the reach list's counts apply. Absent without one.
Station *CoverageStationRadio `json:"station,omitempty"`
}

// CoverageCenter is the transmitter's position, snapped to 4 decimals.
Expand Down
Loading
Loading