lin
2025-03-11 6f4f7a76e03a46fefb056a4b18197f1d9e8aa939
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
/******************************************************************************
 *
 *  Copyright (C) 2010-2014 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/
 
/******************************************************************************
 *
 *  NFA interface for tag Reader/Writer
 *
 ******************************************************************************/
#include <string.h>
 
#include <android-base/stringprintf.h>
#include <base/logging.h>
 
#include "nfa_api.h"
#include "nfa_rw_int.h"
 
using android::base::StringPrintf;
 
extern bool nfc_debug_enabled;
 
/*****************************************************************************
**  Constants
*****************************************************************************/
 
/*****************************************************************************
**  APIs
*****************************************************************************/
 
/*******************************************************************************
**
** Function         NFA_RwDetectNDef
**
** Description      Perform the NDEF detection procedure  using the appropriate
**                  method for the currently activated tag.
**
**                  Upon successful completion of NDEF detection, a
**                  NFA_NDEF_DETECT_EVT will be sent, to notify the application
**                  of the NDEF attributes (NDEF total memory size, current
**                  size, etc.).
**
**                  It is not mandatory to call this function -  NFA_RwReadNDef
**                  and NFA_RwWriteNDef will perform NDEF detection internally
**                  if not performed already. This API may be called to get a
**                  tag's NDEF size before issuing a write-request.
**
** Returns:
**                  NFA_STATUS_OK if successfully initiated
**                  NFC_STATUS_REFUSED if tag does not support NDEF
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwDetectNDef(void) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_DETECT_NDEF;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwReadNDef
**
** Description      Read NDEF message from tag. This function will internally
**                  perform the NDEF detection procedure (if not performed
**                  previously), and read the NDEF tag data using the
**                  appropriate method for the currently activated tag.
**
**                  Upon successful completion of NDEF detection (if performed),
**                  a NFA_NDEF_DETECT_EVT will be sent, to notify the
**                  application of the NDEF attributes (NDEF total memory size,
**                  current size, etc.).
**
**                  Upon receiving the NDEF message, the message will be sent to
**                  the handler registered with NFA_RegisterNDefTypeHandler or
**                  NFA_RequestExclusiveRfControl (if exclusive RF mode is
**                  active)
**
** Returns:
**                  NFA_STATUS_OK if successfully initiated
**                  NFC_STATUS_REFUSED if tag does not support NDEF
**                  NFC_STATUS_NOT_INITIALIZED if NULL NDEF was detected on the
**                  tag
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwReadNDef(void) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_READ_NDEF;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwWriteNDef
**
** Description      Write NDEF data to the activated tag. This function will
**                  internally perform NDEF detection if necessary, and write
**                  the NDEF tag data using the appropriate method for the
**                  currently activated tag.
**
**                  When the entire message has been written, or if an error
**                  occurs, the app will be notified with NFA_WRITE_CPLT_EVT.
**
**                  p_data needs to be persistent until NFA_WRITE_CPLT_EVT
**
**
** Returns:
**                  NFA_STATUS_OK if successfully initiated
**                  NFC_STATUS_REFUSED if tag does not support NDEF/locked
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwWriteNDef(uint8_t* p_data, uint32_t len) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("ndef len: %i", len);
 
  /* Validate parameters */
  if (p_data == nullptr) return (NFA_STATUS_INVALID_PARAM);
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_WRITE_NDEF;
    p_msg->params.write_ndef.len = len;
    p_msg->params.write_ndef.p_data = p_data;
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*****************************************************************************
**
** Function         NFA_RwPresenceCheck
**
** Description      Check if the tag is still in the field.
**
**                  The NFA_RW_PRESENCE_CHECK_EVT w/ status is used to
**                  indicate presence or non-presence.
**
**                  option is used only with ISO-DEP protocol
**
** Returns
**                  NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*****************************************************************************/
tNFA_STATUS NFA_RwPresenceCheck(tNFA_RW_PRES_CHK_OPTION option) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_PRESENCE_CHECK;
    p_msg->params.option = option;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*****************************************************************************
