rpc

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package rpc holds the helpers the area packages (device, wifi, clients, ...) share on top of amplifi.Client's public Call and CallJSON: typed result decoding, read-modify-write setters, and access to the integer-keyed option maps the Device and ClusterNode interfaces use.

Index

Constants

View Source
const Redacted = "********"

Redacted replaces secrets in redacted output.

Variables

View Source
var (
	// BandNames maps WifiBand codes.
	BandNames = map[string]string{"1": "2.4GHz", "2": "5GHz"}

	// NetworkNames maps WifiInterfaceRole codes. 7 (friend) is not in the
	// app's enum but routers report it.
	NetworkNames = map[string]string{
		"1": "setup", "2": "internal", "3": "main", "4": "guest",
		"5": "device", "6": "relay", "7": "friend", "8": "iot",
	}

	// ClientNetworks are the networks that carry clients; the others are
	// mesh backhaul and setup networks.
	ClientNetworks = map[string]bool{"3": true, "4": true, "5": true, "7": true, "8": true}
)

Code tables shared by several areas. Option maps and enums use integer codes, keyed here by their decimal string as in decoded option maps.

Null and TwoNulls are the msgpack nil and [nil, nil] arguments many getters expect, ready to pass to Call.

Functions

func Addr

func Addr(v any) netip.Addr

Addr decodes a packed address: 4 or 16 bytes, or 5 or 17 bytes with a trailing prefix length, which is dropped. Anything else gives the zero Addr.

func Addrs

func Addrs(v any) []netip.Addr

Addrs decodes a list of packed addresses, skipping invalid ones. A single binary value is accepted as a list of one.

func Bool

func Bool(m map[string]any, key string) bool

Bool returns m[key] as a bool, or false if it is missing or not one.

func Bytes

func Bytes(m map[string]any, key string) []byte

Bytes returns m[key] as bytes, or nil if it is missing or not binary.

func ClusterMap

func ClusterMap(ctx context.Context, c Caller, method string) (map[string]any, error)

ClusterMap calls a ClusterNode getter, which takes a msgpack nil and answers [metadata, payload], and returns the payload.

func Code

func Code(kind string, names map[string]string, name string) (int64, error)

Code returns the integer code for name in names, ignoring case. "#N" is accepted for codes without a name. kind names the enum in the error.

func CodeName

func CodeName(names map[string]string, code int64) string

CodeName returns the name for an integer code in names, or "#code" when it is not known.

func CompareNumeric

func CompareNumeric(a, b string) int

CompareNumeric orders strings numerically when both are integers, numbers before other strings, and other strings lexically. It suits option codes, IDs and port names.

func Decode

func Decode(method string, v, out any) error

Decode converts a decoded result into out by way of JSON. A string holding JSON is parsed first, as some methods answer with one.

func ExpectOK

func ExpectOK(method string, v any) error

ExpectOK checks a setter's result: a msgpack boolean that must be true. A nil result is accepted, as some setters return nothing.

func Flat

func Flat(m map[string]any) map[string]any

Flat drops nested maps from an option map, which the app's serializer never sends back (MsgPackBase.serializeToMsgPack only packs scalars, binary and lists).

func Int

func Int(m map[string]any, key string) int64

Int returns m[key] as an integer, or 0 if it is missing or not one.

func JSON

func JSON(ctx context.Context, c Caller, iface, method string, arg, out any) error

JSON calls a method with a JSON argument (payload.value) and decodes the result into out. A nil out discards the result.

func JSONString

func JSONString(v any) any

JSONString returns v parsed as JSON if it is a string holding JSON, and v unchanged otherwise.

func KeyName

func KeyName(names map[string]string, key string) string

KeyName is CodeName for a code that is already a decimal string, such as an option map key.

func MAC

func MAC(v any) amplifi.MAC

MAC converts a binary MAC, as a []byte value or a raw-byte map key, to a MAC. Anything that is not 6 bytes yields nil.

func Map

