Change expected return type in direct report VTS

If a sensor doesn't support a particular memory type for direct
reporting, then registerChannel will return an invalid channel handle.
When this handle is used in configureDirectReport, it will return
BAD_VALUE and when used in unregisterDirectReport, it will return OK.
Currently, the VTS tests assert it will return INVALID_OPERATION, but
that will only happen if the entire HAL doesn't support direct
reporting instead of a single sensor not supporting a certain memory
type.

Bug: 138758242
Test: Run VTS and verify DirectChannel* tests now pass
Change-Id: Ifba4262b68ec0c4ca6921dad40a03e0a52088d28
diff --git a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp
index 6ff393d..73789fe 100644
--- a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp
+++ b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp
@@ -181,14 +181,15 @@
     int32_t getInvalidSensorHandle();
     bool getDirectChannelSensor(SensorInfo* sensor, SharedMemType* memType, RateLevel* rate);
     void verifyDirectChannel(SharedMemType memType);
-    void verifyRegisterDirectChannel(const SensorInfo& sensor, SharedMemType memType,
-                                     std::shared_ptr<SensorsTestSharedMemory> mem,
-                                     int32_t* directChannelHandle);
+    void verifyRegisterDirectChannel(std::shared_ptr<SensorsTestSharedMemory> mem,
+                                     int32_t* directChannelHandle, bool supportsSharedMemType,
+                                     bool supportsAnyDirectChannel);
     void verifyConfigure(const SensorInfo& sensor, SharedMemType memType,
-                         int32_t directChannelHandle);
-    void verifyUnregisterDirectChannel(const SensorInfo& sensor, SharedMemType memType,
-                                       int32_t directChannelHandle);
+                         int32_t directChannelHandle, bool directChannelSupported);
+    void verifyUnregisterDirectChannel(int32_t directChannelHandle, bool directChannelSupported);
     void checkRateLevel(const SensorInfo& sensor, int32_t directChannelHandle, RateLevel rateLevel);
+    void queryDirectChannelSupport(SharedMemType memType, bool* supportsSharedMemType,
+                                   bool* supportsAnyDirectChannel);
 };
 
 Return<Result> SensorsHidlTest::activate(int32_t sensorHandle, bool enabled) {
@@ -885,14 +886,34 @@
                        });
 }
 
