[automerge] check all active ports 2p: 3c4b22cef2

Original change: https://googleplex-android-review.googlesource.com/c/platform/hardware/interfaces/+/17709036

Bug: 226018537
Change-Id: Ibff54481edf0b7b936f330ce3346ad23149dd1bd
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h b/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
index 892b406..34b2b24 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
@@ -17,6 +17,7 @@
 #ifndef android_hardware_automotive_vehicle_aidl_impl_fake_impl_hardware_include_FakeVehicleHardware_H_
 #define android_hardware_automotive_vehicle_aidl_impl_fake_impl_hardware_include_FakeVehicleHardware_H_
 
+#include <ConcurrentQueue.h>
 #include <DefaultConfig.h>
 #include <FakeObd2Frame.h>
 #include <FakeUserHal.h>
@@ -48,6 +49,8 @@
 
     explicit FakeVehicleHardware(std::unique_ptr<VehiclePropValuePool> valuePool);
 
+    ~FakeVehicleHardware();
+
     // Get all the property configs.
     std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>
     getAllPropertyConfigs() const override;
@@ -102,6 +105,29 @@
     // Expose private methods to unit test.
     friend class FakeVehicleHardwareTestHelper;
 
+    template <class CallbackType, class RequestType>
+    struct RequestWithCallback {
+        RequestType request;
+        std::shared_ptr<const CallbackType> callback;
+    };
+
+    template <class CallbackType, class RequestType>
+    class PendingRequestHandler {
+      public:
+        PendingRequestHandler(FakeVehicleHardware* hardware);
+
+        void addRequest(RequestType request, std::shared_ptr<const CallbackType> callback);
+
+        void stop();
+
+      private:
+        FakeVehicleHardware* mHardware;
+        std::thread mThread;
+        ConcurrentQueue<RequestWithCallback<CallbackType, RequestType>> mRequests;
+
+        void handleRequestsOnce();
+    };
+
     const std::unique_ptr<obd2frame::FakeObd2Frame> mFakeObd2Frame;
     const std::unique_ptr<FakeUserHal> mFakeUserHal;
     // RecurrentTimer is thread-safe.
@@ -111,6 +137,13 @@
     std::unique_ptr<const PropertySetErrorCallback> mOnPropertySetErrorCallback GUARDED_BY(mLock);
     std::unordered_map<PropIdAreaId, std::shared_ptr<RecurrentTimer::Callback>, PropIdAreaIdHash>
             mRecurrentActions GUARDED_BY(mLock);
+    // PendingRequestHandler is thread-safe.
+    mutable PendingRequestHandler<GetValuesCallback,
+                                  aidl::android::hardware::automotive::vehicle::GetValueRequest>
+            mPendingGetValueRequests;
+    mutable PendingRequestHandler<SetValuesCallback,
+                                  aidl::android::hardware::automotive::vehicle::SetValueRequest>
+            mPendingSetValueRequests;
 
     void init();
     // Stores the initial value to property store.
@@ -170,6 +203,10 @@
 
     android::base::Result<void> checkArgumentsSize(const std::vector<std::string>& options,
                                                    size_t minSize);
+    aidl::android::hardware::automotive::vehicle::GetValueResult handleGetValueRequest(
+            const aidl::android::hardware::automotive::vehicle::GetValueRequest& request);
+    aidl::android::hardware::automotive::vehicle::SetValueResult handleSetValueRequest(
+            const aidl::android::hardware::automotive::vehicle::SetValueRequest& request);
 };
 
 }  // namespace fake
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
index 8f8cc5c..7b3de58 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
@@ -139,10 +139,17 @@
       mServerSidePropStore(new VehiclePropertyStore(mValuePool)),
       mFakeObd2Frame(new obd2frame::FakeObd2Frame(mServerSidePropStore)),
       mFakeUserHal(new FakeUserHal(mValuePool)),
