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
|
/*****************************************************************************
**
** Name: HostAidRouter.cpp
**
** Description: Manage listen-mode AID routing to the host.
**
** Copyright (c) 2012, Broadcom Corp., All Rights Reserved.
** Proprietary and confidential.
**
*****************************************************************************/
#include "HostAidRouter.h"
#include "config.h"
#include "SecureElement.h"
HostAidRouter HostAidRouter::sHostAidRouter; //singleton HostAidRouter object
/*******************************************************************************
**
** Function: HostAidRouter
**
** Description: Private constructor to prevent public call.
**
** Returns: None.
**
*******************************************************************************/
HostAidRouter::HostAidRouter ()
: mTempHandle (NFA_HANDLE_INVALID),
mIsFeatureEnabled (true)
{
}
/*******************************************************************************
**
** Function: ~HostAidRouter
**
** Description: Private destructor to prevent public call.
**
** Returns: None.
**
*******************************************************************************/
HostAidRouter::~HostAidRouter ()
{
}
/*******************************************************************************
**
** Function: getInstance
**
** Description: Obtain a reference to the singleton object of HostAidRouter
**
** Returns: Reference to HostAidRouter object.
**
*******************************************************************************/
HostAidRouter& HostAidRouter::getInstance ()
{
return sHostAidRouter;
}
/*******************************************************************************
**
** Function: initialize
**
** Description: Initialize all resources.
**
** Returns: True if ok.
**
*******************************************************************************/
bool HostAidRouter::initialize ()
{
unsigned long num = 0;
mTempHandle = NFA_HANDLE_INVALID;
mHandleDatabase.clear ();
if (GetNumValue (NAME_REGISTER_VIRTUAL_SE, &num, sizeof (num)))
mIsFeatureEnabled = num != 0;
return true;
}
/*******************************************************************************
**
** Function: addPpseRoute
**
** Description: Route Proximity Payment System Environment request
** to the host. This function is called when there is no
** route data.
**
** Returns: True if ok.
**
*******************************************************************************/
bool HostAidRouter::addPpseRoute ()
{
static const char fn [] = "HostAidRouter::addPpseRoute";
ALOGD ("%s: enter", fn);
tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
bool retval = false;
if (! mIsFeatureEnabled)
{
ALOGD ("%s: feature disabled", fn);
goto TheEnd;
}
{
ALOGD ("%s: register PPSE AID", fn);
SyncEventGuard guard (mRegisterEvent);
mTempHandle = NFA_HANDLE_INVALID;
nfaStat = NFA_CeRegisterAidOnDH ((UINT8*) "2PAY.SYS.DDF01", 14, stackCallback);
if (nfaStat == NFA_STATUS_OK)
{
mRegisterEvent.wait (); //wait for NFA_CE_REGISTERED_EVT
if (mTempHandle == NFA_HANDLE_INVALID)
{
ALOGE ("%s: received invalid handle", fn);
goto TheEnd;
}
else
mHandleDatabase.push_back (mTempHandle);
}
else
{
ALOGE ("%s: fail register", fn);
goto TheEnd;
}
}
retval = true;
TheEnd:
ALOGD ("%s: exit; ok=%u", fn, retval);
return retval;
}
/*******************************************************************************
**
** Function: deleteAllRoutes
**
** Description: Delete all AID routes to the host.
**
** Returns: True if ok.
**
*******************************************************************************/
bool HostAidRouter::deleteAllRoutes ()
{
static const char fn [] = "HostAidRouter::deleteAllRoutes";
ALOGD ("%s: enter", fn);
tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
bool retval = false;
if (! mIsFeatureEnabled)
{
ALOGD ("%s: feature disabled", fn);
goto TheEnd;
}
//deregister each registered AID from the stack
for (AidHandleDatabase::iterator iter1 = mHandleDatabase.begin(); iter1 != mHandleDatabase.end(); iter1++)
{
tNFA_HANDLE aidHandle = *iter1;
ALOGD ("%s: deregister h=0x%X", fn, aidHandle);
SyncEventGuard guard (mDeregisterEvent);
nfaStat = NFA_CeDeregisterAidOnDH (aidHandle);
if (nfaStat == NFA_STATUS_OK)
mDeregisterEvent.wait (); //wait for NFA_CE_DEREGISTERED_EVT
else
ALOGE ("%s: fail deregister", fn);
}
mHandleDatabase.clear ();
retval = true;
TheEnd:
ALOGD ("%s: exit; ok=%u", fn, retval);
return retval;
}
/*******************************************************************************
**
** Function: startRoute
**
** Description: Begin to route AID request to the host.
** aid: Buffer that contains Application ID
** aidLen: Actual length of the buffer.
**
** Returns: True if ok.
**
*******************************************************************************/
bool HostAidRouter::startRoute (const UINT8* aid, UINT8 aidLen)
{
static const char fn [] = "HostAidRouter::startRoute";
ALOGD ("%s: enter", fn);
tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
bool retval = false;
if (! mIsFeatureEnabled)
{
ALOGD ("%s: feature disabled", fn);
goto TheEnd;
}
{
ALOGD ("%s: register AID; len=%u", fn, aidLen);
SyncEventGuard guard (mRegisterEvent);
mTempHandle = NFA_HANDLE_INVALID;
nfaStat = NFA_CeRegisterAidOnDH ((UINT8*) aid, aidLen, stackCallback);
if (nfaStat == NFA_STATUS_OK)
{
mRegisterEvent.wait (); //wait for NFA_CE_REGISTERED_EVT
if (mTempHandle == NFA_HANDLE_INVALID)
{
ALOGE ("%s: received invalid handle", fn);
goto TheEnd;
}
else
mHandleDatabase.push_back (mTempHandle);
}
else
{
ALOGE ("%s: fail register", fn);
goto TheEnd;
}
}
TheEnd:
ALOGD ("%s: exit; ok=%u", fn, retval);
return retval;
}
/*******************************************************************************
**
** Function: stackCallback
**
** Description: Receive events from the NFC stack.
**
** Returns: None.
**
*******************************************************************************/
void HostAidRouter::stackCallback (UINT8 event, tNFA_CONN_EVT_DATA* eventData)
{
static const char fn [] = "HostAidRouter::stackCallback";
ALOGD("%s: event=0x%X", fn, event);
switch (event)
{
case NFA_CE_REGISTERED_EVT:
{
tNFA_CE_REGISTERED& ce_registered = eventData->ce_registered;
ALOGD("%s: NFA_CE_REGISTERED_EVT; status=0x%X; h=0x%X", fn, ce_registered.status, ce_registered.handle);
SyncEventGuard guard (sHostAidRouter.mRegisterEvent);
if (ce_registered.status == NFA_STATUS_OK)
sHostAidRouter.mTempHandle = ce_registered.handle;
else
sHostAidRouter.mTempHandle = NFA_HANDLE_INVALID;
sHostAidRouter.mRegisterEvent.notifyOne();
}
break;
case NFA_CE_DEREGISTERED_EVT:
{
tNFA_CE_DEREGISTERED& ce_deregistered = eventData->ce_deregistered;
ALOGD("%s: NFA_CE_DEREGISTERED_EVT; h=0x%X", fn, ce_deregistered.handle);
SyncEventGuard guard (sHostAidRouter.mDeregisterEvent);
sHostAidRouter.mDeregisterEvent.notifyOne();
}
break;
case NFA_CE_DATA_EVT:
{
tNFA_CE_DATA& ce_data = eventData->ce_data;
ALOGD("%s: NFA_CE_DATA_EVT; h=0x%X; data len=%u", fn, ce_data.handle, ce_data.len);
SecureElement::getInstance().notifyTransactionListenersOfAid ((UINT8 *)"2PAY.SYS.DDF01", 14);
}
break;
}
}
|