prod2u_status.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """Status decoding for the two Prod2U products matching the single frame protocol.
  2. GET_DATA:
  3. - 6400 V2: 86 bytes (old 83-byte fields plus SSR1/SSR2/SSR4 at 83..85)
  4. - 6400 V1: 83 bytes (PTC0..23, Front0..3, Rear0..3, BMC0..23, CT0..26)
  5. - MRDIMM: 63 bytes (FAN0..3, PTC0..3, BMC0..23, CT0..26, lower_mask:u16le, upper_mask:u16le)
  6. """
  7. from __future__ import annotations
  8. from typing import Any
  9. TEMPERATURE_INVALID = 0xFF
  10. def decode_temperature(byte: int) -> int | None:
  11. value = int(byte) & 0xFF
  12. return None if value == TEMPERATURE_INVALID else value
  13. def state_6400(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]:
  14. p = bytes(payload)
  15. if len(p) == 94:
  16. ptc = list(p[0:35])
  17. front = list(p[35:39])
  18. rear = list(p[39:43])
  19. bmc_raw = list(p[43:67])
  20. ct_raw = list(p[67:94])
  21. elif len(p) in (83, 86):
  22. ptc = list(p[0:24])
  23. front = list(p[24:28])
  24. rear = list(p[28:32])
  25. bmc_raw = list(p[32:56])
  26. ct_raw = list(p[56:83])
  27. else:
  28. raise ValueError(f"6400 GET_DATA payload length must be 83, 86 or 94, got {len(p)}")
  29. bmc_temps = [decode_temperature(b) for b in bmc_raw]
  30. ct_temps = [decode_temperature(b) for b in ct_raw]
  31. return {
  32. "product": "6400-v2" if len(p) == 86 else "6400-front-rear",
  33. "ptc_applied": ptc,
  34. "front_applied": front,
  35. "rear_applied": rear,
  36. "ptc": ptc,
  37. "front": front,
  38. "rear": rear,
  39. "ssr": list(p[83:86]) if len(p) == 86 else [],
  40. "ssr_applied": list(p[83:86]) if len(p) == 86 else [],
  41. "bmc": {
  42. "raw": bmc_raw,
  43. "temperature_c": bmc_temps,
  44. "valid": [t is not None for t in bmc_temps],
  45. },
  46. "ct": {
  47. "raw": ct_raw,
  48. "temperature_c": ct_temps,
  49. "valid": [t is not None for t in ct_temps],
  50. },
  51. }
  52. def mrdimm_state(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]:
  53. p = bytes(payload)
  54. if len(p) != 63:
  55. raise ValueError(f"MRDIMM GET_DATA payload length must be 63, got {len(p)}")
  56. fan = list(p[0:4])
  57. ptc = list(p[4:8])
  58. bmc_raw = list(p[8:32])
  59. bmc_temps = [decode_temperature(b) for b in bmc_raw]
  60. ct_raw = list(p[32:59])
  61. ct_temps = [decode_temperature(b) for b in ct_raw]
  62. lower_mask = int.from_bytes(p[59:61], "little")
  63. upper_mask = int.from_bytes(p[61:63], "little")
  64. axes = {}
  65. for i in range(12):
  66. axes[f"axis{i}"] = {
  67. "index": i,
  68. "lower_limit": bool(lower_mask & (1 << i)),
  69. "upper_limit": bool(upper_mask & (1 << i)),
  70. }
  71. return {
  72. "product": "mrdimm-chamber",
  73. "fan_applied": fan,
  74. "ptc_applied": ptc,
  75. "fan": fan,
  76. "ptc": ptc,
  77. "lower_limit_mask": lower_mask,
  78. "upper_limit_mask": upper_mask,
  79. "axes": axes,
  80. "bmc": {
  81. "raw": bmc_raw,
  82. "temperature_c": bmc_temps,
  83. "valid": [t is not None for t in bmc_temps],
  84. },
  85. "ct": {
  86. "raw": ct_raw,
  87. "temperature_c": ct_temps,
  88. "valid": [t is not None for t in ct_temps],
  89. },
  90. }
  91. def diagnostic_6400_v2(payload: bytes | bytearray) -> dict[str, Any]:
  92. """Decode only diagnostic format 1; retain unknown formats as raw evidence."""
  93. p = bytes(payload)
  94. if len(p) != 20:
  95. raise ValueError(f"6400 V2 DIAGNOSTIC payload length must be 20, got {len(p)}")
  96. result = {"format": p[0], "board_id": p[1], "raw_hex": p.hex(" ").upper(),
  97. "supported": p[0] == 1 and p[1] == 0x64}
  98. if not result["supported"]:
  99. return result
  100. error_names = {0: "SUCCESS", 1: "WRITE_ADDRESS_NACK", 2: "REGISTER_NACK",
  101. 3: "READ_ADDRESS_NACK", 4: "TIMEOUT", 5: "STUCK_LOW", 6: "ARBITRATION"}
  102. result.update(
  103. firmware_version=".".join(str(v) for v in p[2:5]),
  104. scl_high=bool(p[5] & 1), sda_high=bool(p[5] & 2),
  105. scl_drive_low=bool(p[5] & 4), sda_drive_low=bool(p[5] & 8),
  106. i2c_busy=bool(p[5] & 16), sample_seen=bool(p[5] & 32),
  107. scan_state=p[6] >> 6, current_channel=p[6] & 31,
  108. last_channel=p[7], device_address=p[8], register=p[9],
  109. error=p[10], error_name=error_names.get(p[10], "UNKNOWN"),
  110. is_temperature=p[7] <= 8 and p[9] <= 8,
  111. raw_register=int.from_bytes(p[11:13], "little"),
  112. raw_temperature=int.from_bytes(p[11:13], "little"),
  113. temperature_c=decode_temperature(p[13]),
  114. valid_temperature_mask=int.from_bytes(p[14:16], "little") & 0x1FF,
  115. scan_round=p[16], observed_result_mask=p[17], bmc_valid_count=p[18],
  116. bmc_failed=bool(p[19] & 1), bmc_framing_error_seen=bool(p[19] & 2),
  117. temperature_queue_overflow=bool(p[19] & 4),
  118. watchdog_expired=bool(p[19] & 8), duty_bank_valid=bool(p[19] & 16),
  119. )
  120. return result