cremalink.ecam.builder module

Parametric ECAM command-frame construction.

Wire frame layout (verified against the decompiled Coffee Link builders and live LAN captures):

<0x0D> <len:1B> <family:2B> <payload…> <crc16:2B>

len counts the bytes from the length byte itself through the CRC — i.e. len == total_frame_length - 1; it is derived from emitted content so variable-length payloads are supported (FR-003). The CRC is crc16_ccitt (init 0x1D0F, poly 0x1021) over every byte preceding it (FR-004/008).

Transport encoding appends a 4-byte big-endian unix timestamp (never CRC-covered) and, on striker machines, the registered 4-byte app-id trailer (FR-005/007).

All functions are pure: no I/O, no network, no Home Assistant imports.

cremalink.ecam.builder.BREW_DROP_TAGS = frozenset({8})

Param ids excluded from a brew recipe by the app’s O() builder: param 8 (DUExPER) is always dropped; the E0 predicate then keeps only ids < 23 or in {27, 28, 31, 33, 38, 39} (INDEX_LENGTH, ACCESSORIO, ICED, MUG_ADJUST, INTENSITY, RINSE). On the verified Soul corpus the two rules together remove tags {0x08, 0x19}.

class cremalink.ecam.builder.BrewAction(*values)[source]

Bases: int, Enum

Action/strength byte of a 0x83 brew frame.

START = 1
STOP = 2
cremalink.ecam.builder.DEFAULT_RECIPE = b'\x0f\x00\xfa\x1b\x01'

250 ml, temp index 1). The trailing brew trailer byte is NOT part of the recipe — build_brew appends (profile_slot<<2)|accessory itself. Opaque bytes — brews are normally built from the machine’s own 0xA6 recipe or a command-map entry; this is only a safe fallback.

Type:

