Refactor HidRawSensor::enable

Bug: 298450041
Test: Verify head tracking samples can be collected
Change-Id: Id629edec2f6d165ac12899b10a4b7595f6bb8e18
diff --git a/modules/sensors/dynamic_sensor/HidRawSensor.cpp b/modules/sensors/dynamic_sensor/HidRawSensor.cpp
index 939b58d..5baa006 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensor.cpp
+++ b/modules/sensors/dynamic_sensor/HidRawSensor.cpp
@@ -995,11 +995,22 @@
         return NO_ERROR;
     }
 
+    bool setLeAudioTransportOk = setLeAudioTransport(device, enable);
+    bool setPowerOk = setPower(device, enable);
+    bool setReportingOk = setReportingState(device, enable);
+    if (setPowerOk && setReportingOk && setLeAudioTransportOk) {
+        mEnabled = enable;
+        return NO_ERROR;
+    } else {
+        return INVALID_OPERATION;
+    }
+}
+
+bool HidRawSensor::setLeAudioTransport(const SP(HidDevice) &device, bool enable) {
     std::vector<uint8_t> buffer;
-    // TODO(b/298450041): Refactor the operations below in a separate function.
-    bool setLeAudioTransportOk = true;
+    bool success = true;
     if (mLeTransportId >= 0 && enable) {
-        setLeAudioTransportOk = false;
+        success = false;
         uint8_t id = static_cast<uint8_t>(mLeTransportId);
         if (device->getFeature(id, &buffer)
                 && (8 * buffer.size()) >=
@@ -1030,18 +1041,22 @@
 
             HidUtil::copyBits(&index, &(buffer[0]), buffer.size(), 0,
                               mLeTransportBitOffset, mLeTransportBitSize);
-            setLeAudioTransportOk = device->setFeature(id, buffer);
-            if (!setLeAudioTransportOk) {
+            success = device->setFeature(id, buffer);
+            if (!success) {
               LOG_E << "enable: setFeature VENDOR LE TRANSPORT failed" << LOG_ENDL;
             }
         } else {
             LOG_E << "enable: changing VENDOR LE TRANSPORT failed" << LOG_ENDL;
         }
     }
+    return success;
+}
 
-    bool setPowerOk = true;
+bool HidRawSensor::setPower(const SP(HidDevice) &device, bool enable) {
+    std::vector<uint8_t> buffer;
+    bool success = true;
     if (mPowerStateId >= 0) {
-        setPowerOk = false;
+        success = false;
         uint8_t id = static_cast<uint8_t>(mPowerStateId);
         if (device->getFeature(id, &buffer)
                 && (8 * buffer.size()) >=
@@ -1049,18 +1064,22 @@
             uint8_t index = enable ? mPowerStateOnIndex : mPowerStateOffIndex;
             HidUtil::copyBits(&index, &(buffer[0]), buffer.size(),
                               0, mPowerStateBitOffset, mPowerStateBitSize);
-            setPowerOk = device->setFeature(id, buffer);
-            if (!setPowerOk) {
+            success = device->setFeature(id, buffer);
+            if (!success) {
               LOG_E << "enable: setFeature POWER STATE failed" << LOG_ENDL;
             }
         } else {
             LOG_E << "enable: changing POWER STATE failed" << LOG_ENDL;
         }
     }
+    return success;
+}
 
-    bool setReportingOk = true;
+bool HidRawSensor::setReportingState(const SP(HidDevice) &device, bool enable) {
+    std::vector<uint8_t> buffer;
+    bool success = true;
     if (mReportingStateId >= 0) {
-        setReportingOk = false;
+        success = false;
         uint8_t id = static_cast<uint8_t>(mReportingStateId);
         if (device->getFeature(id, &buffer)
                 && (8 * buffer.size()) >
@@ -1069,21 +1088,15 @@
                                      mReportingStateDisableIndex;
             HidUtil::copyBits(&index, &(buffer[0]), buffer.size(),0,
                               mReportingStateBitOffset, mReportingStateBitSize);
-            setReportingOk = device->setFeature(id, buffer);
-            if (!setReportingOk) {
+            success = device->setFeature(id, buffer);
+            if (!success) {
               LOG_E << "enable: setFeature REPORTING STATE failed" << LOG_ENDL;
             }
         } else {
             LOG_E << "enable: changing REPORTING STATE failed" << LOG_ENDL;
         }
     }
-
-    if (setPowerOk && setReportingOk && setLeAudioTransportOk) {
-        mEnabled = enable;
-        return NO_ERROR;
-    } else {
-        return INVALID_OPERATION;
-    }
+    return success;
 }
 
 int HidRawSensor::batch(int64_t samplingPeriod, int64_t batchingPeriod) {
diff --git a/modules/sensors/dynamic_sensor/HidRawSensor.h b/modules/sensors/dynamic_sensor/HidRawSensor.h
index a9847c8..71d80b1 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensor.h
+++ b/modules/sensors/dynamic_sensor/HidRawSensor.h
@@ -149,6 +149,10 @@
     // process HID snesor spec defined orientation(quaternion) sensor usages.
     bool processQuaternionUsage(const std::vector<HidParser::ReportPacket> &packets);
 
+    bool setLeAudioTransport(const SP(HidDevice) &device, bool enable);
+    bool setPower(const SP(HidDevice) &device, bool enable);
+    bool setReportingState(const SP(HidDevice) &device, bool enable);
+
     // get the value of a report field
     template<typename ValueType>
     bool getReportFieldValue(const std::vector<uint8_t> &message,