@@ -128,6 +128,113 @@ pub async fn rfid_task(
128128 continue ;
129129 }
130130
131+ if unsafe { crate :: state:: UNSIGN_CARDS_MODE } {
132+ let fkm_token = unsafe { crate :: state:: FKM_TOKEN } ;
133+ let mut key = [ 0 ; 6 ] ;
134+ key[ ..4 ] . copy_from_slice ( & fkm_token. to_be_bytes ( ) ) ;
135+
136+ let status = mfrc522
137+ . pcd_authenticate (
138+ esp_hal_mfrc522:: consts:: PICCCommand :: PICC_CMD_MF_AUTH_KEY_A ,
139+ 63 ,
140+ & key,
141+ & card,
142+ )
143+ . await ;
144+
145+ log:: debug!( "unsigning auth status: {status:?}" ) ;
146+ if status. is_ok ( ) {
147+ let buff = [ 0 ; 16 ] ;
148+ _ = mfrc522. mifare_write ( 62 , & buff, 16 ) . await ;
149+
150+ let mut buff = [ 0 ; 18 ] ;
151+ buff[ ..6 ] . copy_from_slice ( & [ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ] ) ;
152+ buff[ 6 ] = 0xFF ;
153+ buff[ 7 ] = 0x07 ;
154+ buff[ 8 ] = 0x80 ;
155+ buff[ 9 ] = 0x69 ;
156+ buff[ 10 ..16 ] . copy_from_slice ( & [ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ] ) ;
157+
158+ let res = mfrc522. mifare_write ( 63 , & buff, 16 ) . await ;
159+ if let Err ( e) = res {
160+ log:: error!( "cannot unsecure card error: {e:?}" ) ;
161+ _ = mfrc522. picc_halta ( ) . await ;
162+ _ = mfrc522. pcd_stop_crypto1 ( ) . await ;
163+ continue ;
164+ }
165+ }
166+
167+ _ = mfrc522. picc_halta ( ) . await ;
168+ _ = mfrc522. pcd_stop_crypto1 ( ) . await ;
169+ continue ;
170+ }
171+
172+ if unsafe { crate :: state:: SIGN_CARDS_MODE } {
173+ let fkm_token = unsafe { crate :: state:: FKM_TOKEN } ;
174+
175+ let status = mfrc522
176+ . pcd_authenticate (
177+ esp_hal_mfrc522:: consts:: PICCCommand :: PICC_CMD_MF_AUTH_KEY_A ,
178+ 63 ,
179+ & [ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ] ,
180+ & card,
181+ )
182+ . await ;
183+
184+ let mut key = [ 0 ; 6 ] ;
185+ key[ ..4 ] . copy_from_slice ( & fkm_token. to_be_bytes ( ) ) ;
186+
187+ log:: debug!( "signing auth status: {status:?}" ) ;
188+ if status. is_ok ( ) {
189+ let mut buff = [ 0 ; 18 ] ;
190+
191+ buff[ ..6 ] . copy_from_slice ( & key) ;
192+ buff[ 6 ] = 0xFF ;
193+ buff[ 7 ] = 0x07 ;
194+ buff[ 8 ] = 0x80 ;
195+ buff[ 9 ] = 0x69 ;
196+ buff[ 10 ..16 ] . copy_from_slice ( & key) ;
197+
198+ let res = mfrc522. mifare_write ( 63 , & buff, 16 ) . await ;
199+ if let Err ( e) = res {
200+ log:: error!( "write res: {e:?}" ) ;
201+ _ = mfrc522. picc_halta ( ) . await ;
202+ _ = mfrc522. pcd_stop_crypto1 ( ) . await ;
203+ continue ;
204+ }
205+
206+ //_ = mfrc522.pcd_stop_crypto1().await;
207+ let status = mfrc522
208+ . pcd_authenticate (
209+ esp_hal_mfrc522:: consts:: PICCCommand :: PICC_CMD_MF_AUTH_KEY_A ,
210+ 63 ,
211+ & key,
212+ & card,
213+ )
214+ . await ;
215+ if status. is_err ( ) {
216+ log:: error!( "Cannot auth card!" ) ;
217+ _ = mfrc522. picc_halta ( ) . await ;
218+ _ = mfrc522. pcd_stop_crypto1 ( ) . await ;
219+ continue ;
220+ }
221+
222+ let mut buff = [ 0 ; 16 ] ;
223+ buff[ ..16 ] . clone_from_slice ( & card_uid. to_be_bytes ( ) ) ;
224+ let res = mfrc522. mifare_write ( 62 , & buff, 16 ) . await ;
225+ if res. is_err ( ) {
226+ log:: error!( "Cannot write secured rfid info!" ) ;
227+ _ = mfrc522. picc_halta ( ) . await ;
228+ _ = mfrc522. pcd_stop_crypto1 ( ) . await ;
229+ continue ;
230+ }
231+ }
232+
233+ _ = mfrc522. picc_halta ( ) . await ;
234+ _ = mfrc522. pcd_stop_crypto1 ( ) . await ;
235+ continue ;
236+ }
237+
131238 if unsafe { crate :: state:: SECURE_RFID } {
132239 let fkm_token = unsafe { crate :: state:: FKM_TOKEN } ;
133240 let mut key = [ 0 ; 6 ] ;
@@ -169,37 +276,6 @@ pub async fn rfid_task(
169276 }
170277 }
171278
172- // TODO: WRITE SECURED
173- /*
174- let status = mfrc522
175- .pcd_authenticate(
176- esp_hal_mfrc522::consts::PICCCommand::PICC_CMD_MF_AUTH_KEY_A,
177- 63,
178- &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
179- &card,
180- )
181- .await;
182- log::info!("auth status: {status:?}");
183- if status.is_ok() {
184- let mut buff = [0; 18];
185- let mut byte_count = 18;
186- let res = mfrc522.mifare_read(63, &mut buff, &mut byte_count).await;
187- log::info!("read: {res:?}, data: {buff:#?}");
188-
189- let mut custom_key = [0x69, 0x42, 0x00, 0x13, 0x56, 0xAE];
190-
191- buff[..6].copy_from_slice(&custom_key);
192- buff[6] = 0xFF;
193- buff[7] = 0x07;
194- buff[8] = 0x80;
195- buff[9] = 0x69;
196- buff[10..16].copy_from_slice(&custom_key);
197-
198- let res = mfrc522.mifare_write(63, &buff, 16).await;
199- log::info!("write res: {res:?}");
200- }
201- */
202-
203279 let resp = crate :: ws:: send_request :: < CardInfoResponsePacket > (
204280 crate :: structs:: TimerPacketInner :: CardInfoRequest {
205281 card_id : card_uid as u64 ,
@@ -237,7 +313,9 @@ pub async fn rfid_task(
237313 #[ cfg( not( feature = "e2e" ) ) ]
238314 {
239315 _ = mfrc522. picc_halta ( ) . await ;
240- //_ = mfrc522.pcd_stop_crypto1().await;
316+ if unsafe { crate :: state:: SECURE_RFID } {
317+ _ = mfrc522. pcd_stop_crypto1 ( ) . await ;
318+ }
241319 }
242320 }
243321}
0 commit comments