test_binary921_protocol.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/usr/bin/env python3
  2. """Tests for the J6 binary921 host/debug protocol helpers."""
  3. from __future__ import annotations
  4. import sys
  5. import unittest
  6. from pathlib import Path
  7. sys.dont_write_bytecode = True
  8. ROOT = Path(__file__).resolve().parents[2]
  9. sys.path.insert(0, str(ROOT / "tools"))
  10. from binary921 import ( # noqa: E402
  11. Binary921FrameError,
  12. CommandId,
  13. FrameType,
  14. build_host_command,
  15. decode_frame,
  16. decode_stream,
  17. encode_frame,
  18. frame_to_dict,
  19. profile_id_for_name,
  20. profile_name_for_id,
  21. )
  22. class Binary921ProtocolTests(unittest.TestCase):
  23. def test_encode_decode_roundtrip_uses_magic_version_type_len_and_crc16(self) -> None:
  24. payload = bytes([CommandId.SET_PTC, 0x01, 0x0A])
  25. frame = encode_frame(
  26. profile_id=1,
  27. frame_type=FrameType.HOST_CMD,
  28. seq=0x01020304,
  29. timestamp_tick=0x11223344,
  30. payload=payload,
  31. )
  32. self.assertEqual(frame[:2], b"\xA5\x5A")
  33. self.assertEqual(frame[2], 1)
  34. self.assertEqual(frame[3], 1)
  35. self.assertEqual(frame[4], FrameType.HOST_CMD)
  36. self.assertEqual(frame[5:9], b"\x04\x03\x02\x01")
  37. self.assertEqual(frame[9:13], b"\x44\x33\x22\x11")
  38. self.assertEqual(frame[13], len(payload))
  39. decoded = decode_frame(frame)
  40. self.assertEqual(decoded.profile_id, 1)
  41. self.assertEqual(decoded.frame_type, FrameType.HOST_CMD)
  42. self.assertEqual(decoded.seq, 0x01020304)
  43. self.assertEqual(decoded.timestamp_tick, 0x11223344)
  44. self.assertEqual(decoded.payload, payload)
  45. def test_decode_rejects_bad_crc_and_short_frames(self) -> None:
  46. good = encode_frame(
  47. profile_id=1,
  48. frame_type=FrameType.READY,
  49. seq=1,
  50. timestamp_tick=2,
  51. payload=b"",
  52. )
  53. bad = bytearray(good)
  54. bad[-1] ^= 0x01
  55. with self.assertRaisesRegex(Binary921FrameError, "crc"):
  56. decode_frame(bytes(bad))
  57. with self.assertRaisesRegex(Binary921FrameError, "short"):
  58. decode_frame(good[:7])
  59. def test_decode_stream_resynchronizes_after_noise(self) -> None:
  60. first = encode_frame(profile_id=1, frame_type=FrameType.READY, seq=1, timestamp_tick=0, payload=b"")
  61. second = encode_frame(
  62. profile_id=1,
  63. frame_type=FrameType.PWM_STATE,
  64. seq=2,
  65. timestamp_tick=20,
  66. payload=bytes([10, 20, 40, 30]),
  67. )
  68. frames, remainder = decode_stream(b"\x00noise" + first + b"\xFF" + second + b"\xA5")
  69. self.assertEqual([frame.frame_type for frame in frames], [FrameType.READY, FrameType.PWM_STATE])
  70. self.assertEqual(remainder, b"\xA5")
  71. def test_decode_stream_accepts_mixed_combined_uart_frames(self) -> None:
  72. profile_id = 3
  73. stream = b"".join(
  74. [
  75. encode_frame(profile_id=profile_id, frame_type=FrameType.READY, seq=0, timestamp_tick=0, payload=bytes([profile_id, 0, 0, 0])),
  76. encode_frame(profile_id=profile_id, frame_type=FrameType.HOST_ACK, seq=1, timestamp_tick=1, payload=bytes([1, CommandId.SET_PTC, 0, 0, 0x34, 0x12])),
  77. encode_frame(profile_id=profile_id, frame_type=FrameType.BMC_PAYLOAD, seq=2, timestamp_tick=2, payload=bytes([48, 48, 0, 1, 0x1E, 0xFF, 0, 0])),
  78. encode_frame(profile_id=profile_id, frame_type=FrameType.SENSOR_CACHE, seq=3, timestamp_tick=3, payload=bytes([profile_id, 0, 1, 0xFE, 0xEF, 0xFF, 2, 22])),
  79. ]
  80. )
  81. frames, remainder = decode_stream(stream)
  82. self.assertEqual(remainder, b"")
  83. self.assertEqual([frame.profile_id for frame in frames], [profile_id] * 4)
  84. self.assertEqual(
  85. [frame.frame_type for frame in frames],
  86. [FrameType.READY, FrameType.HOST_ACK, FrameType.BMC_PAYLOAD, FrameType.SENSOR_CACHE],
  87. )
  88. def test_host_command_builder_validates_channels_and_percentages(self) -> None:
  89. set_ptc = build_host_command(seq=1, command=CommandId.SET_PTC, channel=24, percent=20)
  90. decoded_ptc = decode_frame(set_ptc)
  91. self.assertEqual(decoded_ptc.frame_type, FrameType.HOST_CMD)
  92. self.assertEqual(decoded_ptc.payload, bytes([CommandId.SET_PTC, 24, 20]))
  93. set_fan = build_host_command(seq=2, command=CommandId.SET_FAN, channel=4, percent=30)
  94. self.assertEqual(decode_frame(set_fan).payload, bytes([CommandId.SET_FAN, 4, 30]))
  95. for channel in (0, 25):
  96. with self.subTest(channel=channel):
  97. with self.assertRaisesRegex(ValueError, "PTC"):
  98. build_host_command(seq=3, command=CommandId.SET_PTC, channel=channel, percent=10)
  99. for channel in (0, 5):
  100. with self.subTest(channel=channel):
  101. with self.assertRaisesRegex(ValueError, "FAN"):
  102. build_host_command(seq=4, command=CommandId.SET_FAN, channel=channel, percent=10)
  103. with self.assertRaisesRegex(ValueError, "percent"):
  104. build_host_command(seq=5, command=CommandId.SET_PTC, channel=1, percent=101)
  105. def test_zero_payload_commands_have_stable_binary_payloads(self) -> None:
  106. for command in [
  107. CommandId.GET_SENSOR,
  108. CommandId.GET_MAP,
  109. CommandId.GET_STATUS,
  110. CommandId.DISABLE,
  111. ]:
  112. with self.subTest(command=command):
  113. frame = decode_frame(build_host_command(seq=7, command=command))
  114. self.assertEqual(frame.payload, bytes([command]))
  115. def test_frame_to_dict_decodes_ready_host_command_and_ack_payloads(self) -> None:
  116. ready = decode_frame(
  117. encode_frame(profile_id=1, frame_type=FrameType.READY, seq=1, timestamp_tick=2, payload=bytes([1, 0, 0, 0]))
  118. )
  119. command = decode_frame(build_host_command(seq=9, command=CommandId.SET_PTC, channel=1, percent=10))
  120. ack = decode_frame(
  121. encode_frame(
  122. profile_id=1,
  123. frame_type=FrameType.HOST_ACK,
  124. seq=9,
  125. timestamp_tick=12,
  126. payload=bytes([1, CommandId.SET_PTC, 0, 0, 0x34, 0x12]),
  127. )
  128. )
  129. self.assertEqual(frame_to_dict(ready)["decoded"], {"kind": "ready", "profile_id_payload": 1})
  130. self.assertEqual(
  131. frame_to_dict(command)["decoded"],
  132. {"kind": "host_cmd", "cmd": "SET_PTC", "cmd_id": 32, "channel": 1, "percent": 10},
  133. )
  134. self.assertEqual(
  135. frame_to_dict(ack)["decoded"],
  136. {
  137. "kind": "host_ack",
  138. "ok": True,
  139. "cmd": "SET_PTC",
  140. "cmd_id": 32,
  141. "status_code": 0,
  142. "applied_state_hash": 0x1234,
  143. },
  144. )
  145. def test_production_two_uart_profile_has_stable_id_three(self) -> None:
  146. profile = "production-two-uart-bmc-replay-binary921"
  147. self.assertEqual(profile_id_for_name(profile), 3)
  148. self.assertEqual(profile_name_for_id(3), profile)
  149. frame = decode_frame(
  150. build_host_command(
  151. seq=1,
  152. command=CommandId.SET_PTC,
  153. channel=1,
  154. percent=10,
  155. profile_id=profile_id_for_name(profile),
  156. )
  157. )
  158. self.assertEqual(frame.profile_id, 3)
  159. self.assertEqual(frame.payload, bytes([CommandId.SET_PTC, 1, 10]))
  160. def test_module_snapshot_decodes_pwm_pid_and_timeout_summary(self) -> None:
  161. payload = bytes(
  162. [
  163. 3, # profile
  164. 0, # host_err_count
  165. 12,
  166. 0, # command_count_seen
  167. 0x1E,
  168. 0xFF,
  169. 0x08,
  170. 0x14,
  171. CommandId.DISABLE,
  172. 0,
  173. 0,
  174. 0,
  175. 0, # pid requested false
  176. 1, # timeout active
  177. 2,
  178. 22,
  179. 0,
  180. 0,
  181. 30,
  182. 30,
  183. 0,
  184. 0x0F,
  185. 0,
  186. 0,
  187. ]
  188. )
  189. frame = decode_frame(
  190. encode_frame(profile_id=3, frame_type=FrameType.MODULE_SNAPSHOT, seq=9, timestamp_tick=10, payload=payload)
  191. )
  192. decoded = frame_to_dict(frame)["decoded"]
  193. self.assertEqual(decoded["kind"], "module_snapshot")
  194. self.assertEqual(decoded["profile_id_payload"], 3)
  195. self.assertEqual(decoded["command_count_seen"], 12)
  196. self.assertEqual(decoded["last_cmd"], "DISABLE")
  197. self.assertFalse(decoded["pid_requested"])
  198. self.assertTrue(decoded["timeout_active"])
  199. self.assertEqual(decoded["ptc01_percent"], 0)
  200. self.assertEqual(decoded["ptc24_percent"], 0)
  201. self.assertEqual(decoded["fan01_percent"], 30)
  202. self.assertEqual(decoded["fan04_percent"], 30)
  203. if __name__ == "__main__":
  204. unittest.main()