This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
forked from DimiL/nfcd
-
Notifications
You must be signed in to change notification settings - Fork 7
/
NfcService.cpp
787 lines (655 loc) · 20.1 KB
/
NfcService.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
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
/*
* Copyright (C) 2013-2014 Mozilla Foundation
*
* 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.
*/
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <memory>
#include "MessageHandler.h"
#include "INfcManager.h"
#include "INfcTag.h"
#include "IP2pDevice.h"
#include "DeviceHost.h"
#include "NfcService.h"
#include "NfcUtil.h"
#include "NfcDebug.h"
#include "P2pLinkManager.h"
#include "SessionId.h"
using namespace android;
typedef enum {
MSG_UNDEFINED = 0,
MSG_LLCP_LINK_ACTIVATION,
MSG_LLCP_LINK_DEACTIVATION,
MSG_TAG_DISCOVERED,
MSG_TAG_LOST,
MSG_SE_FIELD_ACTIVATED,
MSG_SE_FIELD_DEACTIVATED,
MSG_SE_NOTIFY_TRANSACTION_EVENT,
MSG_READ_NDEF,
MSG_WRITE_NDEF,
MSG_SOCKET_CONNECTED,
MSG_PUSH_NDEF,
MSG_NDEF_TAG_LIST,
MSG_MAKE_NDEF_READONLY,
MSG_LOW_POWER,
MSG_ENABLE,
MSG_RECEIVE_NDEF_EVENT,
MSG_NDEF_FORMAT,
MSG_TAG_TRANSCEIVE
} NfcEventType;
typedef enum {
STATE_NFC_OFF = 0,
STATE_NFC_ON_LOW_POWER,
STATE_NFC_ON,
} NfcState;
class NfcEvent {
public:
NfcEvent (NfcEventType type) :mType(type) {}
NfcEventType getType() { return mType; }
int arg1;
int arg2;
void* obj;
private:
NfcEventType mType;
};
class PollingThreadParam {
public:
INfcTag* pINfcTag;
int sessionId;
};
static pthread_t thread_id;
static sem_t thread_sem;
NfcService* NfcService::sInstance = NULL;
NfcManager* NfcService::sNfcManager = NULL;
NfcService::NfcService()
: mState(STATE_NFC_OFF)
, mIsTagPresent(false)
{
mP2pLinkManager = new P2pLinkManager(this);
}
NfcService::~NfcService()
{
delete mP2pLinkManager;
}
static void *serviceThreadFunc(void *arg)
{
pthread_setname_np(pthread_self(), "NFCService thread");
NfcService* service = reinterpret_cast<NfcService*>(arg);
return service->eventLoop();
}
void NfcService::initialize(NfcManager* pNfcManager, MessageHandler* msgHandler)
{
if (sem_init(&thread_sem, 0, 0) == -1) {
ALOGE("%s: init_nfc_service Semaphore creation failed", FUNC);
abort();
}
if (pthread_create(&thread_id, NULL, serviceThreadFunc, this) != 0) {
ALOGE("%s: init_nfc_service pthread_create failed", FUNC);
abort();
}
mMsgHandler = msgHandler;
sNfcManager = pNfcManager;
}
void NfcService::notifyLlcpLinkActivated(IP2pDevice* pDevice)
{
ALOGD("%s: enter", FUNC);
NfcEvent *event = new NfcEvent(MSG_LLCP_LINK_ACTIVATION);
event->obj = reinterpret_cast<void*>(pDevice);
NfcService::Instance()->mQueue.push_back(event);
sem_post(&thread_sem);
}
void NfcService::notifyLlcpLinkDeactivated(IP2pDevice* pDevice)
{
ALOGD("%s: enter", FUNC);
NfcEvent *event = new NfcEvent(MSG_LLCP_LINK_DEACTIVATION);
event->obj = reinterpret_cast<void*>(pDevice);
NfcService::Instance()->mQueue.push_back(event);
sem_post(&thread_sem);
}
void NfcService::notifyTagDiscovered(INfcTag* pTag)
{
ALOGD("%s: enter", FUNC);
NfcEvent *event = new NfcEvent(MSG_TAG_DISCOVERED);
event->obj = reinterpret_cast<void*>(pTag);
NfcService::Instance()->mQueue.push_back(event);
sem_post(&thread_sem);
}
void NfcService::notifyTagLost(int sessionId)
{
ALOGD("%s: enter", FUNC);
NfcEvent *event = new NfcEvent(MSG_TAG_LOST);
event->obj = reinterpret_cast<void*>(sessionId);
NfcService::Instance()->mQueue.push_back(event);
sem_post(&thread_sem);
}
void NfcService::notifySEFieldActivated()
{
ALOGD("%s: enter", FUNC);
NfcEvent *event = new NfcEvent(MSG_SE_FIELD_ACTIVATED);
NfcService::Instance()->mQueue.push_back(event);
sem_post(&thread_sem);
}
void NfcService::notifySEFieldDeactivated()
{
ALOGD("%s: enter", FUNC);
NfcEvent *event = new NfcEvent(MSG_SE_FIELD_DEACTIVATED);
NfcService::Instance()->mQueue.push_back(event);
sem_post(&thread_sem);
}
void NfcService::notifySETransactionEvent(TransactionEvent* pEvent)
{
ALOGD("%s: enter", FUNC);
NfcEvent *event = new NfcEvent(MSG_SE_NOTIFY_TRANSACTION_EVENT);
event->obj = reinterpret_cast<void*>(pEvent);
NfcService::Instance()->mQueue.push_back(event);
sem_post(&thread_sem);
}
void NfcService::handleLlcpLinkDeactivation(NfcEvent* event)
{
ALOGD("%s: enter", FUNC);
void* pDevice = event->obj;
IP2pDevice* pIP2pDevice = reinterpret_cast<IP2pDevice*>(pDevice);
if (pIP2pDevice->getMode() == NfcDepEndpoint::MODE_P2P_TARGET) {
pIP2pDevice->disconnect();
}
mP2pLinkManager->onLlcpDeactivated();
mMsgHandler->processNotification(NFC_NOTIFICATION_TECH_LOST,
reinterpret_cast<void*>(mP2pLinkManager->getSessionId()));
mP2pLinkManager->setSessionId(-1);
}
void NfcService::handleLlcpLinkActivation(NfcEvent* event)
{
ALOGD("%s: enter", FUNC);
void* pDevice = event->obj;
IP2pDevice* pIP2pDevice = reinterpret_cast<IP2pDevice*>(pDevice);
if (pIP2pDevice->getMode() == NfcDepEndpoint::MODE_P2P_TARGET ||
pIP2pDevice->getMode() == NfcDepEndpoint::MODE_P2P_INITIATOR) {
if (pIP2pDevice->getMode() == NfcDepEndpoint::MODE_P2P_TARGET) {
if (pIP2pDevice->connect()) {
ALOGD("%s: Connected to device!", FUNC);
}
else {
ALOGE("%s: Cannot connect remote Target. Polling loop restarted.", FUNC);
}
}
INfcManager* pINfcManager = NfcService::getNfcManager();
bool ret = pINfcManager->checkLlcp();
if (ret == true) {
ret = pINfcManager->activateLlcp();
if (ret == true) {
ALOGD("%s: Target Activate LLCP OK", FUNC);
} else {
ALOGE("%s: doActivateLLcp failed", FUNC);
}
} else {
ALOGE("%s: doCheckLLcp failed", FUNC);
}
} else {
ALOGE("%s: Unknown LLCP P2P mode", FUNC);
//stop();
}
mP2pLinkManager->onLlcpActivated();
TechDiscoveredEvent* data = new TechDiscoveredEvent();
mP2pLinkManager->setSessionId(SessionId::generateNewId());
data->sessionId = mP2pLinkManager->getSessionId();
data->isP2P = true;
data->techCount = 0;
data->techList = NULL;
data->tagIdCount = 0;
data->tagId = NULL;
data->ndefMsgCount = 0;
data->ndefMsg = NULL;
data->ndefInfo = NULL;
mMsgHandler->processNotification(NFC_NOTIFICATION_TECH_DISCOVERED, data);
delete data;
ALOGD("%s: exit", FUNC);
}
static void *pollingThreadFunc(void *arg)
{
PollingThreadParam* param = reinterpret_cast<PollingThreadParam*>(arg);
INfcTag* pINfcTag = param->pINfcTag;
int sessionId = param->sessionId;
NfcService::Instance()->tagDetected();
while (pINfcTag->presenceCheck()) {
sleep(1);
}
NfcService::Instance()->tagRemoved();
pINfcTag->disconnect();
NfcService::Instance()->notifyTagLost(sessionId);
delete param;
return NULL;
}
void NfcService::handleTagDiscovered(NfcEvent* event)
{
// Do not support multiple tag discover.
if (isTagPresent()) {
return;
}
INfcTag* pINfcTag = reinterpret_cast<INfcTag*>(event->obj);
// To get complete tag information, need to call read ndef first.
// In readNdef function, it will add NDEF related info in NfcTagManager.
std::auto_ptr<NdefMessage> pNdefMessage(pINfcTag->readNdef());
std::auto_ptr<NdefInfo> pNdefInfo(pINfcTag->readNdefInfo());
// Do the following after read ndef.
std::vector<TagTechnology>& techList = pINfcTag->getTechList();
int techCount = techList.size();
uint8_t* gonkTechList = new uint8_t[techCount];
std::copy(techList.begin(), techList.end(), gonkTechList);
TechDiscoveredEvent* data = new TechDiscoveredEvent();
data->sessionId = SessionId::generateNewId();
data->isP2P = false;
data->techCount = techCount;
data->techList = gonkTechList;
data->tagIdCount = pINfcTag->getUid().size();
uint8_t* tagId = new uint8_t[data->tagIdCount];
memcpy(tagId, &pINfcTag->getUid()[0], data->tagIdCount);
data->tagId = tagId;
data->ndefMsgCount = pNdefMessage.get() ? 1 : 0;
data->ndefMsg = pNdefMessage.get();
data->ndefInfo = pNdefInfo.get();
mMsgHandler->processNotification(NFC_NOTIFICATION_TECH_DISCOVERED, data);
PollingThreadParam* param = new PollingThreadParam();
param->sessionId = data->sessionId;
param->pINfcTag = pINfcTag;
delete tagId;
delete gonkTechList;
delete data;
pthread_t tid;
pthread_create(&tid, NULL, pollingThreadFunc, param);
}
void NfcService::handleTagLost(NfcEvent* event)
{
mMsgHandler->processNotification(NFC_NOTIFICATION_TECH_LOST, event->obj);
}
void NfcService::handleTransactionEvent(NfcEvent* event)
{
mMsgHandler->processNotification(NFC_NOTIFICATION_TRANSACTION_EVENT, event->obj);
}
void* NfcService::eventLoop()
{
ALOGD("%s: NFCService started", FUNC);
while(true) {
if(sem_wait(&thread_sem)) {
ALOGE("%s: Failed to wait for semaphore", FUNC);
abort();
}
while (!mQueue.empty()) {
NfcEvent* event = *mQueue.begin();
mQueue.erase(mQueue.begin());
NfcEventType eventType = event->getType();
ALOGD("%s: NFCService msg=%d", FUNC, eventType);
switch(eventType) {
case MSG_LLCP_LINK_ACTIVATION:
handleLlcpLinkActivation(event);
break;
case MSG_LLCP_LINK_DEACTIVATION:
handleLlcpLinkDeactivation(event);
break;
case MSG_TAG_DISCOVERED:
handleTagDiscovered(event);
break;
case MSG_TAG_LOST:
handleTagLost(event);
break;
case MSG_SE_NOTIFY_TRANSACTION_EVENT:
handleTransactionEvent(event);
break;
case MSG_READ_NDEF:
handleReadNdefResponse(event);
break;
case MSG_WRITE_NDEF:
handleWriteNdefResponse(event);
break;
case MSG_SOCKET_CONNECTED:
mMsgHandler->processNotification(NFC_NOTIFICATION_INITIALIZED , NULL);
break;
case MSG_PUSH_NDEF:
handlePushNdefResponse(event);
break;
case MSG_MAKE_NDEF_READONLY:
handleMakeNdefReadonlyResponse(event);
break;
case MSG_LOW_POWER:
handleEnterLowPowerResponse(event);
break;
case MSG_ENABLE:
handleEnableResponse(event);
break;
case MSG_RECEIVE_NDEF_EVENT:
handleReceiveNdefEvent(event);
break;
case MSG_NDEF_FORMAT:
handleNdefFormatResponse(event);
break;
case MSG_TAG_TRANSCEIVE:
handleTagTransceiveResponse(event);
break;
default:
ALOGE("%s: NFCService bad message", FUNC);
abort();
}
//TODO delete event->data?
delete event;
}
}
}
NfcService* NfcService::Instance() {
if (!sInstance)
sInstance = new NfcService();
return sInstance;
}
INfcManager* NfcService::getNfcManager()
{
return reinterpret_cast<INfcManager*>(NfcService::sNfcManager);
}
bool NfcService::handleDisconnect()
{
INfcTag* pINfcTag = reinterpret_cast<INfcTag*>
(sNfcManager->queryInterface(INTERFACE_TAG_MANAGER));
bool result = pINfcTag->disconnect();
return result;
}
bool NfcService::handleReadNdefRequest()
{
NfcEvent *event = new NfcEvent(MSG_READ_NDEF);
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
void NfcService::handleReadNdefResponse(NfcEvent* event)
{
NfcResponseType resType = NFC_RESPONSE_READ_NDEF;
INfcTag* pINfcTag = reinterpret_cast<INfcTag*>(sNfcManager->queryInterface(INTERFACE_TAG_MANAGER));
if (!pINfcTag) {
mMsgHandler->processResponse(resType, NFC_ERROR_NOT_SUPPORTED, NULL);
return;
}
std::auto_ptr<NdefMessage> pNdefMessage(pINfcTag->readNdef());
if (!pNdefMessage.get()) {
mMsgHandler->processResponse(resType, NFC_ERROR_READ, NULL);
return;
}
mMsgHandler->processResponse(resType, NFC_SUCCESS, pNdefMessage.get());
}
void NfcService::handleReceiveNdefEvent(NfcEvent* event)
{
NdefMessage* ndef = reinterpret_cast<NdefMessage*>(event->obj);
TechDiscoveredEvent* data = new TechDiscoveredEvent();
data->sessionId = SessionId::getCurrentId();
data->isP2P = true;
data->techCount = 0;
data->techList = NULL;
data->tagIdCount = 0;
data->tagId = NULL;
data->ndefMsgCount = ndef ? 1 : 0;
data->ndefMsg = ndef;
mMsgHandler->processNotification(NFC_NOTIFICATION_TECH_DISCOVERED, data);
delete data;
delete ndef;
}
bool NfcService::handleWriteNdefRequest(NdefMessage* ndef, bool isP2P)
{
NfcEvent *event = new NfcEvent(MSG_WRITE_NDEF);
event->arg1 = isP2P;
event->obj = ndef;
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
void NfcService::handleWriteNdefResponse(NfcEvent* event)
{
NfcResponseType resType = NFC_RESPONSE_GENERAL;
NfcErrorCode code = NFC_SUCCESS;
std::auto_ptr<NdefMessage> pNdef(reinterpret_cast<NdefMessage*>(event->obj));
if (!pNdef.get()) {
mMsgHandler->processResponse(resType, NFC_ERROR_INVALID_PARAM, NULL);
return;
}
bool isP2P = event->arg1;
if (isP2P && mP2pLinkManager->isLlcpActive()) {
mP2pLinkManager->push(*pNdef.get());
} else if (!isP2P && isTagPresent()) {
INfcTag* pINfcTag = reinterpret_cast<INfcTag*>
(sNfcManager->queryInterface(INTERFACE_TAG_MANAGER));
code = !!pINfcTag ?
(pINfcTag->writeNdef(*pNdef.get()) ? NFC_SUCCESS : NFC_ERROR_IO) :
NFC_ERROR_NOT_SUPPORTED;
} else {
code = NFC_ERROR_IO;
}
mMsgHandler->processResponse(resType, code, NULL);
}
void NfcService::onConnected()
{
NfcEvent *event = new NfcEvent(MSG_SOCKET_CONNECTED);
mQueue.push_back(event);
sem_post(&thread_sem);
}
bool NfcService::handlePushNdefRequest(NdefMessage* ndef)
{
NfcEvent *event = new NfcEvent(MSG_PUSH_NDEF);
event->obj = ndef;
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
void NfcService::handlePushNdefResponse(NfcEvent* event)
{
NfcResponseType resType = NFC_RESPONSE_GENERAL;
std::auto_ptr<NdefMessage> pNdef(reinterpret_cast<NdefMessage*>(event->obj));
if (!pNdef.get()) {
mMsgHandler->processResponse(resType, NFC_ERROR_INVALID_PARAM, NULL);
return;
}
mP2pLinkManager->push(*pNdef.get());
mMsgHandler->processResponse(resType, NFC_SUCCESS, NULL);
}
bool NfcService::handleMakeNdefReadonlyRequest()
{
NfcEvent *event = new NfcEvent(MSG_MAKE_NDEF_READONLY);
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
void NfcService::handleMakeNdefReadonlyResponse(NfcEvent* event)
{
INfcTag* pINfcTag = reinterpret_cast<INfcTag*>
(sNfcManager->queryInterface(INTERFACE_TAG_MANAGER));
NfcErrorCode code = !!pINfcTag ?
(pINfcTag->makeReadOnly() ? NFC_SUCCESS : NFC_ERROR_IO) :
NFC_ERROR_NOT_SUPPORTED;
mMsgHandler->processResponse(NFC_RESPONSE_GENERAL, code, NULL);
}
bool NfcService::handleNdefFormatRequest()
{
NfcEvent *event = new NfcEvent(MSG_NDEF_FORMAT);
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
bool NfcService::handleTagTransceiveRequest(int tech, const uint8_t* buf, uint32_t bufLen)
{
std::vector<uint8_t>* cmd = new std::vector<uint8_t>(buf, buf + bufLen);
NfcEvent *event = new NfcEvent(MSG_TAG_TRANSCEIVE);
event->arg1 = tech;
event->obj = reinterpret_cast<void*>(cmd);
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
void NfcService::handleTagTransceiveResponse(NfcEvent* event)
{
INfcTag* pINfcTag = reinterpret_cast<INfcTag*>
(sNfcManager->queryInterface(INTERFACE_TAG_MANAGER));
int tech = event->arg1;
std::vector<uint8_t>* command = reinterpret_cast<std::vector<uint8_t>*>(event->obj);
std::vector<uint8_t> response;
NfcErrorCode code = pINfcTag->connect(static_cast<TagTechnology>(tech)) ?
NFC_SUCCESS : NFC_ERROR_IO;
if (NFC_SUCCESS == code) {
code = !!pINfcTag ? (pINfcTag->transceive(*command, response) ?
NFC_SUCCESS : NFC_ERROR_IO) : NFC_ERROR_NOT_SUPPORTED;
}
delete command;
mMsgHandler->processResponse(NFC_RESPONSE_TAG_TRANSCEIVE, code,
reinterpret_cast<void*>(&response));
}
void NfcService::handleNdefFormatResponse(NfcEvent* event)
{
INfcTag* pINfcTag = reinterpret_cast<INfcTag*>
(sNfcManager->queryInterface(INTERFACE_TAG_MANAGER));
NfcErrorCode code = !!pINfcTag ?
(pINfcTag->formatNdef() ? NFC_SUCCESS : NFC_ERROR_IO) :
NFC_ERROR_NOT_SUPPORTED;
mMsgHandler->processResponse(NFC_RESPONSE_GENERAL, code, NULL);
}
bool NfcService::handleEnterLowPowerRequest(bool enter)
{
NfcEvent *event = new NfcEvent(MSG_LOW_POWER);
event->arg1 = enter;
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
void NfcService::handleEnterLowPowerResponse(NfcEvent* event)
{
bool low = event->arg1;
NfcErrorCode code = setLowPowerMode(low);
ALOGD("%s mState=%d", __func__, mState);
mMsgHandler->processResponse(NFC_RESPONSE_CHANGE_RF_STATE, code, &mState);
}
bool NfcService::handleEnableRequest(bool enable)
{
NfcEvent *event = new NfcEvent(MSG_ENABLE);
event->arg1 = enable;
mQueue.push_back(event);
sem_post(&thread_sem);
return true;
}
/**
* There are two case for enable:
* 1. NFC is off -> enable NFC and then enable discovery.
* 2. NFC is already on but discovery mode is off -> enable discovery.
*/
void NfcService::handleEnableResponse(NfcEvent* event)
{
NfcErrorCode code = NFC_SUCCESS;
bool enable = event->arg1;
if (enable) {
// Disable low power mode if already in low power mode
if (mState == STATE_NFC_ON_LOW_POWER) {
code = setLowPowerMode(false);
} else if (mState == STATE_NFC_OFF) {
code = enableNfc();
if (code != NFC_SUCCESS) {
goto TheEnd;
}
code = enableSE();
}
} else {
code = disableSE();
if (code != NFC_SUCCESS) {
goto TheEnd;
}
code = disableNfc();
}
TheEnd:
ALOGD("%s mState=%d", __func__, mState);
mMsgHandler->processResponse(NFC_RESPONSE_CHANGE_RF_STATE, code, &mState);
}
NfcErrorCode NfcService::enableNfc()
{
ALOGD("Enable NFC");
if (mState != STATE_NFC_OFF) {
return NFC_SUCCESS;
}
if (!sNfcManager->initialize()) {
return NFC_ERROR_INITIALIZE_FAIL;
}
if (mP2pLinkManager) {
mP2pLinkManager->enableDisable(true);
}
if (!sNfcManager->enableDiscovery()) {
return NFC_ERROR_FAIL_ENABLE_DISCOVERY;
}
mState = STATE_NFC_ON;
return NFC_SUCCESS;
}
NfcErrorCode NfcService::disableNfc()
{
ALOGD("Disable NFC");
if (mState == STATE_NFC_OFF) {
return NFC_SUCCESS;
}
if (mP2pLinkManager) {
mP2pLinkManager->enableDisable(false);
}
if (!sNfcManager->deinitialize()) {
return NFC_ERROR_DEINITIALIZE_FAIL;
}
if (!sNfcManager->disableDiscovery()) {
return NFC_ERROR_FAIL_DISABLE_DISCOVERY;
}
mState = STATE_NFC_OFF;
return NFC_SUCCESS;
}
NfcErrorCode NfcService::setLowPowerMode(bool low)
{
if ((low && mState != STATE_NFC_ON) ||
(!low && mState == STATE_NFC_ON)) {
return NFC_SUCCESS;
}
if (low) {
if (!sNfcManager->disableP2pListening() ||
!sNfcManager->disablePolling()) {
return NFC_ERROR_FAIL_ENABLE_LOW_POWER_MODE;
}
mState = STATE_NFC_ON_LOW_POWER;
} else {
if (!sNfcManager->enableP2pListening() ||
!sNfcManager->enablePolling()) {
return NFC_ERROR_FAIL_DISABLE_LOW_POWER_MODE;
}
mState = STATE_NFC_ON;
}
return NFC_SUCCESS;
}
// TODO: Emulator doesn't support SE now.
// So always return sucess to pass testcase.
NfcErrorCode NfcService::enableSE()
{
ALOGD("Enable secure element");
if (!sNfcManager->doSelectSecureElement()) {
ALOGE("Enable secure element fail");
}
return NFC_SUCCESS;
}
// TODO: Emulator doesn't support SE now.
// So always return sucess to pass testcase.
NfcErrorCode NfcService::disableSE()
{
ALOGD("Disable secure element");
if (!sNfcManager->doDeselectSecureElement()) {
ALOGE("Disable secure element fail");
}
return NFC_SUCCESS;
}
void NfcService::onP2pReceivedNdef(NdefMessage* ndef)
{
NfcEvent *event = new NfcEvent(MSG_RECEIVE_NDEF_EVENT);
event->obj = ndef ? new NdefMessage(ndef) : NULL;
mQueue.push_back(event);
sem_post(&thread_sem);
}