test_nfc_reader.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """Tests for daemon.nfc_reader — tray UUID extraction and raw blocks from Bambu tag blocks."""
  2. from unittest.mock import MagicMock
  3. from daemon.nfc_reader import NFCReader, NFCState, _extract_tray_uuid, _raw_blocks, choose_ndef
  4. # Measured on a real spool: block 9 equals the tray_uuid the AMS reports (#984).
  5. BLOCK9 = bytes.fromhex("9E0B0717BEE94D7887EB1D8DFD1A14F3")
  6. # Block 4 of a "PLA Matte" spool -- what the old blocks 4+5 read sent as tray_uuid.
  7. BLOCK4_PLA_MATTE = b"PLA Matte" + b"\x00" * 7
  8. class TestExtractTrayUuid:
  9. def test_reads_block_9(self):
  10. blocks = {1: b"\x01" * 16, 2: b"\x02" * 16, 4: BLOCK4_PLA_MATTE, 5: b"\x00" * 16, 9: BLOCK9}
  11. assert _extract_tray_uuid(blocks) == "9E0B0717BEE94D7887EB1D8DFD1A14F3"
  12. def test_ignores_blocks_4_and_5(self):
  13. """Without block 9 there is no tray UUID, never the filament type from block 4."""
  14. blocks = {4: BLOCK4_PLA_MATTE, 5: b"\x00" * 16}
  15. assert _extract_tray_uuid(blocks) is None
  16. def test_all_zero_block_9_is_no_uuid(self):
  17. assert _extract_tray_uuid({9: b"\x00" * 16}) is None
  18. def test_short_block_9_is_no_uuid(self):
  19. assert _extract_tray_uuid({9: b"\x9e\x0b"}) is None
  20. def test_empty_blocks(self):
  21. assert _extract_tray_uuid({}) is None
  22. class TestRawBlocks:
  23. def test_blocks_as_uppercase_hex_keyed_by_block_number(self):
  24. assert _raw_blocks({9: BLOCK9, 4: BLOCK4_PLA_MATTE}) == {
  25. "4": "504C41204D6174746500000000000000",
  26. "9": "9E0B0717BEE94D7887EB1D8DFD1A14F3",
  27. }
  28. def test_empty_blocks(self):
  29. assert _raw_blocks({}) == {}
  30. class TestChooseNdef:
  31. """#3500: OpenTag3D v2 only where the tag has room for it."""
  32. V1 = b"\x03" + bytes(128) # 129 bytes, fits every NTAG
  33. V2 = b"\x03" + bytes(188) # 189 bytes, more than an NTAG213 holds
  34. def test_ntag215_gets_v2(self):
  35. assert choose_ndef(bytes([0xE1, 0x10, 0x3E, 0x00]), self.V1, self.V2) == (self.V2, "v2")
  36. def test_ntag216_gets_v2(self):
  37. assert choose_ndef(bytes([0xE1, 0x10, 0x6D, 0x00]), self.V1, self.V2) == (self.V2, "v2")
  38. def test_ntag213_gets_v1(self):
  39. assert choose_ndef(bytes([0xE1, 0x10, 0x12, 0x00]), self.V1, self.V2) == (self.V1, "v1")
  40. def test_a_cc_that_is_not_ndef_gets_v1(self):
  41. assert choose_ndef(bytes([0x00, 0x00, 0x3E, 0x00]), self.V1, self.V2) == (self.V1, "v1")
  42. def test_an_unread_cc_gets_v1(self):
  43. assert choose_ndef(None, self.V1, self.V2) == (self.V1, "v1")
  44. assert choose_ndef(b"\xe1", self.V1, self.V2) == (self.V1, "v1")
  45. class TestWriteNtag:
  46. UID = bytes.fromhex("04A1B2C3D4E5F6")
  47. def _reader(self, cc: bytes | None = None, read_error: Exception | None = None):
  48. reader = NFCReader.__new__(NFCReader)
  49. reader._state = NFCState.TAG_PRESENT
  50. reader._current_uid = self.UID.hex().upper()
  51. reader._current_sak = 0x00
  52. nfc = MagicMock()
  53. nfc.reactivate_card.return_value = (self.UID, 0x00)
  54. if read_error is not None:
  55. nfc.ntag_read_pages.side_effect = read_error
  56. else:
  57. nfc.ntag_read_pages.return_value = cc
  58. nfc.ntag_write_pages.return_value = True
  59. reader._nfc = nfc
  60. return reader, nfc
  61. def _written(self, nfc) -> bytes:
  62. return nfc.ntag_write_pages.call_args.kwargs["data"]
  63. def test_an_ntag215_is_written_v2(self):
  64. reader, nfc = self._reader(cc=bytes([0xE1, 0x10, 0x3E, 0x00]))
  65. ok, msg = reader.write_ntag(TestChooseNdef.V1, TestChooseNdef.V2)
  66. assert ok
  67. assert "v2" in msg
  68. nfc.ntag_read_pages.assert_called_once_with(start_page=3, num_pages=1)
  69. assert self._written(nfc) == TestChooseNdef.V2
  70. assert nfc.ntag_write_pages.call_args.kwargs["start_page"] == 4
  71. def test_an_ntag213_is_written_v1(self):
  72. reader, nfc = self._reader(cc=bytes([0xE1, 0x10, 0x12, 0x00]))
  73. ok, msg = reader.write_ntag(TestChooseNdef.V1, TestChooseNdef.V2)
  74. assert ok
  75. assert "v1" in msg
  76. assert self._written(nfc) == TestChooseNdef.V1
  77. def test_a_failed_cc_read_writes_v1(self):
  78. reader, nfc = self._reader(read_error=RuntimeError("BUSY timeout"))
  79. ok, _ = reader.write_ntag(TestChooseNdef.V1, TestChooseNdef.V2)
  80. assert ok
  81. assert self._written(nfc) == TestChooseNdef.V1
  82. def test_without_v2_the_tag_is_not_read_first(self):
  83. reader, nfc = self._reader()
  84. ok, _ = reader.write_ntag(TestChooseNdef.V1)
  85. assert ok
  86. nfc.ntag_read_pages.assert_not_called()
  87. assert self._written(nfc) == TestChooseNdef.V1