-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNfcPcd.cpp
More file actions
1254 lines (1064 loc) · 29.8 KB
/
Copy pathNfcPcd.cpp
File metadata and controls
1254 lines (1064 loc) · 29.8 KB
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
/**
* @file NfcPcd.cpp
* @brief NFCのPCD(Proximity Coupling Device)アクセス実装.
* RC-S620/S用(今のところ).
*/
#include "NfcPcd.h"
#include "devaccess.h"
#include "misc.h"
// ログ用
#define LOG_TAG "NfcPcd"
#include "nfclog.h"
//#define ENABLE_FRAME_LOG
using namespace HkNfcRwMisc;
bool NfcPcd::m_bOpened = false; ///< オープンしているかどうか
uint8_t NfcPcd::s_CommandBuf[NfcPcd::DATA_MAX]; ///< PCDへの送信バッファ
uint8_t NfcPcd::s_ResponseBuf[NfcPcd::DATA_MAX]; ///< PCDからの受信バッファ
uint8_t NfcPcd::m_NfcId[NfcPcd::MAX_NFCID_LEN]; ///< 取得したNFCID
uint8_t NfcPcd::m_NfcIdLen; ///< 取得済みのNFCID長。0の場合は未取得。
/**
* const
*/
namespace {
const uint16_t RWBUF_MAX = NfcPcd::DATA_MAX + 10;
const uint16_t NORMAL_DATA_MAX = 255; //Normalフレームのデータ部最大
const uint16_t RESHEAD_LEN = 2;
const int POS_RESDATA = 2;
const uint8_t ACK[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
const int POS_NORMALFRM_DATA = 5;
const int POS_EXTENDFRM_DATA = 8;
}
/**
* バッファ
*/
namespace {
/// 送信バッファ
uint8_t s_SendBuf[RWBUF_MAX];
/// Normalフレームのデータ部
uint8_t* s_NormalFrmBuf = &(s_SendBuf[POS_NORMALFRM_DATA]);
/// Extendedフレームのデータ部
uint8_t* s_ExtendFrmBuf = &(s_SendBuf[POS_EXTENDFRM_DATA]);
/// 受信バッファ
uint8_t s_ResponseBuf[RWBUF_MAX];
}
/**
* 内部用関数
*/
namespace {
/**
* DCS計算
*
* @param[in] data 計算元データ
* @param[in] len dataの長さ
*
* @return DCS値
*/
uint8_t _calc_dcs(const uint8_t* data, uint16_t len)
{
uint8_t sum = 0;
for(uint16_t i = 0; i < len; i++) {
sum += data[i];
}
return (uint8_t)(0 - (sum & 0xff));
}
}
///////////////////////////////////////////////////////////////////////////
// 実装
///////////////////////////////////////////////////////////////////////////
/**
* シリアルポートオープン
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::portOpen()
{
if(m_bOpened) {
return false;
}
s_SendBuf[0] = 0x00;
s_SendBuf[1] = 0x00;
s_SendBuf[2] = 0xff;
m_bOpened = DevAccess::open();
return m_bOpened;
}
/**
* シリアルポートクローズ
*/
void NfcPcd::portClose()
{
if(m_bOpened) {
DevAccess::close();
m_bOpened = false;
}
}
/**
* デバイス初期化
*
* @retval true 初期化成功(=使用可能)
* @retval false 初期化失敗
* @attention 初期化失敗時には、#rfOff()を呼び出すこと
*/
bool NfcPcd::init()
{
//LOGD("%s", __PRETTY_FUNCTION__);
bool ret;
uint16_t res_len;
sendAck();
ret = reset();
if(!ret) {
return false;
}
// RF通信のT/O
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x32;
s_NormalFrmBuf[2] = 0x02; // T/O
s_NormalFrmBuf[3] = 0x00; // RFU
s_NormalFrmBuf[4] = 0x00; // ATR_RES : no timeout
s_NormalFrmBuf[5] = 0x00; // 非DEP通信時 : no timeout
ret = sendCmd(s_NormalFrmBuf, 6, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("d4 32 02\n");
return false;
}
// Target捕捉時のRF通信リトライ回数
s_NormalFrmBuf[2] = 0x05; // Retry
s_NormalFrmBuf[3] = 0x00; // ATR_REQ/RES : only once
s_NormalFrmBuf[4] = 0x00; // PSL_REQ/RES : only once
s_NormalFrmBuf[5] = 0x00; // InListPassiveTarget : only once
ret = sendCmd(s_NormalFrmBuf, 6, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("d4 32 05(%d)%d\n", ret, res_len);
return false;
}
// RF出力ONからTargetID取得コマンド送信までの追加ウェイト時間
s_NormalFrmBuf[2] = 0x81; // wait
s_NormalFrmBuf[3] = 0xb7; // 0.1 x prm + 5 + 0.7 [ms]
// 0xb7は、サンプルソースの値
ret = sendCmd(s_NormalFrmBuf, 4, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("d4 32 81(%d)%d\n", ret, res_len);
return false;
}
// DEPターゲットでのタイムアウト
s_NormalFrmBuf[2] = 0x82; // DEP Target Timeout
s_NormalFrmBuf[3] = 0x08; // gbyAtrResTo
// 00h : 302us
// 01h : 604us
// 02h : 1.208ms
// 03h : 2.417ms
// 04h : 4.833ms
// 05h : 9.666ms
// 06h : 19.332ms
// 07h : 38.664ms
// 08h : 77.329ms ★
// 09h : 154.657ms
// 0ah : 309.314ms
// 0bh : 618.629ms
// 0ch : 1.237s
// 0dh : 2.474s
// 0eh : 4.949s
s_NormalFrmBuf[4] = 0x01; // gbyRtox
// RWT x gbyRtox
s_NormalFrmBuf[5] = 0x08; // gbyTargetStoTo
// 00h : 951us
// 01h : 1.1ms
// 02h : 1.4ms
// 03h : 2.0ms
// 04h : 3.2ms
// 05h : 5.6ms
// 06h : 10.4ms
// 07h : 20.1ms
// 08h : 39.4ms ★
// 09h : 78.1ms
// 0ah : 155.4ms
// 0bh : 310.0ms
// 0ch : 619.sms
// 0dh : 1.237s
// 0eh : 2.474s
ret = sendCmd(s_NormalFrmBuf, 6, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("d4 32 82(%d)%d\n", ret, res_len);
//return false;
}
#if 0
// nfcpyのまね4
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x32; //RFConfiguration
s_NormalFrmBuf[2] = 0x0b;
hk_memcpy(s_NormalFrmBuf + 3, s_ResponseBuf + POS_RESDATA, READREG_LEN);
ret = sendCmd(s_NormalFrmBuf, 3+READREG_LEN, s_ResponseBuf, &res_len);
if(!ret || res_len != RESHEAD_LEN) {
LOGE("32 0b(%d) / %d\n", ret, res_len);
//return false;
}
#endif
return ret;
}
/**
* 搬送波停止
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::rfOff()
{
//LOGD("%s", __PRETTY_FUNCTION__);
uint16_t res_len;
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x32;
s_NormalFrmBuf[2] = 0x01; // RF field
s_NormalFrmBuf[3] = 0x00; // bit1 : Auto RFCA : OFF
// bit0 : RF ON/OFF : OFF
bool ret = sendCmd(s_NormalFrmBuf, 4, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("rfOff ret=%d\n", ret);
return false;
}
return true;
}
/**
* RFConfiguration
*
* @param[in] cmd コマンド
* @param[in] pCommand 送信するコマンド引数
* @param[in] CommandLen pCommandの長さ
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::rfConfiguration(uint8_t cmd, const uint8_t* pCommand, uint8_t CommandLen)
{
//LOGD("%s", __PRETTY_FUNCTION__);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x32; //RFConfiguration
s_NormalFrmBuf[2] = cmd;
hk_memcpy(s_NormalFrmBuf + 3, pCommand, CommandLen);
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 3 + CommandLen, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("rfConfiguration ret=%d\n", ret);
return false;
}
return true;
}
/**
* Reset
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::reset()
{
//LOGD("%s", __PRETTY_FUNCTION__);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x18; //Reset
s_NormalFrmBuf[2] = 0x01;
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 3, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("reset ret=%d\n", ret);
return false;
}
// execute
sendAck();
msleep(10);
return true;
}
/**
* Diagnose
*
* @param[in] pCommand 送信するコマンド
* @param[in] CommandLen pCommandの長さ
* @param[out] pResponse レスポンス
* @param[out] pResponseLen pResponseの長さ
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::diagnose(uint8_t cmd, const uint8_t* pCommand, uint8_t CommandLen,
uint8_t* pResponse, uint8_t* pResponseLen)
{
//LOGD("%s", __PRETTY_FUNCTION__);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x00; //Diagnose
s_NormalFrmBuf[2] = cmd;
hk_memcpy(s_NormalFrmBuf + 3, pCommand, CommandLen);
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 3 + CommandLen, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN)) {
LOGE("diagnose ret=%d/%d\n", ret, res_len);
return false;
}
*pResponseLen = res_len - RESHEAD_LEN;
hk_memcpy(pResponse, s_ResponseBuf + RESHEAD_LEN, *pResponseLen);
return true;
}
/**
* SetParameters
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::setParameters(uint8_t val)
{
//LOGD("%s", __PRETTY_FUNCTION__);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x12;
s_NormalFrmBuf[2] = val;
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 3, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN)) {
LOGE("setParam ret=%d\n", ret);
}
return true;
}
/**
* WriteRegister
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::writeRegister(const uint8_t* pCommand, uint8_t CommandLen)
{
//LOGD("%s", __PRETTY_FUNCTION__);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x08;
hk_memcpy(s_NormalFrmBuf + 2, pCommand, CommandLen);
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 2 + CommandLen, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN + CommandLen/2)) {
LOGE("writeReg ret=%d res_len=%d\n", ret, res_len);
} else {
for(int i=0; i<CommandLen/2; i++) {
if(s_ResponseBuf[2+i] != 0x00) {
LOGE("writeReg [%02x]\n", s_ResponseBuf[2+i]);
return false;
}
}
}
return true;
}
/**
* GetFirmwareVersion
*
* @param[out] pResponse レスポンス(GF_XXでアクセス)
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::getFirmwareVersion(uint8_t* pResponse)
{
//LOGD("%s\n", __PRETTY_FUNCTION__);
const uint16_t RES_LEN = 4;
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x02;
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 2, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN + RES_LEN)) {
LOGE("getFirmware ret=%d\n", ret);
return false;
}
if(pResponse) {
hk_memcpy(pResponse, s_ResponseBuf + RESHEAD_LEN, RES_LEN);
}
return true;
}
/**
* GetGeneralStatus
*
* @param[out] pResponse レスポンス(GGS_XXでアクセス)
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::getGeneralStatus(uint8_t* pResponse)
{
//LOGD("%s", __PRETTY_FUNCTION__);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x04;
const uint16_t RES_LEN = 5;
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 2, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN + RES_LEN)) {
LOGE("getGeneralStatus ret=%d/%d\n", ret, res_len);
return false;
}
hk_memcpy(pResponse, s_ResponseBuf + RESHEAD_LEN, RES_LEN);
return true;
}
/**
* CommunicateThruEX
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::communicateThruEx()
{
//LOGD("%s : [%d]", __PRETTY_FUNCTION__, CommandLen);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0xa0; //CommunicateThruEX
s_NormalFrmBuf[2] = 0x00;
s_NormalFrmBuf[3] = 0x00;
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 4, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+1)) {
LOGE("communicateThruEx ret=%d\n", ret);
return false;
}
return true;
}
/**
* CommunicateThruEX
*
* @param[in] pCommand 送信するコマンド
* @param[in] CommandLen pCommandの長さ
* @param[out] pResponse レスポンス
* @param[out] pResponseLen pResponseの長さ
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::communicateThruEx(
const uint8_t* pCommand, uint8_t CommandLen,
uint8_t* pResponse, uint8_t* pResponseLen)
{
//LOGD("%s : [%d]", __PRETTY_FUNCTION__, CommandLen);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0xa0; //CommunicateThruEX
hk_memcpy(s_NormalFrmBuf + 2, pCommand, CommandLen);
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 2 + CommandLen, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+1)) {
LOGE("communicateThruEx ret=%d\n", ret);
return false;
}
if(res_len == 3) {
//Statusを返す
pResponse[0] = s_ResponseBuf[2];
*pResponseLen = 1;
} else {
if((s_ResponseBuf[2] != 0x00) || (res_len != (3 + s_ResponseBuf[3]))) {
return false;
}
//Statusは返さない
*pResponseLen = (uint8_t)(s_ResponseBuf[3] - 1);
hk_memcpy(pResponse, s_ResponseBuf + 4, *pResponseLen);
}
return true;
}
/**
* CommunicateThruEX
*
* @param[in] Timeout タイムアウト値[msec]
* @param[in] pCommand 送信するコマンド
* @param[in] CommandLen pCommandの長さ
* @param[out] pResponse レスポンス
* @param[out] pResponseLen pResponseの長さ
*
* @retval true 成功
* @retval false 失敗
*
* @note -# #Timeoutは往復分の時間を設定すること.
*/
bool NfcPcd::communicateThruEx(
uint16_t Timeout,
const uint8_t* pCommand, uint8_t CommandLen,
uint8_t* pResponse, uint8_t* pResponseLen)
{
//LOGD("%s : (%d)", __PRETTY_FUNCTION__, CommandLen);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0xa0; //CommunicateThruEX
s_NormalFrmBuf[2] = l16(Timeout);
s_NormalFrmBuf[3] = h16(Timeout);
if(CommandLen) {
hk_memcpy(s_NormalFrmBuf + 4, pCommand, CommandLen);
CommandLen += 4;
}
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, CommandLen, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+1)) {
LOGE("communicateThruEx ret=%d\n", ret);
return false;
}
if(res_len == 3) {
//Statusを返す
pResponse[0] = s_ResponseBuf[POS_RESDATA];
*pResponseLen = 1;
} else {
//Statusは返さない
if((s_ResponseBuf[POS_RESDATA] != 0x00) || (res_len != (3 + s_ResponseBuf[POS_RESDATA+1]))) {
return false;
}
*pResponseLen = (uint8_t)(s_ResponseBuf[POS_RESDATA+1] - 1);
hk_memcpy(pResponse, s_ResponseBuf + RESHEAD_LEN + 2, *pResponseLen);
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// Initiator Command
/////////////////////////////////////////////////////////////////////////
/**
* InJumpForDEP or InJumpForPSL
*
* @param[in] pParam DEP initiatorパラメータ
*/
bool NfcPcd::_inJump(uint8_t Cmd, DepInitiatorParam* pParam)
{
//LOGD("%s", __PRETTY_FUNCTION__);
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = Cmd;
s_NormalFrmBuf[2] = (uint8_t)pParam->Ap;
s_NormalFrmBuf[3] = (uint8_t)pParam->Br;
s_NormalFrmBuf[4] = 0x00; //Next
uint8_t len = 5;
if(pParam->Ap == AP_PASSIVE) {
if(pParam->Br == BR_106K) {
// s_NormalFrmBuf[4] |= 0x01;
// const uint8_t known_id[4] = { 0x08, 0x01, 0x02, 0x03 };
// hk_memcpy(&(s_NormalFrmBuf[len]), known_id, sizeof(known_id));
// len += sizeof(known_id);
} else {
s_NormalFrmBuf[4] |= 0x01;
const uint8_t pol_req[5] = { 0x00, 0xff, 0xff, 0x01, 0x00 };
hk_memcpy(&(s_NormalFrmBuf[len]), pol_req, sizeof(pol_req));
len += sizeof(pol_req);
}
}
if(pParam->pNfcId3) {
s_NormalFrmBuf[4] |= 0x02;
hk_memcpy(&(s_NormalFrmBuf[len]), pParam->pNfcId3, NFCID3_LEN);
len += NFCID3_LEN;
}
if(pParam->pGb && pParam->GbLen) {
s_NormalFrmBuf[4] |= 0x04;
hk_memcpy(&(s_NormalFrmBuf[len]), pParam->pGb, pParam->GbLen);
len += pParam->GbLen;
}
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, len, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+17)) {
LOGE("inJumpForDep ret=%d/len=%d\n", ret, res_len);
return false;
}
if(pParam->pResponse) {
pParam->ResponseLen = (uint8_t)(res_len - (RESHEAD_LEN + 1));
hk_memcpy(pParam->pResponse, s_ResponseBuf + RESHEAD_LEN+1, pParam->ResponseLen);
}
return true;
}
/**
* InJumpForDEP
*
* @param[in/out] pParam DEP initiatorパラメータ
*/
bool NfcPcd::inJumpForDep(DepInitiatorParam* pParam)
{
//LOGD("%s", __PRETTY_FUNCTION__);
return _inJump(0x56, pParam);
}
/**
* InJumpForPSL
*
* @param[in/out] pParam DEP initiatorパラメータ
*/
bool NfcPcd::inJumpForPsl(DepInitiatorParam* pParam)
{
//LOGD("%s", __PRETTY_FUNCTION__);
return _inJump(0x46, pParam);
}
/**
* InListPassiveTarget
*
* @param[in] pInitData InListPassiveTargetの引数
* @param[in] InitLen pInitDataの長さ
* @param[out] ppTgData InListPassiveTargeの戻り値(内部バッファを返す)
* @param[out] pTgLen *ppTgDataの長さ
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::inListPassiveTarget(
const uint8_t* pInitData, uint8_t InitLen,
uint8_t** ppTgData, uint8_t* pTgLen)
{
uint16_t responseLen;
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x4a; //InListPassiveTarget
s_NormalFrmBuf[2] = 0x01;
hk_memcpy(s_NormalFrmBuf + 3, pInitData, InitLen);
bool ret = sendCmd(s_NormalFrmBuf, 3+InitLen, s_ResponseBuf, &responseLen);
if(!ret || s_ResponseBuf[POS_RESDATA] != 0x01) { //NbTg==1
return false;
}
*ppTgData = s_ResponseBuf;
*pTgLen = static_cast<uint8_t>(responseLen);
return true;
}
/**
* InDataExchange
*
* @param[in] pCommand 送信するコマンド
* @param[in] CommandLen pCommandの長さ
* @param[out] pResponse レスポンス
* @param[out] pResponseLen pResponseの長さ
* @param[in] bCoutinue MIフラグを立てるかどうか
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::inDataExchange(
const uint8_t* pCommand, uint8_t CommandLen,
uint8_t* pResponse, uint8_t* pResponseLen,
bool bCoutinue/*=false*/)
{
if(CommandLen > 252) {
LOGE("Too large\n");
return false;
}
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x40; //InDataExchange
s_NormalFrmBuf[2] = 0x01; //Tg
if(bCoutinue) {
s_NormalFrmBuf[2] |= 0x40; //MI
}
hk_memcpy(s_NormalFrmBuf + 3, pCommand, CommandLen);
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 3 + CommandLen, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+1) || (s_ResponseBuf[POS_RESDATA] != 0x00)) {
LOGE("inDataExchange ret=%d / len=%d / code=%02x\n", ret, res_len, s_ResponseBuf[POS_RESDATA]);
return false;
}
*pResponseLen = res_len - (RESHEAD_LEN+1);
hk_memcpy(pResponse, s_ResponseBuf + RESHEAD_LEN+1, *pResponseLen);
return true;
}
/**
* InCommunicateThru
*
* @param[in] pCommand 送信するコマンド
* @param[in] CommandLen pCommandの長さ
* @param[out] pResponse レスポンス
* @param[out] pResponseLen pResponseの長さ
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::inCommunicateThru(
const uint8_t* pCommand, uint8_t CommandLen,
uint8_t* pResponse, uint8_t* pResponseLen)
{
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x42; //InCommunicateThru
hk_memcpy(s_NormalFrmBuf + 2, pCommand, CommandLen);
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 2 + CommandLen, s_ResponseBuf, &res_len);
// for(int i=0; i<res_len; i++) {
// LOGD("%02x \n", s_ResponseBuf[i]);
// }
if(!ret || (res_len < RESHEAD_LEN+1) || (s_ResponseBuf[POS_RESDATA] != 0x00)) {
LOGE("InCommunicateThru ret=%d\n", ret);
return false;
}
*pResponseLen = res_len - (RESHEAD_LEN+1);
hk_memcpy(pResponse, s_ResponseBuf + RESHEAD_LEN+1, *pResponseLen);
return true;
}
/**
* InRelease
*
* DEP用。Targetの解放
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::inRelease()
{
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x52; //InRelease
s_NormalFrmBuf[2] = 0x00;
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 3, s_ResponseBuf, &res_len);
if(!ret || (res_len != RESHEAD_LEN+1) || (s_ResponseBuf[POS_RESDATA] != 0x00)) {
LOGE("inRelease ret=%d / len=%d / code=%02x\n", ret, res_len, s_ResponseBuf[POS_RESDATA]);
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// Target Command
/////////////////////////////////////////////////////////////////////////
/**
* TgInitAsTarget
*
* ターゲットとして起動する.
* 応答はTgResponseToInitiatorで返す.
*
* [in/out] pParam ターゲットパラメータ
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::tgInitAsTarget(TargetParam* pParam)
{
//LOGD("%s", __PRETTY_FUNCTION__);
uint16_t len = 0;
s_NormalFrmBuf[len++] = 0xd4;
s_NormalFrmBuf[len++] = 0x8c; //TgInitAsTarget
//mode
//s_NormalFrmBuf[len++] = 0x00; // Card Emu. OK && active OK
//s_NormalFrmBuf[len++] = 0x01; // Card Emu. OK && Passive Only
s_NormalFrmBuf[len++] = 0x02; // DEP only && active OK
//s_NormalFrmBuf[len++] = 0x03; // DEP only && Passive Only
//MIFARE
//SENS_RES
s_NormalFrmBuf[len++] = 0x00;
s_NormalFrmBuf[len++] = 0x04; //0x00はだめ
//NFCID1
s_NormalFrmBuf[len++] = 0x01;
s_NormalFrmBuf[len++] = 0x02;
s_NormalFrmBuf[len++] = 0x03;
//SEL_RES
s_NormalFrmBuf[len++] = 0x40; //固定
//FeliCaParams
//IDm
uint8_t idm_pos = len;
//自動生成
s_NormalFrmBuf[len++] = 0x01;
s_NormalFrmBuf[len++] = 0xfe;
s_NormalFrmBuf[len++] = 0xff;
s_NormalFrmBuf[len++] = 0xff;
s_NormalFrmBuf[len++] = 0xff;
s_NormalFrmBuf[len++] = 0xff;
s_NormalFrmBuf[len++] = 0xff;
s_NormalFrmBuf[len++] = 0xff;
//PMm
hk_memset(s_NormalFrmBuf + len, 0xff, 8);
len += 8;
//SystemCode
#if 0
s_NormalFrmBuf[len++] = h16(pParam->SystemCode);
s_NormalFrmBuf[len++] = l16(pParam->SystemCode);
#else
s_NormalFrmBuf[len++] = 0xff;
s_NormalFrmBuf[len++] = 0xff;
#endif
//NFCID3t
hk_memcpy(s_NormalFrmBuf + len, s_NormalFrmBuf + idm_pos, NFCID2_LEN);
len += NFCID2_LEN;
s_NormalFrmBuf[len++] = 0x00;
s_NormalFrmBuf[len++] = 0x00;
//General Bytes
if(pParam->GbLen) {
hk_memcpy(s_NormalFrmBuf + len, pParam->pGb, pParam->GbLen);
len += pParam->GbLen;
}
// for(int i=0; i<len; i++) {
// LOGD("%02x ", s_NormalFrmBuf[i]);
// }
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, len, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+1)) {
LOGE("TgInitAsTarget ret=%d/len=%d\n", ret, res_len);
return ret;
}
if(pParam->pCommand) {
//Activated情報以下
pParam->CommandLen = (uint8_t)(res_len - 2);
hk_memcpy(pParam->pCommand, s_ResponseBuf + 2, pParam->CommandLen);
}
return true;
}
bool NfcPcd::tgSetGeneralBytes(const TargetParam* pParam)
{
if(pParam->GbLen > 48) {
LOGE("Too large\n");
return false;
}
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x92; //TgSetGeneralBytes
if(pParam->GbLen) {
hk_memcpy(s_NormalFrmBuf + 2, pParam->pGb, pParam->GbLen);
}
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 2 + pParam->GbLen, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+1) || (s_ResponseBuf[POS_RESDATA] != 0x00)) {
LOGE("tgSetGeneralBytes ret=%d / len=%d / code=%02x\n", ret, res_len, s_ResponseBuf[POS_RESDATA]);
return false;
}
return true;
}
/**
* TgResponseToInitiator
*
* TgInitAsTargetおよびTgGetInitiatorCommandのレスポンスに対するカードとしての応答
*
* @param[in] pData 応答データ
* @param[in] DataLen pDataの長さ
* @param[out] pResponse レスポンス
* @param[out] pResponseLen pResponseの長さ
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::tgResponseToInitiator(
const uint8_t* pData, uint8_t DataLen,
uint8_t* pResponse/*=0*/, uint8_t* pResponseLen/*=0*/)
{
//LOGD("%s", __PRETTY_FUNCTION__);
uint16_t len = 0;
s_NormalFrmBuf[len++] = 0xd4;
s_NormalFrmBuf[len++] = 0x90; //TgResponseToInitiator
//応答
//ここは練り直した方がいい
s_NormalFrmBuf[len++] = 0xd5;
hk_memcpy(&(s_NormalFrmBuf[len]), pData, DataLen);
len += DataLen;
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, len, s_ResponseBuf, &res_len);
if(!ret || (res_len != 3) || s_ResponseBuf[POS_RESDATA] != 0x00) {
LOGE("tgResponseToInitiator ret=%d\n", ret);
return false;
}
if(pResponse && pResponseLen) {
*pResponseLen = uint8_t(res_len - (RESHEAD_LEN+1));
hk_memcpy(pResponse, &(s_ResponseBuf[POS_RESDATA+1]), *pResponseLen);
}
return true;
}
/**
* TgGetInitiatorCommand
*
* Initiatorからのコマンドを取得する。
* 応答はTgResponseToInitiatorで返す。
*
* @param[out] pResponse 応答データ
* @param[out] pResponseLen pResponseの長さ
*
* @retval true 成功
* @retval false 失敗
*/
bool NfcPcd::tgGetInitiatorCommand(uint8_t* pResponse, uint8_t* pResponseLen)
{
s_NormalFrmBuf[0] = 0xd4;
s_NormalFrmBuf[1] = 0x88; //TgGetInitiatorCommand
uint16_t res_len;
bool ret = sendCmd(s_NormalFrmBuf, 2, s_ResponseBuf, &res_len);
if(!ret || (res_len < RESHEAD_LEN+1) || (s_ResponseBuf[POS_RESDATA] != 0x00)) {
LOGE("tgGetInitiatorCommand ret=%d / len=%d / code=%02x\n", ret, res_len, s_ResponseBuf[POS_RESDATA]);
return false;
}