xref: /aosp_15_r20/system/nfc/src/adaptation/NfcAdaptation.cc (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #include "NfcAdaptation.h"
19 
20 #include <aidl/android/hardware/nfc/BnNfc.h>
21 #include <aidl/android/hardware/nfc/BnNfcClientCallback.h>
22 #include <aidl/android/hardware/nfc/INfc.h>
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android-base/stringprintf.h>
26 #include <android/binder_ibinder.h>
27 #include <android/binder_manager.h>
28 #include <android/binder_process.h>
29 #include <android/hardware/nfc/1.1/INfc.h>
30 #include <android/hardware/nfc/1.2/INfc.h>
31 #include <cutils/properties.h>
32 #include <hwbinder/ProcessState.h>
33 
34 #include "debug_nfcsnoop.h"
35 #include "nfa_api.h"
36 #include "nfa_rw_api.h"
37 #include "nfc_config.h"
38 #include "nfc_int.h"
39 
40 using ::android::wp;
41 using ::android::hardware::hidl_death_recipient;
42 using ::android::hidl::base::V1_0::IBase;
43 
44 using android::OK;
45 using android::sp;
46 using android::status_t;
47 
48 using android::base::StringPrintf;
49 using android::hardware::ProcessState;
50 using android::hardware::Return;
51 using android::hardware::Void;
52 using android::hardware::nfc::V1_0::INfc;
53 using android::hardware::nfc::V1_1::PresenceCheckAlgorithm;
54 using INfcV1_1 = android::hardware::nfc::V1_1::INfc;
55 using INfcV1_2 = android::hardware::nfc::V1_2::INfc;
56 using NfcVendorConfigV1_1 = android::hardware::nfc::V1_1::NfcConfig;
57 using NfcVendorConfigV1_2 = android::hardware::nfc::V1_2::NfcConfig;
58 using android::hardware::nfc::V1_1::INfcClientCallback;
59 using android::hardware::hidl_vec;
60 using INfcAidl = ::aidl::android::hardware::nfc::INfc;
61 using NfcAidlConfig = ::aidl::android::hardware::nfc::NfcConfig;
62 using AidlPresenceCheckAlgorithm =
63     ::aidl::android::hardware::nfc::PresenceCheckAlgorithm;
64 using INfcAidlClientCallback =
65     ::aidl::android::hardware::nfc::INfcClientCallback;
66 using NfcAidlEvent = ::aidl::android::hardware::nfc::NfcEvent;
67 using NfcAidlStatus = ::aidl::android::hardware::nfc::NfcStatus;
68 using ::aidl::android::hardware::nfc::NfcCloseType;
69 using Status = ::ndk::ScopedAStatus;
70 
71 #define VERBOSE_VENDOR_LOG_PROPERTY "persist.nfc.vendor_debug_enabled"
72 
73 std::string NFC_AIDL_HAL_SERVICE_NAME = "android.hardware.nfc.INfc/default";
74 
75 extern void GKI_shutdown();
76 extern void verify_stack_non_volatile_store();
77 extern void delete_stack_non_volatile_store(bool forceDelete);
78 
79 NfcAdaptation* NfcAdaptation::mpInstance = nullptr;
80 ThreadMutex NfcAdaptation::sLock;
81 ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent;
82 ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent;
83 sp<INfc> NfcAdaptation::mHal;
84 sp<INfcV1_1> NfcAdaptation::mHal_1_1;
85 sp<INfcV1_2> NfcAdaptation::mHal_1_2;
86 INfcClientCallback* NfcAdaptation::mCallback;
87 std::shared_ptr<INfcAidlClientCallback> mAidlCallback;
88 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
89 std::shared_ptr<INfcAidl> mAidlHal;
90 int32_t mAidlHalVer;
91 
92 bool nfc_nci_reset_keep_cfg_enabled = false;
93 uint8_t nfc_nci_reset_type = 0x00;
94 std::string nfc_storage_path;
95 uint8_t appl_dta_mode_flag = 0x00;
96 bool isDownloadFirmwareCompleted = false;
97 bool use_aidl = false;
98 uint8_t mute_tech_route_option = 0x00;
99 unsigned int t5t_mute_legacy = 0;
100 
101 extern tNFA_DM_CFG nfa_dm_cfg;
102 extern tNFA_PROPRIETARY_CFG nfa_proprietary_cfg;
103 extern tNFA_HCI_CFG nfa_hci_cfg;
104 extern uint8_t nfa_ee_max_ee_cfg;
105 extern bool nfa_poll_bail_out_mode;
106 
107 // Whitelist for hosts allowed to create a pipe
108 // See ADM_CREATE_PIPE command in the ETSI test specification
109 // ETSI TS 102 622, section 6.1.3.1
110 static std::vector<uint8_t> host_allowlist;
111 
112 namespace {
initializeGlobalDebugEnabledFlag()113 void initializeGlobalDebugEnabledFlag() {
114   bool nfc_debug_enabled =
115       (NfcConfig::getUnsigned(NAME_NFC_DEBUG_ENABLED, 0) != 0) ||
116       property_get_bool("persist.nfc.debug_enabled", true);
117 
118   android::base::SetMinimumLogSeverity(nfc_debug_enabled ? android::base::DEBUG
119                                                          : android::base::INFO);
120 
121   LOG(VERBOSE) << StringPrintf("%s: level=%u", __func__, nfc_debug_enabled);
122 }
123 
124 // initialize NciResetType Flag
125 // NCI_RESET_TYPE
126 // 0x00 default, reset configurations every time.
127 // 0x01, reset configurations only once every boot.
128 // 0x02, keep configurations.
initializeNciResetTypeFlag()129 void initializeNciResetTypeFlag() {
130   nfc_nci_reset_type = NfcConfig::getUnsigned(NAME_NCI_RESET_TYPE, 0);
131   LOG(VERBOSE) << StringPrintf("%s: nfc_nci_reset_type=%u", __func__,
132                              nfc_nci_reset_type);
133 }
134 
135 // initialize MuteTechRouteOption Flag
136 // MUTE_TECH_ROUTE_OPTION
137 // 0x00: Default. Route mute techs to DH, enable block bit and set power state
138 // to 0x00 0x01: Remove mute techs from rf discover cmd
initializeNfcMuteTechRouteOptionFlag()139 void initializeNfcMuteTechRouteOptionFlag() {
140   mute_tech_route_option =
141       NfcConfig::getUnsigned(NAME_MUTE_TECH_ROUTE_OPTION, 0);
142   LOG(VERBOSE) << StringPrintf("%s: mute_tech_route_option=%u", __func__,
143                                mute_tech_route_option);
144 }
145 
146 // Abort nfc service when AIDL process died.
HalAidlBinderDied(void *)147 void HalAidlBinderDied(void* /* cookie */) {
148   LOG(ERROR) << __func__ << "INfc aidl hal died, exiting procces to restart";
149   exit(0);
150 }
151 
152 }  // namespace
153 
154 class NfcClientCallback : public INfcClientCallback {
155  public:
NfcClientCallback(tHAL_NFC_CBACK * eventCallback,tHAL_NFC_DATA_CBACK dataCallback)156   NfcClientCallback(tHAL_NFC_CBACK* eventCallback,
157                     tHAL_NFC_DATA_CBACK dataCallback) {
158     mEventCallback = eventCallback;
159     mDataCallback = dataCallback;
160   };
161   virtual ~NfcClientCallback() = default;
sendEvent_1_1(::android::hardware::nfc::V1_1::NfcEvent event,::android::hardware::nfc::V1_0::NfcStatus event_status)162   Return<void> sendEvent_1_1(
163       ::android::hardware::nfc::V1_1::NfcEvent event,
164       ::android::hardware::nfc::V1_0::NfcStatus event_status) override {
165     mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)event_status);
166     return Void();
167   };
sendEvent(::android::hardware::nfc::V1_0::NfcEvent event,::android::hardware::nfc::V1_0::NfcStatus event_status)168   Return<void> sendEvent(
169       ::android::hardware::nfc::V1_0::NfcEvent event,
170       ::android::hardware::nfc::V1_0::NfcStatus event_status) override {
171     mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)event_status);
172     return Void();
173   };
sendData(const::android::hardware::nfc::V1_0::NfcData & data)174   Return<void> sendData(
175       const ::android::hardware::nfc::V1_0::NfcData& data) override {
176     ::android::hardware::nfc::V1_0::NfcData copy = data;
177     mDataCallback(copy.size(), &copy[0]);
178     return Void();
179   };
180 
181  private:
182   tHAL_NFC_CBACK* mEventCallback;
183   tHAL_NFC_DATA_CBACK* mDataCallback;
184 };
185 
186 class NfcHalDeathRecipient : public hidl_death_recipient {
187  public:
188   android::sp<android::hardware::nfc::V1_0::INfc> mNfcDeathHal;
NfcHalDeathRecipient(android::sp<android::hardware::nfc::V1_0::INfc> & mHal)189   NfcHalDeathRecipient(android::sp<android::hardware::nfc::V1_0::INfc>& mHal) {
190     mNfcDeathHal = mHal;
191   }
192 
serviceDied(uint64_t,const wp<::android::hidl::base::V1_0::IBase> &)193   virtual void serviceDied(
194       uint64_t /* cookie */,
195       const wp<::android::hidl::base::V1_0::IBase>& /* who */) {
196     ALOGE(
197         "NfcHalDeathRecipient::serviceDied - Nfc-Hal service died. Killing "
198         "NfcService");
199     if (mNfcDeathHal) {
200       mNfcDeathHal->unlinkToDeath(this);
201     }
202     mNfcDeathHal = NULL;
203     exit(0);
204   }
finalize()205   void finalize() {
206     if (mNfcDeathHal) {
207       mNfcDeathHal->unlinkToDeath(this);
208     } else {
209       LOG(VERBOSE) << StringPrintf("%s: mNfcDeathHal is not set", __func__);
210     }
211 
212     ALOGI("NfcHalDeathRecipient::destructor - NfcService");
213     mNfcDeathHal = NULL;
214   }
215 };
216 
217 class NfcAidlClientCallback
218     : public ::aidl::android::hardware::nfc::BnNfcClientCallback {
219  public:
NfcAidlClientCallback(tHAL_NFC_CBACK * eventCallback,tHAL_NFC_DATA_CBACK dataCallback)220   NfcAidlClientCallback(tHAL_NFC_CBACK* eventCallback,
221                         tHAL_NFC_DATA_CBACK dataCallback) {
222     mEventCallback = eventCallback;
223     mDataCallback = dataCallback;
224   };
225   virtual ~NfcAidlClientCallback() = default;
226 
sendEvent(NfcAidlEvent event,NfcAidlStatus event_status)227   ::ndk::ScopedAStatus sendEvent(NfcAidlEvent event,
228                                  NfcAidlStatus event_status) override {
229     uint8_t e_num;
230     uint8_t s_num;
231     switch (event) {
232       case NfcAidlEvent::OPEN_CPLT:
233         e_num = HAL_NFC_OPEN_CPLT_EVT;
234         break;
235       case NfcAidlEvent::CLOSE_CPLT:
236         e_num = HAL_NFC_CLOSE_CPLT_EVT;
237         break;
238       case NfcAidlEvent::POST_INIT_CPLT:
239         e_num = HAL_NFC_POST_INIT_CPLT_EVT;
240         break;
241       case NfcAidlEvent::PRE_DISCOVER_CPLT:
242         e_num = HAL_NFC_PRE_DISCOVER_CPLT_EVT;
243         break;
244       case NfcAidlEvent::HCI_NETWORK_RESET:
245         e_num = HAL_HCI_NETWORK_RESET;
246         break;
247       case NfcAidlEvent::REQUEST_CONTROL:
248         e_num = HAL_NFC_REQUEST_CONTROL_EVT;
249         break;
250       case NfcAidlEvent::RELEASE_CONTROL:
251         e_num = HAL_NFC_RELEASE_CONTROL_EVT;
252         break;
253       case NfcAidlEvent::ERROR:
254       default:
255         e_num = HAL_NFC_ERROR_EVT;
256     }
257     switch (event_status) {
258       case NfcAidlStatus::OK:
259         s_num = HAL_NFC_STATUS_OK;
260         break;
261       case NfcAidlStatus::FAILED:
262         s_num = HAL_NFC_STATUS_FAILED;
263         break;
264       case NfcAidlStatus::ERR_TRANSPORT:
265         s_num = HAL_NFC_STATUS_ERR_TRANSPORT;
266         break;
267       case NfcAidlStatus::ERR_CMD_TIMEOUT:
268         s_num = HAL_NFC_STATUS_ERR_CMD_TIMEOUT;
269         break;
270       case NfcAidlStatus::REFUSED:
271         s_num = HAL_NFC_STATUS_REFUSED;
272         break;
273       default:
274         s_num = HAL_NFC_STATUS_FAILED;
275     }
276     mEventCallback(e_num, (tHAL_NFC_STATUS)s_num);
277     return ::ndk::ScopedAStatus::ok();
278   };
sendData(const std::vector<uint8_t> & data)279   ::ndk::ScopedAStatus sendData(const std::vector<uint8_t>& data) override {
280     std::vector<uint8_t> copy = data;
281     mDataCallback(copy.size(), &copy[0]);
282     return ::ndk::ScopedAStatus::ok();
283   };
284 
285  private:
286   tHAL_NFC_CBACK* mEventCallback;
287   tHAL_NFC_DATA_CBACK* mDataCallback;
288 };
289 
290 /*******************************************************************************
291 **
292 ** Function:    NfcAdaptation::NfcAdaptation()
293 **
294 ** Description: class constructor
295 **
296 ** Returns:     none
297 **
298 *******************************************************************************/
NfcAdaptation()299 NfcAdaptation::NfcAdaptation() {
300   memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
301   mDeathRecipient = ::ndk::ScopedAIBinder_DeathRecipient(
302       AIBinder_DeathRecipient_new(HalAidlBinderDied));
303 }
304 
305 /*******************************************************************************
306 **
307 ** Function:    NfcAdaptation::~NfcAdaptation()
308 **
309 ** Description: class destructor
310 **
311 ** Returns:     none
312 **
313 *******************************************************************************/
~NfcAdaptation()314 NfcAdaptation::~NfcAdaptation() { mpInstance = nullptr; }
315 
316 /*******************************************************************************
317 **
318 ** Function:    NfcAdaptation::GetInstance()
319 **
320 ** Description: access class singleton
321 **
322 ** Returns:     pointer to the singleton object
323 **
324 *******************************************************************************/
GetInstance()325 NfcAdaptation& NfcAdaptation::GetInstance() {
326   AutoThreadMutex a(sLock);
327 
328   if (!mpInstance) {
329     mpInstance = new NfcAdaptation;
330     mpInstance->InitializeHalDeviceContext();
331   }
332   return *mpInstance;
333 }
334 
GetVendorConfigs(std::map<std::string,ConfigValue> & configMap)335 void NfcAdaptation::GetVendorConfigs(
336     std::map<std::string, ConfigValue>& configMap) {
337   NfcVendorConfigV1_2 configValue;
338   NfcAidlConfig aidlConfigValue;
339   if (mAidlHal) {
340     mAidlHal->getConfig(&aidlConfigValue);
341   } else if (mHal_1_2) {
342     mHal_1_2->getConfig_1_2(
343         [&configValue](NfcVendorConfigV1_2 config) { configValue = config; });
344   } else if (mHal_1_1) {
345     mHal_1_1->getConfig([&configValue](NfcVendorConfigV1_1 config) {
346       configValue.v1_1 = config;
347       configValue.defaultIsoDepRoute = 0x00;
348     });
349   }
350 
351   if (mAidlHal) {
352     std::vector<int8_t> nfaPropCfg = {
353         aidlConfigValue.nfaProprietaryCfg.protocol18092Active,
354         aidlConfigValue.nfaProprietaryCfg.protocolBPrime,
355         aidlConfigValue.nfaProprietaryCfg.protocolDual,
356         aidlConfigValue.nfaProprietaryCfg.protocol15693,
357         aidlConfigValue.nfaProprietaryCfg.protocolKovio,
358         aidlConfigValue.nfaProprietaryCfg.protocolMifare,
359         aidlConfigValue.nfaProprietaryCfg.discoveryPollKovio,
360         aidlConfigValue.nfaProprietaryCfg.discoveryPollBPrime,
361         aidlConfigValue.nfaProprietaryCfg.discoveryListenBPrime,
362         aidlConfigValue.nfaProprietaryCfg.protocolChineseId};
363     configMap.emplace(NAME_NFA_PROPRIETARY_CFG, ConfigValue(nfaPropCfg));
364     configMap.emplace(NAME_NFA_POLL_BAIL_OUT_MODE,
365                       ConfigValue(aidlConfigValue.nfaPollBailOutMode ? 1 : 0));
366     if (aidlConfigValue.offHostRouteUicc.size() != 0) {
367       configMap.emplace(NAME_OFFHOST_ROUTE_UICC,
368                         ConfigValue(aidlConfigValue.offHostRouteUicc));
369     }
370     if (aidlConfigValue.offHostRouteEse.size() != 0) {
371       configMap.emplace(NAME_OFFHOST_ROUTE_ESE,
372                         ConfigValue(aidlConfigValue.offHostRouteEse));
373     }
374     // AIDL byte would be int8_t in C++.
375     // Here we force cast int8_t to uint8_t for ConfigValue
376     configMap.emplace(
377         NAME_DEFAULT_OFFHOST_ROUTE,
378         ConfigValue((uint8_t)aidlConfigValue.defaultOffHostRoute));
379     configMap.emplace(NAME_DEFAULT_ROUTE,
380                       ConfigValue((uint8_t)aidlConfigValue.defaultRoute));
381     configMap.emplace(
382         NAME_DEFAULT_NFCF_ROUTE,
383         ConfigValue((uint8_t)aidlConfigValue.defaultOffHostRouteFelica));
384     configMap.emplace(NAME_DEFAULT_ISODEP_ROUTE,
385                       ConfigValue((uint8_t)aidlConfigValue.defaultIsoDepRoute));
386     configMap.emplace(
387         NAME_DEFAULT_SYS_CODE_ROUTE,
388         ConfigValue((uint8_t)aidlConfigValue.defaultSystemCodeRoute));
389     configMap.emplace(
390         NAME_DEFAULT_SYS_CODE_PWR_STATE,
391         ConfigValue((uint8_t)aidlConfigValue.defaultSystemCodePowerState));
392     configMap.emplace(NAME_OFF_HOST_SIM_PIPE_ID,
393                       ConfigValue((uint8_t)aidlConfigValue.offHostSIMPipeId));
394     configMap.emplace(NAME_OFF_HOST_ESE_PIPE_ID,
395                       ConfigValue((uint8_t)aidlConfigValue.offHostESEPipeId));
396     configMap.emplace(NAME_T4T_NFCEE_ENABLE,
397                       ConfigValue(aidlConfigValue.t4tNfceeEnable ? 1 : 0));
398 
399     if (aidlConfigValue.offHostSimPipeIds.size() != 0) {
400       configMap.emplace(NAME_OFF_HOST_SIM_PIPE_IDS,
401                         ConfigValue(aidlConfigValue.offHostSimPipeIds));
402     }
403     configMap.emplace(NAME_ISO_DEP_MAX_TRANSCEIVE,
404                       ConfigValue(aidlConfigValue.maxIsoDepTransceiveLength));
405     if (aidlConfigValue.hostAllowlist.size() != 0) {
406       configMap.emplace(NAME_DEVICE_HOST_ALLOW_LIST,
407                         ConfigValue(aidlConfigValue.hostAllowlist));
408     }
409     /* For Backwards compatibility */
410     if (aidlConfigValue.presenceCheckAlgorithm ==
411         AidlPresenceCheckAlgorithm::ISO_DEP_NAK) {
412       configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
413                         ConfigValue((uint32_t)NFA_RW_PRES_CHK_ISO_DEP_NAK));
414     } else {
415       configMap.emplace(
416           NAME_PRESENCE_CHECK_ALGORITHM,
417           ConfigValue((uint32_t)aidlConfigValue.presenceCheckAlgorithm));
418     }
419   } else if (mHal_1_1 || mHal_1_2) {
420     std::vector<uint8_t> nfaPropCfg = {
421         configValue.v1_1.nfaProprietaryCfg.protocol18092Active,
422         configValue.v1_1.nfaProprietaryCfg.protocolBPrime,
423         configValue.v1_1.nfaProprietaryCfg.protocolDual,
424         configValue.v1_1.nfaProprietaryCfg.protocol15693,
425         configValue.v1_1.nfaProprietaryCfg.protocolKovio,
426         configValue.v1_1.nfaProprietaryCfg.protocolMifare,
427         configValue.v1_1.nfaProprietaryCfg.discoveryPollKovio,
428         configValue.v1_1.nfaProprietaryCfg.discoveryPollBPrime,
429         configValue.v1_1.nfaProprietaryCfg.discoveryListenBPrime};
430     configMap.emplace(NAME_NFA_PROPRIETARY_CFG, ConfigValue(nfaPropCfg));
431     configMap.emplace(NAME_NFA_POLL_BAIL_OUT_MODE,
432                       ConfigValue(configValue.v1_1.nfaPollBailOutMode ? 1 : 0));
433     configMap.emplace(NAME_DEFAULT_OFFHOST_ROUTE,
434                       ConfigValue(configValue.v1_1.defaultOffHostRoute));
435     if (configValue.offHostRouteUicc.size() != 0) {
436       configMap.emplace(NAME_OFFHOST_ROUTE_UICC,
437                         ConfigValue(configValue.offHostRouteUicc));
438     }
439     if (configValue.offHostRouteEse.size() != 0) {
440       configMap.emplace(NAME_OFFHOST_ROUTE_ESE,
441                         ConfigValue(configValue.offHostRouteEse));
442     }
443     configMap.emplace(NAME_DEFAULT_ROUTE,
444                       ConfigValue(configValue.v1_1.defaultRoute));
445     configMap.emplace(NAME_DEFAULT_NFCF_ROUTE,
446                       ConfigValue(configValue.v1_1.defaultOffHostRouteFelica));
447     configMap.emplace(NAME_DEFAULT_ISODEP_ROUTE,
448                       ConfigValue(configValue.defaultIsoDepRoute));
449     configMap.emplace(NAME_DEFAULT_SYS_CODE_ROUTE,
450                       ConfigValue(configValue.v1_1.defaultSystemCodeRoute));
451     configMap.emplace(
452         NAME_DEFAULT_SYS_CODE_PWR_STATE,
453         ConfigValue(configValue.v1_1.defaultSystemCodePowerState));
454     configMap.emplace(NAME_OFF_HOST_SIM_PIPE_ID,
455                       ConfigValue(configValue.v1_1.offHostSIMPipeId));
456     configMap.emplace(NAME_OFF_HOST_ESE_PIPE_ID,
457                       ConfigValue(configValue.v1_1.offHostESEPipeId));
458     configMap.emplace(NAME_ISO_DEP_MAX_TRANSCEIVE,
459                       ConfigValue(configValue.v1_1.maxIsoDepTransceiveLength));
460     if (configValue.v1_1.hostWhitelist.size() != 0) {
461       configMap.emplace(NAME_DEVICE_HOST_ALLOW_LIST,
462                         ConfigValue(configValue.v1_1.hostWhitelist));
463     }
464     /* For Backwards compatibility */
465     if (configValue.v1_1.presenceCheckAlgorithm ==
466         PresenceCheckAlgorithm::ISO_DEP_NAK) {
467       configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
468                         ConfigValue((uint32_t)NFA_RW_PRES_CHK_ISO_DEP_NAK));
469     } else {
470       configMap.emplace(
471           NAME_PRESENCE_CHECK_ALGORITHM,
472           ConfigValue((uint32_t)configValue.v1_1.presenceCheckAlgorithm));
473     }
474   }
475 }
476 /*******************************************************************************
477 **
478 ** Function:    NfcAdaptation::Initialize()
479 **
480 ** Description: class initializer
481 **
482 ** Returns:     none
483 **
484 *******************************************************************************/
Initialize()485 void NfcAdaptation::Initialize() {
486   const char* func = "NfcAdaptation::Initialize";
487   // Init log tag
488   android::base::InitLogging(nullptr);
489   android::base::SetDefaultTag("libnfc_nci");
490 
491   initializeGlobalDebugEnabledFlag();
492   initializeNciResetTypeFlag();
493   initializeNfcMuteTechRouteOptionFlag();
494 
495   LOG(VERBOSE) << StringPrintf("%s: enter", func);
496 
497   nfc_storage_path = NfcConfig::getString(NAME_NFA_STORAGE, "/data/nfc");
498 
499   if (NfcConfig::hasKey(NAME_NFA_DM_CFG)) {
500     std::vector<uint8_t> dm_config = NfcConfig::getBytes(NAME_NFA_DM_CFG);
501     if (dm_config.size() > 0) nfa_dm_cfg.auto_detect_ndef = dm_config[0];
502     if (dm_config.size() > 1) nfa_dm_cfg.auto_read_ndef = dm_config[1];
503     if (dm_config.size() > 2) nfa_dm_cfg.auto_presence_check = dm_config[2];
504     if (dm_config.size() > 3) nfa_dm_cfg.presence_check_option = dm_config[3];
505     // NOTE: The timeout value is not configurable here because the endianness
506     // of a byte array is ambiguous and needlessly difficult to configure.
507     // If this value needs to be configurable, a numeric config option should
508     // be used.
509   }
510 
511   if (NfcConfig::hasKey(NAME_NFA_MAX_EE_SUPPORTED)) {
512     nfa_ee_max_ee_cfg = NfcConfig::getUnsigned(NAME_NFA_MAX_EE_SUPPORTED);
513     LOG(VERBOSE) << StringPrintf(
514         "%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d", func,
515         nfa_ee_max_ee_cfg);
516   }
517 
518   if (NfcConfig::hasKey(NAME_NFA_POLL_BAIL_OUT_MODE)) {
519     nfa_poll_bail_out_mode =
520         NfcConfig::getUnsigned(NAME_NFA_POLL_BAIL_OUT_MODE);
521     LOG(VERBOSE) << StringPrintf(
522         "%s: Overriding NFA_POLL_BAIL_OUT_MODE to use %d", func,
523         nfa_poll_bail_out_mode);
524   }
525 
526   if (NfcConfig::hasKey(NAME_NFA_PROPRIETARY_CFG)) {
527     std::vector<uint8_t> p_config =
528         NfcConfig::getBytes(NAME_NFA_PROPRIETARY_CFG);
529     if (p_config.size() > 0)
530       nfa_proprietary_cfg.pro_protocol_18092_active = p_config[0];
531     if (p_config.size() > 1)
532       nfa_proprietary_cfg.pro_protocol_b_prime = p_config[1];
533     if (p_config.size() > 2)
534       nfa_proprietary_cfg.pro_protocol_dual = p_config[2];
535     if (p_config.size() > 3)
536       nfa_proprietary_cfg.pro_protocol_15693 = p_config[3];
537     if (p_config.size() > 4)
538       nfa_proprietary_cfg.pro_protocol_kovio = p_config[4];
539     if (p_config.size() > 5) nfa_proprietary_cfg.pro_protocol_mfc = p_config[5];
540     if (p_config.size() > 6)
541       nfa_proprietary_cfg.pro_discovery_kovio_poll = p_config[6];
542     if (p_config.size() > 7)
543       nfa_proprietary_cfg.pro_discovery_b_prime_poll = p_config[7];
544     if (p_config.size() > 8)
545       nfa_proprietary_cfg.pro_discovery_b_prime_listen = p_config[8];
546     if (p_config.size() > 9)
547       nfa_proprietary_cfg.pro_protocol_chinese_id = p_config[9];
548   }
549 
550   // Configure allowlist of HCI host ID's
551   // See specification: ETSI TS 102 622, section 6.1.3.1
552   if (NfcConfig::hasKey(NAME_DEVICE_HOST_ALLOW_LIST)) {
553     host_allowlist = NfcConfig::getBytes(NAME_DEVICE_HOST_ALLOW_LIST);
554     nfa_hci_cfg.num_allowlist_host = host_allowlist.size();
555     nfa_hci_cfg.p_allowlist = &host_allowlist[0];
556   }
557 
558   if (NfcConfig::hasKey(NAME_ISO15693_SKIP_GET_SYS_INFO_CMD)) {
559     t5t_mute_legacy =
560         NfcConfig::getUnsigned(NAME_ISO15693_SKIP_GET_SYS_INFO_CMD);
561   }
562 
563   if (NfcConfig::hasKey(NAME_NFA_DM_LISTEN_ACTIVE_DEACT_NTF_TIMEOUT)) {
564     unsigned int value =
565         NfcConfig::getUnsigned(NAME_NFA_DM_LISTEN_ACTIVE_DEACT_NTF_TIMEOUT);
566     if (value > 0) {
567       nfa_dm_cfg.deact_ntf_listen_active_timeout = value * 1000;
568     }
569   }
570 
571   verify_stack_non_volatile_store();
572   if (NfcConfig::hasKey(NAME_PRESERVE_STORAGE) &&
573       NfcConfig::getUnsigned(NAME_PRESERVE_STORAGE) == 1) {
574     LOG(VERBOSE) << StringPrintf("%s: preserve stack NV store", __func__);
575   } else {
576     delete_stack_non_volatile_store(FALSE);
577   }
578 
579   GKI_init();
580   GKI_enable();
581   GKI_create_task((TASKPTR)NFCA_TASK, BTU_TASK, (int8_t*)"NFCA_TASK", nullptr, 0,
582                   (pthread_cond_t*)nullptr, nullptr);
583   {
584     AutoThreadMutex guard(mCondVar);
585     GKI_create_task((TASKPTR)Thread, MMI_TASK, (int8_t*)"NFCA_THREAD", nullptr, 0,
586                     (pthread_cond_t*)nullptr, nullptr);
587     mCondVar.wait();
588   }
589 
590   debug_nfcsnoop_init();
591   LOG(VERBOSE) << StringPrintf("%s: exit", func);
592 }
593 
594 /*******************************************************************************
595 **
596 ** Function:    NfcAdaptation::Finalize()
597 **
598 ** Description: class finalizer
599 **
600 ** Returns:     none
601 **
602 *******************************************************************************/
Finalize()603 void NfcAdaptation::Finalize() {
604   const char* func = "NfcAdaptation::Finalize";
605   AutoThreadMutex a(sLock);
606 
607   LOG(VERBOSE) << StringPrintf("%s: enter", func);
608   GKI_shutdown();
609 
610   NfcConfig::clear();
611 
612   if (mAidlHal != nullptr) {
613     AIBinder_unlinkToDeath(mAidlHal->asBinder().get(), mDeathRecipient.get(),
614                            nullptr);
615   } else if (mHal != nullptr) {
616     mNfcHalDeathRecipient->finalize();
617   }
618   LOG(VERBOSE) << StringPrintf("%s: exit", func);
619   delete this;
620 }
621 
FactoryReset()622 void NfcAdaptation::FactoryReset() {
623   if (mAidlHal != nullptr) {
624     mAidlHal->factoryReset();
625   } else if (mHal_1_2 != nullptr) {
626     mHal_1_2->factoryReset();
627   } else if (mHal_1_1 != nullptr) {
628     mHal_1_1->factoryReset();
629   }
630 }
631 
DeviceShutdown()632 void NfcAdaptation::DeviceShutdown() {
633   if (mAidlHal != nullptr) {
634     mAidlHal->close(NfcCloseType::HOST_SWITCHED_OFF);
635     AIBinder_unlinkToDeath(mAidlHal->asBinder().get(), mDeathRecipient.get(),
636                            nullptr);
637     mAidlHal = nullptr;
638   } else {
639     if (mHal_1_2 != nullptr) {
640       mHal_1_2->closeForPowerOffCase();
641     } else if (mHal_1_1 != nullptr) {
642       mHal_1_1->closeForPowerOffCase();
643     }
644     if (mHal != nullptr) {
645       mHal->unlinkToDeath(mNfcHalDeathRecipient);
646     }
647   }
648 }
649 
650 /*******************************************************************************
651 **
652 ** Function:    NfcAdaptation::Dump
653 **
654 ** Description: Native support for dumpsys function.
655 **
656 ** Returns:     None.
657 **
658 *******************************************************************************/
Dump(int fd)659 void NfcAdaptation::Dump(int fd) { debug_nfcsnoop_dump(fd); }
660 
661 /*******************************************************************************
662 **
663 ** Function:    NfcAdaptation::signal()
664 **
665 ** Description: signal the CondVar to release the thread that is waiting
666 **
667 ** Returns:     none
668 **
669 *******************************************************************************/
signal()670 void NfcAdaptation::signal() { mCondVar.signal(); }
671 
672 /*******************************************************************************
673 **
674 ** Function:    NfcAdaptation::NFCA_TASK()
675 **
676 ** Description: NFCA_TASK runs the GKI main task
677 **
678 ** Returns:     none
679 **
680 *******************************************************************************/
NFCA_TASK(uint32_t arg)681 uint32_t NfcAdaptation::NFCA_TASK(__attribute__((unused)) uint32_t arg) {
682   const char* func = "NfcAdaptation::NFCA_TASK";
683   LOG(VERBOSE) << StringPrintf("%s: enter", func);
684   GKI_run(nullptr);
685   LOG(VERBOSE) << StringPrintf("%s: exit", func);
686   return 0;
687 }
688 
689 /*******************************************************************************
690 **
691 ** Function:    NfcAdaptation::Thread()
692 **
693 ** Description: Creates work threads
694 **
695 ** Returns:     none
696 **
697 *******************************************************************************/
Thread(uint32_t arg)698 uint32_t NfcAdaptation::Thread(__attribute__((unused)) uint32_t arg) {
699   const char* func = "NfcAdaptation::Thread";
700   LOG(VERBOSE) << StringPrintf("%s: enter", func);
701 
702   {
703     ThreadCondVar CondVar;
704     AutoThreadMutex guard(CondVar);
705     GKI_create_task((TASKPTR)nfc_task, NFC_TASK, (int8_t*)"NFC_TASK", nullptr, 0,
706                     (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
707     CondVar.wait();
708   }
709 
710   NfcAdaptation::GetInstance().signal();
711 
712   GKI_exit_task(GKI_get_taskid());
713   LOG(VERBOSE) << StringPrintf("%s: exit", func);
714   return 0;
715 }
716 
717 /*******************************************************************************
718 **
719 ** Function:    NfcAdaptation::GetHalEntryFuncs()
720 **
721 ** Description: Get the set of HAL entry points.
722 **
723 ** Returns:     Functions pointers for HAL entry points.
724 **
725 *******************************************************************************/
GetHalEntryFuncs()726 tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs() { return &mHalEntryFuncs; }
727 
728 /*******************************************************************************
729 **
730 ** Function:    NfcAdaptation::InitializeHalDeviceContext
731 **
732 ** Description: Check validity of current handle to the nfc HAL service
733 **
734 ** Returns:     None.
735 **
736 *******************************************************************************/
InitializeHalDeviceContext()737 void NfcAdaptation::InitializeHalDeviceContext() {
738   const char* func = "NfcAdaptation::InitializeHalDeviceContext";
739 
740   mHalEntryFuncs.initialize = HalInitialize;
741   mHalEntryFuncs.terminate = HalTerminate;
742   mHalEntryFuncs.open = HalOpen;
743   mHalEntryFuncs.close = HalClose;
744   mHalEntryFuncs.core_initialized = HalCoreInitialized;
745   mHalEntryFuncs.write = HalWrite;
746   mHalEntryFuncs.prediscover = HalPrediscover;
747   mHalEntryFuncs.control_granted = HalControlGranted;
748   mHalEntryFuncs.power_cycle = HalPowerCycle;
749   mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;
750   LOG(INFO) << StringPrintf("%s: INfc::getService()", func);
751   mAidlHal = nullptr;
752   mHal = mHal_1_1 = mHal_1_2 = nullptr;
753   if (!use_aidl) {
754     mHal = mHal_1_1 = mHal_1_2 = INfcV1_2::getService();
755   }
756   if (!use_aidl && mHal_1_2 == nullptr) {
757     mHal = mHal_1_1 = INfcV1_1::getService();
758     if (mHal_1_1 == nullptr) {
759       mHal = INfc::getService();
760     }
761   }
762   if (mHal == nullptr) {
763     // Try get AIDL
764     ::ndk::SpAIBinder binder(
765         AServiceManager_waitForService(NFC_AIDL_HAL_SERVICE_NAME.c_str()));
766     mAidlHal = INfcAidl::fromBinder(binder);
767     if (mAidlHal != nullptr) {
768       use_aidl = true;
769       AIBinder_linkToDeath(mAidlHal->asBinder().get(), mDeathRecipient.get(),
770                            nullptr /* cookie */);
771       mHal = mHal_1_1 = mHal_1_2 = nullptr;
772       mAidlHal->getInterfaceVersion(&mAidlHalVer);
773       LOG(INFO) << StringPrintf("%s: INfcAidl::fromBinder returned ver(%d)",
774                                 func, mAidlHalVer);
775     }
776     LOG_ALWAYS_FATAL_IF(mAidlHal == nullptr,
777                         "Failed to retrieve the NFC AIDL!");
778   } else {
779     LOG(INFO) << StringPrintf("%s: INfc::getService() returned %p (%s)", func,
780                               mHal.get(),
781                               (mHal->isRemote() ? "remote" : "local"));
782     mNfcHalDeathRecipient = new NfcHalDeathRecipient(mHal);
783     mHal->linkToDeath(mNfcHalDeathRecipient, 0);
784   }
785 }
786 
787 /*******************************************************************************
788 **
789 ** Function:    NfcAdaptation::HalInitialize
790 **
791 ** Description: Not implemented because this function is only needed
792 **              within the HAL.
793 **
794 ** Returns:     None.
795 **
796 *******************************************************************************/
HalInitialize()797 void NfcAdaptation::HalInitialize() {
798   const char* func = "NfcAdaptation::HalInitialize";
799   LOG(VERBOSE) << StringPrintf("%s", func);
800 }
801 
802 /*******************************************************************************
803 **
804 ** Function:    NfcAdaptation::HalTerminate
805 **
806 ** Description: Not implemented because this function is only needed
807 **              within the HAL.
808 **
809 ** Returns:     None.
810 **
811 *******************************************************************************/
HalTerminate()812 void NfcAdaptation::HalTerminate() {
813   const char* func = "NfcAdaptation::HalTerminate";
814   LOG(VERBOSE) << StringPrintf("%s", func);
815 }
816 
817 /*******************************************************************************
818 **
819 ** Function:    NfcAdaptation::HalOpen
820 **
821 ** Description: Turn on controller, download firmware.
822 **
823 ** Returns:     None.
824 **
825 *******************************************************************************/
HalOpen(tHAL_NFC_CBACK * p_hal_cback,tHAL_NFC_DATA_CBACK * p_data_cback)826 void NfcAdaptation::HalOpen(tHAL_NFC_CBACK* p_hal_cback,
827                             tHAL_NFC_DATA_CBACK* p_data_cback) {
828   const char* func = "NfcAdaptation::HalOpen";
829   LOG(VERBOSE) << StringPrintf("%s", func);
830 
831   if (mAidlHal != nullptr) {
832     mAidlCallback = ::ndk::SharedRefBase::make<NfcAidlClientCallback>(
833         p_hal_cback, p_data_cback);
834     Status status = mAidlHal->open(mAidlCallback);
835     if (!status.isOk()) {
836       LOG(ERROR) << "Open Error: "
837                  << ::aidl::android::hardware::nfc::toString(
838                         static_cast<NfcAidlStatus>(
839                             status.getServiceSpecificError()));
840     } else {
841       bool verbose_vendor_log =
842           android::base::GetBoolProperty(VERBOSE_VENDOR_LOG_PROPERTY, false);
843       mAidlHal->setEnableVerboseLogging(verbose_vendor_log);
844       LOG(VERBOSE) << StringPrintf("%s: verbose_vendor_log=%u", __func__,
845                                  verbose_vendor_log);
846     }
847   } else if (mHal_1_1 != nullptr) {
848     mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
849     mHal_1_1->open_1_1(mCallback);
850   } else if (mHal != nullptr) {
851     mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
852     mHal->open(mCallback);
853   }
854 }
855 
856 /*******************************************************************************
857 **
858 ** Function:    NfcAdaptation::HalClose
859 **
860 ** Description: Turn off controller.
861 **
862 ** Returns:     None.
863 **
864 *******************************************************************************/
HalClose()865 void NfcAdaptation::HalClose() {
866   const char* func = "NfcAdaptation::HalClose";
867   LOG(VERBOSE) << StringPrintf("%s", func);
868   if (mAidlHal != nullptr) {
869     mAidlHal->close(NfcCloseType::DISABLE);
870   } else if (mHal != nullptr) {
871     mHal->close();
872   }
873 }
874 
875 /*******************************************************************************
876 **
877 ** Function:    NfcAdaptation::HalWrite
878 **
879 ** Description: Write NCI message to the controller.
880 **
881 ** Returns:     None.
882 **
883 *******************************************************************************/
HalWrite(uint16_t data_len,uint8_t * p_data)884 void NfcAdaptation::HalWrite(uint16_t data_len, uint8_t* p_data) {
885   const char* func = "NfcAdaptation::HalWrite";
886   LOG(VERBOSE) << StringPrintf("%s", func);
887 
888   if (mAidlHal != nullptr) {
889     int ret;
890     std::vector<uint8_t> aidl_data(p_data, p_data + data_len);
891     mAidlHal->write(aidl_data, &ret);
892   } else if (mHal != nullptr) {
893     ::android::hardware::nfc::V1_0::NfcData data;
894     data.setToExternal(p_data, data_len);
895     mHal->write(data);
896   }
897 }
898 
899 /*******************************************************************************
900 **
901 ** Function:    NfcAdaptation::HalCoreInitialized
902 **
903 ** Description: Adjust the configurable parameters in the controller.
904 **
905 ** Returns:     None.
906 **
907 *******************************************************************************/
HalCoreInitialized(uint16_t data_len,uint8_t * p_core_init_rsp_params)908 void NfcAdaptation::HalCoreInitialized(uint16_t data_len,
909                                        uint8_t* p_core_init_rsp_params) {
910   const char* func = "NfcAdaptation::HalCoreInitialized";
911   LOG(VERBOSE) << StringPrintf("%s", func);
912   if (mAidlHal != nullptr) {
913     // AIDL coreInitialized doesn't send data to HAL.
914     mAidlHal->coreInitialized();
915   } else if (mHal != nullptr) {
916     hidl_vec<uint8_t> data;
917     data.setToExternal(p_core_init_rsp_params, data_len);
918     mHal->coreInitialized(data);
919   }
920 }
921 
922 /*******************************************************************************
923 **
924 ** Function:    NfcAdaptation::HalPrediscover
925 **
926 ** Description:     Perform any vendor-specific pre-discovery actions (if
927 **                  needed) If any actions were performed TRUE will be returned,
928 **                  and HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are
929 **                  completed.
930 **
931 ** Returns:         TRUE if vendor-specific pre-discovery actions initialized
932 **                  FALSE if no vendor-specific pre-discovery actions are
933 **                  needed.
934 **
935 *******************************************************************************/
HalPrediscover()936 bool NfcAdaptation::HalPrediscover() {
937   const char* func = "NfcAdaptation::HalPrediscover";
938   LOG(VERBOSE) << StringPrintf("%s", func);
939   if (mAidlHal != nullptr) {
940     Status status = mAidlHal->preDiscover();
941     if (status.isOk()) {
942       LOG(VERBOSE) << StringPrintf("%s wait for NFC_PRE_DISCOVER_CPLT_EVT", func);
943       return true;
944     }
945   } else if (mHal != nullptr) {
946     mHal->prediscover();
947   }
948 
949   return false;
950 }
951 
952 /*******************************************************************************
953 **
954 ** Function:        HAL_NfcControlGranted
955 **
956 ** Description:     Grant control to HAL control for sending NCI commands.
957 **                  Call in response to HAL_REQUEST_CONTROL_EVT.
958 **                  Must only be called when there are no NCI commands pending.
959 **                  HAL_RELEASE_CONTROL_EVT will notify when HAL no longer
960 **                  needs control of NCI.
961 **
962 ** Returns:         void
963 **
964 *******************************************************************************/
HalControlGranted()965 void NfcAdaptation::HalControlGranted() {
966   const char* func = "NfcAdaptation::HalControlGranted";
967   LOG(VERBOSE) << StringPrintf("%s", func);
968   if (mAidlHal != nullptr) {
969     if (mAidlHalVer > 1) {
970       NfcAidlStatus aidl_status;
971       mAidlHal->controlGranted(&aidl_status);
972     } else {
973       LOG(ERROR) << StringPrintf("Unsupported function %s", func);
974     }
975   } else if (mHal != nullptr) {
976     mHal->controlGranted();
977   }
978 }
979 
980 /*******************************************************************************
981 **
982 ** Function:    NfcAdaptation::HalPowerCycle
983 **
984 ** Description: Turn off and turn on the controller.
985 **
986 ** Returns:     None.
987 **
988 *******************************************************************************/
HalPowerCycle()989 void NfcAdaptation::HalPowerCycle() {
990   const char* func = "NfcAdaptation::HalPowerCycle";
991   LOG(VERBOSE) << StringPrintf("%s", func);
992   if (mAidlHal != nullptr) {
993     mAidlHal->powerCycle();
994   } else if (mHal != nullptr) {
995     mHal->powerCycle();
996   }
997 }
998 
999 /*******************************************************************************
1000 **
1001 ** Function:    NfcAdaptation::HalGetMaxNfcee
1002 **
1003 ** Description: Turn off and turn on the controller.
1004 **
1005 ** Returns:     None.
1006 **
1007 *******************************************************************************/
HalGetMaxNfcee()1008 uint8_t NfcAdaptation::HalGetMaxNfcee() {
1009   const char* func = "NfcAdaptation::HalGetMaxNfcee";
1010   LOG(VERBOSE) << StringPrintf("%s", func);
1011 
1012   return nfa_ee_max_ee_cfg;
1013 }
1014 
1015 /*******************************************************************************
1016 **
1017 ** Function:    NfcAdaptation::DownloadFirmware
1018 **
1019 ** Description: Download firmware patch files.
1020 **
1021 ** Returns:     None.
1022 **
1023 *******************************************************************************/
DownloadFirmware()1024 bool NfcAdaptation::DownloadFirmware() {
1025   const char* func = "NfcAdaptation::DownloadFirmware";
1026   isDownloadFirmwareCompleted = false;
1027   LOG(VERBOSE) << StringPrintf("%s: enter", func);
1028   HalInitialize();
1029 
1030   mHalOpenCompletedEvent.lock();
1031   LOG(VERBOSE) << StringPrintf("%s: try open HAL", func);
1032   HalOpen(HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback);
1033   mHalOpenCompletedEvent.wait();
1034 
1035   LOG(VERBOSE) << StringPrintf("%s: try close HAL", func);
1036   HalClose();
1037 
1038   HalTerminate();
1039   LOG(VERBOSE) << StringPrintf("%s: exit", func);
1040 
1041   return isDownloadFirmwareCompleted;
1042 }
1043 
1044 /*******************************************************************************
1045 **
1046 ** Function:    NfcAdaptation::HalDownloadFirmwareCallback
1047 **
1048 ** Description: Receive events from the HAL.
1049 **
1050 ** Returns:     None.
1051 **
1052 *******************************************************************************/
HalDownloadFirmwareCallback(nfc_event_t event,nfc_status_t event_status)1053 void NfcAdaptation::HalDownloadFirmwareCallback(nfc_event_t event,
1054                                                 __attribute__((unused))
1055                                                 nfc_status_t event_status) {
1056   const char* func = "NfcAdaptation::HalDownloadFirmwareCallback";
1057   LOG(VERBOSE) << StringPrintf("%s: event=0x%X", func, event);
1058   switch (event) {
1059     case HAL_NFC_OPEN_CPLT_EVT: {
1060       LOG(VERBOSE) << StringPrintf("%s: HAL_NFC_OPEN_CPLT_EVT", func);
1061       if (event_status == HAL_NFC_STATUS_OK) isDownloadFirmwareCompleted = true;
1062       mHalOpenCompletedEvent.signal();
1063       break;
1064     }
1065     case HAL_NFC_CLOSE_CPLT_EVT: {
1066       LOG(VERBOSE) << StringPrintf("%s: HAL_NFC_CLOSE_CPLT_EVT", func);
1067       break;
1068     }
1069   }
1070 }
1071 
1072 /*******************************************************************************
1073 **
1074 ** Function:    NfcAdaptation::HalDownloadFirmwareDataCallback
1075 **
1076 ** Description: Receive data events from the HAL.
1077 **
1078 ** Returns:     None.
1079 **
1080 *******************************************************************************/
HalDownloadFirmwareDataCallback(uint16_t data_len,uint8_t * p_data)1081 void NfcAdaptation::HalDownloadFirmwareDataCallback(__attribute__((unused))
1082                                                     uint16_t data_len,
1083                                                     __attribute__((unused))
1084                                                     uint8_t* p_data) {}
1085 
1086 /*******************************************************************************
1087 **
1088 ** Function:    ThreadMutex::ThreadMutex()
1089 **
1090 ** Description: class constructor
1091 **
1092 ** Returns:     none
1093 **
1094 *******************************************************************************/
ThreadMutex()1095 ThreadMutex::ThreadMutex() {
1096   pthread_mutexattr_t mutexAttr;
1097 
1098   pthread_mutexattr_init(&mutexAttr);
1099   pthread_mutex_init(&mMutex, &mutexAttr);
1100   pthread_mutexattr_destroy(&mutexAttr);
1101 }
1102 
1103 /*******************************************************************************
1104 **
1105 ** Function:    ThreadMutex::~ThreadMutex()
1106 **
1107 ** Description: class destructor
1108 **
1109 ** Returns:     none
1110 **
1111 *******************************************************************************/
~ThreadMutex()1112 ThreadMutex::~ThreadMutex() { pthread_mutex_destroy(&mMutex); }
1113 
1114 /*******************************************************************************
1115 **
1116 ** Function:    ThreadMutex::lock()
1117 **
1118 ** Description: lock kthe mutex
1119 **
1120 ** Returns:     none
1121 **
1122 *******************************************************************************/
lock()1123 void ThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
1124 
1125 /*******************************************************************************
1126 **
1127 ** Function:    ThreadMutex::unblock()
1128 **
1129 ** Description: unlock the mutex
1130 **
1131 ** Returns:     none
1132 **
1133 *******************************************************************************/
unlock()1134 void ThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
1135 
1136 /*******************************************************************************
1137 **
1138 ** Function:    ThreadCondVar::ThreadCondVar()
1139 **
1140 ** Description: class constructor
1141 **
1142 ** Returns:     none
1143 **
1144 *******************************************************************************/
ThreadCondVar()1145 ThreadCondVar::ThreadCondVar() {
1146   pthread_condattr_t CondAttr;
1147 
1148   pthread_condattr_init(&CondAttr);
1149   pthread_cond_init(&mCondVar, &CondAttr);
1150 
1151   pthread_condattr_destroy(&CondAttr);
1152 }
1153 
1154 /*******************************************************************************
1155 **
1156 ** Function:    ThreadCondVar::~ThreadCondVar()
1157 **
1158 ** Description: class destructor
1159 **
1160 ** Returns:     none
1161 **
1162 *******************************************************************************/
~ThreadCondVar()1163 ThreadCondVar::~ThreadCondVar() { pthread_cond_destroy(&mCondVar); }
1164 
1165 /*******************************************************************************
1166 **
1167 ** Function:    ThreadCondVar::wait()
1168 **
1169 ** Description: wait on the mCondVar
1170 **
1171 ** Returns:     none
1172 **
1173 *******************************************************************************/
wait()1174 void ThreadCondVar::wait() {
1175   pthread_cond_wait(&mCondVar, *this);
1176   pthread_mutex_unlock(*this);
1177 }
1178 
1179 /*******************************************************************************
1180 **
1181 ** Function:    ThreadCondVar::signal()
1182 **
1183 ** Description: signal the mCondVar
1184 **
1185 ** Returns:     none
1186 **
1187 *******************************************************************************/
signal()1188 void ThreadCondVar::signal() {
1189   AutoThreadMutex a(*this);
1190   pthread_cond_signal(&mCondVar);
1191 }
1192 
1193 /*******************************************************************************
1194 **
1195 ** Function:    AutoThreadMutex::AutoThreadMutex()
1196 **
1197 ** Description: class constructor, automatically lock the mutex
1198 **
1199 ** Returns:     none
1200 **
1201 *******************************************************************************/
AutoThreadMutex(ThreadMutex & m)1202 AutoThreadMutex::AutoThreadMutex(ThreadMutex& m) : mm(m) { mm.lock(); }
1203 
1204 /*******************************************************************************
1205 **
1206 ** Function:    AutoThreadMutex::~AutoThreadMutex()
1207 **
1208 ** Description: class destructor, automatically unlock the mutex
1209 **
1210 ** Returns:     none
1211 **
1212 *******************************************************************************/
~AutoThreadMutex()1213 AutoThreadMutex::~AutoThreadMutex() { mm.unlock(); }
1214