1 /* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef NFCC_NCI_ADAPTER_H 16 #define NFCC_NCI_ADAPTER_H 17 #include <mutex> 18 #include "extns.h" 19 #include "ndef_utils.h" 20 #include "nfa_api.h" 21 #include "nfa_ce_api.h" 22 #include "nfa_ee_api.h" 23 #include "nfa_hci_api.h" 24 #include "nfa_rw_api.h" 25 #include "nfc_hal_api.h" 26 #include "NfcAdaptation.h" 27 #include "nfc_config.h" 28 #include "synchronize_event.h" 29 #include "inci_ce_interface.h" 30 31 namespace OHOS { 32 namespace NFC { 33 namespace NCI { 34 class NfccNciAdapter final { 35 public: 36 static NfccNciAdapter& GetInstance(); 37 ~NfccNciAdapter(); 38 39 /** 40 * @brief Set card emulation listener to receive field on/off event. 41 * @param listener The listener to receive field on/off event. 42 */ 43 void SetCeHostListener(std::weak_ptr<INciCeInterface::ICeHostListener> listener); 44 void ClearT3tIdentifiersCache(); 45 uint32_t GetLfT3tMax(); 46 uint32_t GetLastError(); 47 void Abort(); 48 49 /** 50 * @brief Whether nfc is enabled or disabled. 51 * @return true/false - nfc is enabled/disabled. 52 */ 53 bool IsNfcActive(); 54 55 /** 56 * @brief Initialize nfc. 57 * @return true/false - initialize is successful or not successful. 58 */ 59 bool Initialize(); 60 61 /** 62 * @brief Deinitialize nfc. 63 * @return true/false - deinitialize is successful or not successful. 64 */ 65 bool Deinitialize(); 66 67 /** 68 * @brief Enable discovery for nfc. 69 * @param techMask Supported rf technology for nfc. 70 * @param enableReaderMode True/false to enable/disable reader mode 71 * @param enableHostRouting True/false to enable/disable host routing 72 * @param restart True/false to restart or not restart 73 */ 74 void EnableDiscovery(uint16_t techMask, bool enableReaderMode, bool enableHostRouting, bool restart); 75 76 /** 77 * @brief Disable discovery for nfc. 78 */ 79 void DisableDiscovery(); 80 81 /** 82 * @brief Send raw data. 83 * @param rawData Data needed to send 84 * @return True/false to successful/failed to send 85 */ 86 bool SendRawFrame(std::string& rawData); 87 88 /** 89 * @brief Send the status of screen. 90 * @param screenStateMask The state of screen 91 */ 92 void SetScreenStatus(unsigned char screenStateMask); 93 94 /** 95 * @brief Get nci version. 96 * @return Nci version 97 */ 98 uint32_t GetNciVersion() const; 99 bool RegisterT3tIdentifier(const std::string& t3tIdentifier) const; 100 void DeregisterT3tIdentifier(uint32_t handle) const; 101 102 /** 103 * @brief Check whether to load firmware. 104 * @return True/false to success/fail to load firmware. 105 */ 106 bool CheckFirmware(); 107 108 /** 109 * @brief Dump debug info for nfc. 110 * @param fd File descriptor to store debug info. 111 */ 112 void Dump(uint32_t fd) const; 113 114 /** 115 * @brief Reset nfc chip. 116 */ 117 void FactoryReset() const; 118 119 /** 120 * @brief Close nfc. 121 */ 122 void Shutdown() const; 123 124 /** 125 * @brief whether tag is active. 126 * @return True/false tag is active/deactive. 127 */ 128 bool IsTagActive() const; 129 130 /** 131 * @brief Start or stop rf discovery. 132 * @param isStart True/false start/stop rf discovery. 133 */ 134 void StartRfDiscovery(bool isStart); 135 136 /** 137 * @brief Query whether to start rf discovery. 138 * @return True/false to start/stop rf discovery. 139 */ 140 bool IsRfEbabled(); 141 142 /** 143 * @brief Config commit routing table for nfc. 144 * @return True/false to be successful/failed to config routing table. 145 */ 146 bool CommitRouting(); 147 148 /** 149 * @brief Computer routing params. 150 * @return True/false to be successful/failed to computer params. 151 */ 152 bool ComputeRoutingParams(int defaultPaymentType); 153 154 /** 155 * @brief Whether rf field is on or off. 156 * @return True/false to be field on/off. 157 */ 158 bool isRfFieldOn(); 159 void OnCardEmulationData(const std::vector<uint8_t> &data); 160 void OnCardEmulationActivated(); 161 void OnCardEmulationDeactivated(); 162 // method for SAK28 issue 163 void SendActEvtForSak28Tag(uint8_t connEvent, tNFA_CONN_EVT_DATA* eventData); 164 165 private: 166 NfccNciAdapter(); 167 168 tNFA_STATUS StartPolling(tNFA_TECHNOLOGY_MASK techMask); 169 tNFA_STATUS StopPolling(); 170 void DoNfaPollEnabledDisabledEvt(); 171 void DoNfaActivatedEvt(tNFA_CONN_EVT_DATA* eventData); 172 void DoNfaSelectResultEvt(uint8_t status); 173 void DoNfaDeactivatedEvt(tNFA_CONN_EVT_DATA* eventData); 174 void DoNfaDiscResultEvt(tNFA_CONN_EVT_DATA* eventData); 175 void DoNfaPresenceEvt(tNFA_CONN_EVT_DATA* eventData); 176 void DoNfaDmEnableEvt(tNFA_DM_CBACK_DATA* eventData); 177 void DoNfaDmDisableEvt(tNFA_DM_CBACK_DATA* eventData); 178 void DoNfaDmRfFieldEvt(tNFA_DM_CBACK_DATA* eventData); 179 void DoNfaDmNfccTimeoutEvt(tNFA_DM_CBACK_DATA* eventData); 180 uint8_t GetDiscovryParam(unsigned char screenState, unsigned char screenStateMask); 181 bool IsDiscTypeListen(tNFC_ACTIVATE_DEVT& actNtf); 182 void HandleDiscNtf(tNFC_RESULT_DEVT* discNtf); 183 tNFA_STATUS NfaRegVSCback(bool isRegster, tNFA_VSC_CBACK* vscCback); 184 void DoNfaDmSetConfig(); 185 void DoNfaSetPowerSubState(); 186 // static callback functions regiter to nci stack. 187 static void NfcConnectionCallback(uint8_t connEvent, tNFA_CONN_EVT_DATA* eventData); 188 static void NfcDeviceManagementCallback(uint8_t dmEvent, tNFA_DM_CBACK_DATA* eventData); 189 static void PrivateNciCallback(uint8_t event, uint16_t paramLen, uint8_t *param); 190 191 static const tNFA_TECHNOLOGY_MASK DEFAULT_TECH_MASK = 192 (NFA_TECHNOLOGY_MASK_A | NFA_TECHNOLOGY_MASK_B | NFA_TECHNOLOGY_MASK_F | 193 NFA_TECHNOLOGY_MASK_V); 194 static const uint32_t DEFAULT_DISCOVERY_DURATION = 500; 195 static const uint32_t DISCOVERY_DURATION = 200; 196 static const uint32_t NFA_SCREEN_POLLING_TAG_MASK = 0x10; 197 OHOS::NFC::SynchronizeEvent nfcEnableEvent_; 198 OHOS::NFC::SynchronizeEvent nfcDisableEvent_; 199 OHOS::NFC::SynchronizeEvent nfcStartStopPollingEvent_; 200 OHOS::NFC::SynchronizeEvent nfcSetPowerSubStateEvent_; 201 OHOS::NFC::SynchronizeEvent nfcSetConfigEvent_; 202 bool isNfcEnabled_ = false; 203 bool isRoutingInited_ = false; 204 bool rfEnabled_ = false; 205 bool discoveryEnabled_ = false; // is polling or listening 206 bool pollingEnabled_ = false; // is polling for tag 207 bool isDisabling_ = false; 208 bool readerModeEnabled_ = false; 209 unsigned long discoveryDuration_ = 0; 210 bool isTagActive_ = false; 211 unsigned char curScreenState_ = NFA_SCREEN_STATE_OFF_LOCKED; 212 uint64_t lastRfFieldTime = 0; 213 bool isRfFieldOn_ = false; 214 std::weak_ptr<INciCeInterface::ICeHostListener> cardEmulationListener_; 215 }; 216 } // namespace NCI 217 } // namespace NFC 218 } // namespace OHOS 219 #endif // NFCC_NCI_ADAPTER_H 220