NfcAdaptation.cpp revision 5a7e857aee2ebba4e369936aa5acdc7bcecf83e7
1/******************************************************************************
2 *
3 *  Copyright (C) 1999-2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18#include "OverrideLog.h"
19#include "NfcAdaptation.h"
20extern "C"
21{
22    #include "gki.h"
23    #include "nfa_api.h"
24    #include "nfc_int.h"
25}
26#include "config.h"
27
28#define LOG_TAG "NfcAdaptation"
29
30extern "C" void GKI_shutdown();
31extern void resetConfig();
32extern "C" void verify_stack_non_volatile_store ();
33extern "C" void delete_stack_non_volatile_store (BOOLEAN forceDelete);
34
35NfcAdaptation* NfcAdaptation::mpInstance = NULL;
36ThreadMutex NfcAdaptation::sLock;
37nfc_nci_device_t* NfcAdaptation::mHalDeviceContext = NULL;
38tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = NULL;
39tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = NULL;
40ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent;
41ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent;
42
43UINT32 ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; //0x017F00;
44UINT8 appl_trace_level = 0xff;
45char bcm_nfc_location[120];
46
47static UINT8 nfa_dm_cfg[sizeof ( tNFA_DM_CFG ) ];
48extern tNFA_DM_CFG *p_nfa_dm_cfg;
49extern UINT8 nfa_ee_max_ee_cfg;
50extern const UINT8  nfca_version_string [];
51extern const UINT8  nfa_version_string [];
52
53/*******************************************************************************
54**
55** Function:    NfcAdaptation::NfcAdaptation()
56**
57** Description: class constructor
58**
59** Returns:     none
60**
61*******************************************************************************/
62NfcAdaptation::NfcAdaptation()
63{
64}
65
66/*******************************************************************************
67**
68** Function:    NfcAdaptation::~NfcAdaptation()
69**
70** Description: class destructor
71**
72** Returns:     none
73**
74*******************************************************************************/
75NfcAdaptation::~NfcAdaptation()
76{
77    mpInstance = NULL;
78}
79
80/*******************************************************************************
81**
82** Function:    NfcAdaptation::GetInstance()
83**
84** Description: access class singleton
85**
86** Returns:     pointer to the singleton object
87**
88*******************************************************************************/
89NfcAdaptation& NfcAdaptation::GetInstance()
90{
91    AutoThreadMutex  a(sLock);
92
93    if (!mpInstance)
94        mpInstance = new NfcAdaptation;
95    return *mpInstance;
96}
97
98/*******************************************************************************
99**
100** Function:    NfcAdaptation::Initialize()
101**
102** Description: class initializer
103**
104** Returns:     none
105**
106*******************************************************************************/
107void NfcAdaptation::Initialize ()
108{
109    const char* func = "NfcAdaptation::Initialize";
110    ALOGD("%s: enter", func);
111    ALOGE("%s: ver=%s nfa=%s", func, nfca_version_string, nfa_version_string);
112    unsigned long num;
113
114    if ( !GetStrValue ( NAME_NFA_STORAGE, bcm_nfc_location, sizeof ( bcm_nfc_location ) ) )
115        strcpy ( bcm_nfc_location, "/data/nfc" );
116    if ( GetNumValue ( NAME_PROTOCOL_TRACE_LEVEL, &num, sizeof ( num ) ) )
117        ScrProtocolTraceFlag = num;
118
119    if ( GetStrValue ( NAME_NFA_DM_CFG, (char*)nfa_dm_cfg, sizeof ( nfa_dm_cfg ) ) )
120        p_nfa_dm_cfg = ( tNFA_DM_CFG * ) &nfa_dm_cfg[0];
121
122    if ( GetNumValue ( NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof ( num ) ) )
123    {
124        nfa_ee_max_ee_cfg = num;
125        ALOGD("%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d", func, nfa_ee_max_ee_cfg);
126    }
127
128    initializeGlobalAppLogLevel ();
129
130    verify_stack_non_volatile_store ();
131    if ( GetNumValue ( NAME_PRESERVE_STORAGE, (char*)&num, sizeof ( num ) ) &&
132            (num == 1) )
133        ALOGD ("%s: preserve stack NV store", __FUNCTION__);
134    else
135    {
136        delete_stack_non_volatile_store (FALSE);
137    }
138
139    GKI_init ();
140    GKI_enable ();
141    GKI_create_task ((TASKPTR)NFCA_TASK, BTU_TASK, (INT8*)"NFCA_TASK", 0, 0, (pthread_cond_t*)NULL, NULL);
142    {
143        AutoThreadMutex guard(mCondVar);
144        GKI_create_task ((TASKPTR)Thread, MMI_TASK, (INT8*)"NFCA_THREAD", 0, 0, (pthread_cond_t*)NULL, NULL);
145        mCondVar.wait();
146    }
147
148    mHalDeviceContext = NULL;
149    mHalCallback =  NULL;
150    memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
151    InitializeHalDeviceContext ();
152    ALOGD ("%s: exit", func);
153}
154
155/*******************************************************************************
156**
157** Function:    NfcAdaptation::Finalize()
158**
159** Description: class finalizer
160**
161** Returns:     none
162**
163*******************************************************************************/
164void NfcAdaptation::Finalize()
165{
166    const char* func = "NfcAdaptation::Finalize";
167    AutoThreadMutex  a(sLock);
168
169    ALOGD ("%s: enter", func);
170    GKI_shutdown ();
171
172    resetConfig();
173
174    nfc_nci_close(mHalDeviceContext); //close the HAL's device context
175    mHalDeviceContext = NULL;
176    mHalCallback = NULL;
177    memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
178
179    ALOGD ("%s: exit", func);
180    delete this;
181}
182
183/*******************************************************************************
184**
185** Function:    NfcAdaptation::signal()
186**
187** Description: signal the CondVar to release the thread that is waiting
188**
189** Returns:     none
190**
191*******************************************************************************/
192void NfcAdaptation::signal ()
193{
194    mCondVar.signal();
195}
196
197/*******************************************************************************
198**
199** Function:    NfcAdaptation::NFCA_TASK()
200**
201** Description: NFCA_TASK runs the GKI main task
202**
203** Returns:     none
204**
205*******************************************************************************/
206UINT32 NfcAdaptation::NFCA_TASK (UINT32 arg)
207{
208    const char* func = "NfcAdaptation::NFCA_TASK";
209    ALOGD ("%s: enter", func);
210    GKI_run (0);
211    ALOGD ("%s: exit", func);
212    return NULL;
213}
214
215/*******************************************************************************
216**
217** Function:    NfcAdaptation::Thread()
218**
219** Description: Creates work threads
220**
221** Returns:     none
222**
223*******************************************************************************/
224UINT32 NfcAdaptation::Thread (UINT32 arg)
225{
226    const char* func = "NfcAdaptation::Thread";
227    ALOGD ("%s: enter", func);
228
229    {
230        ThreadCondVar    CondVar;
231        AutoThreadMutex  guard(CondVar);
232        GKI_create_task ((TASKPTR)nfc_task, NFC_TASK, (INT8*)"NFC_TASK", 0, 0, (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
233        CondVar.wait();
234    }
235
236    NfcAdaptation::GetInstance().signal();
237
238    GKI_exit_task (GKI_get_taskid ());
239    ALOGD ("%s: exit", func);
240    return NULL;
241}
242
243/*******************************************************************************
244**
245** Function:    NfcAdaptation::GetHalEntryFuncs()
246**
247** Description: Get the set of HAL entry points.
248**
249** Returns:     Functions pointers for HAL entry points.
250**
251*******************************************************************************/
252tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs ()
253{
254    return &mHalEntryFuncs;
255}
256
257/*******************************************************************************
258**
259** Function:    NfcAdaptation::InitializeHalDeviceContext
260**
261** Description: Ask the generic Android HAL to find the Broadcom-specific HAL.
262**
263** Returns:     None.
264**
265*******************************************************************************/
266void NfcAdaptation::InitializeHalDeviceContext ()
267{
268    const char* func = "NfcAdaptation::InitializeHalDeviceContext";
269    ALOGD ("%s: enter", func);
270    int ret = 0; //0 means success
271    const hw_module_t* hw_module = NULL;
272
273    mHalEntryFuncs.initialize = HalInitialize;
274    mHalEntryFuncs.terminate = HalTerminate;
275    mHalEntryFuncs.open = HalOpen;
276    mHalEntryFuncs.close = HalClose;
277    mHalEntryFuncs.core_initialized = HalCoreInitialized;
278    mHalEntryFuncs.write = HalWrite;
279    mHalEntryFuncs.prediscover = HalPrediscover;
280    mHalEntryFuncs.control_granted = HalControlGranted;
281    mHalEntryFuncs.power_cycle = HalPowerCycle;
282
283    ret = hw_get_module (NFC_NCI_HARDWARE_MODULE_ID, &hw_module);
284    if (ret == 0)
285    {
286        ret = nfc_nci_open (hw_module, &mHalDeviceContext);
287        if (ret != 0)
288            ALOGE ("%s: nfc_nci_open fail", func);
289    }
290    else
291        ALOGE ("%s: fail hw_get_module", func);
292    ALOGD ("%s: exit", func);
293}
294
295/*******************************************************************************
296**
297** Function:    NfcAdaptation::HalInitialize
298**
299** Description: Not implemented because this function is only needed
300**              within the HAL.
301**
302** Returns:     None.
303**
304*******************************************************************************/
305void NfcAdaptation::HalInitialize ()
306{
307    const char* func = "NfcAdaptation::HalInitialize";
308    ALOGD ("%s", func);
309}
310
311/*******************************************************************************
312**
313** Function:    NfcAdaptation::HalTerminate
314**
315** Description: Not implemented because this function is only needed
316**              within the HAL.
317**
318** Returns:     None.
319**
320*******************************************************************************/
321void NfcAdaptation::HalTerminate ()
322{
323    const char* func = "NfcAdaptation::HalTerminate";
324    ALOGD ("%s", func);
325}
326
327/*******************************************************************************
328**
329** Function:    NfcAdaptation::HalOpen
330**
331** Description: Turn on controller, download firmware.
332**
333** Returns:     None.
334**
335*******************************************************************************/
336void NfcAdaptation::HalOpen (tHAL_NFC_CBACK *p_hal_cback, tHAL_NFC_DATA_CBACK* p_data_cback)
337{
338    const char* func = "NfcAdaptation::HalOpen";
339    ALOGD ("%s", func);
340    if (mHalDeviceContext)
341    {
342        mHalCallback = p_hal_cback;
343        mHalDataCallback = p_data_cback;
344        mHalDeviceContext->open (mHalDeviceContext, HalDeviceContextCallback, HalDeviceContextDataCallback);
345    }
346}
347
348/*******************************************************************************
349**
350** Function:    NfcAdaptation::HalClose
351**
352** Description: Turn off controller.
353**
354** Returns:     None.
355**
356*******************************************************************************/
357void NfcAdaptation::HalClose ()
358{
359    const char* func = "NfcAdaptation::HalClose";
360    ALOGD ("%s", func);
361    if (mHalDeviceContext)
362    {
363        mHalDeviceContext->close (mHalDeviceContext);
364    }
365}
366
367/*******************************************************************************
368**
369** Function:    NfcAdaptation::HalDeviceContextCallback
370**
371** Description: Translate generic Android HAL's callback into Broadcom-specific
372**              callback function.
373**
374** Returns:     None.
375**
376*******************************************************************************/
377void NfcAdaptation::HalDeviceContextCallback (nfc_event_t event, nfc_status_t event_status)
378{
379    const char* func = "NfcAdaptation::HalDeviceContextCallback";
380    ALOGD ("%s: event=%u", func, event);
381    if (mHalCallback)
382        mHalCallback (event, (tHAL_NFC_STATUS) event_status);
383}
384
385/*******************************************************************************
386**
387** Function:    NfcAdaptation::HalDeviceContextDataCallback
388**
389** Description: Translate generic Android HAL's callback into Broadcom-specific
390**              callback function.
391**
392** Returns:     None.
393**
394*******************************************************************************/
395void NfcAdaptation::HalDeviceContextDataCallback (uint16_t data_len, uint8_t* p_data)
396{
397    const char* func = "NfcAdaptation::HalDeviceContextDataCallback";
398    ALOGD ("%s: len=%u", func, data_len);
399    if (mHalDataCallback)
400        mHalDataCallback (data_len, p_data);
401}
402
403/*******************************************************************************
404**
405** Function:    NfcAdaptation::HalWrite
406**
407** Description: Write NCI message to the controller.
408**
409** Returns:     None.
410**
411*******************************************************************************/
412void NfcAdaptation::HalWrite (UINT16 data_len, UINT8* p_data)
413{
414    const char* func = "NfcAdaptation::HalWrite";
415    ALOGD ("%s", func);
416    if (mHalDeviceContext)
417    {
418        mHalDeviceContext->write (mHalDeviceContext, data_len, p_data);
419    }
420}
421
422/*******************************************************************************
423**
424** Function:    NfcAdaptation::HalCoreInitialized
425**
426** Description: Adjust the configurable parameters in the controller.
427**
428** Returns:     None.
429**
430*******************************************************************************/
431void NfcAdaptation::HalCoreInitialized (UINT8* p_core_init_rsp_params)
432{
433    const char* func = "NfcAdaptation::HalCoreInitialized";
434    ALOGD ("%s", func);
435    if (mHalDeviceContext)
436    {
437        mHalDeviceContext->core_initialized (mHalDeviceContext, p_core_init_rsp_params);
438    }
439}
440
441/*******************************************************************************
442**
443** Function:    NfcAdaptation::HalPrediscover
444**
445** Description:     Perform any vendor-specific pre-discovery actions (if needed)
446**                  If any actions were performed TRUE will be returned, and
447**                  HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are
448**                  completed.
449**
450** Returns:          TRUE if vendor-specific pre-discovery actions initialized
451**                  FALSE if no vendor-specific pre-discovery actions are needed.
452**
453*******************************************************************************/
454BOOLEAN NfcAdaptation::HalPrediscover ()
455{
456    const char* func = "NfcAdaptation::HalPrediscover";
457    ALOGD ("%s", func);
458    BOOLEAN retval = FALSE;
459
460    if (mHalDeviceContext)
461    {
462        retval = mHalDeviceContext->pre_discover (mHalDeviceContext);
463    }
464    return retval;
465}
466
467/*******************************************************************************
468**
469** Function:        HAL_NfcControlGranted
470**
471** Description:     Grant control to HAL control for sending NCI commands.
472**                  Call in response to HAL_REQUEST_CONTROL_EVT.
473**                  Must only be called when there are no NCI commands pending.
474**                  HAL_RELEASE_CONTROL_EVT will notify when HAL no longer
475**                  needs control of NCI.
476**
477** Returns:         void
478**
479*******************************************************************************/
480void NfcAdaptation::HalControlGranted ()
481{
482    const char* func = "NfcAdaptation::HalControlGranted";
483    ALOGD ("%s", func);
484    if (mHalDeviceContext)
485    {
486        mHalDeviceContext->control_granted (mHalDeviceContext);
487    }
488}
489
490/*******************************************************************************
491**
492** Function:    NfcAdaptation::HalPowerCycle
493**
494** Description: Turn off and turn on the controller.
495**
496** Returns:     None.
497**
498*******************************************************************************/
499void NfcAdaptation::HalPowerCycle ()
500{
501    const char* func = "NfcAdaptation::HalPowerCycle";
502    ALOGD ("%s", func);
503    if (mHalDeviceContext)
504    {
505        mHalDeviceContext->power_cycle (mHalDeviceContext);
506    }
507}
508
509
510/*******************************************************************************
511**
512** Function:    NfcAdaptation::DownloadFirmware
513**
514** Description: Download firmware patch files.
515**
516** Returns:     None.
517**
518*******************************************************************************/
519void NfcAdaptation::DownloadFirmware ()
520{
521    const char* func = "NfcAdaptation::DownloadFirmware";
522    ALOGD ("%s: enter", func);
523    HalInitialize ();
524
525    mHalOpenCompletedEvent.lock ();
526    ALOGD ("%s: try open HAL", func);
527    HalOpen (HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback);
528    mHalOpenCompletedEvent.wait ();
529
530    mHalCloseCompletedEvent.lock ();
531    ALOGD ("%s: try close HAL", func);
532    HalClose ();
533    mHalCloseCompletedEvent.wait ();
534
535    HalTerminate ();
536    ALOGD ("%s: exit", func);
537}
538
539/*******************************************************************************
540**
541** Function:    NfcAdaptation::HalDownloadFirmwareCallback
542**
543** Description: Receive events from the HAL.
544**
545** Returns:     None.
546**
547*******************************************************************************/
548void NfcAdaptation::HalDownloadFirmwareCallback (nfc_event_t event, nfc_status_t event_status)
549{
550    const char* func = "NfcAdaptation::HalDownloadFirmwareCallback";
551    ALOGD ("%s: event=0x%X", func, event);
552    switch (event)
553    {
554    case HAL_NFC_OPEN_CPLT_EVT:
555        {
556            ALOGD ("%s: HAL_NFC_OPEN_CPLT_EVT", func);
557            mHalOpenCompletedEvent.signal ();
558            break;
559        }
560    case HAL_NFC_CLOSE_CPLT_EVT:
561        {
562            ALOGD ("%s: HAL_NFC_CLOSE_CPLT_EVT", func);
563            mHalCloseCompletedEvent.signal ();
564            break;
565        }
566    }
567}
568
569/*******************************************************************************
570**
571** Function:    NfcAdaptation::HalDownloadFirmwareDataCallback
572**
573** Description: Receive data events from the HAL.
574**
575** Returns:     None.
576**
577*******************************************************************************/
578void NfcAdaptation::HalDownloadFirmwareDataCallback (uint16_t data_len, uint8_t* p_data)
579{
580}
581
582
583/*******************************************************************************
584**
585** Function:    ThreadMutex::ThreadMutex()
586**
587** Description: class constructor
588**
589** Returns:     none
590**
591*******************************************************************************/
592ThreadMutex::ThreadMutex()
593{
594    pthread_mutexattr_t mutexAttr;
595
596    pthread_mutexattr_init(&mutexAttr);
597    pthread_mutex_init(&mMutex, &mutexAttr);
598    pthread_mutexattr_destroy(&mutexAttr);
599}
600
601/*******************************************************************************
602**
603** Function:    ThreadMutex::~ThreadMutex()
604**
605** Description: class destructor
606**
607** Returns:     none
608**
609*******************************************************************************/
610ThreadMutex::~ThreadMutex()
611{
612    pthread_mutex_destroy(&mMutex);
613}
614
615/*******************************************************************************
616**
617** Function:    ThreadMutex::lock()
618**
619** Description: lock kthe mutex
620**
621** Returns:     none
622**
623*******************************************************************************/
624void ThreadMutex::lock()
625{
626    pthread_mutex_lock(&mMutex);
627}
628
629/*******************************************************************************
630**
631** Function:    ThreadMutex::unblock()
632**
633** Description: unlock the mutex
634**
635** Returns:     none
636**
637*******************************************************************************/
638void ThreadMutex::unlock()
639{
640    pthread_mutex_unlock(&mMutex);
641}
642
643/*******************************************************************************
644**
645** Function:    ThreadCondVar::ThreadCondVar()
646**
647** Description: class constructor
648**
649** Returns:     none
650**
651*******************************************************************************/
652ThreadCondVar::ThreadCondVar()
653{
654    pthread_condattr_t CondAttr;
655
656    pthread_condattr_init(&CondAttr);
657    pthread_cond_init(&mCondVar, &CondAttr);
658
659    pthread_condattr_destroy(&CondAttr);
660}
661
662/*******************************************************************************
663**
664** Function:    ThreadCondVar::~ThreadCondVar()
665**
666** Description: class destructor
667**
668** Returns:     none
669**
670*******************************************************************************/
671ThreadCondVar::~ThreadCondVar()
672{
673    pthread_cond_destroy(&mCondVar);
674}
675
676/*******************************************************************************
677**
678** Function:    ThreadCondVar::wait()
679**
680** Description: wait on the mCondVar
681**
682** Returns:     none
683**
684*******************************************************************************/
685void ThreadCondVar::wait()
686{
687    pthread_cond_wait(&mCondVar, *this);
688    pthread_mutex_unlock(*this);
689}
690
691/*******************************************************************************
692**
693** Function:    ThreadCondVar::signal()
694**
695** Description: signal the mCondVar
696**
697** Returns:     none
698**
699*******************************************************************************/
700void ThreadCondVar::signal()
701{
702    AutoThreadMutex  a(*this);
703    pthread_cond_signal(&mCondVar);
704}
705
706/*******************************************************************************
707**
708** Function:    AutoThreadMutex::AutoThreadMutex()
709**
710** Description: class constructor, automatically lock the mutex
711**
712** Returns:     none
713**
714*******************************************************************************/
715AutoThreadMutex::AutoThreadMutex(ThreadMutex &m)
716    : mm(m)
717{
718    mm.lock();
719}
720
721/*******************************************************************************
722**
723** Function:    AutoThreadMutex::~AutoThreadMutex()
724**
725** Description: class destructor, automatically unlock the mutex
726**
727** Returns:     none
728**
729*******************************************************************************/
730AutoThreadMutex::~AutoThreadMutex()
731{
732    mm.unlock();
733}
734