LibDriver MIFARE_ULTRALIGHT
Loading...
Searching...
No Matches
driver_mifare_ultralight.c
Go to the documentation of this file.
1
36
38
42#define CHIP_NAME "NXP Ultralight"
43#define MANUFACTURER_NAME "NXP"
44#define SUPPLY_VOLTAGE_MIN 3.3f
45#define SUPPLY_VOLTAGE_MAX 4.0f
46#define MAX_CURRENT 30.0f
47#define TEMPERATURE_MIN -25.0f
48#define TEMPERATURE_MAX 70.0f
49#define DRIVER_VERSION 1000
50
54#define MIFARE_ULTRALIGHT_COMMAND_REQUEST 0x26
55#define MIFARE_ULTRALIGHT_COMMAND_WAKE_UP 0x52
56#define MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL1 0x9320U
57#define MIFARE_ULTRALIGHT_COMMAND_SELECT_CL1 0x9370U
58#define MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL2 0x9520U
59#define MIFARE_ULTRALIGHT_COMMAND_SELECT_CL2 0x9570U
60#define MIFARE_ULTRALIGHT_COMMAND_HALT 0x5000U
61#define MIFARE_ULTRALIGHT_COMMAND_GET_VERSION 0x60
62#define MIFARE_ULTRALIGHT_COMMAND_READ 0x30
63#define MIFARE_ULTRALIGHT_COMMAND_FAST_READ 0x3A
64#define MIFARE_ULTRALIGHT_COMMAND_WRITE 0xA2
65#define MIFARE_ULTRALIGHT_COMMAND_COMP_WRITE 0xA0
66#define MIFARE_ULTRALIGHT_COMMAND_READ_CNT 0x39
67#define MIFARE_ULTRALIGHT_COMMAND_INCR_CNT 0xA5
68#define MIFARE_ULTRALIGHT_COMMAND_PWD_AUTH 0x1B
69#define MIFARE_ULTRALIGHT_COMMAND_READ_SIG 0x3C
70#define MIFARE_ULTRALIGHT_COMMAND_CHECK_TEARING_EVENT 0x3E
71#define MIFARE_ULTRALIGHT_COMMAND_VCSL 0x4B
72
79static void a_mifare_ultralight_iso14443a_crc(uint8_t *p, uint8_t len, uint8_t output[2])
80{
81 uint32_t w_crc = 0x6363;
82
83 do
84 {
85 uint8_t bt;
86
87 bt = *p++; /* get one byte */
88 bt = (bt ^ (uint8_t)(w_crc & 0x00FF)); /* xor */
89 bt = (bt ^ (bt << 4)); /* xor */
90 w_crc = (w_crc >> 8) ^ ((uint32_t) bt << 8) ^ ((uint32_t) bt << 3) ^ ((uint32_t) bt >> 4); /* get the crc */
91 } while (--len); /* len-- */
92
93 output[0] = (uint8_t)(w_crc & 0xFF); /* lsb */
94 output[1] = (uint8_t)((w_crc >> 8) & 0xFF); /* msb */
95}
96
107static uint8_t a_mifare_ultralight_conf_read(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
108{
109 uint8_t res;
110 uint8_t input_len;
111 uint8_t input_buf[5];
112 uint8_t output_len;
113 uint8_t output_buf[6];
114 uint8_t crc_buf[2];
115
116 input_len = 5; /* set the input length */
117 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_FAST_READ; /* set the command */
118 input_buf[1] = page; /* set the start page */
119 input_buf[2] = page; /* set the stop page */
120 a_mifare_ultralight_iso14443a_crc(input_buf , 3, input_buf + 3); /* get the crc */
121 output_len = 6; /* set the output length */
122 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
123 if (res != 0) /* check the result */
124 {
125 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
126
127 return 1; /* return error */
128 }
129 if (output_len != 6) /* check the output_len */
130 {
131 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
132
133 return 1; /* return error */
134 }
135 a_mifare_ultralight_iso14443a_crc(output_buf, 4, crc_buf); /* get the crc */
136 if ((output_buf[4] == crc_buf[0]) && (output_buf[5] == crc_buf[1])) /* check the crc */
137 {
138 memcpy(data, output_buf, 4); /* copy the data */
139
140 return 0; /* success return 0 */
141 }
142 else
143 {
144 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
145
146 return 1; /* return error */
147 }
148}
149
160static uint8_t a_mifare_ultralight_conf_write(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
161{
162 uint8_t res;
163 uint8_t input_len;
164 uint8_t input_buf[8];
165 uint8_t output_len;
166 uint8_t output_buf[1];
167
168 input_len = 8; /* set the input length */
169 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WRITE; /* set the command */
170 input_buf[1] = page; /* set the setting page */
171 input_buf[2] = data[0]; /* set data0 */
172 input_buf[3] = data[1]; /* set data1 */
173 input_buf[4] = data[2]; /* set data2 */
174 input_buf[5] = data[3]; /* set data3 */
175 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
176 output_len = 1; /* set the output length */
177 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
178 if (res != 0) /* check the result */
179 {
180 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
181
182 return 1; /* return error */
183 }
184 if (output_len != 1) /* check the output_len */
185 {
186 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
187
188 return 1; /* return error */
189 }
190 if (output_buf[0] != 0xA) /* check the result */
191 {
192 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
193
194 return 1; /* return error */
195 }
196
197 return 0; /* success return 0 */
198}
199
211{
212 if (handle == NULL) /* check handle */
213 {
214 return 2; /* return error */
215 }
216 if (handle->inited != 1) /* check handle initialization */
217 {
218 return 3; /* return error */
219 }
220
221 handle->end_page = (uint8_t)storage; /* set the storage */
222
223 return 0; /* success return 0 */
224}
225
237{
238 if (handle == NULL) /* check handle */
239 {
240 return 2; /* return error */
241 }
242 if (handle->inited != 1) /* check handle initialization */
243 {
244 return 3; /* return error */
245 }
246
247 *storage = (mifare_ultralight_storage_t)(handle->end_page); /* get the storage */
248
249 return 0; /* success return 0 */
250}
251
263{
264 uint8_t res;
265
266 if (handle == NULL) /* check handle */
267 {
268 return 2; /* return error */
269 }
270 if (handle->debug_print == NULL) /* check debug_print */
271 {
272 return 3; /* return error */
273 }
274 if (handle->contactless_init == NULL) /* check contactless_init */
275 {
276 handle->debug_print("mifare_ultralight: contactless_init is null.\n"); /* contactless_init is null */
277
278 return 3; /* return error */
279 }
280 if (handle->contactless_deinit == NULL) /* check contactless_deinit */
281 {
282 handle->debug_print("mifare_ultralight: contactless_deinit is null.\n"); /* contactless_deinit is null */
283
284 return 3; /* return error */
285 }
286 if (handle->contactless_transceiver == NULL) /* check contactless_transceiver */
287 {
288 handle->debug_print("mifare_ultralight: contactless_transceiver is null.\n"); /* contactless_transceiver is null */
289
290 return 3; /* return error */
291 }
292 if (handle->delay_ms == NULL) /* check delay_ms */
293 {
294 handle->debug_print("mifare_ultralight: delay_ms is null.\n"); /* delay_ms is null */
295
296 return 3; /* return error */
297 }
298
299 res = handle->contactless_init(); /* contactless init */
300 if (res != 0) /* check the result */
301 {
302 handle->debug_print("mifare_ultralight: contactless init failed.\n"); /* contactless init failed */
303
304 return 1; /* return error */
305 }
306 handle->type = (uint8_t)MIFARE_ULTRALIGHT_TYPE_INVALID; /* set the invalid type */
307 handle->end_page = 0xFF; /* set the end page */
308 handle->inited = 1; /* flag inited */
309
310 return 0; /* success return 0 */
311}
312
324{
325 uint8_t res;
326
327 if (handle == NULL) /* check handle */
328 {
329 return 2; /* return error */
330 }
331 if (handle->inited != 1) /* check handle initialization */
332 {
333 return 3; /* return error */
334 }
335
336 res = handle->contactless_deinit(); /* contactless deinit */
337 if (res != 0) /* check the result */
338 {
339 handle->debug_print("mifare_ultralight: contactless deinit failed.\n"); /* contactless deinit failed */
340
341 return 1; /* return error */
342 }
343 handle->inited = 0; /* flag closed */
344
345 return 0; /* success return 0 */
346}
347
362{
363 uint8_t res;
364 uint8_t input_len;
365 uint8_t input_buf[1];
366 uint8_t output_len;
367 uint8_t output_buf[2];
368
369 if (handle == NULL) /* check handle */
370 {
371 return 2; /* return error */
372 }
373 if (handle->inited != 1) /* check handle initialization */
374 {
375 return 3; /* return error */
376 }
377
378 input_len = 1; /* set the input length */
379 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_REQUEST; /* set the command */
380 output_len = 2; /* set the output length */
381 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
382 if (res != 0) /* check the result */
383 {
384 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
385
386 return 1; /* return error */
387 }
388 if (output_len != 2) /* check the output_len */
389 {
390 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
391
392 return 4; /* return error */
393 }
394 if ((output_buf[0] == 0x44) && (output_buf[1] == 0x00)) /* check classic type */
395 {
396 *type = MIFARE_ULTRALIGHT_TYPE_ULTRALIGHT; /* ultralight */
397 handle->type = *type; /* save the type */
398
399 return 0; /* success return 0 */
400 }
401 else
402 {
403 *type = MIFARE_ULTRALIGHT_TYPE_INVALID; /* invalid */
404 handle->type = *type; /* save the type */
405 handle->debug_print("mifare_ultralight: type is invalid.\n"); /* type is invalid */
406
407 return 5; /* return error */
408 }
409}
410
425{
426 uint8_t res;
427 uint8_t input_len;
428 uint8_t input_buf[1];
429 uint8_t output_len;
430 uint8_t output_buf[2];
431
432 if (handle == NULL) /* check handle */
433 {
434 return 2; /* return error */
435 }
436 if (handle->inited != 1) /* check handle initialization */
437 {
438 return 3; /* return error */
439 }
440
441 handle->delay_ms(1); /* delay 1ms */
442 input_len = 1; /* set the input length */
443 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WAKE_UP; /* set the command */
444 output_len = 2; /* set the output length */
445 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
446 if (res != 0) /* check the result */
447 {
448 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
449
450 return 1; /* return error */
451 }
452 if (output_len != 2) /* check the output_len */
453 {
454 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
455
456 return 4; /* return error */
457 }
458 if ((output_buf[0] == 0x44) && (output_buf[1] == 0x00)) /* check classic type */
459 {
460 *type = MIFARE_ULTRALIGHT_TYPE_ULTRALIGHT; /* ultralight */
461 handle->type = *type; /* save the type */
462
463 return 0; /* success return 0 */
464 }
465 else
466 {
467 *type = MIFARE_ULTRALIGHT_TYPE_INVALID; /* invalid */
468 handle->type = *type; /* save the type */
469 handle->debug_print("mifare_ultralight: type is invalid.\n"); /* type is invalid */
470
471 return 5; /* return error */
472 }
473}
474
486{
487 uint8_t input_len;
488 uint8_t input_buf[4];
489 uint8_t output_len;
490 uint8_t output_buf[1];
491
492 if (handle == NULL) /* check handle */
493 {
494 return 2; /* return error */
495 }
496 if (handle->inited != 1) /* check handle initialization */
497 {
498 return 3; /* return error */
499 }
500
501 input_len = 4; /* set the input length */
502 input_buf[0] = (MIFARE_ULTRALIGHT_COMMAND_HALT >> 8) & 0xFF; /* set the command */
503 input_buf[1] = (MIFARE_ULTRALIGHT_COMMAND_HALT >> 0) & 0xFF; /* set the command */
504 a_mifare_ultralight_iso14443a_crc(input_buf, 2, input_buf + 2); /* get the crc */
505 output_len = 1; /* set the output length */
506 (void)handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
507
508 return 0; /* success return 0 */
509}
510
525{
526 uint8_t res;
527 uint8_t i;
528 uint8_t check;
529 uint8_t input_len;
530 uint8_t input_buf[2];
531 uint8_t output_len;
532 uint8_t output_buf[5];
533
534 if (handle == NULL) /* check handle */
535 {
536 return 2; /* return error */
537 }
538 if (handle->inited != 1) /* check handle initialization */
539 {
540 return 3; /* return error */
541 }
542
543 input_len = 2; /* set the input length */
544 input_buf[0] = (MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL1 >> 8) & 0xFF; /* set the command */
545 input_buf[1] = (MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL1 >> 0) & 0xFF; /* set the command */
546 output_len = 5; /* set the output length */
547 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
548 if (res != 0) /* check the result */
549 {
550 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
551
552 return 1; /* return error */
553 }
554 if (output_len != 5) /* check the output_len */
555 {
556 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
557
558 return 4; /* return error */
559 }
560 check = 0; /* init 0 */
561 for (i = 0; i < 4; i++) /* run 4 times */
562 {
563 id[i] = output_buf[i]; /* get one id */
564 check ^= output_buf[i]; /* xor */
565 }
566 if (check != output_buf[4]) /* check the result */
567 {
568 handle->debug_print("mifare_ultralight: check error.\n"); /* check error */
569
570 return 5; /* return error */
571 }
572
573 return 0; /* success return 0 */
574}
575
590{
591 uint8_t res;
592 uint8_t i;
593 uint8_t check;
594 uint8_t input_len;
595 uint8_t input_buf[2];
596 uint8_t output_len;
597 uint8_t output_buf[5];
598
599 if (handle == NULL) /* check handle */
600 {
601 return 2; /* return error */
602 }
603 if (handle->inited != 1) /* check handle initialization */
604 {
605 return 3; /* return error */
606 }
607
608 input_len = 2; /* set the input length */
609 input_buf[0] = (MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL2 >> 8) & 0xFF; /* set the command */
610 input_buf[1] = (MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL2 >> 0) & 0xFF; /* set the command */
611 output_len = 5; /* set the output length */
612 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
613 if (res != 0) /* check the result */
614 {
615 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
616
617 return 1; /* return error */
618 }
619 if (output_len != 5) /* check the output_len */
620 {
621 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
622
623 return 4; /* return error */
624 }
625 check = 0; /* init 0 */
626 for (i = 0; i < 4; i++) /* run 4 times */
627 {
628 id[i] = output_buf[i]; /* get one id */
629 check ^= output_buf[i]; /* xor */
630 }
631 if (check != output_buf[4]) /* check the result */
632 {
633 handle->debug_print("mifare_ultralight: check error.\n"); /* check error */
634
635 return 5; /* return error */
636 }
637
638 return 0; /* success return 0 */
639}
640
655{
656 uint8_t res;
657 uint8_t i;
658 uint8_t input_len;
659 uint8_t input_buf[9];
660 uint8_t output_len;
661 uint8_t output_buf[1];
662
663 if (handle == NULL) /* check handle */
664 {
665 return 2; /* return error */
666 }
667 if (handle->inited != 1) /* check handle initialization */
668 {
669 return 3; /* return error */
670 }
671
672 input_len = 9; /* set the input length */
673 input_buf[0] = (MIFARE_ULTRALIGHT_COMMAND_SELECT_CL1 >> 8) & 0xFF; /* set the command */
674 input_buf[1] = (MIFARE_ULTRALIGHT_COMMAND_SELECT_CL1 >> 0) & 0xFF; /* set the command */
675 input_buf[6] = 0; /* init 0 */
676 for (i = 0; i < 4; i++) /* run 4 times */
677 {
678 input_buf[2 + i] = id[i]; /* get one id */
679 input_buf[6] ^= id[i]; /* xor */
680 }
681 a_mifare_ultralight_iso14443a_crc(input_buf, 7, input_buf + 7); /* get the crc */
682 output_len = 1; /* set the output length */
683 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
684 if (res != 0) /* check the result */
685 {
686 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
687
688 return 1; /* return error */
689 }
690 if (output_len != 1) /* check the output_len */
691 {
692 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
693
694 return 4; /* return error */
695 }
696 if (output_buf[0] == 0x04) /* check the sak */
697 {
698 return 0; /* success return 0 */
699 }
700 else
701 {
702 handle->debug_print("mifare_ultralight: sak error.\n"); /* sak error */
703
704 return 5; /* return error */
705 }
706}
707
722{
723 uint8_t res;
724 uint8_t i;
725 uint8_t input_len;
726 uint8_t input_buf[9];
727 uint8_t output_len;
728 uint8_t output_buf[1];
729
730 if (handle == NULL) /* check handle */
731 {
732 return 2; /* return error */
733 }
734 if (handle->inited != 1) /* check handle initialization */
735 {
736 return 3; /* return error */
737 }
738
739 input_len = 9; /* set the input length */
740 input_buf[0] = (MIFARE_ULTRALIGHT_COMMAND_SELECT_CL2 >> 8) & 0xFF; /* set the command */
741 input_buf[1] = (MIFARE_ULTRALIGHT_COMMAND_SELECT_CL2 >> 0) & 0xFF; /* set the command */
742 input_buf[6] = 0; /* init 0 */
743 for (i = 0; i < 4; i++) /* run 4 times */
744 {
745 input_buf[2 + i] = id[i]; /* get one id */
746 input_buf[6] ^= id[i]; /* xor */
747 }
748 a_mifare_ultralight_iso14443a_crc(input_buf, 7, input_buf + 7); /* get the crc */
749 output_len = 1; /* set the output length */
750 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
751 if (res != 0) /* check the result */
752 {
753 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
754
755 return 1; /* return error */
756 }
757 if (output_len != 1) /* check the output_len */
758 {
759 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
760
761 return 4; /* return error */
762 }
763 if (output_buf[0] == 0x00) /* check the sak */
764 {
765 return 0; /* success return 0 */
766 }
767 else
768 {
769 handle->debug_print("mifare_ultralight: sak error.\n"); /* sak error */
770
771 return 5; /* return error */
772 }
773}
774
789{
790 uint8_t res;
791 uint8_t input_len;
792 uint8_t input_buf[3];
793 uint8_t output_len;
794 uint8_t output_buf[10];
795 uint8_t crc_buf[2];
796
797 if (handle == NULL) /* check handle */
798 {
799 return 2; /* return error */
800 }
801 if (handle->inited != 1) /* check handle initialization */
802 {
803 return 3; /* return error */
804 }
805
806 input_len = 3; /* set the input length */
807 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_GET_VERSION; /* set the command */
808 a_mifare_ultralight_iso14443a_crc(input_buf, 1, input_buf + 1); /* get the crc */
809 output_len = 10; /* set the output length */
810 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
811 if (res != 0) /* check the result */
812 {
813 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
814
815 return 1; /* return error */
816 }
817 if (output_len != 10) /* check the output_len */
818 {
819 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
820
821 return 4; /* return error */
822 }
823 a_mifare_ultralight_iso14443a_crc(output_buf, 8, crc_buf); /* get the crc */
824 if ((output_buf[8] == crc_buf[0]) && (output_buf[9] == crc_buf[1])) /* check the crc */
825 {
826 version->fixed_header = output_buf[0]; /* fixed header */
827 version->vendor_id = output_buf[1]; /* vendor id */
828 version->product_type = output_buf[2]; /* product type */
829 version->product_subtype = output_buf[3]; /* product subtype */
830 version->major_product_version = output_buf[4]; /* major product version */
831 version->minor_product_version = output_buf[5]; /* minor product version */
832 version->storage_size = output_buf[6]; /* storage size */
833 if (version->storage_size == 0x0B) /* if 20 pages */
834 {
835 handle->end_page = MIFARE_ULTRALIGHT_STORAGE_MF0UL11; /* set the end page */
836 }
837 else if (version->storage_size == 0x0E) /* if 41 pages */
838 {
839 handle->end_page = MIFARE_ULTRALIGHT_STORAGE_MF0UL21; /* set the end page */
840 }
841 else
842 {
843 /* do nothing */
844 }
845 version->protocol_type = output_buf[7]; /* protocol type */
846
847 return 0; /* success return 0 */
848 }
849 else
850 {
851 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
852
853 return 5; /* return error */
854 }
855}
856
872uint8_t mifare_ultralight_read_counter(mifare_ultralight_handle_t *handle, uint8_t addr, uint32_t *cnt)
873{
874 uint8_t res;
875 uint8_t input_len;
876 uint8_t input_buf[4];
877 uint8_t output_len;
878 uint8_t output_buf[5];
879 uint8_t crc_buf[2];
880
881 if (handle == NULL) /* check handle */
882 {
883 return 2; /* return error */
884 }
885 if (handle->inited != 1) /* check handle initialization */
886 {
887 return 3; /* return error */
888 }
889 if (addr > 0x2) /* check the addr */
890 {
891 handle->debug_print("mifare_ultralight: addr > 0x2.\n"); /* addr > 0x2 */
892
893 return 6; /* return error */
894 }
895
896 input_len = 4; /* set the input length */
897 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_READ_CNT; /* set the command */
898 input_buf[1] = addr; /* set the address */
899 a_mifare_ultralight_iso14443a_crc(input_buf, 2, input_buf + 2); /* get the crc */
900 output_len = 5; /* set the output length */
901 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
902 if (res != 0) /* check the result */
903 {
904 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
905
906 return 1; /* return error */
907 }
908 if (output_len != 5) /* check the output_len */
909 {
910 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
911
912 return 4; /* return error */
913 }
914 a_mifare_ultralight_iso14443a_crc(output_buf, 3, crc_buf); /* get the crc */
915 if ((output_buf[3] == crc_buf[0]) && (output_buf[4] == crc_buf[1])) /* check the result */
916 {
917 *cnt = ((uint32_t)output_buf[2] << 16) | ((uint32_t)output_buf[1] << 8) |
918 ((uint32_t)output_buf[0] << 0); /* set the counter */
919
920 return 0; /* success return 0 */
921 }
922 else
923 {
924 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
925
926 return 5; /* return error */
927 }
928}
929
945uint8_t mifare_ultralight_increment_counter(mifare_ultralight_handle_t *handle, uint8_t addr, uint32_t cnt)
946{
947 uint8_t res;
948 uint8_t input_len;
949 uint8_t input_buf[8];
950 uint8_t output_len;
951 uint8_t output_buf[1];
952
953 if (handle == NULL) /* check handle */
954 {
955 return 2; /* return error */
956 }
957 if (handle->inited != 1) /* check handle initialization */
958 {
959 return 3; /* return error */
960 }
961 if (addr > 0x2) /* check the addr */
962 {
963 handle->debug_print("mifare_ultralight: addr > 0x2.\n"); /* addr > 0x2 */
964
965 return 6; /* return error */
966 }
967
968 input_len = 8; /* set the input length */
969 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_INCR_CNT; /* set the command */
970 input_buf[1] = addr; /* set the address */
971 input_buf[2] = (cnt >> 0) & 0xFF; /* set cnt */
972 input_buf[3] = (cnt >> 8) & 0xFF; /* set cnt */
973 input_buf[4] = (cnt >> 16) & 0xFF; /* set cnt */
974 input_buf[5] = 0x00;
975 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
976 output_len = 1; /* set the output length */
977 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
978 if (res != 0) /* check the result */
979 {
980 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
981
982 return 1; /* return error */
983 }
984 if (output_len != 1) /* check the output_len */
985 {
986 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
987
988 return 4; /* return error */
989 }
990 if (output_buf[0] != 0xA) /* check the result */
991 {
992 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
993
994 return 5; /* return error */
995 }
996
997 return 0; /* success return 0 */
998}
999
1015uint8_t mifare_ultralight_check_tearing_event(mifare_ultralight_handle_t *handle, uint8_t addr, uint8_t *flag)
1016{
1017 uint8_t res;
1018 uint8_t input_len;
1019 uint8_t input_buf[4];
1020 uint8_t output_len;
1021 uint8_t output_buf[3];
1022 uint8_t crc_buf[2];
1023
1024 if (handle == NULL) /* check handle */
1025 {
1026 return 2; /* return error */
1027 }
1028 if (handle->inited != 1) /* check handle initialization */
1029 {
1030 return 3; /* return error */
1031 }
1032 if (addr > 0x2) /* check the addr */
1033 {
1034 handle->debug_print("mifare_ultralight: addr > 0x2.\n"); /* addr > 0x2 */
1035
1036 return 6; /* return error */
1037 }
1038
1039 input_len = 4; /* set the input length */
1040 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_CHECK_TEARING_EVENT; /* set the command */
1041 input_buf[1] = addr; /* set the address */
1042 a_mifare_ultralight_iso14443a_crc(input_buf, 2, input_buf + 2); /* get the crc */
1043 output_len = 3; /* set the output length */
1044 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1045 if (res != 0) /* check the result */
1046 {
1047 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1048
1049 return 1; /* return error */
1050 }
1051 if (output_len != 3) /* check the output_len */
1052 {
1053 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1054
1055 return 4; /* return error */
1056 }
1057 a_mifare_ultralight_iso14443a_crc(output_buf, 1, crc_buf); /* get the crc */
1058 if ((output_buf[1] == crc_buf[0]) && (output_buf[2] == crc_buf[1])) /* check the result */
1059 {
1060 *flag = output_buf[0]; /* set the output buffer */
1061
1062 return 0; /* success return 0 */
1063 }
1064 else
1065 {
1066 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1067
1068 return 5; /* return error */
1069 }
1070}
1071
1087uint8_t mifare_ultralight_vcsl(mifare_ultralight_handle_t *handle, uint8_t installation_identifier[16],
1088 uint8_t pcd_capabilities[4], uint8_t *identifier)
1089{
1090 uint8_t res;
1091 uint8_t input_len;
1092 uint8_t input_buf[23];
1093 uint8_t output_len;
1094 uint8_t output_buf[3];
1095 uint8_t crc_buf[2];
1096
1097 if (handle == NULL) /* check handle */
1098 {
1099 return 2; /* return error */
1100 }
1101 if (handle->inited != 1) /* check handle initialization */
1102 {
1103 return 3; /* return error */
1104 }
1105
1106 input_len = 23; /* set the input length */
1107 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_CHECK_TEARING_EVENT; /* set the command */
1108 memcpy(input_buf + 1, installation_identifier, 16);
1109 memcpy(input_buf + 17, pcd_capabilities, 4);
1110 a_mifare_ultralight_iso14443a_crc(input_buf, 21, input_buf + 21); /* get the crc */
1111 output_len = 3; /* set the output length */
1112 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1113 if (res != 0) /* check the result */
1114 {
1115 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1116
1117 return 1; /* return error */
1118 }
1119 if (output_len != 3) /* check the output_len */
1120 {
1121 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1122
1123 return 4; /* return error */
1124 }
1125 a_mifare_ultralight_iso14443a_crc(output_buf, 1, crc_buf); /* get the crc */
1126 if ((output_buf[1] == crc_buf[0]) && (output_buf[2] == crc_buf[1])) /* check the result */
1127 {
1128 *identifier = output_buf[0]; /* set the output buffer */
1129
1130 return 0; /* success return 0 */
1131 }
1132 else
1133 {
1134 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1135
1136 return 5; /* return error */
1137 }
1138}
1139
1154{
1155 uint8_t res;
1156 uint8_t input_len;
1157 uint8_t input_buf[4];
1158 uint8_t output_len;
1159 uint8_t output_buf[34];
1160 uint8_t crc_buf[2];
1161
1162 if (handle == NULL) /* check handle */
1163 {
1164 return 2; /* return error */
1165 }
1166 if (handle->inited != 1) /* check handle initialization */
1167 {
1168 return 3; /* return error */
1169 }
1170
1171 input_len = 4; /* set the input length */
1172 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_READ_SIG; /* set the command */
1173 input_buf[1] = 0x00; /* set the address */
1174 a_mifare_ultralight_iso14443a_crc(input_buf, 2, input_buf + 2); /* get the crc */
1175 output_len = 34; /* set the output length */
1176 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1177 if (res != 0) /* check the result */
1178 {
1179 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1180
1181 return 1; /* return error */
1182 }
1183 if (output_len != 34) /* check the output_len */
1184 {
1185 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1186
1187 return 4; /* return error */
1188 }
1189 a_mifare_ultralight_iso14443a_crc(output_buf, 32, crc_buf); /* get the crc */
1190 if ((output_buf[32] == crc_buf[0]) && (output_buf[33] == crc_buf[1])) /* check the result */
1191 {
1192 memcpy(signature, output_buf, 32); /* copy the data */
1193
1194 return 0; /* success return 0 */
1195 }
1196 else
1197 {
1198 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1199
1200 return 5; /* return error */
1201 }
1202}
1203
1218{
1219 uint8_t res;
1220 uint8_t input_len;
1221 uint8_t input_buf[4];
1222 uint8_t output_len;
1223 uint8_t output_buf[18];
1224 uint8_t crc_buf[2];
1225
1226 if (handle == NULL) /* check handle */
1227 {
1228 return 2; /* return error */
1229 }
1230 if (handle->inited != 1) /* check handle initialization */
1231 {
1232 return 3; /* return error */
1233 }
1234
1235 input_len = 4; /* set the input length */
1236 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_READ; /* set the command */
1237 input_buf[1] = 0x00; /* set the read page */
1238 a_mifare_ultralight_iso14443a_crc(input_buf , 2, input_buf + 2); /* get the crc */
1239 output_len = 18; /* set the output length */
1240 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1241 if (res != 0) /* check the result */
1242 {
1243 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1244
1245 return 1; /* return error */
1246 }
1247 if (output_len != 18) /* check the output_len */
1248 {
1249 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1250
1251 return 4; /* return error */
1252 }
1253 a_mifare_ultralight_iso14443a_crc(output_buf, 16, crc_buf); /* get the crc */
1254 if ((output_buf[16] == crc_buf[0]) && (output_buf[17] == crc_buf[1])) /* check the crc */
1255 {
1256 number[0] = output_buf[0]; /* set the number 0 */
1257 number[1] = output_buf[1]; /* set the number 1 */
1258 number[2] = output_buf[2]; /* set the number 2 */
1259 number[3] = output_buf[4]; /* set the number 3 */
1260 number[4] = output_buf[5]; /* set the number 4 */
1261 number[5] = output_buf[6]; /* set the number 5 */
1262 number[6] = output_buf[7]; /* set the number 6 */
1263
1264 return 0; /* success return 0 */
1265 }
1266 else
1267 {
1268 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1269
1270 return 5; /* return error */
1271 }
1272}
1273
1288uint8_t mifare_ultralight_read_four_pages(mifare_ultralight_handle_t *handle, uint8_t start_page, uint8_t data[16])
1289{
1290 uint8_t res;
1291 uint8_t input_len;
1292 uint8_t input_buf[4];
1293 uint8_t output_len;
1294 uint8_t output_buf[18];
1295 uint8_t crc_buf[2];
1296
1297 if (handle == NULL) /* check handle */
1298 {
1299 return 2; /* return error */
1300 }
1301 if (handle->inited != 1) /* check handle initialization */
1302 {
1303 return 3; /* return error */
1304 }
1305
1306 input_len = 4; /* set the input length */
1307 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_READ; /* set the command */
1308 input_buf[1] = start_page; /* set the page */
1309 a_mifare_ultralight_iso14443a_crc(input_buf , 2, input_buf + 2); /* get the crc */
1310 output_len = 18; /* set the output length */
1311 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1312 if (res != 0) /* check the result */
1313 {
1314 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1315
1316 return 1; /* return error */
1317 }
1318 if (output_len != 18) /* check the output_len */
1319 {
1320 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1321
1322 return 4; /* return error */
1323 }
1324 a_mifare_ultralight_iso14443a_crc(output_buf, 16, crc_buf); /* get the crc */
1325 if ((output_buf[16] == crc_buf[0]) && (output_buf[17] == crc_buf[1])) /* check the crc */
1326 {
1327 memcpy(data, output_buf, 16); /* copy the data */
1328
1329 return 0; /* success return 0 */
1330 }
1331 else
1332 {
1333 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1334
1335 return 5; /* return error */
1336 }
1337}
1338
1353uint8_t mifare_ultralight_read_page(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
1354{
1355 uint8_t res;
1356 uint8_t input_len;
1357 uint8_t input_buf[4];
1358 uint8_t output_len;
1359 uint8_t output_buf[18];
1360 uint8_t crc_buf[2];
1361
1362 if (handle == NULL) /* check handle */
1363 {
1364 return 2; /* return error */
1365 }
1366 if (handle->inited != 1) /* check handle initialization */
1367 {
1368 return 3; /* return error */
1369 }
1370
1371 input_len = 4; /* set the input length */
1372 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_READ; /* set the command */
1373 input_buf[1] = page; /* set the page */
1374 a_mifare_ultralight_iso14443a_crc(input_buf , 2, input_buf + 2); /* get the crc */
1375 output_len = 18; /* set the output length */
1376 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1377 if (res != 0) /* check the result */
1378 {
1379 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1380
1381 return 1; /* return error */
1382 }
1383 if (output_len != 18) /* check the output_len */
1384 {
1385 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1386
1387 return 4; /* return error */
1388 }
1389 a_mifare_ultralight_iso14443a_crc(output_buf, 16, crc_buf); /* get the crc */
1390 if ((output_buf[16] == crc_buf[0]) && (output_buf[17] == crc_buf[1])) /* check the crc */
1391 {
1392 memcpy(data, output_buf, 4); /* copy the data */
1393
1394 return 0; /* success return 0 */
1395 }
1396 else
1397 {
1398 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1399
1400 return 5; /* return error */
1401 }
1402}
1403
1424uint8_t mifare_ultralight_fast_read_page(mifare_ultralight_handle_t *handle, uint8_t start_page, uint8_t stop_page, uint8_t *data, uint16_t *len)
1425{
1426 uint8_t res;
1427 uint8_t input_len;
1428 uint8_t input_buf[5];
1429 uint8_t output_len;
1430 uint8_t cal_len;
1431 uint8_t output_buf[64];
1432 uint8_t crc_buf[2];
1433
1434 if (handle == NULL) /* check handle */
1435 {
1436 return 2; /* return error */
1437 }
1438 if (handle->inited != 1) /* check handle initialization */
1439 {
1440 return 3; /* return error */
1441 }
1442 if (stop_page < start_page) /* check start and stop page */
1443 {
1444 handle->debug_print("mifare_ultralight: stop_page < start_page.\n"); /* stop_page < start_page */
1445
1446 return 4; /* return error */
1447 }
1448 if (stop_page - start_page + 1 > 15) /* check start and stop page */
1449 {
1450 handle->debug_print("mifare_ultralight: stop_page - start_page + 1 is over 15.\n"); /* stop_page - start_page + 1 is over 15 */
1451
1452 return 5; /* return error */
1453 }
1454 if ((*len) < (4 * (stop_page - start_page + 1))) /* check the length */
1455 {
1456 handle->debug_print("mifare_ultralight: len < %d.\n", 4 * (stop_page - start_page + 1)); /* len is invalid */
1457
1458 return 6; /* return error */
1459 }
1460
1461 input_len = 5; /* set the input length */
1462 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_FAST_READ; /* set the command */
1463 input_buf[1] = start_page; /* set the start page */
1464 input_buf[2] = stop_page; /* set the stop page */
1465 a_mifare_ultralight_iso14443a_crc(input_buf , 3, input_buf + 3); /* get the crc */
1466 cal_len = 4 * (stop_page - start_page + 1); /* set the cal length */
1467 output_len = (uint8_t)(cal_len + 2); /* set the output length */
1468 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1469 if (res != 0) /* check the result */
1470 {
1471 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1472
1473 return 1; /* return error */
1474 }
1475 if (output_len != (cal_len + 2)) /* check the output_len */
1476 {
1477 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1478
1479 return 7; /* return error */
1480 }
1481 a_mifare_ultralight_iso14443a_crc(output_buf, (uint8_t)cal_len, crc_buf); /* get the crc */
1482 if ((output_buf[cal_len] == crc_buf[0]) && (output_buf[cal_len + 1] == crc_buf[1])) /* check the crc */
1483 {
1484 memcpy(data, output_buf, cal_len); /* copy the data */
1485 *len = cal_len; /* set the length */
1486
1487 return 0; /* success return 0 */
1488 }
1489 else
1490 {
1491 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1492
1493 return 8; /* return error */
1494 }
1495}
1496
1511uint8_t mifare_ultralight_compatibility_write_page(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
1512{
1513 uint8_t res;
1514 uint8_t i;
1515 uint8_t input_len;
1516 uint8_t input_buf[18];
1517 uint8_t output_len;
1518 uint8_t output_buf[1];
1519
1520 if (handle == NULL) /* check handle */
1521 {
1522 return 2; /* return error */
1523 }
1524 if (handle->inited != 1) /* check handle initialization */
1525 {
1526 return 3; /* return error */
1527 }
1528
1529 input_len = 4; /* set the input length */
1530 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_COMP_WRITE; /* set the command */
1531 input_buf[1] = page; /* set the page */
1532 a_mifare_ultralight_iso14443a_crc(input_buf, 2, input_buf + 2); /* get the crc */
1533 output_len = 1; /* set the output length */
1534 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1535 if (res != 0) /* check the result */
1536 {
1537 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1538
1539 return 1; /* return error */
1540 }
1541 if (output_len != 1) /* check the output_len */
1542 {
1543 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1544
1545 return 4; /* return error */
1546 }
1547 if (output_buf[0] != 0xA) /* check the result */
1548 {
1549 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
1550
1551 return 5; /* return error */
1552 }
1553
1554 for (i = 0; i < 4; i ++) /* 4 times */
1555 {
1556 input_buf[i] = data[i]; /* copy data */
1557 }
1558 for (i = 0; i < 12; i ++) /* 12 times */
1559 {
1560 input_buf[4 + i] = 0x00; /* copy data */
1561 }
1562 a_mifare_ultralight_iso14443a_crc(input_buf, 16, input_buf + 16); /* get the crc */
1563 input_len = 18; /* set the input length */
1564 output_len = 1; /* set the output length */
1565 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1566 if (res != 0) /* check the result */
1567 {
1568 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1569
1570 return 1; /* return error */
1571 }
1572 if (output_buf[0] != 0xA) /* check the result */
1573 {
1574 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
1575
1576 return 5; /* return error */
1577 }
1578
1579 return 0; /* success return 0 */
1580}
1581
1596uint8_t mifare_ultralight_write_page(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
1597{
1598 uint8_t res;
1599 uint8_t input_len;
1600 uint8_t input_buf[8];
1601 uint8_t output_len;
1602 uint8_t output_buf[1];
1603
1604 if (handle == NULL) /* check handle */
1605 {
1606 return 2; /* return error */
1607 }
1608 if (handle->inited != 1) /* check handle initialization */
1609 {
1610 return 3; /* return error */
1611 }
1612
1613 input_len = 8; /* set the input length */
1614 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WRITE; /* set the command */
1615 input_buf[1] = page; /* set the page */
1616 input_buf[2] = data[0]; /* set data0 */
1617 input_buf[3] = data[1]; /* set data1 */
1618 input_buf[4] = data[2]; /* set data2 */
1619 input_buf[5] = data[3]; /* set data3 */
1620 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
1621 output_len = 1; /* set the output length */
1622 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1623 if (res != 0) /* check the result */
1624 {
1625 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1626
1627 return 1; /* return error */
1628 }
1629 if (output_len != 1) /* check the output_len */
1630 {
1631 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1632
1633 return 4; /* return error */
1634 }
1635 if (output_buf[0] != 0xA) /* check the result */
1636 {
1637 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
1638
1639 return 5; /* return error */
1640 }
1641
1642 return 0; /* success return 0 */
1643}
1644
1660uint8_t mifare_ultralight_authenticate(mifare_ultralight_handle_t *handle, uint8_t pwd[4], uint8_t pack[2])
1661{
1662 uint8_t res;
1663 uint8_t input_len;
1664 uint8_t input_buf[7];
1665 uint8_t output_len;
1666 uint8_t output_buf[4];
1667 uint8_t crc_buf[2];
1668
1669 if (handle == NULL) /* check handle */
1670 {
1671 return 2; /* return error */
1672 }
1673 if (handle->inited != 1) /* check handle initialization */
1674 {
1675 return 3; /* return error */
1676 }
1677
1678 input_len = 7; /* set the input length */
1679 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_PWD_AUTH; /* set the command */
1680 input_buf[1] = pwd[0]; /* set pwd0 */
1681 input_buf[2] = pwd[1]; /* set pwd1 */
1682 input_buf[3] = pwd[2]; /* set pwd2 */
1683 input_buf[4] = pwd[3]; /* set pwd3 */
1684 a_mifare_ultralight_iso14443a_crc(input_buf, 5, input_buf + 5); /* get the crc */
1685 output_len = 4; /* set the output length */
1686 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1687 if (res != 0) /* check the result */
1688 {
1689 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1690
1691 return 1; /* return error */
1692 }
1693 if (output_len != 4) /* check the output_len */
1694 {
1695 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1696
1697 return 4; /* return error */
1698 }
1699 a_mifare_ultralight_iso14443a_crc(output_buf, 2, crc_buf); /* get the crc */
1700 if ((output_buf[2] == crc_buf[0]) && (output_buf[3] == crc_buf[1])) /* check the crc */
1701 {
1702 if ((output_buf[0] != pack[0]) || (output_buf[1] != pack[1])) /* check the pack */
1703 {
1704 handle->debug_print("mifare_ultralight: pack check failed.\n"); /* pack check failed. */
1705
1706 return 6; /* return error */
1707 }
1708
1709 return 0; /* success return 0 */
1710 }
1711 else
1712 {
1713 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
1714
1715 return 5; /* return error */
1716 }
1717}
1718
1733{
1734 uint8_t res;
1735 uint8_t input_len;
1736 uint8_t input_buf[8];
1737 uint8_t output_len;
1738 uint8_t output_buf[1];
1739
1740 if (handle == NULL) /* check handle */
1741 {
1742 return 2; /* return error */
1743 }
1744 if (handle->inited != 1) /* check handle initialization */
1745 {
1746 return 3; /* return error */
1747 }
1748
1749 input_len = 8; /* set the input length */
1750 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WRITE; /* set the command */
1751 input_buf[1] = handle->end_page - 1; /* set the last page */
1752 input_buf[2] = pwd[0]; /* set pwd0 */
1753 input_buf[3] = pwd[1]; /* set pwd1 */
1754 input_buf[4] = pwd[2]; /* set pwd2 */
1755 input_buf[5] = pwd[3]; /* set pwd3 */
1756 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
1757 output_len = 1; /* set the output length */
1758 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1759 if (res != 0) /* check the result */
1760 {
1761 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1762
1763 return 1; /* return error */
1764 }
1765 if (output_len != 1) /* check the output_len */
1766 {
1767 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1768
1769 return 4; /* return error */
1770 }
1771 if (output_buf[0] != 0xA) /* check the result */
1772 {
1773 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
1774
1775 return 5; /* return error */
1776 }
1777
1778 return 0; /* success return 0 */
1779}
1780
1795{
1796 uint8_t res;
1797 uint8_t input_len;
1798 uint8_t input_buf[8];
1799 uint8_t output_len;
1800 uint8_t output_buf[1];
1801
1802 if (handle == NULL) /* check handle */
1803 {
1804 return 2; /* return error */
1805 }
1806 if (handle->inited != 1) /* check handle initialization */
1807 {
1808 return 3; /* return error */
1809 }
1810
1811 input_len = 8; /* set the input length */
1812 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WRITE; /* set the command */
1813 input_buf[1] = handle->end_page; /* set the last page */
1814 input_buf[2] = pack[0]; /* set pack0 */
1815 input_buf[3] = pack[1]; /* set pack1 */
1816 input_buf[4] = 0x00; /* set 0x00 */
1817 input_buf[5] = 0x00; /* set 0x00 */
1818 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
1819 output_len = 1; /* set the output length */
1820 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
1821 if (res != 0) /* check the result */
1822 {
1823 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
1824
1825 return 1; /* return error */
1826 }
1827 if (output_len != 1) /* check the output_len */
1828 {
1829 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
1830
1831 return 4; /* return error */
1832 }
1833 if (output_buf[0] != 0xA) /* check the result */
1834 {
1835 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
1836
1837 return 5; /* return error */
1838 }
1839
1840 return 0; /* success return 0 */
1841}
1842
1855{
1856 uint8_t res;
1857 uint8_t conf[4];
1858
1859 if (handle == NULL) /* check handle */
1860 {
1861 return 2; /* return error */
1862 }
1863 if (handle->inited != 1) /* check handle initialization */
1864 {
1865 return 3; /* return error */
1866 }
1867
1868 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
1869 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 3, conf); /* read conf */
1870 if (res != 0) /* check the result */
1871 {
1872 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
1873
1874 return 1; /* return error */
1875 }
1876 conf[0] &= ~(1 << 2); /* clear the settings */
1877 conf[0] |= mode << 2; /* set the mode */
1878 res = a_mifare_ultralight_conf_write(handle, handle->end_page - 3, conf); /* write conf */
1879 if (res != 0) /* check the result */
1880 {
1881 handle->debug_print("mifare_ultralight: conf write failed.\n"); /* conf write failed */
1882
1883 return 1; /* return error */
1884 }
1885
1886 return 0; /* success return 0 */
1887}
1888
1901{
1902 uint8_t res;
1903 uint8_t conf[4];
1904
1905 if (handle == NULL) /* check handle */
1906 {
1907 return 2; /* return error */
1908 }
1909 if (handle->inited != 1) /* check handle initialization */
1910 {
1911 return 3; /* return error */
1912 }
1913
1914 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
1915 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 3, conf); /* read conf */
1916 if (res != 0) /* check the result */
1917 {
1918 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
1919
1920 return 1; /* return error */
1921 }
1922 *mode = (mifare_ultralight_modulation_mode_t)((conf[0] >> 2) & 0x1); /* get the conf */
1923
1924 return 0; /* success return 0 */
1925}
1926
1939{
1940 uint8_t res;
1941 uint8_t conf[4];
1942
1943 if (handle == NULL) /* check handle */
1944 {
1945 return 2; /* return error */
1946 }
1947 if (handle->inited != 1) /* check handle initialization */
1948 {
1949 return 3; /* return error */
1950 }
1951
1952 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
1953 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 3, conf); /* read conf */
1954 if (res != 0) /* check the result */
1955 {
1956 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
1957
1958 return 1; /* return error */
1959 }
1960 conf[3] = page; /* set the page */
1961 res = a_mifare_ultralight_conf_write(handle, handle->end_page - 3, conf); /* write conf */
1962 if (res != 0) /* check the result */
1963 {
1964 handle->debug_print("mifare_ultralight: conf write failed.\n"); /* conf write failed */
1965
1966 return 1; /* return error */
1967 }
1968
1969 return 0; /* success return 0 */
1970}
1971
1984{
1985 uint8_t res;
1986 uint8_t conf[4];
1987
1988 if (handle == NULL) /* check handle */
1989 {
1990 return 2; /* return error */
1991 }
1992 if (handle->inited != 1) /* check handle initialization */
1993 {
1994 return 3; /* return error */
1995 }
1996
1997 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
1998 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 3, conf); /* read conf */
1999 if (res != 0) /* check the result */
2000 {
2001 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
2002
2003 return 1; /* return error */
2004 }
2005 *page = conf[3]; /* get the page */
2006
2007 return 0; /* success return 0 */
2008}
2009
2023{
2024 uint8_t res;
2025 uint8_t conf[4];
2026
2027 if (handle == NULL) /* check handle */
2028 {
2029 return 2; /* return error */
2030 }
2031 if (handle->inited != 1) /* check handle initialization */
2032 {
2033 return 3; /* return error */
2034 }
2035
2036 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
2037 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 2, conf); /* read conf */
2038 if (res != 0) /* check the result */
2039 {
2040 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
2041
2042 return 1; /* return error */
2043 }
2044 conf[0] &= ~(1 << access); /* clear the settings */
2045 conf[0] |= enable << access; /* set the access */
2046 res = a_mifare_ultralight_conf_write(handle, handle->end_page - 2, conf); /* write conf */
2047 if (res != 0) /* check the result */
2048 {
2049 handle->debug_print("mifare_ultralight: conf write failed.\n"); /* conf write failed */
2050
2051 return 1; /* return error */
2052 }
2053
2054 return 0; /* success return 0 */
2055}
2056
2070{
2071 uint8_t res;
2072 uint8_t conf[4];
2073
2074 if (handle == NULL) /* check handle */
2075 {
2076 return 2; /* return error */
2077 }
2078 if (handle->inited != 1) /* check handle initialization */
2079 {
2080 return 3; /* return error */
2081 }
2082
2083 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
2084 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 2, conf); /* read conf */
2085 if (res != 0) /* check the result */
2086 {
2087 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
2088
2089 return 1; /* return error */
2090 }
2091 *enable = (mifare_ultralight_bool_t)((conf[0] >> access) & 0x1); /* get the bool */
2092
2093 return 0; /* success return 0 */
2094}
2095
2109{
2110 uint8_t res;
2111 uint8_t conf[4];
2112
2113 if (handle == NULL) /* check handle */
2114 {
2115 return 2; /* return error */
2116 }
2117 if (handle->inited != 1) /* check handle initialization */
2118 {
2119 return 3; /* return error */
2120 }
2121 if (limit > 7) /* check the limit */
2122 {
2123 handle->debug_print("mifare_ultralight: limit > 7.\n"); /* limit > 7 */
2124
2125 return 4; /* return error */
2126 }
2127
2128 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
2129 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 2, conf); /* read conf */
2130 if (res != 0) /* check the result */
2131 {
2132 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
2133
2134 return 1; /* return error */
2135 }
2136 conf[0] &= ~(7 << 0); /* clear the settings */
2137 conf[0] |= limit << 0; /* set the limit */
2138 res = a_mifare_ultralight_conf_write(handle, handle->end_page - 2, conf); /* write conf */
2139 if (res != 0) /* check the result */
2140 {
2141 handle->debug_print("mifare_ultralight: conf write failed.\n"); /* conf write failed */
2142
2143 return 1; /* return error */
2144 }
2145
2146 return 0; /* success return 0 */
2147}
2148
2161{
2162 uint8_t res;
2163 uint8_t conf[4];
2164
2165 if (handle == NULL) /* check handle */
2166 {
2167 return 2; /* return error */
2168 }
2169 if (handle->inited != 1) /* check handle initialization */
2170 {
2171 return 3; /* return error */
2172 }
2173
2174 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
2175 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 2, conf); /* read conf */
2176 if (res != 0) /* check the result */
2177 {
2178 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
2179
2180 return 1; /* return error */
2181 }
2182 *limit = conf[0] & 0x7; /* set the limit */
2183
2184 return 0; /* success return 0 */
2185}
2186
2199{
2200 uint8_t res;
2201 uint8_t conf[4];
2202
2203 if (handle == NULL) /* check handle */
2204 {
2205 return 2; /* return error */
2206 }
2207 if (handle->inited != 1) /* check handle initialization */
2208 {
2209 return 3; /* return error */
2210 }
2211
2212 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
2213 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 2, conf); /* read conf */
2214 if (res != 0) /* check the result */
2215 {
2216 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
2217
2218 return 1; /* return error */
2219 }
2220 conf[1] = identifier; /* set the identifier */
2221 res = a_mifare_ultralight_conf_write(handle, handle->end_page - 2, conf); /* write conf */
2222 if (res != 0) /* check the result */
2223 {
2224 handle->debug_print("mifare_ultralight: conf write failed.\n"); /* conf write failed */
2225
2226 return 1; /* return error */
2227 }
2228
2229 return 0; /* success return 0 */
2230}
2231
2244{
2245 uint8_t res;
2246 uint8_t conf[4];
2247
2248 if (handle == NULL) /* check handle */
2249 {
2250 return 2; /* return error */
2251 }
2252 if (handle->inited != 1) /* check handle initialization */
2253 {
2254 return 3; /* return error */
2255 }
2256
2257 memset(conf, 0, sizeof(uint8_t) * 4); /* clear the conf */
2258 res = a_mifare_ultralight_conf_read(handle, handle->end_page - 2, conf); /* read conf */
2259 if (res != 0) /* check the result */
2260 {
2261 handle->debug_print("mifare_ultralight: conf read failed.\n"); /* conf read failed */
2262
2263 return 1; /* return error */
2264 }
2265 *identifier = conf[1]; /* get the identifier */
2266
2267 return 0; /* success return 0 */
2268}
2269
2298{
2299 uint8_t res;
2300 uint8_t input_len;
2301 uint8_t input_buf[8];
2302 uint8_t output_len;
2303 uint8_t output_buf[1];
2304
2305 if (handle == NULL) /* check handle */
2306 {
2307 return 2; /* return error */
2308 }
2309 if (handle->inited != 1) /* check handle initialization */
2310 {
2311 return 3; /* return error */
2312 }
2313
2314 input_len = 8; /* set the input length */
2315 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WRITE; /* set the command */
2316 input_buf[1] = 0x02; /* set the setting page */
2317 input_buf[2] = 0x00; /* set 0x00 */
2318 input_buf[3] = 0x00; /* set 0x00 */
2319 input_buf[4] = lock[0]; /* set lock0 */
2320 input_buf[5] = lock[1]; /* set lock1 */
2321 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
2322 output_len = 1; /* set the output length */
2323 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
2324 if (res != 0) /* check the result */
2325 {
2326 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
2327
2328 return 1; /* return error */
2329 }
2330 if (output_len != 1) /* check the output_len */
2331 {
2332 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
2333
2334 return 4; /* return error */
2335 }
2336 if (output_buf[0] != 0xA) /* check the result */
2337 {
2338 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
2339
2340 return 5; /* return error */
2341 }
2342
2343 input_len = 8; /* set the input length */
2344 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WRITE; /* set the command */
2345 input_buf[1] = handle->end_page - 4; /* set the setting page */
2346 input_buf[2] = lock[2]; /* set lock2 */
2347 input_buf[3] = lock[3]; /* set lock3 */
2348 input_buf[4] = lock[4]; /* set lock4 */
2349 input_buf[5] = 0x00; /* set 0x00 */
2350 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
2351 output_len = 1; /* set the output length */
2352 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
2353 if (res != 0) /* check the result */
2354 {
2355 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
2356
2357 return 1; /* return error */
2358 }
2359 if (output_len != 1) /* check the output_len */
2360 {
2361 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
2362
2363 return 4; /* return error */
2364 }
2365 if (output_buf[0] != 0xA) /* check the result */
2366 {
2367 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
2368
2369 return 5; /* return error */
2370 }
2371
2372 return 0; /* success return 0 */
2373}
2374
2389{
2390 uint8_t res;
2391 uint8_t input_len;
2392 uint8_t input_buf[5];
2393 uint8_t output_len;
2394 uint8_t output_buf[6];
2395 uint8_t crc_buf[2];
2396
2397 if (handle == NULL) /* check handle */
2398 {
2399 return 2; /* return error */
2400 }
2401 if (handle->inited != 1) /* check handle initialization */
2402 {
2403 return 3; /* return error */
2404 }
2405
2406 input_len = 5; /* set the input length */
2407 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_FAST_READ; /* set the command */
2408 input_buf[1] = 2; /* set the start page */
2409 input_buf[2] = 2; /* set the stop page */
2410 a_mifare_ultralight_iso14443a_crc(input_buf , 3, input_buf + 3); /* get the crc */
2411 output_len = 6; /* set the output length */
2412 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
2413 if (res != 0) /* check the result */
2414 {
2415 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
2416
2417 return 1; /* return error */
2418 }
2419 if (output_len != 6) /* check the output_len */
2420 {
2421 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
2422
2423 return 4; /* return error */
2424 }
2425 a_mifare_ultralight_iso14443a_crc(output_buf, 4, crc_buf); /* get the crc */
2426 if ((output_buf[4] == crc_buf[0]) && (output_buf[5] == crc_buf[1])) /* check the crc */
2427 {
2428 memcpy(lock, output_buf + 2, 2); /* copy the data */
2429 }
2430 else
2431 {
2432 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
2433
2434 return 5; /* return error */
2435 }
2436
2437 input_len = 5; /* set the input length */
2438 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_FAST_READ; /* set the command */
2439 input_buf[1] = handle->end_page - 4; /* set the start page */
2440 input_buf[2] = handle->end_page - 4; /* set the stop page */
2441 a_mifare_ultralight_iso14443a_crc(input_buf , 3, input_buf + 3); /* get the crc */
2442 output_len = 6; /* set the output length */
2443 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
2444 if (res != 0) /* check the result */
2445 {
2446 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
2447
2448 return 1; /* return error */
2449 }
2450 if (output_len != 6) /* check the output_len */
2451 {
2452 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
2453
2454 return 4; /* return error */
2455 }
2456 a_mifare_ultralight_iso14443a_crc(output_buf, 4, crc_buf); /* get the crc */
2457 if ((output_buf[4] == crc_buf[0]) && (output_buf[5] == crc_buf[1])) /* check the crc */
2458 {
2459 memcpy(lock + 2, output_buf, 3); /* copy the data */
2460
2461 return 0; /* success return 0 */
2462 }
2463 else
2464 {
2465 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
2466
2467 return 5; /* return error */
2468 }
2469}
2470
2485{
2486 uint8_t res;
2487 uint8_t input_len;
2488 uint8_t input_buf[4];
2489 uint8_t output_len;
2490 uint8_t output_buf[18];
2491 uint8_t crc_buf[2];
2492
2493 if (handle == NULL) /* check handle */
2494 {
2495 return 2; /* return error */
2496 }
2497 if (handle->inited != 1) /* check handle initialization */
2498 {
2499 return 3; /* return error */
2500 }
2501
2502 input_len = 4; /* set the input length */
2503 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_READ; /* set the command */
2504 input_buf[1] = 0x03; /* set the page */
2505 a_mifare_ultralight_iso14443a_crc(input_buf , 2, input_buf + 2); /* get the crc */
2506 output_len = 18; /* set the output length */
2507 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
2508 if (res != 0) /* check the result */
2509 {
2510 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
2511
2512 return 1; /* return error */
2513 }
2514 if (output_len != 18) /* check the output_len */
2515 {
2516 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
2517
2518 return 4; /* return error */
2519 }
2520 a_mifare_ultralight_iso14443a_crc(output_buf, 16, crc_buf); /* get the crc */
2521 if ((output_buf[16] == crc_buf[0]) && (output_buf[17] == crc_buf[1])) /* check the crc */
2522 {
2523 memcpy(data, output_buf, 4); /* copy the data */
2524
2525 return 0; /* success return 0 */
2526 }
2527 else
2528 {
2529 handle->debug_print("mifare_ultralight: crc error.\n"); /* crc error */
2530
2531 return 5; /* return error */
2532 }
2533}
2534
2549{
2550 uint8_t res;
2551 uint8_t input_len;
2552 uint8_t input_buf[8];
2553 uint8_t output_len;
2554 uint8_t output_buf[1];
2555
2556 if (handle == NULL) /* check handle */
2557 {
2558 return 2; /* return error */
2559 }
2560 if (handle->inited != 1) /* check handle initialization */
2561 {
2562 return 3; /* return error */
2563 }
2564
2565 input_len = 8; /* set the input length */
2566 input_buf[0] = MIFARE_ULTRALIGHT_COMMAND_WRITE; /* set the command */
2567 input_buf[1] = 0x03; /* set the page */
2568 input_buf[2] = data[0]; /* set data0 */
2569 input_buf[3] = data[1]; /* set data1 */
2570 input_buf[4] = data[2]; /* set data2 */
2571 input_buf[5] = data[3]; /* set data3 */
2572 a_mifare_ultralight_iso14443a_crc(input_buf, 6, input_buf + 6); /* get the crc */
2573 output_len = 1; /* set the output length */
2574 res = handle->contactless_transceiver(input_buf, input_len, output_buf, &output_len); /* transceiver */
2575 if (res != 0) /* check the result */
2576 {
2577 handle->debug_print("mifare_ultralight: contactless transceiver failed.\n"); /* contactless transceiver failed */
2578
2579 return 1; /* return error */
2580 }
2581 if (output_len != 1) /* check the output_len */
2582 {
2583 handle->debug_print("mifare_ultralight: output_len is invalid.\n"); /* output_len is invalid */
2584
2585 return 4; /* return error */
2586 }
2587 if (output_buf[0] != 0xA) /* check the result */
2588 {
2589 handle->debug_print("mifare_ultralight: ack error.\n"); /* ack error */
2590
2591 return 5; /* return error */
2592 }
2593
2594 return 0; /* success return 0 */
2595}
2596
2609uint8_t mifare_ultralight_transceiver(mifare_ultralight_handle_t *handle, uint8_t *in_buf, uint8_t in_len, uint8_t *out_buf, uint8_t *out_len)
2610{
2611 if (handle == NULL) /* check handle */
2612 {
2613 return 2; /* return error */
2614 }
2615 if (handle->inited != 1) /* check handle initialization */
2616 {
2617 return 3; /* return error */
2618 }
2619
2620 if (handle->contactless_transceiver(in_buf, in_len,
2621 out_buf, out_len) != 0) /* transceiver data */
2622 {
2623 return 1; /* return error */
2624 }
2625 else
2626 {
2627 return 0; /* success return 0 */
2628 }
2629}
2630
2640{
2641 if (info == NULL) /* check handle */
2642 {
2643 return 2; /* return error */
2644 }
2645
2646 memset(info, 0, sizeof(mifare_ultralight_info_t)); /* initialize mifare_ultralight info structure */
2647 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2648 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2649 strncpy(info->interface, "RF", 8); /* copy interface name */
2650 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2651 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2652 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2653 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2654 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2655 info->driver_version = DRIVER_VERSION; /* set driver version */
2656
2657 return 0; /* success return 0 */
2658}
#define MIFARE_ULTRALIGHT_COMMAND_READ_CNT
#define MAX_CURRENT
#define MIFARE_ULTRALIGHT_COMMAND_PWD_AUTH
#define MIFARE_ULTRALIGHT_COMMAND_INCR_CNT
#define MIFARE_ULTRALIGHT_COMMAND_READ
#define MIFARE_ULTRALIGHT_COMMAND_GET_VERSION
#define MIFARE_ULTRALIGHT_COMMAND_WRITE
#define MIFARE_ULTRALIGHT_COMMAND_CHECK_TEARING_EVENT
#define SUPPLY_VOLTAGE_MAX
#define MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL1
#define MIFARE_ULTRALIGHT_COMMAND_SELECT_CL2
#define MIFARE_ULTRALIGHT_COMMAND_SELECT_CL1
#define MIFARE_ULTRALIGHT_COMMAND_READ_SIG
#define TEMPERATURE_MAX
#define MIFARE_ULTRALIGHT_COMMAND_FAST_READ
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define MIFARE_ULTRALIGHT_COMMAND_HALT
#define MIFARE_ULTRALIGHT_COMMAND_REQUEST
chip command definition
#define MIFARE_ULTRALIGHT_COMMAND_WAKE_UP
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define MIFARE_ULTRALIGHT_COMMAND_COMP_WRITE
#define MIFARE_ULTRALIGHT_COMMAND_ANTICOLLISION_CL2
driver mifare_ultralight header file
uint8_t mifare_ultralight_set_pack(mifare_ultralight_handle_t *handle, uint8_t pack[2])
mifare_ultralight set the pack
uint8_t mifare_ultralight_vcsl(mifare_ultralight_handle_t *handle, uint8_t installation_identifier[16], uint8_t pcd_capabilities[4], uint8_t *identifier)
mifare_ultralight vcsl command
uint8_t mifare_ultralight_get_lock(mifare_ultralight_handle_t *handle, uint8_t lock[5])
mifare_ultralight get the lock
uint8_t mifare_ultralight_set_storage(mifare_ultralight_handle_t *handle, mifare_ultralight_storage_t storage)
set the storage
struct mifare_ultralight_version_s mifare_ultralight_version_t
mifare ultralight version structure definition
mifare_ultralight_storage_t
mifare ultralight storage enumeration definition
uint8_t mifare_ultralight_get_virtual_card_type_identifier(mifare_ultralight_handle_t *handle, uint8_t *identifier)
mifare_ultralight get the virtual card type identifier
uint8_t mifare_ultralight_select_cl1(mifare_ultralight_handle_t *handle, uint8_t id[4])
mifare_ultralight select cl1
uint8_t mifare_ultralight_write_otp(mifare_ultralight_handle_t *handle, uint8_t data[4])
mifare_ultralight write otp page
uint8_t mifare_ultralight_halt(mifare_ultralight_handle_t *handle)
mifare_ultralight halt
struct mifare_ultralight_info_s mifare_ultralight_info_t
mifare ultralight information structure definition
uint8_t mifare_ultralight_get_serial_number(mifare_ultralight_handle_t *handle, uint8_t number[7])
mifare_ultralight get the serial number
uint8_t mifare_ultralight_set_access(mifare_ultralight_handle_t *handle, mifare_ultralight_access_t access, mifare_ultralight_bool_t enable)
mifare_ultralight enable or disable access
uint8_t mifare_ultralight_anticollision_cl1(mifare_ultralight_handle_t *handle, uint8_t id[4])
mifare_ultralight anti collision cl1
uint8_t mifare_ultralight_fast_read_page(mifare_ultralight_handle_t *handle, uint8_t start_page, uint8_t stop_page, uint8_t *data, uint16_t *len)
mifare_ultralight fast read page
mifare_ultralight_type_t
mifare ultralight type enumeration definition
uint8_t mifare_ultralight_set_modulation_mode(mifare_ultralight_handle_t *handle, mifare_ultralight_modulation_mode_t mode)
mifare_ultralight set the modulation mode
uint8_t mifare_ultralight_set_password(mifare_ultralight_handle_t *handle, uint8_t pwd[4])
mifare_ultralight set the password
uint8_t mifare_ultralight_init(mifare_ultralight_handle_t *handle)
initialize the chip
mifare_ultralight_access_t
mifare_ultralight access enumeration definition
uint8_t mifare_ultralight_get_storage(mifare_ultralight_handle_t *handle, mifare_ultralight_storage_t *storage)
get the storage
uint8_t mifare_ultralight_set_protect_start_page(mifare_ultralight_handle_t *handle, uint8_t page)
mifare_ultralight set the start page of protection
uint8_t mifare_ultralight_get_access(mifare_ultralight_handle_t *handle, mifare_ultralight_access_t access, mifare_ultralight_bool_t *enable)
mifare_ultralight get the access status
uint8_t mifare_ultralight_read_otp(mifare_ultralight_handle_t *handle, uint8_t data[4])
mifare_ultralight read otp page
uint8_t mifare_ultralight_read_signature(mifare_ultralight_handle_t *handle, uint8_t signature[32])
mifare_ultralight read the signature
mifare_ultralight_modulation_mode_t
mifare ultralight modulation mode enumeration definition
uint8_t mifare_ultralight_get_protect_start_page(mifare_ultralight_handle_t *handle, uint8_t *page)
mifare_ultralight get the start page of protection
uint8_t mifare_ultralight_request(mifare_ultralight_handle_t *handle, mifare_ultralight_type_t *type)
mifare_ultralight request
uint8_t mifare_ultralight_authenticate(mifare_ultralight_handle_t *handle, uint8_t pwd[4], uint8_t pack[2])
mifare_ultralight authenticate
uint8_t mifare_ultralight_set_authenticate_limitation(mifare_ultralight_handle_t *handle, uint8_t limit)
mifare_ultralight set the authenticate limitation
uint8_t mifare_ultralight_deinit(mifare_ultralight_handle_t *handle)
close the chip
uint8_t mifare_ultralight_increment_counter(mifare_ultralight_handle_t *handle, uint8_t addr, uint32_t cnt)
mifare_ultralight increment the counter
mifare_ultralight_bool_t
mifare ultralight bool enumeration definition
uint8_t mifare_ultralight_wake_up(mifare_ultralight_handle_t *handle, mifare_ultralight_type_t *type)
mifare_ultralight wake up
uint8_t mifare_ultralight_set_virtual_card_type_identifier(mifare_ultralight_handle_t *handle, uint8_t identifier)
mifare_ultralight set the virtual card type identifier
uint8_t mifare_ultralight_get_modulation_mode(mifare_ultralight_handle_t *handle, mifare_ultralight_modulation_mode_t *mode)
mifare_ultralight get the modulation mode
uint8_t mifare_ultralight_set_lock(mifare_ultralight_handle_t *handle, uint8_t lock[5])
mifare_ultralight set the lock
uint8_t mifare_ultralight_info(mifare_ultralight_info_t *info)
get chip information
uint8_t mifare_ultralight_read_page(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
mifare_ultralight read page
uint8_t mifare_ultralight_check_tearing_event(mifare_ultralight_handle_t *handle, uint8_t addr, uint8_t *flag)
mifare_ultralight check the tearing event
uint8_t mifare_ultralight_get_authenticate_limitation(mifare_ultralight_handle_t *handle, uint8_t *limit)
mifare_ultralight get the authenticate limitation
uint8_t mifare_ultralight_anticollision_cl2(mifare_ultralight_handle_t *handle, uint8_t id[4])
mifare_ultralight anti collision cl2
struct mifare_ultralight_handle_s mifare_ultralight_handle_t
mifare ultralight handle structure definition
uint8_t mifare_ultralight_select_cl2(mifare_ultralight_handle_t *handle, uint8_t id[4])
mifare_ultralight select cl2
uint8_t mifare_ultralight_read_counter(mifare_ultralight_handle_t *handle, uint8_t addr, uint32_t *cnt)
mifare_ultralight read the counter
uint8_t mifare_ultralight_read_four_pages(mifare_ultralight_handle_t *handle, uint8_t start_page, uint8_t data[16])
mifare_ultralight read four pages
uint8_t mifare_ultralight_compatibility_write_page(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
mifare_ultralight compatibility write page
uint8_t mifare_ultralight_write_page(mifare_ultralight_handle_t *handle, uint8_t page, uint8_t data[4])
mifare_ultralight write page
uint8_t mifare_ultralight_get_version(mifare_ultralight_handle_t *handle, mifare_ultralight_version_t *version)
mifare_ultralight get the version
@ MIFARE_ULTRALIGHT_STORAGE_MF0UL21
@ MIFARE_ULTRALIGHT_STORAGE_MF0UL11
@ MIFARE_ULTRALIGHT_TYPE_INVALID
@ MIFARE_ULTRALIGHT_TYPE_ULTRALIGHT
uint8_t mifare_ultralight_transceiver(mifare_ultralight_handle_t *handle, uint8_t *in_buf, uint8_t in_len, uint8_t *out_buf, uint8_t *out_len)
transceiver data
void(* debug_print)(const char *const fmt,...)
uint8_t(* contactless_transceiver)(uint8_t *in_buf, uint8_t in_len, uint8_t *out_buf, uint8_t *out_len)