-void SensorsHidlTest::verifyRegisterDirectChannel(const SensorInfo& sensor, SharedMemType memType,
-                                                  std::shared_ptr<SensorsTestSharedMemory> mem,
-                                                  int32_t* directChannelHandle) {
+void SensorsHidlTest::queryDirectChannelSupport(SharedMemType memType, bool* supportsSharedMemType,
+                                                bool* supportsAnyDirectChannel) {
+    *supportsSharedMemType = false;
+    *supportsAnyDirectChannel = false;
+    for (const SensorInfo& curSensor : getSensorsList()) {
+        if (isDirectChannelTypeSupported(curSensor, memType)) {
+            *supportsSharedMemType = true;
+        }
+        if (isDirectChannelTypeSupported(curSensor, SharedMemType::ASHMEM) ||
+            isDirectChannelTypeSupported(curSensor, SharedMemType::GRALLOC)) {
+            *supportsAnyDirectChannel = true;
+        }
+
+        if (*supportsSharedMemType && *supportsAnyDirectChannel) {
+            break;
+        }
+    }
+}
+
+void SensorsHidlTest::verifyRegisterDirectChannel(std::shared_ptr<SensorsTestSharedMemory> mem,
+                                                  int32_t* directChannelHandle,
+                                                  bool supportsSharedMemType,
+                                                  bool supportsAnyDirectChannel) {
     char* buffer = mem->getBuffer();
     memset(buffer, 0xff, mem->getSize());
 
     registerDirectChannel(mem->getSharedMemInfo(), [&](Result result, int32_t channelHandle) {
-        if (isDirectChannelTypeSupported(sensor, memType)) {
+        if (supportsSharedMemType) {
             ASSERT_EQ(result, Result::OK);
             ASSERT_GT(channelHandle, 0);
 
@@ -901,7 +922,9 @@
                 ASSERT_EQ(buffer[i], 0x00);
             }
         } else {
-            ASSERT_EQ(result, Result::INVALID_OPERATION);
+            Result expectedResult =
+                    supportsAnyDirectChannel ? Result::BAD_VALUE : Result::INVALID_OPERATION;
+            ASSERT_EQ(result, expectedResult);
             ASSERT_EQ(channelHandle, -1);
         }
         *directChannelHandle = channelHandle;
@@ -909,7 +932,7 @@
 }
 
 void SensorsHidlTest::verifyConfigure(const SensorInfo& sensor, SharedMemType memType,
-                                      int32_t directChannelHandle) {
+                                      int32_t directChannelHandle, bool supportsAnyDirectChannel) {
     if (isDirectChannelTypeSupported(sensor, memType)) {
         // Verify that each rate level is properly supported
         checkRateLevel(sensor, directChannelHandle, RateLevel::NORMAL);
@@ -925,22 +948,22 @@
             -1 /* sensorHandle */, directChannelHandle, RateLevel::STOP,
             [](Result result, int32_t /* reportToken */) { ASSERT_EQ(result, Result::OK); });
     } else {
-        // Direct channel is not supported for this SharedMemType
+        // directChannelHandle will be -1 here, HAL should either reject it as a bad value if there
+        // is some level of direct channel report, otherwise return INVALID_OPERATION if direct
+        // channel is not supported at all
+        Result expectedResult =
+                supportsAnyDirectChannel ? Result::BAD_VALUE : Result::INVALID_OPERATION;
         configDirectReport(sensor.sensorHandle, directChannelHandle, RateLevel::NORMAL,
-                           [](Result result, int32_t /* reportToken */) {
-                               ASSERT_EQ(result, Result::INVALID_OPERATION);
+                           [expectedResult](Result result, int32_t /* reportToken */) {
+                               ASSERT_EQ(result, expectedResult);
                            });
     }
 }
 
-void SensorsHidlTest::verifyUnregisterDirectChannel(const SensorInfo& sensor, SharedMemType memType,
-                                                    int32_t directChannelHandle) {
-    Result result = unregisterDirectChannel(directChannelHandle);
-    if (isDirectChannelTypeSupported(sensor, memType)) {
-        ASSERT_EQ(result, Result::OK);
-    } else {
-        ASSERT_EQ(result, Result::INVALID_OPERATION);
-    }
+void SensorsHidlTest::verifyUnregisterDirectChannel(int32_t directChannelHandle,
+                                                    bool supportsAnyDirectChannel) {
+    Result expectedResult = supportsAnyDirectChannel ? Result::OK : Result::INVALID_OPERATION;
+    ASSERT_EQ(unregisterDirectChannel(directChannelHandle), expectedResult);
 }
 
 void SensorsHidlTest::verifyDirectChannel(SharedMemType memType) {
@@ -951,11 +974,16 @@
         SensorsTestSharedMemory::create(memType, kMemSize));
     ASSERT_NE(mem, nullptr);
 
+    bool supportsSharedMemType;
+    bool supportsAnyDirectChannel;
+    queryDirectChannelSupport(memType, &supportsSharedMemType, &supportsAnyDirectChannel);
+
     for (const SensorInfo& sensor : getSensorsList()) {
         int32_t directChannelHandle = 0;
-        verifyRegisterDirectChannel(sensor, memType, mem, &directChannelHandle);
-        verifyConfigure(sensor, memType, directChannelHandle);
-        verifyUnregisterDirectChannel(sensor, memType, directChannelHandle);
+        verifyRegisterDirectChannel(mem, &directChannelHandle, supportsSharedMemType,
+                                    supportsAnyDirectChannel);
+        verifyConfigure(sensor, memType, directChannelHandle, supportsAnyDirectChannel);
+        verifyUnregisterDirectChannel(directChannelHandle, supportsAnyDirectChannel);
     }
 }