**
** Function         NFA_RwFormatTag
**
** Description      Check if the tag is NDEF Formatable. If yes Format the tag
**
**                  The NFA_RW_FORMAT_CPLT_EVT w/ status is used to
**                  indicate if tag is successfully formated or not
**
** Returns
**                  NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*****************************************************************************/
tNFA_STATUS NFA_RwFormatTag(void) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_FORMAT_TAG;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwSetTagReadOnly
**
** Description:
**      Sets tag as read only.
**
**      When tag is set as read only, or if an error occurs, the app will be
**      notified with NFA_SET_TAG_RO_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_REJECTED if protocol is not T1/T2/T5T
**                 (or) if hard lock is not requested for protocol T5T
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwSetTagReadOnly(bool b_hard_lock) {
  tNFA_RW_OPERATION* p_msg;
  tNFC_PROTOCOL protocol = nfa_rw_cb.protocol;
 
  if ((protocol != NFC_PROTOCOL_T1T) && (protocol != NFC_PROTOCOL_T2T) &&
      (protocol != NFC_PROTOCOL_T5T) && (protocol != NFC_PROTOCOL_ISO_DEP) &&
      (protocol != NFC_PROTOCOL_T3T)) {
    DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
        "Cannot Configure as read only for Protocol: "
        "%d",
        protocol);
    return (NFA_STATUS_REJECTED);
  }
 
  if ((!b_hard_lock && (protocol == NFC_PROTOCOL_T5T)) ||
      (b_hard_lock && (protocol == NFC_PROTOCOL_ISO_DEP))) {
    DLOG_IF(INFO, nfc_debug_enabled)
        << StringPrintf("Cannot %s for Protocol: %d",
                        b_hard_lock ? "Hard lock" : "Soft lock", protocol);
    return (NFA_STATUS_REJECTED);
  }
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("%s", b_hard_lock ? "Hard lock" : "Soft lock");
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_SET_TAG_RO;
    p_msg->params.set_readonly.b_hard_lock = b_hard_lock;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
** Tag specific APIs
** (note: for Type-4 tags, use NFA_SendRawFrame to exchange APDUs)
*******************************************************************************/
 
