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