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