Files
DriverVault/API Server/internal/plugins/builtin/ankersolix/mqttframe.go
T
tajniak81andClaude Opus 5 90558d60b2 What the app can set, the cloud connection can set
The broker transport could move a session along — start, stop, boost, skip the
delay, cap the current — and nothing else. Everything the charger is actually
configured with sat one field away in the same messages we were already
decoding: the schedule it charges on, the plug lock, auto-start, the LED, load
balancing, solar charging, and the Modbus server the local transport depends on.
Readable, and unreachable.

The obstacle was never the cloud, it was the shape of the protocol. A setting is
not a register write. It is a *command*, and a command owns a set of fields
inside a message type — mostly one, but five own several, and the charger reads
the whole command as the new truth. A light-off schedule sent carrying only its
switch is a schedule whose start and end have just been set to midnight. So a
grouped write resends the siblings the caller did not name, using the values the
charger itself last reported, and refuses when it has never reported them. That
last part is not caution for its own sake: load balancing and solar charging
carry the serial of the meter they watch, and nothing outside the charger knows
it. An empty one would be adopted.

Those values do not arrive with the telemetry, either. The fast 0410 stream a
realtime trigger turns on carries none of them — the settings come on 0405, 0840
and 0900, which the charger sends when it has something to acknowledge. So a
grouped write may have to send a trigger first purely to make the charger talk
about itself, and says so plainly when even that produces nothing.

Everything a caller supplies is encoded before the cloud is touched at all. A
request naming one bad value changes nothing rather than half of what it asked
for, and a mistyped setting costs a validation error instead of a sign-in, a
certificate fetch and a broker connection to be told no.

mqttsettings.go holds one table and it is the only place a setting is defined:
the wire field, the name a caller uses, the state key its current value comes
from, and how a value becomes bytes. The names are the snapshot's own, so a
caller can read a status, change one entry and send it back. The existing limit
command now builds its frame from that table too rather than encoding field a8 a
second time.

Reading grew to match. The frame decoder gains the fields the grouped writes must
carry back — the two load-balance settings, both monitor serials, the solar
monitoring mode — plus the swipe gestures, and the snapshot exposes the rest of
what is now writable. One name was wrong and is corrected: field d9 was called
chargingMode after the Modbus register at 20088, but the reference has it as the
solar charging mode, so it becomes solarChargeMode and moves in beside the solar
settings. A mislabelled reading is bad; a mislabelled writable field is worse.

Over HTTP it is one action rather than a dozen, because the charger groups the
fields anyway: POST .../settings with a settings object, and settings sharing a
command travel in one frame instead of overwriting each other. The other two
transports refuse it by name and say which one has it, the way they already
refuse each other's commands. The audit trail records the values, not just that
a write happened — a setting that changes what the charger will draw, or whether
it answers on the LAN at all, is worth being able to trace afterwards.

Two things worth saying plainly. This is built from the reference project's
message maps and checked against its own frame layout, not against hardware —
there is no charger on this end to point it at. And modbusEnabled is a loaded
gun: writing it off stops the charger serving the register map, and the way back
is this transport, or the app.

The ignore rule for the local Modbus map artifact widens to the protocol maps
that now sit beside it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-02 18:28:45 +02:00

501 lines
17 KiB
Go

