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