-      mRecurrentTimer(new RecurrentTimer()) {
+      mRecurrentTimer(new RecurrentTimer()),
+      mPendingGetValueRequests(this),
+      mPendingSetValueRequests(this) {
     init();
 }
 
+FakeVehicleHardware::~FakeVehicleHardware() {
+    mPendingGetValueRequests.stop();
+    mPendingSetValueRequests.stop();
+}
+
 void FakeVehicleHardware::init() {
     for (auto& it : defaultconfig::getDefaultConfigs()) {
         VehiclePropConfig cfg = it.config;
@@ -427,37 +434,25 @@
 
 StatusCode FakeVehicleHardware::setValues(std::shared_ptr<const SetValuesCallback> callback,
                                           const std::vector<SetValueRequest>& requests) {
-    std::vector<SetValueResult> results;
     for (auto& request : requests) {
-        const VehiclePropValue& value = request.value;
-        int propId = value.prop;
-
         if (FAKE_VEHICLEHARDWARE_DEBUG) {
-            ALOGD("Set value for property ID: %d", propId);
+            ALOGD("Set value for property ID: %d", request.value.prop);
         }
 
-        SetValueResult setValueResult;
-        setValueResult.requestId = request.requestId;
-
-        if (auto result = setValue(value); !result.ok()) {
-            ALOGE("failed to set value, error: %s, code: %d", getErrorMsg(result).c_str(),
-                  getIntErrorCode(result));
-            setValueResult.status = getErrorCode(result);
-        } else {
-            setValueResult.status = StatusCode::OK;
-        }
-
-        results.push_back(std::move(setValueResult));
+        // In a real VHAL implementation, you could either send the setValue request to vehicle bus
+        // here in the binder thread, or you could send the request in setValue which runs in
+        // the handler thread. If you decide to send the setValue request here, you should not
+        // wait for the response here and the handler thread should handle the setValue response.
+        mPendingSetValueRequests.addRequest(request, callback);
     }
 
-    // In the real vhal, the values will be sent to Car ECU. We just pretend it is done here and
-    // send back the updated property values to client.
-    (*callback)(std::move(results));
-
     return StatusCode::OK;
 }
 
 VhalResult<void> FakeVehicleHardware::setValue(const VehiclePropValue& value) {
+    // In a real VHAL implementation, this will send the request to vehicle bus if not already
+    // sent in setValues, and wait for the response from vehicle bus.
+    // Here we are just updating mValuePool.
     bool isSpecialValue = false;
     auto setSpecialValueResult = maybeSetSpecialValue(value, &isSpecialValue);
 
@@ -484,41 +479,59 @@
     return {};
 }
 
-StatusCode FakeVehicleHardware::getValues(std::shared_ptr<const GetValuesCallback> callback,
-                                          const std::vector<GetValueRequest>& requests) const {
-    std::vector<GetValueResult> results;
-    for (auto& request : requests) {
-        const VehiclePropValue& value = request.prop;
+SetValueResult FakeVehicleHardware::handleSetValueRequest(const SetValueRequest& request) {
+    SetValueResult setValueResult;
+    setValueResult.requestId = request.requestId;
 
-        if (FAKE_VEHICLEHARDWARE_DEBUG) {
-            ALOGD("getValues(%d)", value.prop);
-        }
-
-        GetValueResult getValueResult;
-        getValueResult.requestId = request.requestId;
-
-        auto result = getValue(value);
-        if (!result.ok()) {
-            ALOGE("failed to get value, error: %s, code: %d", getErrorMsg(result).c_str(),
-                  getIntErrorCode(result));
-            getValueResult.status = getErrorCode(result);
-        } else {
-            getValueResult.status = StatusCode::OK;
-            getValueResult.prop = *result.value();
-        }
-        results.push_back(std::move(getValueResult));
+    if (auto result = setValue(request.value); !result.ok()) {
+        ALOGE("failed to set value, error: %s, code: %d", getErrorMsg(result).c_str(),
+              getIntErrorCode(result));
+        setValueResult.status = getErrorCode(result);
+    } else {
+        setValueResult.status = StatusCode::OK;
     }
 
-    // In a real VHAL implementation, getValue would be async and we would call the callback after
-    // we actually received the values from vehicle bus. Here we are getting the result
-    // synchronously so we could call the callback here.
-    (*callback)(std::move(results));
+    return setValueResult;
+}
+
+StatusCode FakeVehicleHardware::getValues(std::shared_ptr<const GetValuesCallback> callback,
+                                          const std::vector<GetValueRequest>& requests) const {
+    for (auto& request : requests) {
+        if (FAKE_VEHICLEHARDWARE_DEBUG) {
+            ALOGD("getValues(%d)", request.prop.prop);
+        }
+
+        // In a real VHAL implementation, you could either send the getValue request to vehicle bus
+        // here in the binder thread, or you could send the request in getValue which runs in
+        // the handler thread. If you decide to send the getValue request here, you should not
+        // wait for the response here and the handler thread should handle the getValue response.
+        mPendingGetValueRequests.addRequest(request, callback);
+    }
 
     return StatusCode::OK;
 }
 
+GetValueResult FakeVehicleHardware::handleGetValueRequest(const GetValueRequest& request) {
+    GetValueResult getValueResult;
+    getValueResult.requestId = request.requestId;
+
+    auto result = getValue(request.prop);
+    if (!result.ok()) {
+        ALOGE("failed to get value, error: %s, code: %d", getErrorMsg(result).c_str(),
+              getIntErrorCode(result));
+        getValueResult.status = getErrorCode(result);
+    } else {
+        getValueResult.status = StatusCode::OK;
+        getValueResult.prop = *result.value();
+    }
+    return getValueResult;
+}
+
 FakeVehicleHardware::ValueResultType FakeVehicleHardware::getValue(
         const VehiclePropValue& value) const {
+    // In a real VHAL implementation, this will send the request to vehicle bus if not already
+    // sent in getValues, and wait for the response from vehicle bus.
+    // Here we are just reading value from mValuePool.
     bool isSpecialValue = false;
     auto result = maybeGetSpecialValue(value, &isSpecialValue);
     if (isSpecialValue) {
@@ -564,6 +577,12 @@
         result.buffer = dumpSpecificProperty(options);
     } else if (EqualsIgnoreCase(option, "--set")) {
         result.buffer = dumpSetProperties(options);
+    } else if (EqualsIgnoreCase(option, kUserHalDumpOption)) {
+        if (options.size() == 1) {
+            result.buffer = mFakeUserHal->showDumpHelp();
+        } else {
+            result.buffer = mFakeUserHal->dump(options[1]);
+        }
     } else {
         result.buffer = StringPrintf("Invalid option: %s\n", option.c_str());
     }
@@ -581,7 +600,9 @@
            "[-b BYTES_VALUE] [-a AREA_ID] : sets the value of property PROP. "
            "Notice that the string, bytes and area value can be set just once, while the other can"
            " have multiple values (so they're used in the respective array), "
-           "BYTES_VALUE is in the form of 0xXXXX, e.g. 0xdeadbeef.\n";
+           "BYTES_VALUE is in the form of 0xXXXX, e.g. 0xdeadbeef.\n\n"
+           "Fake user HAL usage: \n" +
+           mFakeUserHal->showDumpHelp();
 }
 
 std::string FakeVehicleHardware::dumpAllProperties() {
@@ -971,6 +992,60 @@
     return bytes;
 }
 
+template <class CallbackType, class RequestType>
+FakeVehicleHardware::PendingRequestHandler<CallbackType, RequestType>::PendingRequestHandler(
+        FakeVehicleHardware* hardware)
+    : mHardware(hardware), mThread([this] {
+          while (mRequests.waitForItems()) {
+              handleRequestsOnce();
+          }
+      }) {}
+
+template <class CallbackType, class RequestType>
+void FakeVehicleHardware::PendingRequestHandler<CallbackType, RequestType>::addRequest(
+        RequestType request, std::shared_ptr<const CallbackType> callback) {
+    mRequests.push({
+            request,
+            callback,
+    });
+}
+
+template <class CallbackType, class RequestType>
+void FakeVehicleHardware::PendingRequestHandler<CallbackType, RequestType>::stop() {
+    mRequests.deactivate();
+    if (mThread.joinable()) {
+        mThread.join();
+    }
+}
+
+template <>
+void FakeVehicleHardware::PendingRequestHandler<FakeVehicleHardware::GetValuesCallback,
+                                                GetValueRequest>::handleRequestsOnce() {
+    std::unordered_map<std::shared_ptr<const GetValuesCallback>, std::vector<GetValueResult>>
+            callbackToResults;
+    for (const auto& rwc : mRequests.flush()) {
+        auto result = mHardware->handleGetValueRequest(rwc.request);
+        callbackToResults[rwc.callback].push_back(std::move(result));
+    }
+    for (const auto& [callback, results] : callbackToResults) {
+        (*callback)(std::move(results));
+    }
+}
+
+template <>
+void FakeVehicleHardware::PendingRequestHandler<FakeVehicleHardware::SetValuesCallback,
+                                                SetValueRequest>::handleRequestsOnce() {
+    std::unordered_map<std::shared_ptr<const SetValuesCallback>, std::vector<SetValueResult>>
+            callbackToResults;
+    for (const auto& rwc : mRequests.flush()) {
+        auto result = mHardware->handleSetValueRequest(rwc.request);
+        callbackToResults[rwc.callback].push_back(std::move(result));
+    }
+    for (const auto& [callback, results] : callbackToResults) {
+        (*callback)(std::move(results));
+    }
+}
+
 }  // namespace fake
 }  // namespace vehicle
 }  // namespace automotive
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp b/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp
index 8839c18..3e8f634 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp
@@ -34,6 +34,8 @@
 #include <inttypes.h>
 #include <chrono>
 #include <condition_variable>
+#include <unordered_map>
+#include <unordered_set>
 #include <vector>
 
 namespace android {
@@ -99,11 +101,51 @@
     FakeVehicleHardware* getHardware() { return &mHardware; }
 
     StatusCode setValues(const std::vector<SetValueRequest>& requests) {
-        return getHardware()->setValues(mSetValuesCallback, requests);
+        {
+            std::scoped_lock<std::mutex> lockGuard(mLock);
+            for (const auto& request : requests) {
+                mPendingSetValueRequests.insert(request.requestId);
+            }
+        }
+        if (StatusCode status = getHardware()->setValues(mSetValuesCallback, requests);
+            status != StatusCode::OK) {
+            return status;
+        }
+        std::unique_lock<std::mutex> lk(mLock);
+        // Wait for the onSetValueResults.
+        bool result = mCv.wait_for(lk, milliseconds(1000), [this] {
+            ScopedLockAssertion lockAssertion(mLock);
+            return mPendingSetValueRequests.size() == 0;
+        });
+        if (!result) {
+            ALOGE("wait for callbacks for setValues timed-out");
+            return StatusCode::INTERNAL_ERROR;
+        }
+        return StatusCode::OK;
     }
 
     StatusCode getValues(const std::vector<GetValueRequest>& requests) {
-        return getHardware()->getValues(mGetValuesCallback, requests);
+        {
+            std::scoped_lock<std::mutex> lockGuard(mLock);
+            for (const auto& request : requests) {
+                mPendingGetValueRequests.insert(request.requestId);
+            }
+        }
+        if (StatusCode status = getHardware()->getValues(mGetValuesCallback, requests);
+            status != StatusCode::OK) {
+            return status;
+        }
+        std::unique_lock<std::mutex> lk(mLock);
+        // Wait for the onGetValueResults.
+        bool result = mCv.wait_for(lk, milliseconds(1000), [this] {
+            ScopedLockAssertion lockAssertion(mLock);
+            return mPendingGetValueRequests.size() == 0;
+        });
+        if (!result) {
+            ALOGE("wait for callbacks for getValues timed-out");
+            return StatusCode::INTERNAL_ERROR;
+        }
+        return StatusCode::OK;
     }
 
     StatusCode setValue(const VehiclePropValue& value) {
@@ -167,7 +209,9 @@
         std::scoped_lock<std::mutex> lockGuard(mLock);
         for (auto& result : results) {
             mSetValueResults.push_back(result);
+            mPendingSetValueRequests.erase(result.requestId);
         }
+        mCv.notify_all();
     }
 
     const std::vector<SetValueResult>& getSetValueResults() {
@@ -179,7 +223,9 @@
         std::scoped_lock<std::mutex> lockGuard(mLock);
         for (auto& result : results) {
             mGetValueResults.push_back(result);
+            mPendingGetValueRequests.erase(result.requestId);
         }
+        mCv.notify_all();
     }
 
     const std::vector<GetValueResult>& getGetValueResults() {
@@ -197,7 +243,7 @@
             };
             mEventCount[propIdAreaId]++;
         }
-        mCv.notify_one();
+        mCv.notify_all();
     }
 
     const std::vector<VehiclePropValue>& getChangedProperties() {
@@ -295,6 +341,8 @@
     std::vector<SetValueResult> mSetValueResults GUARDED_BY(mLock);
     std::vector<GetValueResult> mGetValueResults GUARDED_BY(mLock);
     std::vector<VehiclePropValue> mChangedProperties GUARDED_BY(mLock);
+    std::unordered_set<int64_t> mPendingSetValueRequests GUARDED_BY(mLock);
+    std::unordered_set<int64_t> mPendingGetValueRequests GUARDED_BY(mLock);
 };
 
 TEST_F(FakeVehicleHardwareTest, testGetAllPropertyConfigs) {
@@ -1357,6 +1405,28 @@
     ASSERT_THAT(result.buffer, ContainsRegex("Invalid option: --invalid"));
 }
 
+TEST_F(FakeVehicleHardwareTest, testDumpFakeUserHalHelp) {
+    std::vector<std::string> options;
+    options.push_back("--user-hal");
+
+    DumpResult result = getHardware()->dump(options);
+    ASSERT_FALSE(result.callerShouldDumpState);
+    ASSERT_NE(result.buffer, "");
+    ASSERT_THAT(result.buffer, ContainsRegex("dumps state used for user management"));
+}
+
+TEST_F(FakeVehicleHardwareTest, testDumpFakeUserHal) {
+    std::vector<std::string> options;
+    options.push_back("--user-hal");
+    // Indent: " ".
+    options.push_back(" ");
+
+    DumpResult result = getHardware()->dump(options);
+    ASSERT_FALSE(result.callerShouldDumpState);
+    ASSERT_NE(result.buffer, "");
+    ASSERT_THAT(result.buffer, ContainsRegex(" No InitialUserInfo response\n"));
+}
+
 struct SetPropTestCase {
     std::string test_name;
     std::vector<std::string> options;
diff --git a/sensors/aidl/default/multihal/HalProxyAidl.cpp b/sensors/aidl/default/multihal/HalProxyAidl.cpp
index 327db12..628914c 100644
--- a/sensors/aidl/default/multihal/HalProxyAidl.cpp
+++ b/sensors/aidl/default/multihal/HalProxyAidl.cpp
@@ -29,6 +29,7 @@
 using ::aidl::android::hardware::sensors::ISensors;
 using ::aidl::android::hardware::sensors::ISensorsCallback;
 using ::android::hardware::sensors::V2_1::implementation::convertToOldEvent;
+using ::ndk::ScopedAStatus;
 
 namespace aidl {
 namespace android {
@@ -36,19 +37,22 @@
 namespace sensors {
 namespace implementation {
 
-static binder_status_t resultToBinderStatus(::android::hardware::sensors::V1_0::Result result) {
-    switch (result) {
-        case ::android::hardware::sensors::V1_0::Result::OK:
-            return STATUS_OK;
-        case ::android::hardware::sensors::V1_0::Result::PERMISSION_DENIED:
-            return STATUS_PERMISSION_DENIED;
-        case ::android::hardware::sensors::V1_0::Result::NO_MEMORY:
-            return STATUS_NO_MEMORY;
-        case ::android::hardware::sensors::V1_0::Result::BAD_VALUE:
-            return STATUS_BAD_VALUE;
-        case ::android::hardware::sensors::V1_0::Result::INVALID_OPERATION:
-            return STATUS_INVALID_OPERATION;
-    }
+static ScopedAStatus
+resultToAStatus(::android::hardware::sensors::V1_0::Result result) {
+  switch (result) {
+  case ::android::hardware::sensors::V1_0::Result::OK:
+    return ScopedAStatus::ok();
+  case ::android::hardware::sensors::V1_0::Result::PERMISSION_DENIED:
+    return ScopedAStatus::fromExceptionCode(EX_SECURITY);
+  case ::android::hardware::sensors::V1_0::Result::NO_MEMORY:
+    return ScopedAStatus::fromServiceSpecificError(ISensors::ERROR_NO_MEMORY);
+  case ::android::hardware::sensors::V1_0::Result::BAD_VALUE:
+    return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+  case ::android::hardware::sensors::V1_0::Result::INVALID_OPERATION:
+    return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+  default:
+    return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
+  }
 }
 
 static ::android::hardware::sensors::V1_0::RateLevel convertRateLevel(
@@ -62,6 +66,8 @@
             return ::android::hardware::sensors::V1_0::RateLevel::FAST;
         case ISensors::RateLevel::VERY_FAST:
             return ::android::hardware::sensors::V1_0::RateLevel::VERY_FAST;
+        default:
+          assert(false);
     }
 }
 
@@ -72,6 +78,8 @@
             return ::android::hardware::sensors::V1_0::OperationMode::NORMAL;
         case ISensors::OperationMode::DATA_INJECTION:
             return ::android::hardware::sensors::V1_0::OperationMode::DATA_INJECTION;
+        default:
+          assert(false);
     }
 }
 
@@ -82,6 +90,8 @@
             return ::android::hardware::sensors::V1_0::SharedMemType::ASHMEM;
         case ISensors::SharedMemInfo::SharedMemType::GRALLOC:
             return ::android::hardware::sensors::V1_0::SharedMemType::GRALLOC;
+        default:
+          assert(false);
     }
 }
 
@@ -90,6 +100,8 @@
     switch (sharedMemFormat) {
         case ISensors::SharedMemInfo::SharedMemFormat::SENSORS_EVENT:
             return ::android::hardware::sensors::V1_0::SharedMemFormat::SENSORS_EVENT;
+        default:
+          assert(false);
     }
 }
 
@@ -104,106 +116,121 @@
     return v1SharedMemInfo;
 }
 