package ankersolix
// The wire format Anker's cloud carries between the mobile app and the charger.
//
// An MQTT message is JSON, but only as an envelope: the part that means anything
// is a base64 field inside it holding a binary frame the device itself speaks.
// That frame is the same one the app sends, so a command is not a documented API
// call but a byte layout, reproduced here from the message maps in
// anker-solix-api (src/anker_solix_api/mqtttypes.py, mqttmap.py, mqttcmdmap.py at
// v3.8.1) and checked against a live A5191.
//
// ff 09 2-byte marker on every Anker Solix frame
// xx xx total length in bytes, little endian, counting the checksum
// 03 00 0f fixed pattern; the middle byte is 00 outbound, 01 inbound
// xx xx message type — what the frame is, per device model
// [xx] an optional counter, present on some inbound frames
// <fields> one or more data fields
// xx XOR of every preceding byte
//
// and each data field is
//
// xx field name (a1, a2, … — the model's message map names it)
// xx length of everything that follows in this field
// [xx] value type, present when the field carries more than one byte
// xx … the value
//
// The frame has no version number and no field-type registry: which name means
// what depends on the message type, which is why the maps below are per message
// type rather than one table.
import (
"encoding/binary"
"fmt"
"math"
"strings"
"time"
)
// Frame markers. patternSend is what the app puts in an outbound frame; a device
// answers with 03 01 0f, which is not checked — the message type is what selects
// a decoder.
var (
frameMarker = []byte{0xff, 0x09}
patternSend = []byte{0x03, 0x00, 0x0f}
)
// frameHeaderLen is the marker, length, pattern and message type — everything
// before the first data field, and before any inbound counter byte.
const frameHeaderLen = 9
// Value types. A field longer than one byte starts with one of these; a
// single-byte field carries its value directly with no type at all.
const (
typeString byte = 0x00
typeUint8 byte = 0x01
typeInt16LE byte = 0x02
typeInt32LE byte = 0x03 // "var": four bytes, though not always one value
typeFloat32 byte = 0x05
typeNone byte = 0xff // this package's marker for "no type byte"
)
// typeByteMax is the largest first-value-byte still read as a value type. Field
// names start at 0xa1 and value types stop at 0x06, so the gap is wide; the
// bound matches the reference implementation's rather than narrowing it, since a
// message type we have not seen may use a type we have not seen either.
const typeByteMax byte = 0x31
// Message types this package speaks, for the A5191 (V1 Smart EV Charger).
// Outbound ones are commands, inbound ones are what the charger publishes back.
const (
msgRealtimeTrigger = "0057" // ask for the fast telemetry stream
msgEVSettings = "0100" // the settings group: current limit, brightness, …
msgEVMode = "0105" // start / stop / skip delay / boost
msgEVSchedule = "0106" // the charging schedule: switch, mode and times
msgEVBalancing = "010c" // load balancing and the main breaker limit
msgEVSolar = "010e" // solar charging
msgEVTelemetry = "0410" // fast telemetry, only while a trigger is live
msgEVParams = "0405" // settings and identity, sent after a command
msgEVParamsAlt = "0840" // the same fields, in answer to a status request
msgEVConfirm = "0900" // the same fields again, confirming a control change
msgEVCharging = "0403" // a couple of charging parameters
)
// mqttField is one named value inside a device message. factor scales the raw
// integer (0.1 for a decivolt, 0.001 for a watt-hour reported in kWh); unsigned
// marks the fields whose two- and four-byte types are *not* signed, which is the
// exception rather than the rule; clock marks the two-byte fields that hold a
// minute and an hour rather than a number.
type mqttField struct {
name string
factor float64
unsigned bool
clock bool
}
// evTelemetry decodes the 0410 message: the charger's live electrical state,
// published every few seconds but only while a realtime trigger is live. It is
// where the two signals the Modbus map has no register for — the plug and start
// countdowns — actually come from.
var evTelemetry = map[byte]mqttField{
0xa2: {name: "voltageL1", factor: 0.1},
0xa3: {name: "voltageL2", factor: 0.1},
0xa4: {name: "voltageL3", factor: 0.1},
0xa5: {name: "currentL1", factor: 0.1},
0xa6: {name: "currentL2", factor: 0.1},
0xa7: {name: "currentL3", factor: 0.1},
0xa8: {name: "powerTotal"},
0xa9: {name: "sessionSeconds"},
0xaa: {name: "sessionWh"},
0xab: {name: "sessionStartedAt", unsigned: true},
0xad: {name: "plugCountdownSeconds"},
0xae: {name: "startCountdownSeconds"},
0xaf: {name: "chargingWindowSeconds"},
0xb0: {name: "powerL1"},
0xb1: {name: "powerL2"},
0xb2: {name: "powerL3"},
0xb3: {name: "sessionWhL1"},
0xb4: {name: "sessionWhL2"},
0xb5: {name: "sessionWhL3"},
0xb8: {name: "ocppStatus"},
0xba: {name: "phaseMode"},
0xbb: {name: "status"},
}
// evParams decodes the 0405 message (and the 0840 and 0900 that carry the same
// fields): what the charger is *set* to, plus its identity. The charger sends it
// after a control change rather than on a schedule, so these values arrive with
// a command rather than with the telemetry stream.
var evParams = map[byte]mqttField{
0xa3: {name: "plugLockSwitch"},
0xa4: {name: "autoStartSwitch"},
0xa8: {name: "maxCurrentSetA", factor: 0.1},
0xaa: {name: "ledBrightness"},
0xac: {name: "autoRestartSwitch"},
0xad: {name: "randomDelaySwitch"},
0xaf: {name: "swipeUpMode"},
0xb0: {name: "swipeDownMode"},
0xb2: {name: "smartTouchMode"},
0xb4: {name: "lightOffScheduleSwitch"},
0xb5: {name: "lightOffStart", unsigned: true, clock: true},
0xb6: {name: "lightOffEnd", unsigned: true, clock: true},
0xb7: {name: "modbusSwitch"},
0xcc: {name: "modbusTimeoutSeconds"},
0xce: {name: "maxCurrentA", factor: 0.1},
0xcf: {name: "modbusPort"},
0xd0: {name: "ipAddress"},
0xd3: {name: "loadBalancing"},
0xd4: {name: "mainBreakerLimitA"},
0xd5: {name: "loadBalanceMonitorMode"},
0xd6: {name: "loadBalanceMeterFlag"},
0xd7: {name: "loadBalanceMonitorSN"},
0xd8: {name: "solarBalancing"},
// The charger's solar charging mode (grid-assisted or solar only), not the
// charging mode the Modbus map means by that name.
0xd9: {name: "solarChargeMode"},
0xda: {name: "solarMinCurrentA"},
0xdb: {name: "phaseMode"},
0xdc: {name: "solarMonitoringMode"},
0xdd: {name: "autoPhaseSwitch"},
0xde: {name: "solarMonitorSN"},
0xdf: {name: "boostMode"},
0xe0: {name: "cpSignal"},
0xe2: {name: "plugged"},
0xe3: {name: "status"},
0xe6: {name: "scheduleSwitch"},
0xe7: {name: "weekStart", unsigned: true, clock: true},
0xe8: {name: "weekEnd", unsigned: true, clock: true},
0xe9: {name: "weekendStart", unsigned: true, clock: true},
0xea: {name: "weekendEnd", unsigned: true, clock: true},
0xeb: {name: "weekendMode"},
0xec: {name: "scheduleMode"},
0xfe: {name: "minCurrentA"},
}
// evCharging decodes the 0403 message, a pair of charging parameters the charger
// sends around a mode change.
var evCharging = map[byte]mqttField{
0xa5: {name: "chargingWindowSeconds"},
0xa6: {name: "solarMinCurrentA"},
}
// evMessages selects a field map by message type. A type absent from here is one
// we have no map for; its frame is still parsed, but nothing is named.
var evMessages = map[string]map[byte]mqttField{
msgEVTelemetry: evTelemetry,
msgEVParams: evParams,
msgEVParamsAlt: evParams,
msgEVConfirm: evParams,
msgEVCharging: evCharging,
}
// ---- outbound frames ---------------------------------------------------------
// cmdField is one field of a command frame. typ is typeNone for the single-byte
// fields that carry no value type.
type cmdField struct {
name byte
typ byte
value []byte
}
// rawField builds a field with no value type — the `a1 01 22` pattern that opens
// every command.
func rawField(name byte, value ...byte) cmdField {
return cmdField{name: name, typ: typeNone, value: value}
}
// uintField builds a one-byte unsigned field.
func uintField(name byte, v uint8) cmdField {
return cmdField{name: name, typ: typeUint8, value: []byte{v}}
}
// intField builds a two-byte little-endian signed field.
func intField(name byte, v int16) cmdField {
b := make([]byte, 2)
binary.LittleEndian.PutUint16(b, uint16(v))
return cmdField{name: name, typ: typeInt16LE, value: b}
}
// varField builds a four-byte little-endian field.
func varField(name byte, v uint32) cmdField {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, v)
return cmdField{name: name, typ: typeInt32LE, value: b}
}
// timestampField is the `fe` field every command ends with: the sender's clock,
// in whole seconds.
func timestampField(now time.Time) cmdField {
return varField(0xfe, uint32(now.Unix()))
}
// clockField builds the two-byte field the charger carries a time of day in:
// the hour and the minute, least significant byte first, which is the same
// layout the decoder reads back as "HH:MM".
func clockField(name byte, hour, minute int) cmdField {
return intField(name, int16(uint16(hour)<<8|uint16(minute)))
}
// stringField builds a fixed-length text field, zero padded. The length is the
// charger's, not the value's: a serial field is sixteen bytes whether or not the
// serial fills them.
func stringField(name byte, s string, length int) cmdField {
b := make([]byte, length)
copy(b, s)
return cmdField{name: name, typ: typeString, value: b}
}
// encodeFrame builds one command frame for a message type. The caller supplies
// every field in wire order, starting with the `a1 01 22` opener each command in
// the reference maps carries and ending with the timestamp.
func encodeFrame(msgType string, fields []cmdField) ([]byte, error) {
mt, err := decodeHex(msgType)
if err != nil || len(mt) < 2 || len(mt) > 3 {
return nil, fmt.Errorf("anker-solix: %q is not a message type", msgType)
}
body := make([]byte, 0, 32)
for _, f := range fields {
n := len(f.value)
if f.typ != typeNone {
n++
}
if n < 1 || n > 255 {
return nil, fmt.Errorf("anker-solix: field %02x does not fit one frame field", f.name)
}
body = append(body, f.name, byte(n))
if f.typ != typeNone {
body = append(body, f.typ)
}
body = append(body, f.value...)
}
// The length counts the whole frame, checksum byte included.
total := frameHeaderLen + (len(mt) - 2) + len(body) + 1
out := make([]byte, 0, total)
out = append(out, frameMarker...)
out = binary.LittleEndian.AppendUint16(out, uint16(total))
out = append(out, patternSend...)
out = append(out, mt...)
out = append(out, body...)
return append(out, xorChecksum(out)), nil
}
// xorChecksum is the frame's only integrity check: every byte XORed together.
func xorChecksum(b []byte) byte {
var sum byte
for _, x := range b {
sum ^= x
}
return sum
}
// ---- inbound frames ----------------------------------------------------------
// decodeFrame parses one device frame and returns its message type together with
// the values its field map names. A field the map does not know is skipped
// rather than guessed at, and a frame whose checksum does not add up is
// rejected: these arrive over a cloud connection we do not control, so a
// truncated one must not be read as a charger reporting zeros.
func decodeFrame(data []byte) (string, map[string]any, error) {
if len(data) < frameHeaderLen+2 {
return "", nil, fmt.Errorf("anker-solix: device frame is %d bytes, too short to hold a header", len(data))
}
if data[0] != frameMarker[0] || data[1] != frameMarker[1] {
return "", nil, fmt.Errorf("anker-solix: device frame does not start with the Anker marker (%02x%02x)", data[0], data[1])
}
if n := int(binary.LittleEndian.Uint16(data[2:4])); n != len(data) {
return "", nil, fmt.Errorf("anker-solix: device frame says it is %d bytes but %d arrived", n, len(data))
}
if xorChecksum(data) != 0 {
return "", nil, fmt.Errorf("anker-solix: device frame checksum does not match")
}
msgType := encodeHex(data[7:9])
// Some inbound frames carry a counter byte between the header and the first
// data field, and nothing in the frame says which kind this is. Rather than
// guess from the byte's value — field names run high enough to be mistaken for
// a counter — try both and keep the reading whose fields tile the frame
// exactly, from the first field to the checksum with nothing left over.
raw, ok := splitFields(data, frameHeaderLen)
if !ok {
if raw, ok = splitFields(data, frameHeaderLen+1); !ok {
return "", nil, fmt.Errorf("anker-solix: device frame %s does not divide into whole data fields", msgType)
}
}
fields := evMessages[msgType]
values := map[string]any{}
for _, r := range raw {
f, known := fields[r.name]
if !known || f.name == "" {
continue
}
if v, ok := decodeValue(r.typ, r.value, f); ok {
values[f.name] = v
}
}
return msgType, values, nil
}
// rawFieldBytes is one data field as it sat in the frame, before its map entry
// decides what it means.
type rawFieldBytes struct {
name byte
typ byte
value []byte
}
// splitFields walks the data fields from start and reports them only if they end
// exactly at the checksum byte. A frame read from the wrong offset runs off the
// end or stops short, which is what makes the exact fit a usable test.
func splitFields(data []byte, start int) ([]rawFieldBytes, bool) {
end := len(data) - 1 // the last byte is the checksum
var out []rawFieldBytes
for idx := start; idx < end; {
if idx+2 > end {
return nil, false
}
name := data[idx]
flen := int(data[idx+1])
if flen == 0 || idx+2+flen > end {
return nil, false
}
body := data[idx+2 : idx+2+flen]
idx += 2 + flen
typ, value := typeNone, body
if flen > 1 && body[0] <= typeByteMax {
typ, value = body[0], body[1:]
}
out = append(out, rawFieldBytes{name: name, typ: typ, value: value})
}
return out, len(out) > 0
}
// decodeValue turns one field's bytes into a value, following the type byte the
// field carries and the scaling its map entry gives. It reports false for a
// field whose bytes do not fit its type, so a short value is dropped rather than
// read as a smaller number.
func decodeValue(typ byte, b []byte, f mqttField) (any, bool) {
if len(b) == 0 {
return nil, false
}
factor := f.factor
if factor == 0 {
factor = 1
}
scale := func(n int64) any {
if factor == 1 {
return float64(n)
}
return round(float64(n)*factor, factor)
}
switch typ {
case typeString:
return printable(b), true
case typeUint8:
return scale(int64(b[0])), true
case typeInt16LE:
if len(b) < 2 {
return nil, false
}
if f.clock {
// Two bytes holding a minute and an hour, least significant first.
return fmt.Sprintf("%02d:%02d", b[1], b[0]), true
}
if f.unsigned {
return scale(int64(binary.LittleEndian.Uint16(b))), true
}
return scale(int64(int16(binary.LittleEndian.Uint16(b)))), true
case typeInt32LE:
if len(b) < 4 {
return nil, false
}
if f.unsigned {
return scale(int64(binary.LittleEndian.Uint32(b))), true
}
return scale(int64(int32(binary.LittleEndian.Uint32(b)))), true
case typeFloat32:
if len(b) < 4 {
return nil, false
}
return float64(math.Float32frombits(binary.LittleEndian.Uint32(b))), true
default:
// No value type: the bytes are the number, most significant first.
var n int64
for _, x := range b {
n = n<<8 | int64(x)
}
return scale(n), true
}
}
// round trims the floating-point noise a factor introduces, to the precision the
// factor itself implies — 0.1 keeps one decimal, 0.001 keeps three.
func round(v, factor float64) float64 {
digits := 0
for f := factor; f < 1 && digits < 6; f *= 10 {
digits++
}
p := math.Pow(10, float64(digits))
return math.Round(v*p) / p
}
// printable keeps the readable part of a string field; the charger pads some of
// them with control bytes.
func printable(b []byte) string {
var sb strings.Builder
for _, r := range string(b) {
if r >= 0x20 && r != 0x7f {
sb.WriteRune(r)
}
}
return strings.TrimSpace(sb.String())
}
// ---- small hex helpers -------------------------------------------------------
const hexDigits = "0123456789abcdef"
// encodeHex renders bytes as lowercase hex, which is how message types are keyed.
func encodeHex(b []byte) string {
out := make([]byte, 0, len(b)*2)
for _, x := range b {
out = append(out, hexDigits[x>>4], hexDigits[x&0x0f])
}
return string(out)
}
// decodeHex parses a lowercase or uppercase hex string.
func decodeHex(s string) ([]byte, error) {
if len(s)%2 != 0 {
return nil, fmt.Errorf("hex string %q has an odd length", s)
}
out := make([]byte, 0, len(s)/2)
for i := 0; i < len(s); i += 2 {
hi, err1 := hexNibble(s[i])
lo, err2 := hexNibble(s[i+1])
if err1 != nil || err2 != nil {
return nil, fmt.Errorf("hex string %q holds a non-hex character", s)
}
out = append(out, hi<<4|lo)
}
return out, nil
}
func hexNibble(c byte) (byte, error) {
switch {
case c >= '0' && c <= '9':
return c - '0', nil
case c >= 'a' && c <= 'f':
return c - 'a' + 10, nil
case c >= 'A' && c <= 'F':
return c - 'A' + 10, nil
}
return 0, fmt.Errorf("not a hex digit: %q", string(rune(c)))
}