Weaver libese module update am: 8c8efba908 am: 22db2f5bec am: 1ffd603480

Original change: https://android-review.googlesource.com/c/platform/hardware/nxp/weaver/+/2812613

Change-Id: I6496aa8199424ed7aafafba1f32fac4da65996ed
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/libese_weaver/inc/TransportFactory.h b/libese_weaver/inc/TransportFactory.h
index 9b68ffc..416f6d5 100644
--- a/libese_weaver/inc/TransportFactory.h
+++ b/libese_weaver/inc/TransportFactory.h
@@ -30,7 +30,7 @@
  ** See the License for the specific language governing permissions and
  ** limitations under the License.
  **
- ** Copyright 2020-2022 NXP
+ ** Copyright 2020-2023 NXP
  **
  *********************************************************************************/
 
@@ -73,6 +73,13 @@
     ~TransportFactory() {}
 
     /**
+     * Sets Applet AID.
+     */
+    inline bool setAppletAid(const std::vector<uint8_t> &aid) {
+        return mTransport->setAppletAid(aid);
+    }
+
+    /**
      * Establishes a communication channel with the secure element.
      */
     inline bool openConnection() {
diff --git a/libese_weaver/inc/weaver_parser-impl.h b/libese_weaver/inc/weaver_parser-impl.h
index 7ca0267..14c7ef8 100644
--- a/libese_weaver/inc/weaver_parser-impl.h
+++ b/libese_weaver/inc/weaver_parser-impl.h
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- *  Copyright 2020, 2022 NXP
+ *  Copyright 2020, 2022-2023 NXP
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -139,12 +139,12 @@
   /**
    * \brief Function to get Weaver Applet ID
    *
-   * \param[out]    aid  - applet id of the weaver applet.
+   * \param[out]    aid  - applet ids of the weaver applet.
    *
    * \retval This function return true in case of success
    *         In case of failure returns false.
    */
-  bool getAppletId(std::vector<uint8_t> &aid);
+  bool getAppletId(std::vector<std::vector<uint8_t>> &aid);
 
   /**
    * \brief static function to get the singleton instance of WeaverParserImpl
@@ -162,7 +162,7 @@
     APP_INVALID_LEN,
     APP_INVALID_SLOT,
     APP_INVALID_P1_P2,
-    APP_UNKONWN_ERR,
+    APP_UNKNOWN_ERR,
   };
 
   /**
diff --git a/libese_weaver/inc/weaver_parser.h b/libese_weaver/inc/weaver_parser.h
index 5c505a2..71c1dce 100644
--- a/libese_weaver/inc/weaver_parser.h
+++ b/libese_weaver/inc/weaver_parser.h
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- *  Copyright 2020, 2022 NXP
+ *  Copyright 2020, 2023 NXP
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -136,12 +136,12 @@
   /**
    * \brief virtual Function to get Weaver Applet ID
    *
-   * \param[out]    aid  - applet id of the weaver applet.
+   * \param[out]    aid  - applet ids of the weaver applet.
    *
    * \retval This function return true in case of success
    *         In case of failure returns false.
    */
-  virtual bool getAppletId(std::vector<uint8_t> &aid) = 0;
+  virtual bool getAppletId(std::vector<std::vector<uint8_t>> &aid) = 0;
 
   /**
    * \brief virtual destructor for weaver parser
diff --git a/libese_weaver/inc/weaver_transport-impl.h b/libese_weaver/inc/weaver_transport-impl.h
index c34a9e3..5fa4075 100644
--- a/libese_weaver/inc/weaver_transport-impl.h
+++ b/libese_weaver/inc/weaver_transport-impl.h
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- *  Copyright 2020, 2022 NXP
+ *  Copyright 2020, 2022-2023 NXP
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
    * \retval This function return true in case of success
    *         In case of failure returns false.
    */
-  bool Init(std::vector<uint8_t> aid) override;
+  bool Init(std::vector<std::vector<uint8_t>> aid) override;
 
   /**
    * \brief Function to open applet connection
@@ -105,6 +105,10 @@
    * Same will be used for std::call_once
    */
   static void createInstance();
+  /* Private wrapper function to send apdu.
+   * It will try with alternate aids if sending is failed.
+   */
+  bool sendInternal(std::vector<uint8_t> data, std::vector<uint8_t> &resp);
 };
 
 #endif /* _WEAVER_TRANSPORT_IMPL_H_ */
