Snap for 4829593 from 56f93b611d24d1bfa847c7039b7ea49dc806726c to pi-release

Change-Id: I6b72f69f1f1d5c651e0e540f5464651ac0512d34
diff --git a/platform/slpi/include/chre/platform/slpi/see/see_helper.h b/platform/slpi/include/chre/platform/slpi/see/see_helper.h
index 6854e8c..32481c3 100644
--- a/platform/slpi/include/chre/platform/slpi/see/see_helper.h
+++ b/platform/slpi/include/chre/platform/slpi/see/see_helper.h
@@ -101,14 +101,14 @@
 
 /**
  * A helper class for making requests to Qualcomm's Sensors Execution
- * Environment (SEE) via Qsocket and waiting for the response and the
+ * Environment (SEE) via the sns_client API and waiting for the response and the
  * corresponding indication message if applicable.
  * Not safe to use from multiple threads. Only one synchronous request can be
  * made at a time.
  */
 class SeeHelper : public NonCopyable {
  public:
-  //! A struct to facilitate mapping between 'SUID + Qsocket client' and
+  //! A struct to facilitate mapping between 'SUID + sns_client' and
   //! SensorType.
   struct SensorInfo {
     sns_std_suid suid;
@@ -117,7 +117,7 @@
   };
 
   /**
-   * Deinits Qsocket clients before destructing this object.
+   * Deinits clients before destructing this object.
    */
   ~SeeHelper();
 
@@ -165,10 +165,10 @@
   bool getAttributesSync(const sns_std_suid& suid, SeeAttributes *attr);
 
   /**
-   * Initializes and waits for the sensor client Qsocket service to become
-   * available, and obtains remote_proc and cal sensors' info for future
-   * operations. This function must be called first to initialize the object and
-   * be called only once.
+   * Initializes and waits for the sensor client service to become available,
+   * and obtains remote_proc and cal sensors' info for future operations. This
+   * function must be called first to initialize the object and be called only
+   * once.
    *
    * @param cbIf A pointer to the callback interface that will be invoked to
    *             handle all async requests with callback data type defined in
@@ -185,7 +185,7 @@
    *
    * @param request The sensor request to make.
    *
-   * @return true if the Qsocket request has been successfully made.
+   * @return true if the request has been successfully made.
    */
   bool makeRequest(const SeeSensorRequest& request);
 
@@ -196,8 +196,8 @@
    * with populated CHRE sensor events. Each SUID/SensorType pair can only be
    * registered once. It's illegal to register SensorType::Unknown.
    *
-   * If an SUID is registered with a second SensorType, another Qsocket client
-   * may be created to disambiguate the SUID representation.
+   * If an SUID is registered with a second SensorType, another client may be
+   * created to disambiguate the SUID representation.
    *
    * @param sensorType The SensorType to register.
    * @param suid The SUID of the sensor.
@@ -220,6 +220,12 @@
   bool sensorIsRegistered(SensorType sensorType) const;
 
  protected:
+  struct SnsClientApi {
+    decltype(sns_client_init)   *sns_client_init;
+    decltype(sns_client_deinit) *sns_client_deinit;
+    decltype(sns_client_send)   *sns_client_send;
+  };
+
   /**
    * Get the cached SUID of a calibration sensor that corresponds to the
    * specified sensorType.
@@ -232,9 +238,8 @@
   const sns_std_suid& getCalSuidFromSensorType(SensorType sensorType) const;
 
   /**
-   * A convenience method to send a Qsocket request and wait for the indication
-   * if it's a synchronous one using the default Qsocket client obtained in
-   * init().
+   * A convenience method to send a request and wait for the indication if it's
+   * a synchronous one using the default client obtained in init().
    *
    * @see sendReq
    */
@@ -246,7 +251,7 @@
       bool waitForIndication,
       Nanoseconds timeoutResp = kDefaultSeeRespTimeout,
       Nanoseconds timeoutInd = kDefaultSeeIndTimeout) {
-    return sendReq(mQsocketClients[0], suid,
+    return sendReq(mSeeClients[0], suid,
                    syncData, syncDataType,
                    msgId, payload, payloadLen,
                    batchValid, batchPeriodUs, passive,
@@ -254,7 +259,13 @@
                    timeoutResp, timeoutInd);
   }
 
+  void setSnsClientApi(const SnsClientApi *api) {
+    mSnsClientApi = api;
+  }
+
  private:
+  static const SnsClientApi kDefaultApi;
+
   //! Used to synchronize responses and indications.
   ConditionVariable mCond;
 
@@ -265,11 +276,11 @@
   //! Callback interface for sensor events.
   SeeHelperCallbackInterface *mCbIf = nullptr;
 
-  //! The list of Qsocket clients initiated by SeeHelper.
-  DynamicVector<sns_client *> mQsocketClients;
+  //! The list of SEE clients initiated by SeeHelper.
+  DynamicVector<sns_client *> mSeeClients;
 
   //! The list of SensorTypes registered and their corresponding SUID and
-  //! Qsocket client.
+  //! client.
   DynamicVector<SensorInfo> mSensorInfos;
 
   //! Data struct to store sync APIs data.
@@ -285,16 +296,16 @@
   //! true if we are waiting on an indication for a sync call.
   bool mWaitingOnInd = false;
 
-  //! true if we are waiting on a response of a Qsocket request.
+  //! true if we are waiting on a response of a request.
   bool mWaitingOnResp = false;
 
   //! true if we've timed out in findSuidSync at least once
   bool mHaveTimedOutOnSuidLookup = false;
 
-  //! The Qsocket response error of the request we just made.
+  //! The response error of the request we just made.
   sns_std_error mRespError;
 
-  //! A transaction ID that increments for each Qsocket request.
+  //! A transaction ID that increments for each request.
   uint32_t mCurrentTxnId = 0;
 
   //! The SUID for the remote_proc sensor.
@@ -303,6 +314,9 @@
   //! Cal info of all the cal sensors.
   SeeCalInfo mCalInfo[kNumSeeCalSensors];
 
+  //! Contains the API this SeeHelper instance uses to interact with SEE
+  const SnsClientApi *mSnsClientApi = &kDefaultApi;
+
   /**
    * Initializes SEE calibration sensors and makes data request.
    *
@@ -318,25 +332,25 @@
   bool initRemoteProcSensor();
 
   /**
-   * Sends a QSocket request and waits for the response.
+   * Sends a request to SEE and waits for the response.
    *
-   * @param client The pointer to Qsocket client to make request with.
+   * @param client The pointer to sns_client to make the request with.
    * @param req A pointer to the sns_client_request_msg to be sent.
    * @param timeoutResp How long to wait for the response before abandoning it.
    *
    * @return true if the request was sent and the response was received
    *         successfully.
    */
-  bool sendQsocketReqSync(sns_client *client, sns_client_request_msg *req,
-                          Nanoseconds timeoutResp);
+  bool sendSeeReqSync(sns_client *client, sns_client_request_msg *req,
+                      Nanoseconds timeoutResp);
 
   /**
-   * Wrapper to send a Qsocket request and wait for the indication if it's a
+   * Wrapper to send a SEE request and wait for the indication if it's a
    * synchronous one.
    *
    * Only one request can be pending at a time per instance of SeeHelper.
    *
-   * @param client The pointer to Qsocket client to make requests with.
+   * @param client The pointer to sns_client to make requests with.
    * @param suid The SUID of the sensor the request is sent to
    * @param syncData The data struct or container to receive a sync call's data
    * @param syncDataType The data type we are waiting for.
@@ -388,28 +402,28 @@
       sns_client *client, const void *payload, size_t payloadLen);
 
   /**
-   * Handles a Qsocket response for request with specified transaction ID.
+   * Handles a response from SEE for a request sent with the specified
+   * transaction ID.
    */
-  void handleQsocketResp(uint32_t txnId, sns_std_error error);
+  void handleSeeResp(uint32_t txnId, sns_std_error error);
 
   /**
    * Extracts "this" from cbData and calls through to handleSnsClientEventMsg()
    *
    * @see sns_client_ind
    */
-  static void qsocketIndCb(sns_client *client, void *msg,
-                           uint32_t msgLen, void *cbData);
+  static void seeIndCb(sns_client *client, void *msg, uint32_t msgLen,
+                       void *cbData);
 
   /**
-   * Extracts "this" from cbData and calls through to handleQsocketResp()
+   * Extracts "this" from cbData and calls through to handleSeeResp()
    *
    * @see sns_client_resp
    */
-  static void qsocketRespCb(sns_client *client, sns_std_error error,
-                            void *cbData);
+  static void seeRespCb(sns_client *client, sns_std_error error, void *cbData);
 
   /**
-   * A wrapper to initialize a Qsocket client.
+   * A wrapper to initialize a sns_client.
    *
    * @see sns_client_init
    */
diff --git a/platform/slpi/see/see_helper.cc b/platform/slpi/see/see_helper.cc
index 5484c49..7f49190 100644
--- a/platform/slpi/see/see_helper.cc
+++ b/platform/slpi/see/see_helper.cc
@@ -56,8 +56,8 @@
 //! The SUID of the look up sensor.
 const sns_std_suid kSuidLookup = sns_suid_sensor_init_default;
 
-//! A struct to facilitate qsocket response handling
-struct QsocketRespCbData {
+//! A struct to facilitate SEE response handling
+struct SeeRespCbData {
   SeeHelper *seeHelper;
   uint32_t txnId;
 };
@@ -1294,11 +1294,17 @@
 
 }  // anonymous namespace
 
+const SeeHelper::SnsClientApi SeeHelper::kDefaultApi = {
+  .sns_client_init   = sns_client_init,
+  .sns_client_deinit = sns_client_deinit,
+  .sns_client_send   = sns_client_send,
+};
+
 SeeHelper::~SeeHelper() {
-  for (auto *client : mQsocketClients) {
-    int status = sns_client_deinit(client);
+  for (auto *client : mSeeClients) {
+    int status = mSnsClientApi->sns_client_deinit(client);
     if (status != 0) {
-      LOGE("Failed to release sensor Qsocket client: %d", status);
+      LOGE("Failed to release sensor client: %d", status);
     }
   }
 }
@@ -1395,7 +1401,7 @@
   }
 }
 
