• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <pthread.h>
21 #include <utils/RefBase.h>
22 
23 #include "config.h"
24 #include "nfc_hal_api.h"
25 #include "nfc_target.h"
26 
27 using ::android::sp;
28 
29 namespace android {
30 namespace hardware {
31 namespace nfc {
32 namespace V1_0 {
33 struct INfc;
34 struct INfcClientCallback;
35 }  // namespace V1_0
36 namespace V1_1 {
37 struct INfc;
38 struct INfcClientCallback;
39 }  // namespace V1_1
40 namespace V1_2 {
41 struct INfc;
42 }
43 }  // namespace nfc
44 }  // namespace hardware
45 }  // namespace android
46 
47 class ThreadMutex {
48  public:
49   ThreadMutex();
50   virtual ~ThreadMutex();
51   void lock();
52   void unlock();
53   explicit operator pthread_mutex_t*() { return &mMutex; }
54 
55  private:
56   pthread_mutex_t mMutex;
57 };
58 
59 class ThreadCondVar : public ThreadMutex {
60  public:
61   ThreadCondVar();
62   virtual ~ThreadCondVar();
63   void signal();
64   void wait();
65   bool wait(long millisec);
66   explicit operator pthread_cond_t*() { return &mCondVar; }
67   // NOLINTNEXTLINE(google-explicit-constructor)
68   operator pthread_mutex_t*() {
69     return ThreadMutex::operator pthread_mutex_t*();
70   }
71 
72  private:
73   pthread_cond_t mCondVar;
74 };
75 
76 class AutoThreadMutex {
77  public:
78   explicit AutoThreadMutex(ThreadMutex& m);
79   virtual ~AutoThreadMutex();
80   explicit operator ThreadMutex&() { return mm; }
81   explicit operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; }
82 
83  private:
84   ThreadMutex& mm;
85 };
86 
87 class NfcHalDeathRecipient;
88 
89 class NfcAdaptation {
90  public:
91   virtual ~NfcAdaptation();
92   void Initialize();
93   void Finalize();
94   void FactoryReset();
95   void DeviceShutdown();
96   static NfcAdaptation& GetInstance();
97   tHAL_NFC_ENTRY* GetHalEntryFuncs();
98   bool DownloadFirmware();
99   void GetVendorConfigs(std::map<std::string, ConfigValue>& configMap);
100   void Dump(int fd);
101 
102  private:
103   NfcAdaptation();
104   void signal();
105   static NfcAdaptation* mpInstance;
106   static ThreadMutex sLock;
107   ThreadCondVar mCondVar;
108   tHAL_NFC_ENTRY mHalEntryFuncs;  // function pointers for HAL entry points
109   static android::sp<android::hardware::nfc::V1_0::INfc> mHal;
110   static android::sp<android::hardware::nfc::V1_1::INfc> mHal_1_1;
111   static android::sp<android::hardware::nfc::V1_2::INfc> mHal_1_2;
112   static android::hardware::nfc::V1_1::INfcClientCallback* mCallback;
113   sp<NfcHalDeathRecipient> mNfcHalDeathRecipient;
114   static tHAL_NFC_CBACK* mHalCallback;
115   static tHAL_NFC_DATA_CBACK* mHalDataCallback;
116   static ThreadCondVar mHalOpenCompletedEvent;
117 
118 
119   static uint32_t NFCA_TASK(uint32_t arg);
120   static uint32_t Thread(uint32_t arg);
121   void InitializeHalDeviceContext();
122   static void HalDeviceContextCallback(nfc_event_t event,
123                                        nfc_status_t event_status);
124   static void HalDeviceContextDataCallback(uint16_t data_len, uint8_t* p_data);
125 
126   static void HalInitialize();
127   static void HalTerminate();
128   static void HalOpenInternal(tHAL_NFC_CBACK* p_hal_cback,
129                               tHAL_NFC_DATA_CBACK* p_data_cback);
130   static void HalOpen(tHAL_NFC_CBACK* p_hal_cback,
131                       tHAL_NFC_DATA_CBACK* p_data_cback);
132   static void HalClose();
133   static void HalCoreInitialized(uint16_t data_len,
134                                  uint8_t* p_core_init_rsp_params);
135   static void HalWrite(uint16_t data_len, uint8_t* p_data);
136   static bool HalPrediscover();
137   static void HalControlGranted();
138   static void HalPowerCycle();
139   static uint8_t HalGetMaxNfcee();
140   static void HalDownloadFirmwareCallback(nfc_event_t event,
141                                           nfc_status_t event_status);
142   static void HalDownloadFirmwareDataCallback(uint16_t data_len,
143                                               uint8_t* p_data);
144 };
145