diff --git a/libese_weaver/inc/weaver_transport.h b/libese_weaver/inc/weaver_transport.h
index 17b83e8..f8a76b6 100644
--- a/libese_weaver/inc/weaver_transport.h
+++ b/libese_weaver/inc/weaver_transport.h
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- *  Copyright 2020 NXP
+ *  Copyright 2020, 2023 NXP
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -26,12 +26,12 @@
   /**
    * \brief virtual Function to initialize Weaver Transport Interface
    *
-   * \param[in]    aid -  applet id to be set to transport interface
+   * \param[in]    aid -  applet ids to be set to transport interface
    *
    * \retval This function return true in case of success
    *         In case of failure returns false.
    */
-  virtual bool Init(std::vector<uint8_t> aid) = 0;
+  virtual bool Init(std::vector<std::vector<uint8_t>> aid) = 0;
 
   /**
    * \brief virtual Function to open applet connection
diff --git a/libese_weaver/src/weaver-impl.cpp b/libese_weaver/src/weaver-impl.cpp
index d682b68..85f950b 100644
--- a/libese_weaver/src/weaver-impl.cpp
+++ b/libese_weaver/src/weaver-impl.cpp
@@ -58,10 +58,10 @@
   mParser = WeaverParserImpl::getInstance();
   RETURN_IF_NULL(mTransport, WEAVER_STATUS_FAILED, "Transport is NULL");
   RETURN_IF_NULL(mParser, WEAVER_STATUS_FAILED, "Parser is NULL");
-  std::vector<uint8_t> aid;
+  std::vector<std::vector<uint8_t>> aid;
   mParser->getAppletId(aid);
-  if (!mTransport->Init(aid)) {
-    LOG_E(TAG, "Not able to Initilaize Transport Interface");
+  if (!mTransport->Init(std::move(aid))) {
+    LOG_E(TAG, "Not able to Initialize Transport Interface");
     LOG_D(TAG, "Exit : FAILED");
     return WEAVER_STATUS_FAILED;
   }
@@ -98,7 +98,7 @@
   }
 #endif
   if (status == WEAVER_STATUS_OK) {
-    status = mParser->ParseSlotInfo(resp, slotInfo);
+    status = mParser->ParseSlotInfo(std::move(resp), slotInfo);
     LOG_D(TAG, "Total Slots (%u) ", slotInfo.slots);
   } else {
     LOG_E(TAG, "Failed Parsing getSlot Response");
@@ -153,8 +153,8 @@
       if (mParser->FrameGetDataCmd(WeaverParserImpl::sThrottleGetDataP1, (uint8_t)slotId, cmd) &&
           (mTransport->Send(cmd, resp))) {
         GetDataRespInfo getDataInfo;
-        if (mParser->ParseGetDataInfo(resp, getDataInfo) == WEAVER_STATUS_OK) {
-          /* convert timeout from getDataInfo sec to millisec assign same to read response */
+        if (mParser->ParseGetDataInfo(std::move(resp), getDataInfo) == WEAVER_STATUS_OK) {
+          /* convert timeout from getDataInfo sec to millisecond assign same to read response */
           readRespInfo.timeout = (getDataInfo.timeout * 1000);
         }
       }
@@ -204,7 +204,7 @@
     // Channel Close Failed
   }
 #endif
