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 
21 #include "nfc_hal_api.h"
22 #include "nfc_target.h"
23 
24 #include <utils/RefBase.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace nfc {
29 namespace V1_0 {
30 struct INfc;
31 struct INfcClientCallback;
32 }
33 }
34 }
35 }
36 
37 class ThreadMutex {
38  public:
39   ThreadMutex();
40   virtual ~ThreadMutex();
41   void lock();
42   void unlock();
43   operator pthread_mutex_t*() { return &mMutex; }
44 
45  private:
46   pthread_mutex_t mMutex;
47 };
48 
49 class ThreadCondVar : public ThreadMutex {
50  public:
51   ThreadCondVar();
52   virtual ~ThreadCondVar();
53   void signal();
54   void wait();
55   operator pthread_cond_t*() { return &mCondVar; }
56   operator pthread_mutex_t*() {
57     return ThreadMutex::operator pthread_mutex_t*();
58   }
59 
60  private:
61   pthread_cond_t mCondVar;
62 };
63 
64 class AutoThreadMutex {
65  public:
66   AutoThreadMutex(ThreadMutex& m);
67   virtual ~AutoThreadMutex();
68   operator ThreadMutex&() { return mm; }
69   operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; }
70 
71  private:
72   ThreadMutex& mm;
73 };
74 
75 class NfcAdaptation {
76  public:
77   virtual ~NfcAdaptation();
78   void Initialize();
79   void Finalize();
80   static NfcAdaptation& GetInstance();
81   tHAL_NFC_ENTRY* GetHalEntryFuncs();
82   void DownloadFirmware();
83 
84  private:
85   NfcAdaptation();
86   void signal();
87   static NfcAdaptation* mpInstance;
88   static ThreadMutex sLock;
89   ThreadCondVar mCondVar;
90   tHAL_NFC_ENTRY mHalEntryFuncs;  // function pointers for HAL entry points
91   static nfc_nci_device_t* mHalDeviceContext;
92   static android::sp<android::hardware::nfc::V1_0::INfc> mHal;
93   static android::hardware::nfc::V1_0::INfcClientCallback* mCallback;
94   static tHAL_NFC_CBACK* mHalCallback;
95   static tHAL_NFC_DATA_CBACK* mHalDataCallback;
96   static ThreadCondVar mHalOpenCompletedEvent;
97   static ThreadCondVar mHalCloseCompletedEvent;
98 
99   static uint32_t NFCA_TASK(uint32_t arg);
100   static uint32_t Thread(uint32_t arg);
101   void InitializeHalDeviceContext();
102   static void HalDeviceContextCallback(nfc_event_t event,
103                                        nfc_status_t event_status);
104   static void HalDeviceContextDataCallback(uint16_t data_len, uint8_t* p_data);
105 
106   static void HalInitialize();
107   static void HalTerminate();
108   static void HalOpen(tHAL_NFC_CBACK* p_hal_cback,
109                       tHAL_NFC_DATA_CBACK* p_data_cback);
110   static void HalClose();
111   static void HalCoreInitialized(uint16_t data_len,
112                                  uint8_t* p_core_init_rsp_params);
113   static void HalWrite(uint16_t data_len, uint8_t* p_data);
114   static bool HalPrediscover();
115   static void HalControlGranted();
116   static void HalPowerCycle();
117   static uint8_t HalGetMaxNfcee();
118   static void HalDownloadFirmwareCallback(nfc_event_t event,
119                                           nfc_status_t event_status);
120   static void HalDownloadFirmwareDataCallback(uint16_t data_len,
121                                               uint8_t* p_data);
122 };
123