1 /******************************************************************************
2  *
3  *  Copyright (C) 2011-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 #pragma once
19 #include <pthread.h>
20 #ifndef UINT32
21 typedef unsigned long   UINT32;
22 #endif
23 #include "nfc_target.h"
24 #include "nfc_hal_api.h"
25 #include <hardware/nfc.h>
26 
27 
28 class ThreadMutex
29 {
30 public:
31     ThreadMutex();
32     virtual ~ThreadMutex();
33     void lock();
34     void unlock();
35     operator pthread_mutex_t* () {return &mMutex;}
36 private:
37     pthread_mutex_t mMutex;
38 };
39 
40 class ThreadCondVar : public ThreadMutex
41 {
42 public:
43     ThreadCondVar();
44     virtual ~ThreadCondVar();
45     void signal();
46     void wait();
47     operator pthread_cond_t* () {return &mCondVar;}
48     operator pthread_mutex_t* () {return ThreadMutex::operator pthread_mutex_t*();}
49 private:
50     pthread_cond_t  mCondVar;
51 };
52 
53 class AutoThreadMutex
54 {
55 public:
56     AutoThreadMutex(ThreadMutex &m);
57     virtual ~AutoThreadMutex();
58     operator ThreadMutex& ()          {return mm;}
59     operator pthread_mutex_t* () {return (pthread_mutex_t*)mm;}
60 private:
61     ThreadMutex  &mm;
62 };
63 
64 class NfcAdaptation
65 {
66 public:
67     virtual ~NfcAdaptation();
68     void    Initialize();
69     void    Finalize();
70     static  NfcAdaptation& GetInstance();
71     tHAL_NFC_ENTRY* GetHalEntryFuncs ();
72     void    DownloadFirmware ();
73 
74 private:
75     NfcAdaptation();
76     void    signal();
77     static  NfcAdaptation* mpInstance;
78     static  ThreadMutex sLock;
79     ThreadCondVar    mCondVar;
80     tHAL_NFC_ENTRY   mHalEntryFuncs; // function pointers for HAL entry points
81     static nfc_nci_device_t* mHalDeviceContext;
82     static tHAL_NFC_CBACK* mHalCallback;
83     static tHAL_NFC_DATA_CBACK* mHalDataCallback;
84     static ThreadCondVar mHalOpenCompletedEvent;
85     static ThreadCondVar mHalCloseCompletedEvent;
86 
87     static UINT32 NFCA_TASK (UINT32 arg);
88     static UINT32 Thread (UINT32 arg);
89     void InitializeHalDeviceContext ();
90     static void HalDeviceContextCallback (nfc_event_t event, nfc_status_t event_status);
91     static void HalDeviceContextDataCallback (uint16_t data_len, uint8_t* p_data);
92 
93     static void HalInitialize ();
94     static void HalTerminate ();
95     static void HalOpen (tHAL_NFC_CBACK* p_hal_cback, tHAL_NFC_DATA_CBACK* p_data_cback);
96     static void HalClose ();
97     static void HalCoreInitialized (UINT8* p_core_init_rsp_params);
98     static void HalWrite (UINT16 data_len, UINT8* p_data);
99     static BOOLEAN HalPrediscover ();
100     static void HalControlGranted ();
101     static void HalPowerCycle ();
102     static UINT8 HalGetMaxNfcee ();
103     static void HalDownloadFirmwareCallback (nfc_event_t event, nfc_status_t event_status);
104     static void HalDownloadFirmwareDataCallback (uint16_t data_len, uint8_t* p_data);
105 };
106 
107