-  if (status != WEAVER_STATUS_OK || (!mParser->isSuccess(resp))) {
+  if (status != WEAVER_STATUS_OK || (!mParser->isSuccess(std::move(resp)))) {
     status = WEAVER_STATUS_FAILED;
   }
   LOG_D(TAG, "Exit");
diff --git a/libese_weaver/src/weaver-parser-impl.cpp b/libese_weaver/src/weaver-parser-impl.cpp
index 229a349..0804281 100644
--- a/libese_weaver/src/weaver-parser-impl.cpp
+++ b/libese_weaver/src/weaver-parser-impl.cpp
@@ -66,8 +66,11 @@
 #define INS_GET_DATA 0xCA
 
 /* Applet ID to be used for Weaver */
-const std::vector<uint8_t> kWeaverAID = {0xA0, 0x00, 0x00, 0x03,
-                                         0x96, 0x10, 0x10};
+const std::vector<std::vector<uint8_t>> kWeaverAIDs = {
+    {0xA0, 0x00, 0x00, 0x03, 0x96, 0x10, 0x10}, // Primary AID
+    {0xA0, 0x00, 0x00, 0x03, 0x96, 0x54, 0x53, 0x00, 0x00, 0x00, 0x01, 0x00,
+     0x23, 0x00, 0x00, 0x00}, // Alternate AID
+};
 
 /**
  * \brief static function to get the singleton instance of WeaverParserImpl
@@ -370,7 +373,7 @@
  *         and false in other cases.
  */
 bool WeaverParserImpl::isSuccess(std::vector<uint8_t> response) {
-  return (checkStatus(response) == APP_SUCCESS) ? true : false;
+  return (checkStatus(std::move(response)) == APP_SUCCESS) ? true : false;
 }
 
 /**
@@ -419,11 +422,11 @@
  * \retval This function return true in case of success
  *         In case of failure returns false.
  */
-bool WeaverParserImpl::getAppletId(std::vector<uint8_t> &aid) {
+bool WeaverParserImpl::getAppletId(std::vector<std::vector<uint8_t>> &aid) {
   LOG_D(TAG, "Entry");
   bool status = false;
-  if (kWeaverAID.size() > 0) {
-    aid = kWeaverAID;
+  if (kWeaverAIDs.size() > 0) {
+    aid = kWeaverAIDs;
     status = true;
   }
   LOG_D(TAG, "Exit");
diff --git a/libese_weaver/src/weaver-transport-impl.cpp b/libese_weaver/src/weaver-transport-impl.cpp
index c1d59ef..08f4fd0 100644
--- a/libese_weaver/src/weaver-transport-impl.cpp
+++ b/libese_weaver/src/weaver-transport-impl.cpp
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- *  Copyright 2020-2022 NXP
+ *  Copyright 2020-2023 NXP
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -27,12 +27,15 @@
 #define RETRY_DELAY_INTERVAL_SEC 1
 #define PROP_SYSBOOT_COMPLETED "sys.boot_completed"
 #define SYSBOOT_COMPLETED_VALUE 1
+#define IS_APPLET_SELECTION_FAILED(resp)                                       \
+  (!resp.empty() && resp[0] == APP_NOT_FOUND_SW1 &&                            \
+   resp[1] == APP_NOT_FOUND_SW2)
 
 WeaverTransportImpl *WeaverTransportImpl::s_instance = NULL;
 std::once_flag WeaverTransportImpl::s_instanceFlag;
 
 /* Applet ID to be use for communication */
-std::vector<uint8_t> kAppletId;
+std::vector<std::vector<uint8_t>> kAppletId;
 
 /* Interface instance of libese-transport library */
 static std::unique_ptr<se_transport::TransportFactory> pTransportFactory =
@@ -45,7 +48,7 @@
 getTransportFactoryInstance() {
   if (pTransportFactory == nullptr) {
     pTransportFactory = std::unique_ptr<se_transport::TransportFactory>(
-        new se_transport::TransportFactory(false, kAppletId));
+        new se_transport::TransportFactory(false, kAppletId[0]));
     pTransportFactory->openConnection();
   }
   return pTransportFactory;
@@ -82,9 +85,9 @@
  * \retval This function return true in case of success
  *         In case of failure returns false.
  */
-bool WeaverTransportImpl::Init(std::vector<uint8_t> aid) {
+bool WeaverTransportImpl::Init(std::vector<std::vector<uint8_t>> aid) {
   LOG_D(TAG, "Entry");
-  kAppletId = aid;
+  kAppletId = std::move(aid);
   LOG_D(TAG, "Exit");
   return true;
 }
@@ -125,6 +128,40 @@
 }
 
 /**
+ * \brief Private wrapper function to send apdu.
+ * It will try with alternate aids if sending is failed.
+ *
+ * \param[in]    data -         command to be send to applet
+ * \param[out]   resp -         response from applet
+ *
+ * \retval This function return true in case of success
+ *         In case of failure returns false.
+ */
+bool WeaverTransportImpl::sendInternal(std::vector<uint8_t> data,
+                                       std::vector<uint8_t> &resp) {
+  bool status = false;
+  status =
+      getTransportFactoryInstance()->sendData(data.data(), data.size(), resp);
+  if (!status && IS_APPLET_SELECTION_FAILED(resp)) {
+    LOG_E(TAG, ": send Failed, trying with alternateAids");
+    // If Applet selection failed, try with alternate Aids
+    for (int i = 1; i < kAppletId.size(); i++) {
+      getTransportFactoryInstance()->setAppletAid(kAppletId[i]);
+      status = getTransportFactoryInstance()->sendData(data.data(), data.size(),
+                                                       resp);
+      if (status) {
+        return status;
+      }
+    }
+    if (!status) {
+      // None of alternate Aids success, Revert back to primary AID
+      getTransportFactoryInstance()->setAppletAid(kAppletId[0]);
+    }
+  }
+  return status;
+}
+
+/**
  * \brief Function to send commands to applet
  *
  * \param[in]    data -         command to be send to applet
@@ -140,8 +177,7 @@
   bool status = false;
   // Opens the channel with aid and transmit the data
   do {
-    status =
-        getTransportFactoryInstance()->sendData(data.data(), data.size(), resp);
+    status = sendInternal(data, resp);
     if (!status) {
       if (!isDeviceBootCompleted()) {
         LOG_D(TAG, ": Device boot not completed, no retry required");