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 
eventCallbackNfc64   static void eventCallback(uint8_t event, uint8_t status) {
65     if (mCallback != nullptr) {
66       if (event == HAL_HCI_NETWORK_RESET_EVT) {
67         event = (uint8_t)NfcEvent::HCI_NETWORK_RESET;
68       }
69       auto ret = mCallback->sendEvent((NfcEvent)event, (NfcStatus)status);
70       if (!ret.isOk()) {
71         LOG(ERROR) << "Failed to send event!";
72       }
73     }
74   }
75 
dataCallbackNfc76   static void dataCallback(uint16_t data_len, uint8_t* p_data) {
77     std::vector<uint8_t> data(p_data, p_data + data_len);
78     if (mCallback != nullptr) {
79       auto ret = mCallback->sendData(data);
80       if (!ret.isOk()) {
81         LOG(ERROR) << "Failed to send data!";
82       }
83     }
84   }
85 
86   static std::shared_ptr<INfcClientCallback> mCallback;
87 };
88 
89 }  // namespace nfc
90 }  // namespace hardware
91 }  // namespace android
92 }  // namespace aidl
93