-::ndk::ScopedAStatus HalProxyAidl::activate(int32_t in_sensorHandle, bool in_enabled) {
-    return ndk::ScopedAStatus::fromStatus(
-            resultToBinderStatus(HalProxy::activate(in_sensorHandle, in_enabled)));
+ScopedAStatus HalProxyAidl::activate(int32_t in_sensorHandle, bool in_enabled) {
+  return resultToAStatus(HalProxy::activate(in_sensorHandle, in_enabled));
 }
 
-::ndk::ScopedAStatus HalProxyAidl::batch(int32_t in_sensorHandle, int64_t in_samplingPeriodNs,
-                                         int64_t in_maxReportLatencyNs) {
-    return ndk::ScopedAStatus::fromStatus(resultToBinderStatus(
-            HalProxy::batch(in_sensorHandle, in_samplingPeriodNs, in_maxReportLatencyNs)));
+ScopedAStatus HalProxyAidl::batch(int32_t in_sensorHandle,
+                                  int64_t in_samplingPeriodNs,
+                                  int64_t in_maxReportLatencyNs) {
+  return resultToAStatus(HalProxy::batch(in_sensorHandle, in_samplingPeriodNs,
+                                         in_maxReportLatencyNs));
 }
 
-::ndk::ScopedAStatus HalProxyAidl::configDirectReport(int32_t in_sensorHandle,
-                                                      int32_t in_channelHandle,
-                                                      ISensors::RateLevel in_rate,
-                                                      int32_t* _aidl_return) {
-    binder_status_t binderStatus;
-    HalProxy::configDirectReport(
-            in_sensorHandle, in_channelHandle, convertRateLevel(in_rate),
-            [&binderStatus, _aidl_return](::android::hardware::sensors::V1_0::Result result,
-                                          int32_t reportToken) {
-                binderStatus = resultToBinderStatus(result);
-                *_aidl_return = reportToken;
-            });
-    return ndk::ScopedAStatus::fromStatus(binderStatus);
+ScopedAStatus HalProxyAidl::configDirectReport(int32_t in_sensorHandle,
+                                               int32_t in_channelHandle,
+                                               ISensors::RateLevel in_rate,
+                                               int32_t *_aidl_return) {
+  ScopedAStatus status =
+      ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+  HalProxy::configDirectReport(
+      in_sensorHandle, in_channelHandle, convertRateLevel(in_rate),
+      [&status, _aidl_return](::android::hardware::sensors::V1_0::Result result,
+                              int32_t reportToken) {
+        status = resultToAStatus(result);
+        *_aidl_return = reportToken;
+      });
+
+  if (!status.isOk()) {
+    *_aidl_return = -1;
+  }
+
+  return status;
 }
 
-::ndk::ScopedAStatus HalProxyAidl::flush(int32_t in_sensorHandle) {
-    return ndk::ScopedAStatus::fromStatus(resultToBinderStatus(HalProxy::flush(in_sensorHandle)));
+ScopedAStatus HalProxyAidl::flush(int32_t in_sensorHandle) {
+  return resultToAStatus(HalProxy::flush(in_sensorHandle));
 }
 
-::ndk::ScopedAStatus HalProxyAidl::getSensorsList(
-        std::vector<::aidl::android::hardware::sensors::SensorInfo>* _aidl_return) {
-    for (const auto& sensor : HalProxy::getSensors()) {
-        _aidl_return->push_back(convertSensorInfo(sensor.second));
-    }
-    return ndk::ScopedAStatus::ok();
+ScopedAStatus HalProxyAidl::getSensorsList(
+    std::vector<::aidl::android::hardware::sensors::SensorInfo> *_aidl_return) {
+  for (const auto &sensor : HalProxy::getSensors()) {
+    _aidl_return->push_back(convertSensorInfo(sensor.second));
+  }
+  return ScopedAStatus::ok();
 }
 
-::ndk::ScopedAStatus HalProxyAidl::initialize(
-        const MQDescriptor<::aidl::android::hardware::sensors::Event, SynchronizedReadWrite>&
-                in_eventQueueDescriptor,
-        const MQDescriptor<int32_t, SynchronizedReadWrite>& in_wakeLockDescriptor,
-        const std::shared_ptr<ISensorsCallback>& in_sensorsCallback) {
-    ::android::sp<::android::hardware::sensors::V2_1::implementation::ISensorsCallbackWrapperBase>
-            dynamicCallback = new ISensorsCallbackWrapperAidl(in_sensorsCallback);
+ScopedAStatus HalProxyAidl::initialize(
+    const MQDescriptor<::aidl::android::hardware::sensors::Event,
+                       SynchronizedReadWrite> &in_eventQueueDescriptor,
+    const MQDescriptor<int32_t, SynchronizedReadWrite> &in_wakeLockDescriptor,
+    const std::shared_ptr<ISensorsCallback> &in_sensorsCallback) {
+  ::android::sp<::android::hardware::sensors::V2_1::implementation::
+                    ISensorsCallbackWrapperBase>
+      dynamicCallback = new ISensorsCallbackWrapperAidl(in_sensorsCallback);
 
-    auto aidlEventQueue =
-            std::make_unique<::android::AidlMessageQueue<::aidl::android::hardware::sensors::Event,
-                                                         SynchronizedReadWrite>>(
-                    in_eventQueueDescriptor, true /* resetPointers */);
-    std::unique_ptr<
-            ::android::hardware::sensors::V2_1::implementation::EventMessageQueueWrapperBase>
-            eventQueue = std::make_unique<EventMessageQueueWrapperAidl>(aidlEventQueue);
+  auto aidlEventQueue = std::make_unique<::android::AidlMessageQueue<
+      ::aidl::android::hardware::sensors::Event, SynchronizedReadWrite>>(
+      in_eventQueueDescriptor, true /* resetPointers */);
+  std::unique_ptr<::android::hardware::sensors::V2_1::implementation::
+                      EventMessageQueueWrapperBase>
+      eventQueue =
+          std::make_unique<EventMessageQueueWrapperAidl>(aidlEventQueue);
 
-    auto aidlWakeLockQueue =
-            std::make_unique<::android::AidlMessageQueue<int32_t, SynchronizedReadWrite>>(
-                    in_wakeLockDescriptor, true /* resetPointers */);
-    std::unique_ptr<
-            ::android::hardware::sensors::V2_1::implementation::WakeLockMessageQueueWrapperBase>
-            wakeLockQueue = std::make_unique<WakeLockMessageQueueWrapperAidl>(aidlWakeLockQueue);
+  auto aidlWakeLockQueue = std::make_unique<
+      ::android::AidlMessageQueue<int32_t, SynchronizedReadWrite>>(
+      in_wakeLockDescriptor, true /* resetPointers */);
+  std::unique_ptr<::android::hardware::sensors::V2_1::implementation::
+                      WakeLockMessageQueueWrapperBase>
+      wakeLockQueue =
+          std::make_unique<WakeLockMessageQueueWrapperAidl>(aidlWakeLockQueue);
 
-    return ndk::ScopedAStatus::fromStatus(
-            resultToBinderStatus(initializeCommon(eventQueue, wakeLockQueue, dynamicCallback)));
+  return resultToAStatus(
+      initializeCommon(eventQueue, wakeLockQueue, dynamicCallback));
 }
 
-::ndk::ScopedAStatus HalProxyAidl::injectSensorData(
-        const ::aidl::android::hardware::sensors::Event& in_event) {
-    ::android::hardware::sensors::V2_1::Event hidlEvent;
-    convertToHidlEvent(in_event, &hidlEvent);
-    return ndk::ScopedAStatus::fromStatus(
-            resultToBinderStatus(HalProxy::injectSensorData(convertToOldEvent(hidlEvent))));
+ScopedAStatus HalProxyAidl::injectSensorData(
+    const ::aidl::android::hardware::sensors::Event &in_event) {
+  ::android::hardware::sensors::V2_1::Event hidlEvent;
+  convertToHidlEvent(in_event, &hidlEvent);
+  return resultToAStatus(
+      HalProxy::injectSensorData(convertToOldEvent(hidlEvent)));
 }
 
-::ndk::ScopedAStatus HalProxyAidl::registerDirectChannel(const ISensors::SharedMemInfo& in_mem,
-                                                         int32_t* _aidl_return) {
-    binder_status_t binderStatus;
-    ::android::hardware::sensors::V1_0::SharedMemInfo sharedMemInfo = convertSharedMemInfo(in_mem);
+ScopedAStatus
+HalProxyAidl::registerDirectChannel(const ISensors::SharedMemInfo &in_mem,
+                                    int32_t *_aidl_return) {
+  ScopedAStatus status =
+      ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+  ::android::hardware::sensors::V1_0::SharedMemInfo sharedMemInfo =
+      convertSharedMemInfo(in_mem);
 
-    HalProxy::registerDirectChannel(
-            sharedMemInfo,
-            [&binderStatus, _aidl_return](::android::hardware::sensors::V1_0::Result result,
-                                          int32_t reportToken) {
-                binderStatus = resultToBinderStatus(result);
-                *_aidl_return = reportToken;
-            });
+  HalProxy::registerDirectChannel(
+      sharedMemInfo,
+      [&status, _aidl_return](::android::hardware::sensors::V1_0::Result result,
+                              int32_t reportToken) {
+        status = resultToAStatus(result);
+        *_aidl_return = reportToken;
+      });
 
-    native_handle_delete(
-            const_cast<native_handle_t*>(sharedMemInfo.memoryHandle.getNativeHandle()));
-    return ndk::ScopedAStatus::fromStatus(binderStatus);
+  native_handle_delete(const_cast<native_handle_t *>(
+      sharedMemInfo.memoryHandle.getNativeHandle()));
+
+  if (!status.isOk()) {
+    *_aidl_return = -1;
+  }
+
+  return status;
 }
 
-::ndk::ScopedAStatus HalProxyAidl::setOperationMode(
-        ::aidl::android::hardware::sensors::ISensors::OperationMode in_mode) {
-    return ndk::ScopedAStatus::fromStatus(
-            resultToBinderStatus(HalProxy::setOperationMode(convertOperationMode(in_mode))));
+ScopedAStatus HalProxyAidl::setOperationMode(
+    ::aidl::android::hardware::sensors::ISensors::OperationMode in_mode) {
+  return resultToAStatus(
+      HalProxy::setOperationMode(convertOperationMode(in_mode)));
 }
 
-::ndk::ScopedAStatus HalProxyAidl::unregisterDirectChannel(int32_t in_channelHandle) {
-    return ndk::ScopedAStatus::fromStatus(
-            resultToBinderStatus(HalProxy::unregisterDirectChannel(in_channelHandle)));
+ScopedAStatus HalProxyAidl::unregisterDirectChannel(int32_t in_channelHandle) {
+  return resultToAStatus(HalProxy::unregisterDirectChannel(in_channelHandle));
 }
 
 binder_status_t HalProxyAidl::dump(int fd, const char ** /* args */,