• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "Nfc.h"
18 
19 #include <android-base/logging.h>
20 
21 #include "Cf_hal_api.h"
22 
23 namespace aidl {
24 namespace android {
25 namespace hardware {
26 namespace nfc {
27 
28 std::shared_ptr<INfcClientCallback> Nfc::mCallback = nullptr;
29 AIBinder_DeathRecipient* clientDeathRecipient = nullptr;
30 
OnDeath(void * cookie)31 void OnDeath(void* cookie) {
32   if (Nfc::mCallback != nullptr &&
33       !AIBinder_isAlive(Nfc::mCallback->asBinder().get())) {
34     LOG(INFO) << __func__ << " Nfc service has died";
35     Nfc* nfc = static_cast<Nfc*>(cookie);
36     nfc->close(NfcCloseType::DISABLE);
37   }
38 }
39 
open(const std::shared_ptr<INfcClientCallback> & clientCallback)40 ::ndk::ScopedAStatus Nfc::open(
41     const std::shared_ptr<INfcClientCallback>& clientCallback) {
42   LOG(INFO) << "open";
43   if (clientCallback == nullptr) {
44     LOG(INFO) << "Nfc::open null callback";
45     return ndk::ScopedAStatus::fromServiceSpecificError(
46         static_cast<int32_t>(NfcStatus::FAILED));
47   } else {
48     Nfc::mCallback = clientCallback;
49 
50     if (clientDeathRecipient != nullptr) {
51       AIBinder_DeathRecipient_delete(clientDeathRecipient);
52       clientDeathRecipient = nullptr;
53     }
54     clientDeathRecipient = AIBinder_DeathRecipient_new(OnDeath);
55     auto linkRet =
56         AIBinder_linkToDeath(clientCallback->asBinder().get(),
57                              clientDeathRecipient, this /* cookie */);
58     if (linkRet != STATUS_OK) {
59       LOG(ERROR) << __func__ << ": linkToDeath failed: " << linkRet;
60       // Just ignore the error.
61     }
62 
63     int ret = Cf_hal_open(eventCallback, dataCallback);
64     return ret == 0 ? ndk::ScopedAStatus::ok()
65                     : ndk::ScopedAStatus::fromServiceSpecificError(
66                           static_cast<int32_t>(NfcStatus::FAILED));
67     return ndk::ScopedAStatus::ok();
68   }
69   return ndk::ScopedAStatus::ok();
70 }
71 
close(NfcCloseType type)72 ::ndk::ScopedAStatus Nfc::close(NfcCloseType type) {
73   LOG(INFO) << "close";
74   if (Nfc::mCallback == nullptr) {
75     LOG(ERROR) << __func__ << " mCallback null";
76     return ndk::ScopedAStatus::fromServiceSpecificError(
77         static_cast<int32_t>(NfcStatus::FAILED));
78   }
79   int ret = 0;
80   if (type == NfcCloseType::HOST_SWITCHED_OFF) {
81     ret = Cf_hal_close_off();
82   } else {
83     ret = Cf_hal_close();
84   }
85   AIBinder_DeathRecipient_delete(clientDeathRecipient);
86   clientDeathRecipient = nullptr;
87   return ret == 0 ? ndk::ScopedAStatus::ok()
88                   : ndk::ScopedAStatus::fromServiceSpecificError(
89                         static_cast<int32_t>(NfcStatus::FAILED));
90 }
91 
coreInitialized()92 ::ndk::ScopedAStatus Nfc::coreInitialized() {
93   LOG(INFO) << "coreInitialized";
94   if (Nfc::mCallback == nullptr) {
95     LOG(ERROR) << __func__ << "mCallback null";
96     return ndk::ScopedAStatus::fromServiceSpecificError(
97         static_cast<int32_t>(NfcStatus::FAILED));
98   }
99   int ret = Cf_hal_core_initialized();
100 
101   return ret == 0 ? ndk::ScopedAStatus::ok()
102                   : ndk::ScopedAStatus::fromServiceSpecificError(
103                         static_cast<int32_t>(NfcStatus::FAILED));
104 }
105 
factoryReset()106 ::ndk::ScopedAStatus Nfc::factoryReset() {
107   LOG(INFO) << "factoryReset";
108   Cf_hal_factoryReset();
109   return ndk::ScopedAStatus::ok();
110 }
111 
getConfig(NfcConfig * _aidl_return)112 ::ndk::ScopedAStatus Nfc::getConfig(NfcConfig* _aidl_return) {
113   LOG(INFO) << "getConfig";
114   NfcConfig nfcVendorConfig;
115   Cf_hal_getConfig(nfcVendorConfig);
116 
117   *_aidl_return = nfcVendorConfig;
118   return ndk::ScopedAStatus::ok();
119 }
120 
powerCycle()121 ::ndk::ScopedAStatus Nfc::powerCycle() {
122   LOG(INFO) << "powerCycle";
123   if (Nfc::mCallback == nullptr) {
124     LOG(ERROR) << __func__ << "mCallback null";
125     return ndk::ScopedAStatus::fromServiceSpecificError(
126         static_cast<int32_t>(NfcStatus::FAILED));
127   }
128   return Cf_hal_power_cycle() ? ndk::ScopedAStatus::fromServiceSpecificError(
129                                     static_cast<int32_t>(NfcStatus::FAILED))
130                               : ndk::ScopedAStatus::ok();
131 }
132 
preDiscover()133 ::ndk::ScopedAStatus Nfc::preDiscover() {
134   LOG(INFO) << "preDiscover";
135   if (Nfc::mCallback == nullptr) {
136     LOG(ERROR) << __func__ << "mCallback null";
137     return ndk::ScopedAStatus::fromServiceSpecificError(
138         static_cast<int32_t>(NfcStatus::FAILED));
139   }
140   return Cf_hal_pre_discover() ? ndk::ScopedAStatus::fromServiceSpecificError(
141                                      static_cast<int32_t>(NfcStatus::FAILED))
142                                : ndk::ScopedAStatus::ok();
143 }
144 
write(const std::vector<uint8_t> & data,int32_t * _aidl_return)145 ::ndk::ScopedAStatus Nfc::write(const std::vector<uint8_t>& data,
146                                 int32_t* _aidl_return) {
147   LOG(INFO) << "write";
148   if (Nfc::mCallback == nullptr) {
149     LOG(ERROR) << __func__ << "mCallback null";
150     return ndk::ScopedAStatus::fromServiceSpecificError(
151         static_cast<int32_t>(NfcStatus::FAILED));
152   }
153   *_aidl_return = Cf_hal_write(data.size(), &data[0]);
154   return ndk::ScopedAStatus::ok();
155 }
156 
setEnableVerboseLogging(bool enable)157 ::ndk::ScopedAStatus Nfc::setEnableVerboseLogging(bool enable) {
158   LOG(INFO) << "setVerboseLogging";
159   Cf_hal_setVerboseLogging(enable);
160   return ndk::ScopedAStatus::ok();
161 }
162 
isVerboseLoggingEnabled(bool * _aidl_return)163 ::ndk::ScopedAStatus Nfc::isVerboseLoggingEnabled(bool* _aidl_return) {
164   *_aidl_return = Cf_hal_getVerboseLogging();
165   return ndk::ScopedAStatus::ok();
166 }
167 
168 }  // namespace nfc
169 }  // namespace hardware
170 }  // namespace android
171 }  // namespace aidl
172