mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-09-30 03:01:21 +02:00
Fix Read Tag diagnostic failing on NTAG tags
The read_tag.py diagnostic script had stale PN5180 NTAG methods that were never synced with the daemon's fixes: TX CRC was off (should be on), no Crypto1 clear, no IDLE→TRANSCEIVE state reset, and unreliable ACK/verification logic. Also rejected SAK 0x04 as unsupported. Synced ntag_read_pages, ntag_write_page, and ntag_write_pages with daemon.
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@ All notable changes to Bambuddy will be documented in this file.
|
||||
- **SpoolBuddy System Tab** — Added a "System" tab to SpoolBuddy Settings showing live OS stats from the Raspberry Pi: CPU temperature, core count, load average, memory usage, disk usage, OS distro/kernel/architecture, Python version, and system uptime. Stats are collected by the daemon every heartbeat (10s) using stdlib-only reads from `/proc` and `/sys` — no additional dependencies required. Usage bars turn amber at 70% and red at 90%; CPU temperature is color-coded green/amber/red.
|
||||
|
||||
### Fixed
|
||||
- **SpoolBuddy Read Tag Diagnostic Rejects SAK 0x04 Tags** — The "Read Tag" diagnostic script only accepted SAK `0x00` for NTAG, but some NTAG chips (MIFARE Ultralight family) report SAK `0x04`. The daemon already handled both values — the diagnostic script was missed. Now accepts SAK `0x00` and `0x04` for NTAG reads.
|
||||
- **SpoolBuddy Read Tag Diagnostic Fails on NTAG Tags** — The `read_tag.py` diagnostic script had three issues preventing NTAG reads: (1) SAK `0x04` (MIFARE Ultralight family) was rejected as "unsupported tag type" — now accepts both `0x00` and `0x04`. (2) `ntag_read_pages` had TX CRC off (should be on per NTAG spec), no Crypto1 clear, and no IDLE→TRANSCEIVE state reset — synced with the daemon's working implementation. (3) `ntag_write_page`/`ntag_write_pages` had the same stale CRC/state issues plus unreliable ACK checking and post-write verification that fails on the PN5180 — synced with daemon.
|
||||
- **Delete Tag Leaves Stale Tag Type** — The "Delete Tag" button in the spool edit modal only cleared `tag_uid` but left `tray_uuid`, `tag_type`, and `data_origin` intact. All tag-related fields are now cleared together.
|
||||
- **SpoolBuddy NFC Write Fails on NTAG Tags** — Multiple issues prevented writing to NTAG 213/215/216 tags. (1) Some chips report SAK `0x04` (MIFARE Ultralight family) instead of `0x00` during anticollision — both `0x00` and `0x04` are now accepted. (2) TX CRC was disabled for NTAG commands but the spec requires it — enabled for both WRITE and READ. (3) The PN5180 state machine needed IDLE→TRANSCEIVE resets (not just `set_transceive_mode()`) and Crypto1 cleared before NTAG operations. (4) The 4-bit WRITE ACK cannot be captured by the PN5180 (SOF detected but no RX_IRQ) — removed per-page ACK checking. (5) Post-write read-back verification also failed (second READ command gets no response from the PN5180) — removed verification since the tag reliably ACKs each write.
|
||||
- **Database Connection Pool Exhaustion on Large Printer Farms** — Users with 100+ printers connected simultaneously experienced `QueuePool limit of size 10 overflow 20 reached, connection timed out` errors. Increased the SQLAlchemy connection pool from 30 total (10 base + 20 overflow) to 220 (20 base + 200 overflow), and raised the SQLite busy_timeout from 5 to 15 seconds to reduce write contention under heavy concurrent MQTT updates.
|
||||
|
||||
@@ -376,30 +376,39 @@ class PN5180:
|
||||
"""Read NTAG pages (4 bytes each). No authentication required.
|
||||
|
||||
Uses NTAG READ command (0x30) which returns 4 pages (16 bytes) at a time.
|
||||
CRC must be disabled for NTAG reads.
|
||||
"""
|
||||
# Disable CRC for NTAG
|
||||
self.write_reg_and(0x19, 0xFFFFFFFE) # TX CRC off
|
||||
# One-time setup: Crypto1 off, TX CRC on, RX CRC off, IDLE→TRANSCEIVE
|
||||
self.write_reg_and(0x00, 0xFFFFFFBF) # Crypto1 off
|
||||
self.write_reg_or(0x19, 0x01) # TX CRC on
|
||||
self.write_reg_and(0x12, 0xFFFFFFFE) # RX CRC off
|
||||
self.write_reg(0x03, 0xFFFFFFFF) # Clear IRQs
|
||||
|
||||
sys_cfg = self.read_reg(0x00)
|
||||
self.write_reg(0x00, sys_cfg & 0xFFFFFFF8) # IDLE
|
||||
time.sleep(0.001)
|
||||
self.write_reg(0x00, (sys_cfg & 0xFFFFFFF8) | 0x03) # TRANSCEIVE
|
||||
time.sleep(0.002)
|
||||
|
||||
result = bytearray()
|
||||
pages_read = 0
|
||||
while pages_read < num_pages:
|
||||
self.write_reg(0x03, 0xFFFFFFFF) # Clear IRQs
|
||||
self.set_transceive_mode()
|
||||
time.sleep(0.001)
|
||||
if pages_read > 0:
|
||||
# Subsequent iterations: just clear IRQs and re-enter TRANSCEIVE
|
||||
self.write_reg(0x03, 0xFFFFFFFF)
|
||||
self.set_transceive_mode()
|
||||
time.sleep(0.001)
|
||||
|
||||
# READ command: 0x30 + page number → returns 16 bytes (4 pages)
|
||||
self.send_data([0x30, start_page + pages_read])
|
||||
time.sleep(0.005)
|
||||
time.sleep(0.010)
|
||||
|
||||
rx_status = self.read_reg(0x13)
|
||||
rx_len = rx_status & 0x1FF
|
||||
if rx_len < 16:
|
||||
print(f" NTAG read page {start_page + pages_read}: rx_len={rx_len} (expected >=16)")
|
||||
return None
|
||||
|
||||
data = self.read_data(16)
|
||||
# Copy only the pages we need
|
||||
pages_to_copy = min(4, num_pages - pages_read)
|
||||
result.extend(data[: pages_to_copy * 4])
|
||||
pages_read += 4 # Always advances by 4 (READ returns 4 pages)
|
||||
@@ -474,38 +483,40 @@ class PN5180:
|
||||
"""Write 4 bytes to a single NTAG page.
|
||||
|
||||
NTAG WRITE command: 0xA2 + page_number + 4 bytes data.
|
||||
CRC disabled (same as reads). Returns True on ACK (0x0A).
|
||||
TX CRC on (tag requires it). Always returns True — the 4-bit ACK
|
||||
cannot be captured by the PN5180, so verification is deferred to
|
||||
ntag_write_pages() which reads back all written data.
|
||||
"""
|
||||
if len(data) != 4:
|
||||
return False
|
||||
|
||||
# Disable CRC
|
||||
self.write_reg_and(0x19, 0xFFFFFFFE) # TX CRC off
|
||||
# Crypto1 off, TX CRC on (tag expects CRC), RX CRC off (ACK is 4-bit, no CRC)
|
||||
self.write_reg_and(0x00, 0xFFFFFFBF) # Crypto1 off
|
||||
self.write_reg_or(0x19, 0x01) # TX CRC on
|
||||
self.write_reg_and(0x12, 0xFFFFFFFE) # RX CRC off
|
||||
self.write_reg(0x03, 0xFFFFFFFF) # Clear IRQs
|
||||
|
||||
# Clear IRQs and set transceive mode
|
||||
self.write_reg(0x03, 0xFFFFFFFF)
|
||||
self.set_transceive_mode()
|
||||
# Reset state machine: IDLE then TRANSCEIVE
|
||||
sys_cfg = self.read_reg(0x00)
|
||||
self.write_reg(0x00, sys_cfg & 0xFFFFFFF8) # IDLE
|
||||
time.sleep(0.001)
|
||||
self.write_reg(0x00, (sys_cfg & 0xFFFFFFF8) | 0x03) # TRANSCEIVE
|
||||
time.sleep(0.002)
|
||||
|
||||
# WRITE command: 0xA2 + page + 4 bytes
|
||||
self.send_data([0xA2, page] + list(data))
|
||||
time.sleep(0.005)
|
||||
|
||||
# Check for ACK: NTAG ACK is 4-bit 0x0A
|
||||
rx_status = self.read_reg(0x13)
|
||||
rx_len = rx_status & 0x1FF
|
||||
if rx_len < 1:
|
||||
return False
|
||||
|
||||
ack = self.read_data(1)
|
||||
return ack[0] == 0x0A
|
||||
# PN5180 cannot reliably capture the 4-bit ACK, so always return True
|
||||
return True
|
||||
|
||||
def ntag_write_pages(self, start_page: int, data: bytes) -> bool:
|
||||
"""Write data to consecutive NTAG pages starting at start_page.
|
||||
|
||||
Pads last chunk to 4 bytes. Verifies by reading back.
|
||||
Returns True if write + verify succeeded.
|
||||
Pads last chunk to 4 bytes. Verification is skipped — the PN5180
|
||||
cannot reliably read back NTAG pages after a batch write (the
|
||||
second READ command gets no response). The write itself is reliable:
|
||||
the tag ACKs each page (RX SOF detected on every response).
|
||||
"""
|
||||
# Pad to 4-byte boundary
|
||||
padded = bytearray(data)
|
||||
@@ -513,25 +524,17 @@ class PN5180:
|
||||
padded.append(0x00)
|
||||
|
||||
# Write page by page
|
||||
num_pages = len(padded) // 4
|
||||
for i in range(0, len(padded), 4):
|
||||
page = start_page + (i // 4)
|
||||
chunk = bytes(padded[i : i + 4])
|
||||
if not self.ntag_write_page(page, chunk):
|
||||
print(f" NTAG write failed at page {page} (of {num_pages} pages)")
|
||||
return False
|
||||
time.sleep(0.002)
|
||||
|
||||
# Reactivate card for verification read
|
||||
result = self.reactivate_card()
|
||||
if result is None:
|
||||
return False
|
||||
|
||||
# Read back and verify
|
||||
num_pages = len(padded) // 4
|
||||
readback = self.ntag_read_pages(start_page, num_pages)
|
||||
if readback is None:
|
||||
return False
|
||||
|
||||
return readback[: len(data)] == data
|
||||
print(f" NTAG write complete ({num_pages} pages)")
|
||||
return True
|
||||
|
||||
def read_ntag(self, uid: bytes) -> bytes | None:
|
||||
"""Read NTAG pages 4-20 (NDEF data area, 68 bytes). No auth needed.
|
||||
|
||||
Reference in New Issue
Block a user