Merge "Define 2 new entries for libnfc-hal-st.conf"
diff --git a/1.2/hal_st21nfc.cc b/1.2/hal_st21nfc.cc
index 5efc6b1..71cb7aa 100644
--- a/1.2/hal_st21nfc.cc
+++ b/1.2/hal_st21nfc.cc
@@ -30,6 +30,7 @@
 #include "android_logmsg.h"
 #include "hal_config.h"
 #include "halcore.h"
+#include "st21nfc_dev.h"
 
 #define VENDOR_LIB_PATH "/vendor/lib64/"
 #define VENDOR_LIB_EXT ".so"
@@ -40,15 +41,6 @@
 
 typedef int (*STEseReset)(void);
 
-typedef struct {
-  struct nfc_nci_device nci_device;  // nci_device must be first struct member
-  // below declarations are private variables within HAL
-  nfc_stack_callback_t* p_cback;
-  nfc_stack_data_callback_t* p_data_cback;
-  HALHANDLE hHAL;
-  nfc_stack_callback_t* p_cback_unwrap;
-} st21nfc_dev_t;
-
 const char* halVersion = "ST21NFC HAL1.2 Version 3.2.54";
 
 uint8_t cmd_set_nfc_mode_enable[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
diff --git a/aidl/Nfc.cpp b/aidl/Nfc.cpp
index dc8ee62..bcadc16 100644
--- a/aidl/Nfc.cpp
+++ b/aidl/Nfc.cpp
@@ -25,22 +25,28 @@
 namespace hardware {
 namespace nfc {
 
+typedef struct {
+  uint64_t mOpenCount;
+  Nfc* nfc;
+} stHalCookie;
+
 std::shared_ptr<INfcClientCallback> Nfc::mCallback = nullptr;
 AIBinder_DeathRecipient* clientDeathRecipient = nullptr;
 pthread_mutex_t mLockOpenClose = PTHREAD_MUTEX_INITIALIZER;
 uint64_t mOpenCount = 0;
+stHalCookie mHalCookie;
 
 void OnDeath(void* cookie) {
   pthread_mutex_lock(&mLockOpenClose);
-  LOG(INFO) << __func__ << "cookie : " << (*(uint64_t*)cookie) << " mOpenCount "
-            << (unsigned long long)mOpenCount;
-  if (*(uint64_t*)cookie == mOpenCount) {
+  stHalCookie* mCookie = static_cast<stHalCookie*>(cookie);
+  LOG(INFO) << __func__ << "cookie : " << mCookie->mOpenCount
+            << " mOpenCount : " << mOpenCount;
+  if (mCookie->mOpenCount == mOpenCount) {
     if (Nfc::mCallback != nullptr &&
         !AIBinder_isAlive(Nfc::mCallback->asBinder().get())) {
       LOG(INFO) << __func__ << " Nfc service has died";
       pthread_mutex_unlock(&mLockOpenClose);
-      Nfc* nfc = static_cast<Nfc*>(cookie);
-      nfc->close(NfcCloseType::DISABLE);
+      mCookie->nfc->close(NfcCloseType::DISABLE);
     }
   }
   pthread_mutex_unlock(&mLockOpenClose);
@@ -57,18 +63,20 @@
     pthread_mutex_lock(&mLockOpenClose);
     mOpenCount++;
     Nfc::mCallback = clientCallback;
+    mHalCookie.mOpenCount = mOpenCount;
+    mHalCookie.nfc = this;
 
     clientDeathRecipient = AIBinder_DeathRecipient_new(OnDeath);
     auto linkRet =
         AIBinder_linkToDeath(clientCallback->asBinder().get(),
-                             clientDeathRecipient, &mOpenCount /* cookie */);
+                             clientDeathRecipient, &mHalCookie /* cookie */);
     if (linkRet != STATUS_OK) {
       LOG(ERROR) << __func__ << ": linkToDeath failed: " << linkRet;
       // Just ignore the error.
     }
 
     int ret = StNfc_hal_open(eventCallback, dataCallback);
-    LOG(INFO) << "Nfc::open Exit count " << (unsigned long long)mOpenCount;
+    LOG(INFO) << "Nfc::open Exit count " << mOpenCount;
     pthread_mutex_unlock(&mLockOpenClose);
     return ret == 0 ? ndk::ScopedAStatus::ok()
                     : ndk::ScopedAStatus::fromServiceSpecificError(
diff --git a/aidl/hal_st21nfc.cc b/aidl/hal_st21nfc.cc
index 2ae28c8..755ece1 100644
--- a/aidl/hal_st21nfc.cc
+++ b/aidl/hal_st21nfc.cc
@@ -29,6 +29,7 @@
 #include "android_logmsg.h"
 #include "hal_config.h"
 #include "halcore.h"
+#include "st21nfc_dev.h"
 
 #define VENDOR_LIB_PATH "/vendor/lib64/"
 #define VENDOR_LIB_EXT ".so"
@@ -41,14 +42,6 @@
 
 typedef int (*STEseReset)(void);
 
-typedef struct {
-  // below declarations are private variables within HAL
-  nfc_stack_callback_t* p_cback;
-  nfc_stack_data_callback_t* p_data_cback;
-  HALHANDLE hHAL;
-  nfc_stack_callback_t* p_cback_unwrap;
-} st21nfc_dev_t;
-
 const char* halVersion = "ST21NFC AIDL Version 1.0.0";
 
 uint8_t cmd_set_nfc_mode_enable[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
diff --git a/st21nfc/hal/halcore.cc b/st21nfc/hal/halcore.cc
index e7bb319..67ecbf9 100644
--- a/st21nfc/hal/halcore.cc
+++ b/st21nfc/hal/halcore.cc
@@ -25,8 +25,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+
 #include "android_logmsg.h"
 #include "halcore_private.h"
+#include "st21nfc_dev.h"
 
 extern int I2cWriteCmd(const uint8_t* x, size_t len);
 extern void DispHal(const char* title, const void* data, size_t length);
@@ -38,14 +40,6 @@
 static bool rf_deactivate_delay;
 struct timespec start_tx_data;
 
-typedef struct {
-  struct nfc_nci_device nci_device;  // nci_device must be first struct member
-  // below declarations are private variables within HAL
-  nfc_stack_callback_t* p_cback;
-  nfc_stack_data_callback_t* p_data_cback;
-  HALHANDLE hHAL;
-} st21nfc_dev_t;  // beware, is a duplication of structure in nfc_nci_st21nfc.c
-
 /**************************************************************************************************
  *
  *                                      Private API Declaration
diff --git a/st21nfc/hal_wrapper.cc b/st21nfc/hal_wrapper.cc
index 38653f6..5c97179 100644
--- a/st21nfc/hal_wrapper.cc
+++ b/st21nfc/hal_wrapper.cc
@@ -23,9 +23,11 @@
 #include <log/log.h>
 #include <string.h>
 #include <unistd.h>
+
 #include "android_logmsg.h"
 #include "hal_fd.h"
 #include "halcore.h"
+#include "st21nfc_dev.h"
 
 extern void HalCoreCallback(void* context, uint32_t event, const void* d,
                             size_t length);
@@ -33,14 +35,6 @@
 extern void I2cCloseLayer();
 extern void I2cRecovery();
 
-typedef struct {
-  struct nfc_nci_device nci_device;  // nci_device must be first struct member
-  // below declarations are private variables within HAL
-  nfc_stack_callback_t* p_cback;
-  nfc_stack_data_callback_t* p_data_cback;
-  HALHANDLE hHAL;
-} st21nfc_dev_t;
-
 static void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data);
 static void halWrapperCallback(uint8_t event, uint8_t event_status);
 
diff --git a/st21nfc/include/st21nfc_dev.h b/st21nfc/include/st21nfc_dev.h
new file mode 100644
index 0000000..de48e70
--- /dev/null
+++ b/st21nfc/include/st21nfc_dev.h
@@ -0,0 +1,29 @@
+/** ----------------------------------------------------------------------
+ *
+ * Copyright (C) 2022 ST Microelectronics S.A.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *
+ ----------------------------------------------------------------------*/
+#ifndef __ST21NFC_DEV__
+#define __ST21NFC_DEV__
+
+typedef struct {
+  nfc_stack_callback_t* p_cback;
+  nfc_stack_data_callback_t* p_data_cback;
+  HALHANDLE hHAL;
+  nfc_stack_callback_t* p_cback_unwrap;
+} st21nfc_dev_t;
+
+#endif