"""Status decoding for the two Prod2U products matching the single frame protocol. GET_DATA: - 6400 V2: 86 bytes (old 83-byte fields plus SSR1/SSR2/SSR4 at 83..85) - 6400 V1: 83 bytes (PTC0..23, Front0..3, Rear0..3, BMC0..23, CT0..26) - MRDIMM: 63 bytes (FAN0..3, PTC0..3, BMC0..23, CT0..26, lower_mask:u16le, upper_mask:u16le) """ from __future__ import annotations from typing import Any TEMPERATURE_INVALID = 0xFF def decode_temperature(byte: int) -> int | None: value = int(byte) & 0xFF return None if value == TEMPERATURE_INVALID else value def state_6400(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]: p = bytes(payload) if len(p) == 94: ptc = list(p[0:35]) front = list(p[35:39]) rear = list(p[39:43]) bmc_raw = list(p[43:67]) ct_raw = list(p[67:94]) elif len(p) in (83, 86): ptc = list(p[0:24]) front = list(p[24:28]) rear = list(p[28:32]) bmc_raw = list(p[32:56]) ct_raw = list(p[56:83]) else: raise ValueError(f"6400 GET_DATA payload length must be 83, 86 or 94, got {len(p)}") bmc_temps = [decode_temperature(b) for b in bmc_raw] ct_temps = [decode_temperature(b) for b in ct_raw] return { "product": "6400-v2" if len(p) == 86 else "6400-front-rear", "ptc_applied": ptc, "front_applied": front, "rear_applied": rear, "ptc": ptc, "front": front, "rear": rear, "ssr": list(p[83:86]) if len(p) == 86 else [], "ssr_applied": list(p[83:86]) if len(p) == 86 else [], "bmc": { "raw": bmc_raw, "temperature_c": bmc_temps, "valid": [t is not None for t in bmc_temps], }, "ct": { "raw": ct_raw, "temperature_c": ct_temps, "valid": [t is not None for t in ct_temps], }, } def mrdimm_state(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]: p = bytes(payload) if len(p) != 63: raise ValueError(f"MRDIMM GET_DATA payload length must be 63, got {len(p)}") fan = list(p[0:4]) ptc = list(p[4:8]) bmc_raw = list(p[8:32]) bmc_temps = [decode_temperature(b) for b in bmc_raw] ct_raw = list(p[32:59]) ct_temps = [decode_temperature(b) for b in ct_raw] lower_mask = int.from_bytes(p[59:61], "little") upper_mask = int.from_bytes(p[61:63], "little") axes = {} for i in range(12): axes[f"axis{i}"] = { "index": i, "lower_limit": bool(lower_mask & (1 << i)), "upper_limit": bool(upper_mask & (1 << i)), } return { "product": "mrdimm-chamber", "fan_applied": fan, "ptc_applied": ptc, "fan": fan, "ptc": ptc, "lower_limit_mask": lower_mask, "upper_limit_mask": upper_mask, "axes": axes, "bmc": { "raw": bmc_raw, "temperature_c": bmc_temps, "valid": [t is not None for t in bmc_temps], }, "ct": { "raw": ct_raw, "temperature_c": ct_temps, "valid": [t is not None for t in ct_temps], }, } def diagnostic_6400_v2(payload: bytes | bytearray) -> dict[str, Any]: """Decode only diagnostic format 1; retain unknown formats as raw evidence.""" p = bytes(payload) if len(p) != 20: raise ValueError(f"6400 V2 DIAGNOSTIC payload length must be 20, got {len(p)}") result = {"format": p[0], "board_id": p[1], "raw_hex": p.hex(" ").upper(), "supported": p[0] == 1 and p[1] == 0x64} if not result["supported"]: return result error_names = {0: "SUCCESS", 1: "WRITE_ADDRESS_NACK", 2: "REGISTER_NACK", 3: "READ_ADDRESS_NACK", 4: "TIMEOUT", 5: "STUCK_LOW", 6: "ARBITRATION"} result.update( firmware_version=".".join(str(v) for v in p[2:5]), scl_high=bool(p[5] & 1), sda_high=bool(p[5] & 2), scl_drive_low=bool(p[5] & 4), sda_drive_low=bool(p[5] & 8), i2c_busy=bool(p[5] & 16), sample_seen=bool(p[5] & 32), scan_state=p[6] >> 6, current_channel=p[6] & 31, last_channel=p[7], device_address=p[8], register=p[9], error=p[10], error_name=error_names.get(p[10], "UNKNOWN"), is_temperature=p[7] <= 8 and p[9] <= 8, raw_register=int.from_bytes(p[11:13], "little"), raw_temperature=int.from_bytes(p[11:13], "little"), temperature_c=decode_temperature(p[13]), valid_temperature_mask=int.from_bytes(p[14:16], "little") & 0x1FF, scan_round=p[16], observed_result_mask=p[17], bmc_valid_count=p[18], bmc_failed=bool(p[19] & 1), bmc_framing_error_seen=bool(p[19] & 2), temperature_queue_overflow=bool(p[19] & 4), watchdog_expired=bool(p[19] & 8), duty_bank_valid=bool(p[19] & 16), ) return result