1 2 /****************************************************************************** 3 * 4 * Copyright 2022-2023 NXP 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 ******************************************************************************/ 19 20 #pragma once 21 22 #include <aidl/android/hardware/nfc/BnNfc.h> 23 #include <aidl/android/hardware/nfc/INfc.h> 24 #include <aidl/android/hardware/nfc/INfcClientCallback.h> 25 #include <aidl/android/hardware/nfc/NfcConfig.h> 26 #include <aidl/android/hardware/nfc/NfcEvent.h> 27 #include <aidl/android/hardware/nfc/NfcStatus.h> 28 #include <aidl/android/hardware/nfc/PresenceCheckAlgorithm.h> 29 #include <aidl/android/hardware/nfc/ProtocolDiscoveryConfig.h> 30 #include <android-base/logging.h> 31 #include <log/log.h> 32 33 #include "phNxpNciHal_ext.h" 34 35 namespace aidl { 36 namespace android { 37 namespace hardware { 38 namespace nfc { 39 40 using ::aidl::android::hardware::nfc::NfcCloseType; 41 using ::aidl::android::hardware::nfc::NfcConfig; 42 using ::aidl::android::hardware::nfc::NfcStatus; 43 using NfcConfig = aidl::android::hardware::nfc::NfcConfig; 44 using ::aidl::android::hardware::nfc::NfcEvent; 45 46 // Default implementation that reports no support NFC. 47 struct Nfc : public BnNfc { 48 public: 49 Nfc() = default; 50 51 ::ndk::ScopedAStatus open( 52 const std::shared_ptr<INfcClientCallback>& clientCallback) override; 53 ::ndk::ScopedAStatus close(NfcCloseType type) override; 54 ::ndk::ScopedAStatus coreInitialized() override; 55 ::ndk::ScopedAStatus factoryReset() override; 56 ::ndk::ScopedAStatus getConfig(NfcConfig* _aidl_return) override; 57 ::ndk::ScopedAStatus powerCycle() override; 58 ::ndk::ScopedAStatus preDiscover() override; 59 ::ndk::ScopedAStatus write(const std::vector<uint8_t>& data, 60 int32_t* _aidl_return) override; 61 ::ndk::ScopedAStatus setEnableVerboseLogging(bool enable) override; 62 ::ndk::ScopedAStatus isVerboseLoggingEnabled(bool* _aidl_return) override; 63 ::ndk::ScopedAStatus controlGranted(NfcStatus* _aidl_return_) override; 64 mapToAidlIfRequiredNfc65 static uint8_t mapToAidlIfRequired(uint8_t event) { 66 switch (event) { 67 case HAL_HCI_NETWORK_RESET_EVT: 68 event = (uint8_t)NfcEvent::HCI_NETWORK_RESET; 69 break; 70 case HAL_NFC_REQUEST_CONTROL_EVT: 71 event = (uint8_t)NfcEvent::REQUEST_CONTROL; 72 break; 73 case HAL_NFC_RELEASE_CONTROL_EVT: 74 event = (uint8_t)NfcEvent::RELEASE_CONTROL; 75 break; 76 default: 77 break; 78 } 79 return event; 80 } 81 eventCallbackNfc82 static void eventCallback(uint8_t event, uint8_t status) { 83 if (mCallback != nullptr) { 84 event = mapToAidlIfRequired(event); 85 auto ret = mCallback->sendEvent((NfcEvent)event, (NfcStatus)status); 86 if (!ret.isOk()) { 87 LOG(ERROR) << "Failed to send event!"; 88 } 89 } 90 } 91 dataCallbackNfc92 static void dataCallback(uint16_t data_len, uint8_t* p_data) { 93 std::vector<uint8_t> data(p_data, p_data + data_len); 94 if (mCallback != nullptr) { 95 auto ret = mCallback->sendData(data); 96 if (!ret.isOk()) { 97 LOG(ERROR) << "Failed to send data!"; 98 } 99 } 100 } 101 102 static std::shared_ptr<INfcClientCallback> mCallback; 103 }; 104 105 } // namespace nfc 106 } // namespace hardware 107 } // namespace android 108 } // namespace aidl 109