-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHkNfcSnep.cpp
223 lines (191 loc) · 4.17 KB
/
HkNfcSnep.cpp
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
#include "HkNfcRule.h"
#include "HkNfcSnep.h"
#include "HkNfcLlcpI.h"
#include "HkNfcLlcpT.h"
#include "NfcPcd.h"
const HkNfcNdefMsg* HkNfcSnep::m_pMessage = 0;
HkNfcSnep::Mode HkNfcSnep::m_Mode = HkNfcSnep::MD_INITIATOR;
HkNfcSnep::Status HkNfcSnep::m_Status = HkNfcSnep::ST_INIT;
bool (*HkNfcSnep::m_PollFunc)();
namespace {
const uint8_t SNEP_SUCCESS = 0x81;
}
HkNfcSnep::Result HkNfcSnep::getResult()
{
switch(m_Status) {
case ST_INIT:
case ST_SUCCESS:
return SUCCESS;
case ST_ABORT:
return FAIL;
default:
return PROCESSING;
}
}
/**
* SNEP PUT開始.
* NDEFメッセージはPUT完了まで保持するので、解放したり書き換えたりしないこと.
*
* @param[in] mode モード
* @param[in] pMsg NDEFメッセージ(送信が終わるまで保持する)
* @return 開始成功/失敗
*/
bool HkNfcSnep::putStart(Mode mode, const HkNfcNdefMsg* pMsg)
{
if((m_pMessage != 0) || (pMsg == 0)) {
return false;
}
m_Status = ST_START_PUT;
m_pMessage = pMsg;
if(mode == MD_INITIATOR) {
m_Mode = MD_INITIATOR;
#ifdef HKNFCRW_USE_SNEPI
m_PollFunc = HkNfcSnep::pollI;
#endif
} else {
m_Mode = MD_TARGET;
#ifdef HKNFCRW_USE_SNEPT
m_PollFunc = HkNfcSnep::pollT;
#endif
}
return true;
}
bool HkNfcSnep::poll()
{
return (*m_PollFunc)();
}
void HkNfcSnep::stop()
{
if(m_Mode == MD_INITIATOR) {
#ifdef HKNFCRW_USE_SNEPI
HkNfcLlcpI::stopRequest();
HkNfcDep::stopAsInitiator();
#endif
} else {
#ifdef HKNFCRW_USE_SNEPT
HkNfcLlcpT::stopRequest();
#endif
}
}
#ifdef HKNFCRW_USE_SNEPI
bool HkNfcSnep::pollI()
{
bool b = false;
switch(m_Status) {
case ST_START_PUT:
b = HkNfcLlcpI::start(HkNfcLlcpI::PSV_424K, HkNfcSnep::recvCbI);
if(b) {
uint8_t snep_head[6];
snep_head[0] = 0x10;
snep_head[1] = 0x02; //PUT
snep_head[2] = (uint8_t)((m_pMessage->Length) >> 24);
snep_head[3] = (uint8_t)((m_pMessage->Length) >> 16);
snep_head[4] = (uint8_t)((m_pMessage->Length) >> 8);
snep_head[5] = (uint8_t)(m_pMessage->Length);
b = HkNfcLlcpI::addSendData(snep_head, sizeof(snep_head));
b = b && HkNfcLlcpI::addSendData(m_pMessage->Data, m_pMessage->Length);
}
if(b) {
m_Status = ST_PUT;
} else {
m_Status = ST_ABORT;
HkNfcLlcpI::stopRequest();
}
break;
case ST_PUT:
b = HkNfcLlcpI::sendRequest();
if(b) {
m_Status = ST_PUT_RESPONSE;
} else {
m_Status = ST_ABORT;
HkNfcLlcpI::stopRequest();
}
break;
case ST_PUT_RESPONSE:
case ST_SUCCESS:
case ST_ABORT:
b = HkNfcLlcpI::poll();
break;
}
if(!b) {
m_pMessage = 0;
}
return b;
}
void HkNfcSnep::recvCbI(const void* pBuf, uint8_t len)
{
const uint8_t* pData = reinterpret_cast<const uint8_t*>(pBuf);
switch(m_Status) {
case ST_PUT_RESPONSE:
//PUT後の応答
if(pData[1] == SNEP_SUCCESS) {
m_Status = ST_SUCCESS;
} else {
m_Status = ST_ABORT;
}
HkNfcLlcpI::stopRequest();
break;
}
}
#endif
#ifdef HKNFCRW_USE_SNEPT
bool HkNfcSnep::pollT()
{
bool b = false;
switch(m_Status) {
case ST_START_PUT:
b = HkNfcLlcpT::start(HkNfcSnep::recvCbT);
if(b) {
uint8_t snep_head[6];
snep_head[0] = 0x10;
snep_head[1] = 0x02; //PUT
snep_head[2] = (uint8_t)((m_pMessage->Length) >> 24);
snep_head[3] = (uint8_t)((m_pMessage->Length) >> 16);
snep_head[4] = (uint8_t)((m_pMessage->Length) >> 8);
snep_head[5] = (uint8_t)(m_pMessage->Length);
b = HkNfcLlcpT::addSendData(snep_head, sizeof(snep_head));
b = b && HkNfcLlcpT::addSendData(m_pMessage->Data, m_pMessage->Length);
}
if(b) {
m_Status = ST_PUT;
} else {
m_Status = ST_ABORT;
HkNfcLlcpT::stopRequest();
}
break;
case ST_PUT:
b = HkNfcLlcpT::sendRequest();
if(b) {
m_Status = ST_PUT_RESPONSE;
} else {
m_Status = ST_ABORT;
HkNfcLlcpT::stopRequest();
}
break;
case ST_PUT_RESPONSE:
case ST_SUCCESS:
case ST_ABORT:
b = HkNfcLlcpT::poll();
break;
}
if(!b) {
m_pMessage = 0;
}
return b;
}
void HkNfcSnep::recvCbT(const void* pBuf, uint8_t len)
{
const uint8_t* pData = reinterpret_cast<const uint8_t*>(pBuf);
switch(m_Status) {
case ST_PUT_RESPONSE:
//PUT後の応答
if(pData[1] == SNEP_SUCCESS) {
m_Status = ST_SUCCESS;
} else {
m_Status = ST_ABORT;
}
HkNfcLlcpT::stopRequest();
break;
}
}
#endif