Default recipe TLVs (captured hot-water command

class cremalink.ecam.builder.PowerCommand(*values)[source]

Bases: bytes, Enum

Payloads of the 0x84 0x0F power/session family (verified).

SESSION_REFRESH = b'\x03\x02'
STANDBY = b'\x01\x01'
WAKE = b'\x02\x01'
cremalink.ecam.builder.REQUEST_NAMES = {117: 'monitor', 131: 'brew', 132: 'power', 144: 'write_param', 149: 'read_param', 161: 'read_extended', 162: 'statistics', 164: 'profile_names', 165: 'profile_names_write', 166: 'recipe_values', 168: 'priority_list', 169: 'profile_select', 170: 'recipe_names', 176: 'factory_descriptor', 185: 'bean_select', 186: 'bean_system', 232: 'striker_handshake'}

Human-readable labels for request ids (diagnostics/logging).

cremalink.ecam.builder.brew_param_included(param_id: int) → bool[source]

The app-side E0 predicate: which recipe params go on the wire.

cremalink.ecam.builder.build_bean_select(slot: int, profile: MachineProfile) → bytes[source]

0D 06 B9 F0 <slot> — bean-system select (APK Q0``→``g0 / selectBeanSystem / getPacketForSelectBean, DECODED.md).

This is a gated write: the session must be announced first (protocol.md §14.5). The frame sits on §13’s untested list — structured after the verified 0xA9 path, ack semantics ok=(b5==0) per the DECODED 0xB9``→``h.m dispatch.

cremalink.ecam.builder.build_bean_system(index: int, profile: MachineProfile) → bytes[source]

0D 06 BA F0 <index> — bean-system slot read (APK U).

cremalink.ecam.builder.build_brew(beverage_id: int, action: int | BrewAction, recipe: bytes | bytearray | Iterable[int], profile: MachineProfile, *, profile_slot: int = 0, accessory: int = 0) → bytes[source]

Build a 0x83 brew frame.

0D LEN 83 F0 <bev> <action> <recipe…> <(profile_slot<<2)|accessory> — the frame names its profile in the trailer byte; the recipe bytes must be that same profile’s values (0xA6 reads are profile-scoped).

cremalink.ecam.builder.build_brew_params(beverage_id: int, action: int | BrewAction, params: Mapping[int, int], profile: MachineProfile, *, profile_slot: int = 0, accessory: int = 0) → bytes[source]

Build a 0x83 brew frame straight from a {param_id: value} map.

Encodes each param as <id><value> — 1 byte, or big-endian u16 for WIDE_TAGS — after applying the app’s E0 filter (brew_param_included; id 8 dropped). Bean-system beverages (beverage_id 0xC8–0xCD) send action | 0x80 — the APK l0 fromBeanSystem flag, which also retains param 2 for bev 200 (research.md R2/R10). Byte-identical to build_brew() given the same effective TLV set.

cremalink.ecam.builder.build_factory_descriptor(beverage_id: int, profile: MachineProfile) → bytes[source]

0D 06 B0 F0 <bev> — min/default/max descriptor read (APK Y0).

cremalink.ecam.builder.build_handshake_blob() → bytes[source]

45 DA 37 88 34 EB AF FF FF FA + CRC — non-striker session handshake blob (APK s0()/buildHandshakeBlob).

No 0x0D header — the CRC covers the 10 payload bytes exactly like the app’s crc16 (which iterates len-2). The app’s connect sequence is device_connected → this blob → traffic; without it the machine served property republishes and 0xA2 but ignored session commands (0x95 silent, 0x90 acked-not- committed, 0xA9 unacked — observed 2026-10-14/25, §14.8).

cremalink.ecam.builder.build_monitor_request(profile: MachineProfile) → bytes[source]

0D 05 <monitor_id> 0F — monitor snapshot request (APK V).

cremalink.ecam.builder.build_power(kind: PowerCommand, profile: MachineProfile) → bytes[source]

Build an 0x84 0x0F power/session frame.

cremalink.ecam.builder.build_priority_list(profile_slot: int, profile: MachineProfile) → bytes[source]

0D 06 A8 F0 <prof> — per-profile ordered beverage list.

cremalink.ecam.builder.build_profile_names(first: int, count: int, profile: MachineProfile) → bytes[source]

0D 07 A4 F0 <first> <count> — profile-name read (APK t0).

cremalink.ecam.builder.build_profile_select(profile_slot: int, profile: MachineProfile) → bytes[source]

0D 06 A9 F0 <prof> — profile select (verified live).

This is a gated write: the session must be announced first.

cremalink.ecam.builder.build_read(request_id: int, args: bytes | bytearray | Iterable[int] = b'', profile: MachineProfile | None = None, *, flag: int | None = None) → bytes[source]

Build a read-style request frame for the given request id.

args is the raw payload after the flag byte. Flags: 0x0F for the statistics family, 0xF0 (answer wanted) elsewhere — both verified on live LAN. Use the build_read_* helpers below for the structured payloads.

cremalink.ecam.builder.build_read_block(param_id: int, count: int, profile: MachineProfile) → bytes[source]

0D 08 <0x95|0xA1> <flag> <param:2B> <count> — APK r0 block read.

count > 4 goes out as the 0xA1 extended read; the machine normalises the answer back to a 0x95 param list.

cremalink.ecam.builder.build_read_param(param_id: int, profile: MachineProfile) → bytes[source]

0D 08 95 <flag> <param:2B> <qty=1> — single parameter read.

The app’s buildParamRead (protocol.md §8) always carries the qty byte; the legacy qty-less len-7 form is only tolerated by the machine (answered sporadically — see §14.8’s 2026-10-14 captures where len-7 reads were dropped while 0D 08 app reads answered). Delegates to build_read_block() with count=1, which emits the identical wire frame.

cremalink.ecam.builder.build_read_params(first_param: int, last_param: int, profile: MachineProfile) → bytes[source]

0D 09 95 <flag> <first:2B> <last:2B> — param range read (captured).

cremalink.ecam.builder.build_recipe_names(first: int, count: int, profile: MachineProfile) → bytes[source]

0D 07 AA F0 <first> <count> — custom recipe-name read (APK v0).

cremalink.ecam.builder.build_recipe_values(profile_slot: int, beverage_id: int, profile: MachineProfile) → bytes[source]

0D 07 A6 F0 <prof> <bev> — profile-scoped recipe read (APK M0).

cremalink.ecam.builder.build_statistics_page(start_id: int, count: int, profile: MachineProfile) → bytes[source]

0D 08 A2 0F <start_id:2B> <count:1B> — statistics page read.

cremalink.ecam.builder.build_striker_handshake(profile: MachineProfile) → bytes[source]

0D 06 E8 F0 00 — striker session handshake (APK z0).

cremalink.ecam.builder.build_write_param(param_id: int, value: int, profile: MachineProfile, *, flag: int | None = None) → bytes[source]

Build a 0x90 parameter-write frame.

0D 0B 90 <flag> <param:2B> <u32:4B> — byte-identical to the app’s n0() builder: flag 0xF0 for param >= 1000, else 0x0F (protocol.md §14.5 verified 90 0F 00 3E … committing under the device_connected session announce; a 90 F0 write without the announce only staged transiently). flag stays overridable.

cremalink.ecam.builder.describe_request_frame(blob: bytes | bytearray) → dict[source]

Summarize an outbound (0x0D) request frame for logging.

Best-effort and never raises: the returned dict always has a frame hex string; a well-formed request adds request_id, request (label from REQUEST_NAMES), flag, params (hex) and crc_ok. Transport trailers (timestamp/app-id) are trimmed first.

cremalink.ecam.builder.encode_for_transport(frame: bytes | bytearray, profile: MachineProfile, *, timestamp: int | None = None, app_id: bytes | bytearray | None = None, raw: bool = False) → str[source]

Base64-encode frame + <ts:4B BE> [+ <app_id:4B>] for the wire.

The timestamp is never CRC-covered; the striker app-id trailer follows it (FR-004/005/007). raw=True skips frame validation — the non-striker s0() handshake blob is a valid data_request payload that carries no 0x0D header (§14.8).

cremalink.ecam.builder.merge_recipe_params(recipe: bytes | bytearray | Iterable[int], overrides: Mapping[int, int]) → bytes[source]

Splice override values into a brew-ready recipe TLV byte string.

Existing entries keep their order and width (u16 per WIDE_TAGS — mirrored from catalog to avoid an import cycle); override tags absent from recipe are appended. Pure — filtering for the wire (brew_param_included) is the caller’s job, as with the app’s O() builder (research.md R1–R2).

cremalink.ecam.builder.trim_frame(blob: bytes | bytearray) → bytes[source]

Cut a received blob to its declared frame, dropping any trailer.

data_response/republished datapoints append a 4-byte timestamp after the CRC — the trailing bytes are transport metadata, not frame.

cremalink.ecam.builder.verify_frame(frame: bytes | bytearray) → bool[source]

Validate a wire frame’s length byte and CRC.

Bytes beyond the declared length (e.g. the datapoint timestamp trailer) are ignored. Returns False on any structural violation — never raises.