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