-void SeeHelper::handleQsocketResp(uint32_t txnId, sns_std_error error) {
+void SeeHelper::handleSeeResp(uint32_t txnId, sns_std_error error) {
   LockGuard<Mutex> lock(mMutex);
   if (mWaitingOnResp && txnId == mCurrentTxnId) {
     mRespError = error;
@@ -1412,16 +1418,15 @@
   CHRE_ASSERT(minNumSuids > 0);
 
   bool success = false;
-  if (mQsocketClients.empty()) {
-    LOGE("Sensor Qsocket client wasn't initialized");
+  if (mSeeClients.empty()) {
+    LOGE("Sensor client wasn't initialized");
   } else {
     UniquePtr<pb_byte_t> msg;
     size_t msgLen;
     if (encodeSnsSuidReq(dataType, &msg, &msgLen)) {
-      // Sensor client Qsocket service may come up before SEE sensors are
-      // enumerated. A max dwell time is set and retries are performed as
-      // currently there's no message indicating that SEE intialization is
-      // complete.
+      // Sensor client service may come up before SEE sensors are enumerated. A
+      // max dwell time is set and retries are performed as currently there's no
+      // message indicating that SEE intialization is complete.
       uint32_t trialCount = 0;
       do {
         suids->clear();
@@ -1458,8 +1463,8 @@
   CHRE_ASSERT(attr);
   bool success = false;
 
-  if (mQsocketClients.empty()) {
-    LOGE("Sensor Qsocket client wasn't initialized");
+  if (mSeeClients.empty()) {
+    LOGE("Sensor client wasn't initialized");
   } else {
     UniquePtr<pb_byte_t> msg;
     size_t msgLen;
@@ -1483,7 +1488,7 @@
 
   // Initialize cal/remote_proc_state sensors before making sensor data request.
   return (waitForService(&client, timeout)
-          && mQsocketClients.push_back(client)
+          && mSeeClients.push_back(client)
           && initCalSensors()
           && initRemoteProcSensor());
 }
@@ -1536,16 +1541,15 @@
 }
 
 /**
- * Sends a request to Qsocket and waits for the response.
+ * Sends a request to SEE and waits for the response.
  */
-bool SeeHelper::sendQsocketReqSync(sns_client *client,
-                                   sns_client_request_msg *req,
-                                   Nanoseconds timeoutResp) {
+bool SeeHelper::sendSeeReqSync(
+    sns_client *client, sns_client_request_msg *req, Nanoseconds timeoutResp) {
   CHRE_ASSERT(client);
   CHRE_ASSERT(req);
   bool success = false;
 
-  auto *cbData = memoryAlloc<QsocketRespCbData>();
+  auto *cbData = memoryAlloc<SeeRespCbData>();
   if (cbData == nullptr) {
     LOG_OOM();
   } else {
@@ -1558,10 +1562,10 @@
       cbData->txnId = ++mCurrentTxnId;
     }
 
-    int status = sns_client_send(client, req, SeeHelper::qsocketRespCb, cbData);
-
+    int status = mSnsClientApi->sns_client_send(
+        client, req, SeeHelper::seeRespCb, cbData);
     if (status != 0) {
-      LOGE("Error sending Qsocket request %d", status);
+      LOGE("Error sending SEE request %d", status);
       memoryFree(cbData);
     }
 
@@ -1576,10 +1580,10 @@
         }
 
         if (!waitSuccess) {
-          LOGE("Qsocket resp timed out after %" PRIu64 " ms",
+          LOGE("SEE resp timed out after %" PRIu64 " ms",
                Milliseconds(timeoutResp).getMilliseconds());
         } else if (mRespError != SNS_STD_ERROR_NO_ERROR) {
-          LOGE("Qsocket txn ID %" PRIu32 " failed with error %d",
+          LOGE("SEE txn ID %" PRIu32 " failed with error %d",
                mCurrentTxnId, mRespError);
         } else {
           success = true;
@@ -1607,7 +1611,7 @@
       prepareWaitForInd(suid, syncData, syncDataType);
     }
 
-    success = sendQsocketReqSync(client, msg.get(), timeoutResp);
+    success = sendSeeReqSync(client, msg.get(), timeoutResp);
 
     if (waitForIndication) {
       success = waitForInd(success, timeoutInd);
@@ -1641,7 +1645,7 @@
     }
 
     if (!waitSuccess) {
-      LOGE("QSocket indication timed out after %" PRIu64 " ms",
+      LOGE("SEE indication timed out after %" PRIu64 " ms",
            Milliseconds(timeoutInd).getMilliseconds());
       success = false;
     }
@@ -1656,21 +1660,21 @@
   return success;
 }
 
-void SeeHelper::qsocketIndCb(sns_client *client, void *msg,
-                             uint32_t msgLen, void *cbData) {
+void SeeHelper::seeIndCb(
+    sns_client *client, void *msg, uint32_t msgLen, void *cbData) {
   auto *obj = static_cast<SeeHelper *>(cbData);
   obj->handleSnsClientEventMsg(client, msg, msgLen);
 }
 
-void SeeHelper::qsocketRespCb(sns_client *client, sns_std_error error,
+void SeeHelper::seeRespCb(sns_client *client, sns_std_error error,
                               void *cbData) {
-  auto *respCbData = static_cast<QsocketRespCbData *>(cbData);
-  respCbData->seeHelper->handleQsocketResp(respCbData->txnId, error);
+  auto *respCbData = static_cast<SeeRespCbData *>(cbData);
+  respCbData->seeHelper->handleSeeResp(respCbData->txnId, error);
   memoryFree(cbData);
 }
 
-bool SeeHelper::registerSensor(SensorType sensorType, const sns_std_suid& suid,
-                               bool *prevRegistered) {
+bool SeeHelper::registerSensor(
+    SensorType sensorType, const sns_std_suid& suid, bool *prevRegistered) {
   CHRE_ASSERT(sensorType != SensorType::Unknown);
   CHRE_ASSERT(prevRegistered != nullptr);
   bool success = false;
@@ -1688,15 +1692,15 @@
     }
   }
 
-  // Initialize another Qsocket client if the SUID has been previously
-  // registered with more SensorTypes than the number of Qsocket clients can
+  // Initialize another SEE client if the SUID has been previously
+  // registered with more SensorTypes than the number of SEE clients can
   // disambiguate.
   bool clientAvailable = true;
-  if (mQsocketClients.size() <= suidRegCount) {
+  if (mSeeClients.size() <= suidRegCount) {
     sns_client *client;
     clientAvailable = waitForService(&client);
     if (clientAvailable) {
-      clientAvailable = mQsocketClients.push_back(client);
+      clientAvailable = mSeeClients.push_back(client);
     }
   }
 
@@ -1705,7 +1709,7 @@
     SensorInfo sensorInfo = {
       .suid = suid,
       .sensorType = sensorType,
-      .client = mQsocketClients[suidRegCount],
+      .client = mSeeClients[suidRegCount],
     };
     success = mSensorInfos.push_back(sensorInfo);
   }
@@ -1721,14 +1725,14 @@
   CHRE_ASSERT(client);
 
   // TODO: add error_cb and error_cb_data.
-  int status = sns_client_init(
+  int status = mSnsClientApi->sns_client_init(
       client, timeout.getMilliseconds(),
-      SeeHelper::qsocketIndCb, this /* ind_cb_data */,
+      SeeHelper::seeIndCb, this /* ind_cb_data */,
       nullptr /* error_cb */, nullptr /* error_cb_data */);
 
   bool success = (status == 0);
   if (!success) {
-    LOGE("Failed to initialize the sensor Qsocket client: %d", status);
+    LOGE("Failed to initialize the sensor client: %d", status);
   }
   return success;
 }