3232class PcscBackend :
3333 """Backend adapter that wraps a PC/SC reader."""
3434
35- ntag_application_df_name : ClassVar [list [int ]] = [
36- 0xD2 ,
37- 0x76 ,
38- 0x00 ,
39- 0x00 ,
40- 0x85 ,
41- 0x01 ,
42- 0x01 ,
43- ]
44- ndef_file_no : ClassVar [int ] = 0x02
4535 ndef_length_header_size : ClassVar [int ] = 2
4636 ntag424_key_slots : ClassVar [int ] = 5
4737 type2_cc_page : ClassVar [int ] = 3
@@ -82,9 +72,19 @@ def connect(self) -> PcscConnection:
8272 """Connect to the wrapped PC/SC reader."""
8373 return self .client .connect ()
8474
85- def send_apdu (self , apdu : CommandAPDU | list [int ]) -> ResponseAPDU :
75+ def send_apdu (
76+ self ,
77+ apdu : CommandAPDU | list [int ],
78+ * ,
79+ check_status : bool = True ,
80+ ok_statuses : tuple [tuple [int , int ], ...] | None = None ,
81+ ) -> ResponseAPDU :
8682 """Transmit an APDU through the wrapped PC/SC reader."""
87- return self .client .send_apdu (apdu )
83+ return self .client .send_apdu (
84+ apdu ,
85+ check_status = check_status ,
86+ ok_statuses = ok_statuses ,
87+ )
8888
8989 def read_profile (self ) -> NtagProfile :
9090 """Read the currently reachable NTAG profile."""
@@ -95,7 +95,7 @@ def read_profile(self) -> NtagProfile:
9595 return self ._read_type2_profile (uid )
9696
9797 file_settings = Ntag424FileSettings .from_response (
98- self ._get_file_settings (self .ndef_file_no ),
98+ self ._get_file_settings (Ntag424ApduPreset .ndef_file_no ),
9999 )
100100 sections = Ntag424ProfileSections .from_parsed_data (
101101 file_settings = file_settings ,
@@ -127,22 +127,23 @@ def apply_plan(self, plan: ChangePlan) -> Ntag424DnaProfile:
127127
128128 def _read_uid (self ) -> str :
129129 """Read UID using the common PC/SC contactless reader command."""
130- response = self .send_apdu (PcscContactlessApduPreset .get_uid ())
130+ response = self .send_apdu (
131+ PcscContactlessApduPreset .get_uid (),
132+ check_status = False ,
133+ )
131134 if not response .ok :
132135 msg = f"Unable to read tag UID: status { response .status :#x} "
133136 raise self .UnsupportedProfileReadError (msg )
134137 return bytes (response .data ).hex ().upper ()
135138
136139 def _select_ntag_application (self ) -> None :
137140 """Select the NTAG 424 DNA application by DF name."""
138- self .client .send_checked (
139- Ntag424ApduPreset .select_application (self .ntag_application_df_name ),
140- )
141+ self .send_apdu (Ntag424ApduPreset .select_application ())
141142
142143 def _read_ndef_profile (self ) -> NdefProfile :
143144 """Read and parse the NDEF file into profile records."""
144145 length_data = self ._read_data_file (
145- file_no = self .ndef_file_no ,
146+ file_no = Ntag424ApduPreset .ndef_file_no ,
146147 offset = 0 ,
147148 length = self .ndef_length_header_size ,
148149 )
@@ -155,7 +156,7 @@ def _read_ndef_profile(self) -> NdefProfile:
155156 return NdefProfile (present = False )
156157
157158 message = self ._read_data_file (
158- file_no = self .ndef_file_no ,
159+ file_no = Ntag424ApduPreset .ndef_file_no ,
159160 offset = 2 ,
160161 length = ndef_length ,
161162 )
@@ -168,17 +169,19 @@ def _read_ndef_profile(self) -> NdefProfile:
168169 def _read_data_file (self , * , file_no : int , offset : int , length : int ) -> list [int ]:
169170 """Read bytes from an NTAG 424 DNA data file."""
170171 offset_bytes = int_to_3bytes_le (offset )
171- return self .client . send_checked (
172+ return self .send_apdu (
172173 Ntag424ApduPreset .read_data_file (
173174 file_no = file_no ,
174175 offset = offset_bytes ,
175176 length = int_to_3bytes_le (length ),
176177 ),
177- )
178+ ). data
178179
179180 def _get_file_settings (self , file_no : int ) -> list [int ]:
180181 """Send GetFileSettings for an NTAG 424 DNA file."""
181- return self .client .send_checked (Ntag424ApduPreset .get_file_settings (file_no ))
182+ return self .send_apdu (
183+ Ntag424ApduPreset .get_file_settings (file_no ),
184+ ).data
182185
183186 def _get_key_versions (self ) -> list [int ]:
184187 """Send GetKeyVersion for all NTAG 424 DNA application key slots."""
@@ -188,7 +191,7 @@ def _get_key_versions(self) -> list[int]:
188191
189192 def _get_key_version (self , key_no : int ) -> int :
190193 """Send GetKeyVersion for one NTAG 424 DNA application key."""
191- data = self .client . send_checked (Ntag424ApduPreset .get_key_version (key_no ))
194+ data = self .send_apdu (Ntag424ApduPreset .get_key_version (key_no )). data
192195 if len (data ) != 1 :
193196 msg = "NTAG 424 DNA key version response must be 1 byte"
194197 raise self .UnsupportedProfileReadError (msg )
@@ -236,12 +239,12 @@ def _detect_type2_tag_type(self, capacity_bytes: int) -> TagType:
236239
237240 def _read_type2_page (self , page : int ) -> list [int ]:
238241 """Read one Type 2 Tag page using the PC/SC READ BINARY command."""
239- data = self .client . send_checked (
242+ data = self .send_apdu (
240243 PcscContactlessApduPreset .read_binary (
241244 page = page ,
242245 length = self .type2_page_size ,
243246 ),
244- )
247+ ). data
245248 if len (data ) != self .type2_page_size :
246249 msg = "Type 2 Tag page reads must return 4 bytes"
247250 raise self .NdefParseError (msg )
0 commit comments