test_binary921_protocol.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_pid_request_decodes_enabled_flag_without_channel_or_percent_aliases(self) -> None:
  106. command = decode_frame(
  107. build_host_command(
  108. seq=5,
  109. command=CommandId.SET_PID_REQUESTED,
  110. percent=1,
  111. profile_id=3,
  112. )
  113. )
  114. decoded = frame_to_dict(command)["decoded"]
  115. self.assertEqual(
  116. decoded,
  117. {
  118. "kind": "host_cmd",
  119. "cmd": "SET_PID_REQUESTED",
  120. "cmd_id": int(CommandId.SET_PID_REQUESTED),
  121. "enabled": True,
  122. },
  123. )
  124. with self.assertRaisesRegex(ValueError, "0/1"):
  125. build_host_command(seq=6, command=CommandId.SET_PID_REQUESTED, percent=2)
  126. invalid = decode_frame(
  127. encode_frame(
  128. profile_id=3,
  129. frame_type=FrameType.HOST_CMD,
  130. seq=6,
  131. timestamp_tick=0,
  132. payload=bytes([CommandId.SET_PID_REQUESTED, 2]),
  133. )
  134. )
  135. self.assertEqual(frame_to_dict(invalid)["decoded"]["error"], "invalid_enabled_flag")
  136. def test_zero_payload_commands_have_stable_binary_payloads(self) -> None:
  137. for command in [
  138. CommandId.GET_SENSOR,
  139. CommandId.GET_MAP,
  140. CommandId.GET_STATUS,
  141. CommandId.DISABLE,
  142. ]:
  143. with self.subTest(command=command):
  144. frame = decode_frame(build_host_command(seq=7, command=command))
  145. self.assertEqual(frame.payload, bytes([command]))
  146. def test_frame_to_dict_decodes_ready_host_command_and_ack_payloads(self) -> None:
  147. ready = decode_frame(
  148. encode_frame(
  149. profile_id=1,
  150. frame_type=FrameType.READY,
  151. seq=1,
  152. timestamp_tick=2,
  153. payload=bytes([1]) + (0x1234ABCD).to_bytes(4, "little") + bytes([1, 2]),
  154. )
  155. )
  156. command = decode_frame(build_host_command(seq=9, command=CommandId.SET_PTC, channel=1, percent=10))
  157. ack = decode_frame(
  158. encode_frame(
  159. profile_id=1,
  160. frame_type=FrameType.HOST_ACK,
  161. seq=9,
  162. timestamp_tick=12,
  163. payload=bytes([1, CommandId.SET_PTC, 0, 0, 0x34, 0x12]),
  164. )
  165. )
  166. self.assertEqual(
  167. frame_to_dict(ready)["decoded"],
  168. {
  169. "kind": "ready",
  170. "profile_id_payload": 1,
  171. "build_id": "1234ABCD",
  172. "startup_epoch": 1,
  173. "startup_tick": 2,
  174. "por_reason": 1,
  175. "announcement_seq": 2,
  176. },
  177. )
  178. self.assertEqual(
  179. frame_to_dict(command)["decoded"],
  180. {"kind": "host_cmd", "cmd": "SET_PTC", "cmd_id": 32, "channel": 1, "percent": 10},
  181. )
  182. self.assertEqual(
  183. frame_to_dict(ack)["decoded"],
  184. {
  185. "kind": "host_ack",
  186. "ok": True,
  187. "cmd": "SET_PTC",
  188. "cmd_id": 32,
  189. "status_code": 0,
  190. "applied_state_hash": 0x1234,
  191. },
  192. )
  193. def test_production_two_uart_profile_has_stable_id_three(self) -> None:
  194. profile = "production-two-uart-bmc-replay-binary921"
  195. alias = "prod2u-bmc-replay-binary921"
  196. self.assertEqual(profile_id_for_name(profile), 3)
  197. self.assertEqual(profile_id_for_name(alias), 3)
  198. self.assertEqual(profile_name_for_id(3), profile)
  199. frame = decode_frame(
  200. build_host_command(
  201. seq=1,
  202. command=CommandId.SET_PTC,
  203. channel=1,
  204. percent=10,
  205. profile_id=profile_id_for_name(profile),
  206. )
  207. )
  208. self.assertEqual(frame.profile_id, 3)
  209. self.assertEqual(frame.payload, bytes([CommandId.SET_PTC, 1, 10]))
  210. def test_module_snapshot_decodes_pwm_pid_and_timeout_summary(self) -> None:
  211. payload = bytes(
  212. [
  213. 3, # profile
  214. 0, # host_err_count
  215. 12,
  216. 0, # command_count_seen
  217. 0x1E,
  218. 0xFF,
  219. 0x08,
  220. 0x14,
  221. CommandId.DISABLE,
  222. 0,
  223. 0,
  224. 0,
  225. 0, # pid requested false
  226. 1, # timeout active
  227. 2,
  228. 22,
  229. 0,
  230. 0,
  231. 30,
  232. 30,
  233. 0,
  234. 0x0F,
  235. 0,
  236. 0,
  237. ]
  238. )
  239. frame = decode_frame(
  240. encode_frame(profile_id=3, frame_type=FrameType.MODULE_SNAPSHOT, seq=9, timestamp_tick=10, payload=payload)
  241. )
  242. decoded = frame_to_dict(frame)["decoded"]
  243. self.assertEqual(decoded["kind"], "module_snapshot")
  244. self.assertEqual(decoded["profile_id_payload"], 3)
  245. self.assertEqual(decoded["command_count_seen"], 12)
  246. self.assertEqual(decoded["last_cmd"], "DISABLE")
  247. self.assertFalse(decoded["pid_requested"])
  248. self.assertTrue(decoded["timeout_active"])
  249. self.assertEqual(decoded["ptc01_percent"], 0)
  250. self.assertEqual(decoded["ptc24_percent"], 0)
  251. self.assertEqual(decoded["fan01_percent"], 30)
  252. self.assertEqual(decoded["fan04_percent"], 30)
  253. if __name__ == "__main__":
  254. unittest.main()