Snap for 11828632 from cf1414f61f1a014721207ef05795b6d098dc8395 to 24Q3-release Change-Id: I197f913fb645b41a75f55098df1980cc002e5e45
diff --git a/transport/OmapiTransport.cpp b/transport/OmapiTransport.cpp index 928c21d..2c8e0d3 100644 --- a/transport/OmapiTransport.cpp +++ b/transport/OmapiTransport.cpp
@@ -37,13 +37,14 @@ #if defined OMAPI_TRANSPORT #include "OmapiTransport.h" -#include <stdio.h> -#include <sys/socket.h> #include <arpa/inet.h> -#include <unistd.h> -#include <string.h> -#include <vector> #include <iomanip> +#include <map> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> +#include <unistd.h> +#include <vector> #include <android-base/logging.h> #include <android-base/stringprintf.h> @@ -69,6 +70,11 @@ class SEListener : public ::aidl::android::se::omapi::BnSecureElementListener {}; #ifdef NXP_EXTNS + +static std::mutex sCookiesMutex; +static uintptr_t sCookiesKeyCounter = 0; +static std::map<uintptr_t, OmapiTransport *> sCookies; + void omapiSessionTimerFunc(union sigval arg){ LOG(INFO) << "Session Timer expired !!"; OmapiTransport *obj = (OmapiTransport*)arg.sival_ptr; @@ -77,12 +83,33 @@ } void OmapiTransport::BinderDiedCallback(void *cookie) { - LOG(ERROR) << "Received binder died. OMAPI Service died"; - auto thiz = static_cast<OmapiTransport *>(cookie); - thiz->closeConnection(); + std::lock_guard lock(sCookiesMutex); + if (auto it = sCookies.find(reinterpret_cast<uintptr_t>(cookie)); + it != sCookies.end()) { + LOG(ERROR) + << "Received binder died with cookie: " << cookie + << ". OMAPI Service died, closing connection"; + it->second->closeConnection(); + } else { + LOG(ERROR) + << "Received binder died with cookie: " << cookie + << ". OMAPI Service died, but no OmapiTransport."; + } } #endif +OmapiTransport::~OmapiTransport() { +#ifdef NXP_EXTNS + std::lock_guard sLock(sCookiesMutex); + std::lock_guard mLock(mCookieKeysMutex); + for (auto cookie : mCookieKeys) { + LOG(INFO) << "OmapiTransport destructor cleaning up death recipient cookie(" + << cookie << ") as we no longer need to listen for service death."; + sCookies.erase(cookie); + } +#endif +} + bool OmapiTransport::initialize() { LOG(DEBUG) << "Initialize the secure element connection"; @@ -101,8 +128,17 @@ } #ifdef NXP_EXTNS - AIBinder_linkToDeath(omapiSeService->asBinder().get(), - mDeathRecipient.get(), this); + { + std::lock_guard sLock(sCookiesMutex); + uintptr_t cookieKey = sCookiesKeyCounter++; + std::lock_guard mLock(mCookieKeysMutex); + mCookieKeys.push_back(cookieKey); + sCookies[cookieKey] = this; + LOG(INFO) << "linkToDeath on OMAPI service with cookie: " << cookieKey; + AIBinder_linkToDeath(omapiSeService->asBinder().get(), + mDeathRecipient.get(), + reinterpret_cast<void *>(cookieKey)); + } #endif // reset readers, clear readers if already existing @@ -286,8 +322,16 @@ } #ifdef NXP_EXTNS if (omapiSeService != nullptr) { - AIBinder_unlinkToDeath(omapiSeService->asBinder().get(), - mDeathRecipient.get(), this); + std::lock_guard sLock(sCookiesMutex); + std::lock_guard mLock(mCookieKeysMutex); + for (auto cookie : mCookieKeys) { + LOG(INFO) << "unlinkToDeath on OMAPI service with cookie: " << cookie; + AIBinder_unlinkToDeath(omapiSeService->asBinder().get(), + mDeathRecipient.get(), + reinterpret_cast<void *>(cookie)); + sCookies.erase(cookie); + } + mCookieKeys.clear(); omapiSeService = nullptr; } session = nullptr;
diff --git a/transport/include/OmapiTransport.h b/transport/include/OmapiTransport.h index 708828c..5bda394 100644 --- a/transport/include/OmapiTransport.h +++ b/transport/include/OmapiTransport.h
@@ -78,6 +78,7 @@ AIBinder_DeathRecipient_new(BinderDiedCallback)); #endif } + virtual ~OmapiTransport(); #ifdef NXP_EXTNS /** @@ -140,6 +141,8 @@ #ifdef NXP_EXTNS ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; + std::mutex mCookieKeysMutex; + std::vector<uintptr_t> mCookieKeys; static void BinderDiedCallback(void *cookie); bool internalProtectedTransmitApdu(