/*******************************************************************************
**
** Function         NFA_RwLocateTlv
**
** Description:
**      Search for the Lock/Memory contril TLV on the activated Type1/Type2 tag
**
**      Data is returned to the application using the NFA_TLV_DETECT_EVT. When
**      search operation has completed, or if an error occurs, the app will be
**      notified with NFA_TLV_DETECT_EVT.
**
** Description      Perform the TLV detection procedure  using the appropriate
**                  method for the currently activated tag.
**
**                  Upon successful completion of TLV detection in T1/T2 tag, a
**                  NFA_TLV_DETECT_EVT will be sent, to notify the application
**                  of the TLV attributes (total lock/reserved bytes etc.).
**                  However if the TLV type specified is NDEF then it is same as
**                  calling NFA_RwDetectNDef and should expect to receive
**                  NFA_NDEF_DETECT_EVT instead of NFA_TLV_DETECT_EVT
**
**                  It is not mandatory to call this function -
**                  NFA_RwDetectNDef, NFA_RwReadNDef and NFA_RwWriteNDef will
**                  perform TLV detection internally if not performed already.
**                  An application may call this API to check the a
**                  tag/card-emulator's total Reserved/
**                  Lock bytes before issuing a write-request.
**
** Returns:
**                  NFA_STATUS_OK if successfully initiated
**                  NFC_STATUS_REFUSED if tlv_type is NDEF & tag won't support
**                  NDEF
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwLocateTlv(uint8_t tlv_type) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
 
    if (tlv_type == TAG_LOCK_CTRL_TLV) {
      p_msg->op = NFA_RW_OP_DETECT_LOCK_TLV;
    } else if (tlv_type == TAG_MEM_CTRL_TLV) {
      p_msg->op = NFA_RW_OP_DETECT_MEM_TLV;
    } else if (tlv_type == TAG_NDEF_TLV) {
      p_msg->op = NFA_RW_OP_DETECT_NDEF;
    } else
      return (NFA_STATUS_FAILED);
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT1tRid
**
** Description:
**      Send a RID command to the activated Type 1 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT1tRid(void) {
  tNFA_RW_OPERATION* p_msg;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T1T_RID;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT1tReadAll
**
** Description:
**      Send a RALL command to the activated Type 1 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT1tReadAll(void) {
  tNFA_RW_OPERATION* p_msg;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T1T_RALL;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT1tRead
**
** Description:
**      Send a READ command to the activated Type 1 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT1tRead(uint8_t block_number, uint8_t index) {
  tNFA_RW_OPERATION* p_msg;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T1T_READ;
    p_msg->params.t1t_read.block_number = block_number;
    p_msg->params.t1t_read.index = index;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT1tWrite
**
** Description:
**      Send a WRITE command to the activated Type 1 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      write operation has completed, or if an error occurs, the app will be
**      notified with NFA_WRITE_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT1tWrite(uint8_t block_number, uint8_t index, uint8_t data,
                           bool b_erase) {
  tNFA_RW_OPERATION* p_msg;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->params.t1t_write.b_erase = b_erase;
    p_msg->op = NFA_RW_OP_T1T_WRITE;
    p_msg->params.t1t_write.block_number = block_number;
    p_msg->params.t1t_write.index = index;
    p_msg->params.t1t_write.p_block_data[0] = data;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT1tReadSeg
**
** Description:
**      Send a RSEG command to the activated Type 1 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT1tReadSeg(uint8_t segment_number) {
  tNFA_RW_OPERATION* p_msg;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T1T_RSEG;
    p_msg->params.t1t_read.segment_number = segment_number;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT1tRead8
**
** Description:
**      Send a READ8 command to the activated Type 1 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT1tRead8(uint8_t block_number) {
  tNFA_RW_OPERATION* p_msg;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T1T_READ8;
    p_msg->params.t1t_write.block_number = block_number;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT1tWrite8
**
** Description:
**      Send a WRITE8_E / WRITE8_NE command to the activated Type 1 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT1tWrite8(uint8_t block_number, uint8_t* p_data,
                            bool b_erase) {
  tNFA_RW_OPERATION* p_msg;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->params.t1t_write.b_erase = b_erase;
    p_msg->op = NFA_RW_OP_T1T_WRITE8;
    p_msg->params.t1t_write.block_number = block_number;
 
    memcpy(p_msg->params.t1t_write.p_block_data, p_data, 8);
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT2tRead
**
** Description:
**      Send a READ command to the activated Type 2 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT2tRead(uint8_t block_number) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("Block to read: %d", block_number);
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T2T_READ;
    p_msg->params.t2t_read.block_number = block_number;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT2tWrite
**
** Description:
**      Send an WRITE command to the activated Type 2 tag.
**
**      When the write operation has completed (or if an error occurs), the
**      app will be notified with NFA_WRITE_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT2tWrite(uint8_t block_number, uint8_t* p_data) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("Block to write: %d", block_number);
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T2T_WRITE;
 
    p_msg->params.t2t_write.block_number = block_number;
 
    memcpy(p_msg->params.t2t_write.p_block_data, p_data, 4);
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT2tSectorSelect
**
** Description:
**      Send SECTOR SELECT command to the activated Type 2 tag.
**
**      When the sector select operation has completed (or if an error occurs),
**      the app will be notified with NFA_SECTOR_SELECT_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT2tSectorSelect(uint8_t sector_number) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("sector to select: %d", sector_number);
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T2T_SECTOR_SELECT;
 
    p_msg->params.t2t_sector_select.sector_number = sector_number;
 
    nfa_sys_sendmsg(p_msg);
    return (NFA_STATUS_OK);
  }
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT3tRead
**
** Description:
**      Send a CHECK (read) command to the activated Type 3 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT3tRead(uint8_t num_blocks, tNFA_T3T_BLOCK_DESC* t3t_blocks) {
  tNFA_RW_OPERATION* p_msg;
  uint8_t* p_block_desc;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("num_blocks to read: %i", num_blocks);
 
  /* Validate parameters */
  if ((num_blocks == 0) || (t3t_blocks == nullptr))
    return (NFA_STATUS_INVALID_PARAM);
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(
      sizeof(tNFA_RW_OPERATION) + (num_blocks * sizeof(tNFA_T3T_BLOCK_DESC))));
  if (p_msg != nullptr) {
    /* point to area after tNFA_RW_OPERATION */
    p_block_desc = (uint8_t*)(p_msg + 1);
 
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T3T_READ;
 
    p_msg->params.t3t_read.num_blocks = num_blocks;
    p_msg->params.t3t_read.p_block_desc = (tNFA_T3T_BLOCK_DESC*)p_block_desc;
 
    /* Copy block descriptor list */
    memcpy(p_block_desc, t3t_blocks,
           (num_blocks * sizeof(tNFA_T3T_BLOCK_DESC)));
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwT3tWrite
**
** Description:
**      Send an UPDATE (write) command to the activated Type 3 tag.
**
**      When the write operation has completed (or if an error occurs), the
**      app will be notified with NFA_WRITE_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT3tWrite(uint8_t num_blocks, tNFA_T3T_BLOCK_DESC* t3t_blocks,
                           uint8_t* p_data) {
  tNFA_RW_OPERATION* p_msg;
  uint8_t *p_block_desc, *p_data_area;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("num_blocks to write: %i", num_blocks);
 
  /* Validate parameters */
  if ((num_blocks == 0) || (t3t_blocks == nullptr) | (p_data == nullptr))
    return (NFA_STATUS_INVALID_PARAM);
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf(
      (uint16_t)(sizeof(tNFA_RW_OPERATION) +
                 (num_blocks * (sizeof(tNFA_T3T_BLOCK_DESC) + 16))));
  if (p_msg != nullptr) {
    /* point to block descriptor and data areas after tNFA_RW_OPERATION */
    p_block_desc = (uint8_t*)(p_msg + 1);
    p_data_area = p_block_desc + (num_blocks * (sizeof(tNFA_T3T_BLOCK_DESC)));
 
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_T3T_WRITE;
 
    p_msg->params.t3t_write.num_blocks = num_blocks;
    p_msg->params.t3t_write.p_block_desc = (tNFA_T3T_BLOCK_DESC*)p_block_desc;
    p_msg->params.t3t_write.p_block_data = p_data_area;
 
    /* Copy block descriptor list */
    memcpy(p_block_desc, t3t_blocks,
           (num_blocks * sizeof(tNFA_T3T_BLOCK_DESC)));
 
    /* Copy data */
    memcpy(p_data_area, p_data, (num_blocks * 16));
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93Inventory
**
** Description:
**      Send Inventory command to the activated ISO T5T tag with/without AFI
**      If UID is provided then set UID[0]:MSB, ... UID[7]:LSB
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93Inventory(bool afi_present, uint8_t afi, uint8_t* p_uid) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("afi_present:%d, AFI: 0x%02X", afi_present, afi);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_INVENTORY;
 
    p_msg->params.i93_cmd.afi_present = afi_present;
    p_msg->params.i93_cmd.afi = afi;
 
    if (p_uid) {
      p_msg->params.i93_cmd.uid_present = true;
      memcpy(p_msg->params.i93_cmd.uid, p_uid, I93_UID_BYTE_LEN);
    } else {
      p_msg->params.i93_cmd.uid_present = false;
    }
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93StayQuiet
**
** Description:
**      Send Stay Quiet command to the activated T5T tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93StayQuiet(void) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_STAY_QUIET;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93ReadSingleBlock
**
** Description:
**      Send Read Single Block command to the activated T5T tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93ReadSingleBlock(uint8_t block_number) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("block_number: 0x%02X", block_number);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_READ_SINGLE_BLOCK;
 
    p_msg->params.i93_cmd.first_block_number = block_number;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93WriteSingleBlock
**
** Description:
**      Send Write Single Block command to the activated T5T tag.
**
**      When the write operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93WriteSingleBlock(uint8_t block_number, uint8_t* p_data) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("block_number: 0x%02X", block_number);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  /* we don't know block size of tag */
  if ((nfa_rw_cb.i93_block_size == 0) || (nfa_rw_cb.i93_num_block == 0)) {
    return (NFA_STATUS_FAILED);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf(
      (uint16_t)(sizeof(tNFA_RW_OPERATION) + nfa_rw_cb.i93_block_size));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_WRITE_SINGLE_BLOCK;
 
    p_msg->params.i93_cmd.first_block_number = block_number;
    p_msg->params.i93_cmd.p_data = (uint8_t*)(p_msg + 1);
 
    memcpy(p_msg->params.i93_cmd.p_data, p_data, nfa_rw_cb.i93_block_size);
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93LockBlock
**
** Description:
**      Send Lock block command to the activated T5T tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93LockBlock(uint8_t block_number) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("block_number: 0x%02X", block_number);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_LOCK_BLOCK;
 
    p_msg->params.i93_cmd.first_block_number = block_number;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93ReadMultipleBlocks
**
** Description:
**      Send Read Multiple Block command to the activated T5T tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93ReadMultipleBlocks(uint8_t first_block_number,
                                        uint16_t number_blocks) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("%d, %d", first_block_number, number_blocks);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_READ_MULTI_BLOCK;
 
    p_msg->params.i93_cmd.first_block_number = first_block_number;
    p_msg->params.i93_cmd.number_blocks = number_blocks;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93WriteMultipleBlocks
**
** Description:
**      Send Write Multiple Block command to the activated T5T tag.
**
**      When the write operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93WriteMultipleBlocks(uint8_t first_block_number,
                                         uint16_t number_blocks,
                                         uint8_t* p_data) {
  tNFA_RW_OPERATION* p_msg;
  uint16_t data_length;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("%d, %d", first_block_number, number_blocks);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  /* we don't know block size of tag */
  if ((nfa_rw_cb.i93_block_size == 0) || (nfa_rw_cb.i93_num_block == 0)) {
    return (NFA_STATUS_FAILED);
  }
 
  data_length = nfa_rw_cb.i93_block_size * number_blocks;
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf(
      (uint16_t)(sizeof(tNFA_RW_OPERATION) + data_length));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_WRITE_MULTI_BLOCK;
 
    p_msg->params.i93_cmd.first_block_number = first_block_number;
    p_msg->params.i93_cmd.number_blocks = number_blocks;
    p_msg->params.i93_cmd.p_data = (uint8_t*)(p_msg + 1);
 
    memcpy(p_msg->params.i93_cmd.p_data, p_data, data_length);
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93Select
**
** Description:
**      Send Select command to the activated T5T tag.
**
**      UID[0]: 0xE0, MSB
**      UID[1]: IC Mfg Code
**      ...
**      UID[7]: LSB
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93Select(uint8_t* p_uid) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
      "UID: [%02X%02X%02X...]", *(p_uid), *(p_uid + 1), *(p_uid + 2));
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf(
      (uint16_t)(sizeof(tNFA_RW_OPERATION) + I93_UID_BYTE_LEN));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_SELECT;
 
    p_msg->params.i93_cmd.p_data = (uint8_t*)(p_msg + 1);
    memcpy(p_msg->params.i93_cmd.p_data, p_uid, I93_UID_BYTE_LEN);
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93ResetToReady
**
** Description:
**      Send Reset to ready command to the activated T5T tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93ResetToReady(void) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_RESET_TO_READY;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93WriteAFI
**
** Description:
**      Send Write AFI command to the activated T5T tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93WriteAFI(uint8_t afi) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("AFI: 0x%02X", afi);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_WRITE_AFI;
 
    p_msg->params.i93_cmd.afi = afi;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93LockAFI
**
** Description:
**      Send Lock AFI command to the activated T5T tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93LockAFI(void) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_LOCK_AFI;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93WriteDSFID
**
** Description:
**      Send Write DSFID command to the activated T5T tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93WriteDSFID(uint8_t dsfid) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("DSFID: 0x%02X", dsfid);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_WRITE_DSFID;
 
    p_msg->params.i93_cmd.dsfid = dsfid;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93LockDSFID
**
** Description:
**      Send Lock DSFID command to the activated T5T tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93LockDSFID(void) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_LOCK_DSFID;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93GetSysInfo
**
** Description:
**      Send Get system information command to the activated T5T tag.
**      If UID is provided then set UID[0]:MSB, ... UID[7]:LSB
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93GetSysInfo(uint8_t* p_uid) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled) << __func__;
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_GET_SYS_INFO;
 
    if (p_uid) {
      p_msg->params.i93_cmd.uid_present = true;
      memcpy(p_msg->params.i93_cmd.uid, p_uid, I93_UID_BYTE_LEN);
    } else {
      p_msg->params.i93_cmd.uid_present = false;
    }
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}
 
/*******************************************************************************
**
** Function         NFA_RwI93GetMultiBlockSecurityStatus
**
** Description:
**      Send Get Multiple block security status command to the activated
**      T5T tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the
**      read operation has completed, or if an error occurs, the app will be
**      notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: T5T tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93GetMultiBlockSecurityStatus(uint8_t first_block_number,
                                                 uint16_t number_blocks) {
  tNFA_RW_OPERATION* p_msg;
 
  DLOG_IF(INFO, nfc_debug_enabled)
      << StringPrintf("%d, %d", first_block_number, number_blocks);
 
  if (nfa_rw_cb.protocol != NFC_PROTOCOL_T5T) {
    return (NFA_STATUS_WRONG_PROTOCOL);
  }
 
  p_msg = (tNFA_RW_OPERATION*)GKI_getbuf((uint16_t)(sizeof(tNFA_RW_OPERATION)));
  if (p_msg != nullptr) {
    /* Fill in tNFA_RW_OPERATION struct */
    p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    p_msg->op = NFA_RW_OP_I93_GET_MULTI_BLOCK_STATUS;
 
    p_msg->params.i93_cmd.first_block_number = first_block_number;
    p_msg->params.i93_cmd.number_blocks = number_blocks;
 
    nfa_sys_sendmsg(p_msg);
 
    return (NFA_STATUS_OK);
  }
 
  return (NFA_STATUS_FAILED);
}