blob: 4685b592369bbc5f07586f68a6a5ffe470f05094 [file] [log] [blame]
George Changdbc36e52021-10-20 11:13:18 +08001/*
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#include "Nfc.h"
18
19#include <android-base/logging.h>
20
21#include "Vendor_hal_api.h"
22
23namespace aidl {
24namespace android {
25namespace hardware {
26namespace nfc {
27
28std::shared_ptr<INfcClientCallback> Nfc::mCallback = nullptr;
29AIBinder_DeathRecipient* clientDeathRecipient = nullptr;
30
31void OnDeath(void* cookie) {
32 if (Nfc::mCallback != nullptr && !AIBinder_isAlive(Nfc::mCallback->asBinder().get())) {
33 LOG(INFO) << __func__ << " Nfc service has died";
34 Nfc* nfc = static_cast<Nfc*>(cookie);
35 nfc->close(NfcCloseType::DISABLE);
36 }
37}
38
39::ndk::ScopedAStatus Nfc::open(const std::shared_ptr<INfcClientCallback>& clientCallback) {
40 LOG(INFO) << "open";
41 if (clientCallback == nullptr) {
42 LOG(INFO) << "Nfc::open null callback";
43 return ndk::ScopedAStatus::fromServiceSpecificError(
44 static_cast<int32_t>(NfcStatus::FAILED));
George Changdbc36e52021-10-20 11:13:18 +080045 }
Greg Kaiser358a2bb2022-01-24 10:38:37 -080046 Nfc::mCallback = clientCallback;
47
48 clientDeathRecipient = AIBinder_DeathRecipient_new(OnDeath);
49 auto linkRet = AIBinder_linkToDeath(clientCallback->asBinder().get(), clientDeathRecipient,
50 this /* cookie */);
51 if (linkRet != STATUS_OK) {
52 LOG(ERROR) << __func__ << ": linkToDeath failed: " << linkRet;
53 // Just ignore the error.
54 }
55
56 int ret = Vendor_hal_open(eventCallback, dataCallback);
57 return ret == 0 ? ndk::ScopedAStatus::ok()
58 : ndk::ScopedAStatus::fromServiceSpecificError(
59 static_cast<int32_t>(NfcStatus::FAILED));
George Changdbc36e52021-10-20 11:13:18 +080060}
61
62::ndk::ScopedAStatus Nfc::close(NfcCloseType type) {
63 LOG(INFO) << "close";
64 if (Nfc::mCallback == nullptr) {
65 LOG(ERROR) << __func__ << "mCallback null";
66 return ndk::ScopedAStatus::fromServiceSpecificError(
67 static_cast<int32_t>(NfcStatus::FAILED));
68 }
69 int ret = 0;
70 if (type == NfcCloseType::HOST_SWITCHED_OFF) {
71 ret = Vendor_hal_close_off();
72 } else {
73 ret = Vendor_hal_close();
74 }
75 Nfc::mCallback = nullptr;
76 AIBinder_DeathRecipient_delete(clientDeathRecipient);
77 clientDeathRecipient = nullptr;
78 return ret == 0 ? ndk::ScopedAStatus::ok()
79 : ndk::ScopedAStatus::fromServiceSpecificError(
80 static_cast<int32_t>(NfcStatus::FAILED));
81}
82
83::ndk::ScopedAStatus Nfc::coreInitialized() {
84 LOG(INFO) << "coreInitialized";
85 if (Nfc::mCallback == nullptr) {
86 LOG(ERROR) << __func__ << "mCallback null";
87 return ndk::ScopedAStatus::fromServiceSpecificError(
88 static_cast<int32_t>(NfcStatus::FAILED));
89 }
90 int ret = Vendor_hal_core_initialized();
91
92 return ret == 0 ? ndk::ScopedAStatus::ok()
93 : ndk::ScopedAStatus::fromServiceSpecificError(
94 static_cast<int32_t>(NfcStatus::FAILED));
95}
96
97::ndk::ScopedAStatus Nfc::factoryReset() {
98 LOG(INFO) << "factoryReset";
99 Vendor_hal_factoryReset();
100 return ndk::ScopedAStatus::ok();
101}
102
103::ndk::ScopedAStatus Nfc::getConfig(NfcConfig* _aidl_return) {
104 LOG(INFO) << "getConfig";
105 NfcConfig nfcVendorConfig;
106 Vendor_hal_getConfig(nfcVendorConfig);
107
108 *_aidl_return = nfcVendorConfig;
109 return ndk::ScopedAStatus::ok();
110}
111
112::ndk::ScopedAStatus Nfc::powerCycle() {
113 LOG(INFO) << "powerCycle";
114 if (Nfc::mCallback == nullptr) {
115 LOG(ERROR) << __func__ << "mCallback null";
116 return ndk::ScopedAStatus::fromServiceSpecificError(
117 static_cast<int32_t>(NfcStatus::FAILED));
118 }
119 return Vendor_hal_power_cycle() ? ndk::ScopedAStatus::fromServiceSpecificError(
120 static_cast<int32_t>(NfcStatus::FAILED))
121 : ndk::ScopedAStatus::ok();
122}
123
124::ndk::ScopedAStatus Nfc::preDiscover() {
125 LOG(INFO) << "preDiscover";
126 if (Nfc::mCallback == nullptr) {
127 LOG(ERROR) << __func__ << "mCallback null";
128 return ndk::ScopedAStatus::fromServiceSpecificError(
129 static_cast<int32_t>(NfcStatus::FAILED));
130 }
131 return Vendor_hal_pre_discover() ? ndk::ScopedAStatus::fromServiceSpecificError(
132 static_cast<int32_t>(NfcStatus::FAILED))
133 : ndk::ScopedAStatus::ok();
134}
135
136::ndk::ScopedAStatus Nfc::write(const std::vector<uint8_t>& data, int32_t* _aidl_return) {
137 LOG(INFO) << "write";
138 if (Nfc::mCallback == nullptr) {
139 LOG(ERROR) << __func__ << "mCallback null";
140 return ndk::ScopedAStatus::fromServiceSpecificError(
141 static_cast<int32_t>(NfcStatus::FAILED));
142 }
143 *_aidl_return = Vendor_hal_write(data.size(), &data[0]);
144 return ndk::ScopedAStatus::ok();
145}
146::ndk::ScopedAStatus Nfc::setEnableVerboseLogging(bool enable) {
147 LOG(INFO) << "setVerboseLogging";
148 Vendor_hal_setVerboseLogging(enable);
149 return ndk::ScopedAStatus::ok();
150}
151
152::ndk::ScopedAStatus Nfc::isVerboseLoggingEnabled(bool* _aidl_return) {
153 *_aidl_return = Vendor_hal_getVerboseLogging();
154 return ndk::ScopedAStatus::ok();
155}
156
157} // namespace nfc
158} // namespace hardware
159} // namespace android
160} // namespace aidl