1 /* 2 * Copyright 2024, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include <aidl/android/hardware/nfc/INfc.h> 19 #include <android/hardware/nfc/1.1/INfc.h> 20 #include <android/hardware/nfc/1.2/INfc.h> 21 22 #include "config.h" 23 #include "nfc_api.h" 24 #include "nfc_hal_api.h" 25 26 using android::sp; 27 using android::hardware::nfc::V1_0::INfc; 28 using INfcAidl = ::aidl::android::hardware::nfc::INfc; 29 30 #define NFCSTATUS_EXTN_FEATURE_SUCCESS (0x0050) 31 32 // This is only intended for a limited time to handle non-AOSP vendor interface 33 // implementations on existing upgrading devices and not as a new extension point. 34 // This will be removed once all devices are upgraded to the latest NFC HAL. 35 /** 36 * @brief Vendor extension control block holds below data's 37 * hidlHal - reference to HIDL Hal instance 38 * aidlHal - reference to AIDL Hal instance 39 * pHalCback - reference to HAL events callback 40 * pDataCallback - reference to NCI response and notification packets 41 * configMap - holds the configs as keys and values 42 * 43 */ 44 struct VendorExtnCb { 45 sp<INfc> hidlHal; 46 std::shared_ptr<INfcAidl> aidlHal; 47 tHAL_NFC_CBACK* pHalCback; 48 tHAL_NFC_DATA_CBACK* pDataCback; 49 std::map<std::string, ConfigValue> configMap; 50 }; 51 52 /** 53 * @brief Holds NCI packet data length and data buffer 54 * 55 */ 56 typedef struct { 57 uint16_t data_len; 58 uint8_t* p_data; 59 } NciData_t; 60 61 /** 62 * @brief Holds functional event datas to support 63 * extension features 64 */ 65 typedef struct { 66 NciData_t nci_msg; 67 NciData_t nci_rsp_ntf; 68 uint8_t write_status; 69 uint8_t hal_state; 70 uint8_t rf_state; 71 uint8_t hal_event; 72 uint8_t hal_event_status; 73 } NfcExtEventData_t; 74 75 /** 76 * @brief Holds functional event codes to support 77 * extension features. 78 * Begin with 0x0B to avoid conflicts with standard and vendor specific HAL 79 * events 80 */ 81 typedef enum { 82 HANDLE_VENDOR_NCI_MSG = 0x0B, 83 HANDLE_VENDOR_NCI_RSP_NTF, 84 HANDLE_WRITE_COMPLETE_STATUS, 85 HANDLE_HAL_CONTROL_GRANTED, 86 HANDLE_NFC_HAL_STATE_UPDATE, 87 HANDLE_RF_HAL_STATE_UPDATE, 88 HANDLE_HAL_EVENT, 89 HANDLE_FW_DNLD_STATUS_UPDATE, 90 HANDLE_DOWNLOAD_FIRMWARE_REQUEST, 91 HANDLE_NFC_ADAPTATION_INIT, 92 HANDLE_NFC_PRE_DISCOVER, 93 HANDLE_NFC_HAL_CORE_INITIALIZE, 94 HANDLE_NFC_HAL_POWER_CYCLE, 95 HANDLE_NFC_GET_MAX_NFCEE, 96 HANDLE_NFC_HAL_CLOSE, 97 } NfcExtEvent_t; 98 99 typedef enum { 100 NFCC_HAL_TRANS_ERR_CODE = 6u, 101 NFCC_HAL_FATAL_ERR_CODE = 8u, 102 } NfcExtHal_NFCC_ERROR_CODE_t; 103 104 class NfcVendorExtn { 105 public: 106 /** 107 * @brief Get the singleton of this object. 108 * @return Reference to this object. 109 * 110 */ 111 static NfcVendorExtn* getInstance(); 112 113 /** 114 * @brief This function sets up and initialize the extension feature 115 * @param hidlHal reference to HIDL Hal instance 116 * @param aidlHal reference to AIDL Hal instance 117 * @return true if init is success else false 118 * 119 */ 120 bool Initialize(sp<INfc> hidlHal, std::shared_ptr<INfcAidl> aidlHal); 121 122 /** 123 * @brief This function sets ups the NCI event and data callback pointers. 124 * @param pHalCback reference to HAL events callback 125 * @param pDataCback reference to NCI response and notification packets 126 * @return None 127 * \Note: This function pointers will be used to notify the 128 * NCI event and data to upper layer. 129 * 130 */ 131 void setNciCallback(tHAL_NFC_CBACK* pHalCback, 132 tHAL_NFC_DATA_CBACK* pDataCback); 133 134 /** 135 * @brief sends the NCI packet to handle extension feature 136 * @param dataLen length of the NCI packet 137 * @param pData data buffer pointer 138 * @return returns true if it is vendor specific feature, 139 * and handled only by extension library otherwise returns 140 * false and it have to be handled by libnfc. 141 * 142 */ 143 bool processCmd(uint16_t dataLen, uint8_t* pData); 144 145 /** 146 * @brief sends the NCI packet to handle extension feature 147 * @param dataLen length of the NCI packet 148 * @param pData data buffer pointer 149 * @return returns true if it is vendor specific feature, 150 * and handled only by extension library otherwise returns 151 * false and it have to be handled by libnfc. 152 * 153 */ 154 bool processRspNtf(uint16_t dataLen, uint8_t* pData); 155 156 /** 157 * @brief sends the NCI packet to handle extension feature 158 * @param event 159 * @param status 160 * @return returns true if it is vendor specific feature, 161 * and handled only by extension library otherwise returns 162 * false and it have to be handled by libnfc. 163 * 164 */ 165 bool processEvent(uint8_t event, tHAL_NFC_STATUS status); 166 167 /** 168 * @brief Loads the Nfc Vendor Config 169 * @param pConfigMap pointer to the config map 170 * @return None 171 * \Note @param pConfigMap is needed for future use 172 * to add the vendor specific properties. 173 * 174 */ 175 void getVendorConfigs(std::map<std::string, ConfigValue>* pConfigMap); 176 177 /** 178 * @brief return the pointer of vendor extension control block. 179 * @return A pointer to the VendorExtnCb structure or nullptr, 180 * if the structure is not available or invalid. 181 * 182 */ 183 VendorExtnCb* getVendorExtnCb(); 184 185 /** 186 * @brief This function de-initializes the extension feature 187 * @return void 188 * 189 */ 190 bool finalize(); 191 192 private: 193 VendorExtnCb mVendorExtnCb; 194 195 NfcVendorExtn(); 196 197 ~NfcVendorExtn(); 198 }; 199