blob: a31ac5ec8ea787e8989673fbd25b7ef3be1da252 [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));
45 } else {
46 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));
60 return ndk::ScopedAStatus::ok();
61 }
62 return ndk::ScopedAStatus::ok();
63}
64
65::ndk::ScopedAStatus Nfc::close(NfcCloseType type) {
66 LOG(INFO) << "close";
67 if (Nfc::mCallback == nullptr) {
68 LOG(ERROR) << __func__ << "mCallback null";
69 return ndk::ScopedAStatus::fromServiceSpecificError(
70 static_cast<int32_t>(NfcStatus::FAILED));
71 }
72 int ret = 0;
73 if (type == NfcCloseType::HOST_SWITCHED_OFF) {
74 ret = Vendor_hal_close_off();
75 } else {
76 ret = Vendor_hal_close();
77 }
78 Nfc::mCallback = nullptr;
79 AIBinder_DeathRecipient_delete(clientDeathRecipient);
80 clientDeathRecipient = nullptr;
81 return ret == 0 ? ndk::ScopedAStatus::ok()
82 : ndk::ScopedAStatus::fromServiceSpecificError(
83 static_cast<int32_t>(NfcStatus::FAILED));
84}
85
86::ndk::ScopedAStatus Nfc::coreInitialized() {
87 LOG(INFO) << "coreInitialized";
88 if (Nfc::mCallback == nullptr) {
89 LOG(ERROR) << __func__ << "mCallback null";
90 return ndk::ScopedAStatus::fromServiceSpecificError(
91 static_cast<int32_t>(NfcStatus::FAILED));
92 }
93 int ret = Vendor_hal_core_initialized();
94
95 return ret == 0 ? ndk::ScopedAStatus::ok()
96 : ndk::ScopedAStatus::fromServiceSpecificError(
97 static_cast<int32_t>(NfcStatus::FAILED));
98}
99
100::ndk::ScopedAStatus Nfc::factoryReset() {
101 LOG(INFO) << "factoryReset";
102 Vendor_hal_factoryReset();
103 return ndk::ScopedAStatus::ok();
104}
105
106::ndk::ScopedAStatus Nfc::getConfig(NfcConfig* _aidl_return) {
107 LOG(INFO) << "getConfig";
108 NfcConfig nfcVendorConfig;
109 Vendor_hal_getConfig(nfcVendorConfig);
110
111 *_aidl_return = nfcVendorConfig;
112 return ndk::ScopedAStatus::ok();
113}
114
115::ndk::ScopedAStatus Nfc::powerCycle() {
116 LOG(INFO) << "powerCycle";
117 if (Nfc::mCallback == nullptr) {
118 LOG(ERROR) << __func__ << "mCallback null";
119 return ndk::ScopedAStatus::fromServiceSpecificError(
120 static_cast<int32_t>(NfcStatus::FAILED));
121 }
122 return Vendor_hal_power_cycle() ? ndk::ScopedAStatus::fromServiceSpecificError(
123 static_cast<int32_t>(NfcStatus::FAILED))
124 : ndk::ScopedAStatus::ok();
125}
126
127::ndk::ScopedAStatus Nfc::preDiscover() {
128 LOG(INFO) << "preDiscover";
129 if (Nfc::mCallback == nullptr) {
130 LOG(ERROR) << __func__ << "mCallback null";
131 return ndk::ScopedAStatus::fromServiceSpecificError(
132 static_cast<int32_t>(NfcStatus::FAILED));
133 }
134 return Vendor_hal_pre_discover() ? ndk::ScopedAStatus::fromServiceSpecificError(
135 static_cast<int32_t>(NfcStatus::FAILED))
136 : ndk::ScopedAStatus::ok();
137}
138
139::ndk::ScopedAStatus Nfc::write(const std::vector<uint8_t>& data, int32_t* _aidl_return) {
140 LOG(INFO) << "write";
141 if (Nfc::mCallback == nullptr) {
142 LOG(ERROR) << __func__ << "mCallback null";
143 return ndk::ScopedAStatus::fromServiceSpecificError(
144 static_cast<int32_t>(NfcStatus::FAILED));
145 }
146 *_aidl_return = Vendor_hal_write(data.size(), &data[0]);
147 return ndk::ScopedAStatus::ok();
148}
149::ndk::ScopedAStatus Nfc::setEnableVerboseLogging(bool enable) {
150 LOG(INFO) << "setVerboseLogging";
151 Vendor_hal_setVerboseLogging(enable);
152 return ndk::ScopedAStatus::ok();
153}
154
155::ndk::ScopedAStatus Nfc::isVerboseLoggingEnabled(bool* _aidl_return) {
156 *_aidl_return = Vendor_hal_getVerboseLogging();
157 return ndk::ScopedAStatus::ok();
158}
159
160} // namespace nfc
161} // namespace hardware
162} // namespace android
163} // namespace aidl