func Map(ctx context.Context, c Caller, iface, method string, arg any) (map[string]any, error)

Map calls a method that answers with a map, such as an option map.

func Mask

func Mask(s string) string

Mask returns Redacted for a non-empty s and "" otherwise.

func MaskFields

func MaskFields(m map[string]any, codes ...string) map[string]any

MaskFields returns a copy of m with the non-empty strings under codes replaced by Redacted. A nil m gives an empty map.

func Name

func Name(names map[string]string, m map[string]any, key string) string

Name returns the name for the integer code under m[key], or "" if m has no such key.

func PackAddr

func PackAddr(a netip.Addr) ([]byte, error)

PackAddr packs an IPv4 address as 4 bytes. The zero Addr packs as empty binary, which clears the option.

func PackMAC

func PackMAC(m amplifi.MAC) ([]byte, error)

PackMAC packs a MAC as 6 byte binary. An empty MAC packs as empty binary, which clears a MAC override.

func PackPrefix

func PackPrefix(p netip.Prefix) ([]byte, error)

PackPrefix packs an IPv4 address and prefix length as 5 bytes: the address followed by the length. The zero Prefix packs as empty binary.

func Prefix

func Prefix(v any) netip.Prefix

Prefix decodes a packed address with a prefix length: 5 or 17 bytes, the address followed by the length. A bare 4 or 16 byte address gets a full-length prefix. Anything else gives the zero Prefix.

func Prefixes

func Prefixes(v any) []netip.Prefix

Prefixes is Addrs for addresses with a prefix length, as Prefix decodes them.

func Put

func Put[T any](m map[string]any, code string, v *T)

Put sets m[code] to *v when v is set and code is not empty.

func PutInt

func PutInt[T ~int](m map[string]any, code string, v *T)

PutInt is Put for integer types, stored as int64. Named types need this because the msgpack encoder writes a type with a MarshalText method, such as wifi.Encryption, as text.

func Role

func Role(code int64) amplifi.Role

Role converts a NodeRole code. Unknown codes give "#N".

func Set

func Set(ctx context.Context, c Caller, iface, method string, payload any) error

Set calls a setter with a msgpack payload (an option map, or a list for the Add*/Delete* methods) and checks its boolean result.

func SetCluster

func SetCluster(ctx context.Context, c Caller, method string, payload any) error

SetCluster calls a ClusterNode setter, wrapping payload as [{timestamp}, payload].

func String

func String(m map[string]any, key string) string

String returns m[key] as a string, or "" if it is missing or not a string.

func Submaps

func Submaps(v any) iter.Seq2[string, map[string]any]

Submaps yields the entries of v, a map, whose values are maps. Anything else in v, or a v that is not a map, is skipped.

func Update

func Update(ctx context.Context, c Caller, iface, getMethod string, getArg any, setMethod string, changes map[string]any) error

Update does a read-modify-write of an option map: it reads the map with getMethod and getArg, applies changes (option code -> value) and writes the whole map back with setMethod.

func UpdateCluster

func UpdateCluster(ctx context.Context, c Caller, getMethod, setMethod string, changes map[string]any) error

UpdateCluster is Update for a ClusterNode option map.

func UpdateEntry

func UpdateEntry(ctx context.Context, c Caller, iface, getMethod string, getArg any, setMethod, key string, changes map[string]any) error

UpdateEntry does a read-modify-write of a map of option maps keyed by name, such as radios or interfaces: it applies changes to the entry under key and writes every entry back flattened. Empty changes do nothing.

func Value

func Value(ctx context.Context, c Caller, iface, method string, arg, out any) error

Value calls a method with a msgpack argument (nil for none) whose result is JSON, either as payload.value or as a msgpack string holding JSON, and decodes it into out.

Types

type Caller

type Caller interface {
	Call(ctx context.Context, iface, method string, arg any) (any, error)
	CallJSON(ctx context.Context, iface, method string, arg any) (any, error)
}

Caller is the part of *amplifi.Client the helpers need.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL