1 /*
2  * Copyright (C) 2021 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 
17 #pragma once
18 
19 #include <aidl/android/hardware/nfc/BnNfc.h>
20 #include <aidl/android/hardware/nfc/INfcClientCallback.h>
21 #include <android-base/logging.h>
22 
23 #include "hardware_nfc.h"
24 
25 namespace aidl {
26 namespace android {
27 namespace hardware {
28 namespace nfc {
29 
30 using ::aidl::android::hardware::nfc::NfcCloseType;
31 using ::aidl::android::hardware::nfc::NfcConfig;
32 using ::aidl::android::hardware::nfc::NfcEvent;
33 using ::aidl::android::hardware::nfc::NfcStatus;
34 
35 // Default implementation that reports no support NFC.
36 struct Nfc : public BnNfc {
37  public:
38   Nfc() = default;
39 
40   ::ndk::ScopedAStatus open(
41       const std::shared_ptr<INfcClientCallback>& clientCallback) override;
42   ::ndk::ScopedAStatus close(NfcCloseType type) override;
43   ::ndk::ScopedAStatus coreInitialized() override;
44   ::ndk::ScopedAStatus factoryReset() override;
45   ::ndk::ScopedAStatus getConfig(NfcConfig* _aidl_return) override;
46   ::ndk::ScopedAStatus powerCycle() override;
47   ::ndk::ScopedAStatus preDiscover() override;
48   ::ndk::ScopedAStatus write(const std::vector<uint8_t>& data,
49                              int32_t* _aidl_return) override;
50   ::ndk::ScopedAStatus setEnableVerboseLogging(bool enable) override;
51   ::ndk::ScopedAStatus isVerboseLoggingEnabled(bool* _aidl_return) override;
52 
eventCallbackNfc53   static void eventCallback(uint8_t event, uint8_t status) {
54     if (mCallback != nullptr) {
55       NfcEvent mEvent;
56       NfcStatus mStatus;
57       switch (event) {
58         case HAL_NFC_OPEN_CPLT_EVT:
59           mEvent = NfcEvent::OPEN_CPLT;
60           break;
61         case HAL_NFC_CLOSE_CPLT_EVT:
62           mEvent = NfcEvent::CLOSE_CPLT;
63           break;
64         case HAL_NFC_POST_INIT_CPLT_EVT:
65           mEvent = NfcEvent::POST_INIT_CPLT;
66           break;
67         case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
68           mEvent = NfcEvent::PRE_DISCOVER_CPLT;
69           break;
70         case HAL_HCI_NETWORK_RESET:
71           mEvent = NfcEvent::HCI_NETWORK_RESET;
72           break;
73         case HAL_NFC_REQUEST_CONTROL_EVT:
74         case HAL_NFC_RELEASE_CONTROL_EVT:
75         case HAL_NFC_ERROR_EVT:
76         default:
77           mEvent = NfcEvent::ERROR;
78       }
79       switch (status) {
80         case HAL_NFC_STATUS_OK:
81           mStatus = NfcStatus::OK;
82           break;
83         case HAL_NFC_STATUS_FAILED:
84           mStatus = NfcStatus::FAILED;
85           break;
86         case HAL_NFC_STATUS_ERR_TRANSPORT:
87           mStatus = NfcStatus::ERR_TRANSPORT;
88           break;
89         case HAL_NFC_STATUS_ERR_CMD_TIMEOUT:
90           mStatus = NfcStatus::ERR_CMD_TIMEOUT;
91           break;
92         case HAL_NFC_STATUS_REFUSED:
93           mStatus = NfcStatus::REFUSED;
94           break;
95         default:
96           mStatus = NfcStatus::FAILED;
97       }
98       auto ret = mCallback->sendEvent(mEvent, mStatus);
99       if (!ret.isOk()) {
100         LOG(ERROR) << "Failed to send event!";
101       }
102     }
103   }
104 
dataCallbackNfc105   static void dataCallback(uint16_t data_len, uint8_t* p_data) {
106     std::vector<uint8_t> data(p_data, p_data + data_len);
107     if (mCallback != nullptr) {
108       auto ret = mCallback->sendData(data);
109       if (!ret.isOk()) {
110         LOG(ERROR) << "Failed to send data!";
111       }
112     }
113   }
114 
115   static std::shared_ptr<INfcClientCallback> mCallback;
116 };
117 
118 }  // namespace nfc
119 }  // namespace hardware
120 }  // namespace android
121 }  // namespace aidl
122