Remove implementation-specific code for FMQ from libhidl

Bug: 31550267
Test: Built and run FMQ unit tests/benchmarks

Change-Id: Iaeefeeea70e7a54dce3889e6543d1493a189b870
diff --git a/MQDescriptor.cpp b/MQDescriptor.cpp
index 8834ab4..e768589 100644
--- a/MQDescriptor.cpp
+++ b/MQDescriptor.cpp
@@ -66,37 +66,6 @@
     }
 }
 
-/*
- * TODO: Modify design to send access flags over WireMQDescriptor
- * as well and use the same for the mmap call.
- */
-void* MQDescriptor::mapGrantorDescr(uint32_t grantor_idx) {
-  const native_handle_t* handle = mHandle;
-  int fdIndex = mGrantors[grantor_idx].fdIndex;
-  /*
-   * Offset for mmap must be a multiple of PAGE_SIZE.
-   */
-  int mapOffset = (mGrantors[grantor_idx].offset / PAGE_SIZE) * PAGE_SIZE;
-  int mapLength =
-      mGrantors[grantor_idx].offset - mapOffset + mGrantors[grantor_idx].extent;
-
-  void* address = mmap(0, mapLength, PROT_READ | PROT_WRITE, MAP_SHARED,
-                       handle->data[fdIndex], mapOffset);
-  return (address == MAP_FAILED)
-             ? nullptr
-             : (uint8_t*)address + (mGrantors[grantor_idx].offset - mapOffset);
-}
-
-void MQDescriptor::unmapGrantorDescr(void* address, uint32_t grantor_idx) {
-  const native_handle_t* handle = mHandle;
-  int mapOffset = (mGrantors[grantor_idx].offset / PAGE_SIZE) * PAGE_SIZE;
-  int mapLength =
-      mGrantors[grantor_idx].offset - mapOffset + mGrantors[grantor_idx].extent;
-  void* baseAddress =
-      (uint8_t*)address - (mGrantors[grantor_idx].offset - mapOffset);
-  if (baseAddress) munmap(baseAddress, mapLength);
-}
-
 ::android::status_t MQDescriptor::readEmbeddedFromParcel(
     const Parcel &parcel,
     size_t parentHandle,
@@ -154,6 +123,30 @@
     return _hidl_err;
 }
 
+size_t MQDescriptor::getSize() const {
+  return mGrantors[DATAPTRPOS].extent;
+}
+
+size_t MQDescriptor::getQuantum() const { return mQuantum; }
+
+int32_t MQDescriptor::getFlags() const { return mFlags; }
+
+std::vector<GrantorDescriptor> MQDescriptor::getGrantors() const {
+  size_t grantor_count = mGrantors.size();
+  std::vector<GrantorDescriptor> grantors(grantor_count);
+  for (size_t i = 0; i < grantor_count; i++) {
+    grantors[i] = mGrantors[i];
+  }
+  return grantors;
+}
+
+const sp<NativeHandle> MQDescriptor::getNativeHandle() const {
+  /*
+   * Create an sp<NativeHandle> from mHandle.
+   */
+  return NativeHandle::create(mHandle, false /* ownsHandle */);
+}
+
 }  // namespace hardware
 }  // namespace android
 
diff --git a/include/hidl/MQDescriptor.h b/include/hidl/MQDescriptor.h
index 47b3dea..327b61c 100644
--- a/include/hidl/MQDescriptor.h
+++ b/include/hidl/MQDescriptor.h
@@ -20,6 +20,7 @@
 #include <android-base/macros.h>
 #include <cutils/native_handle.h>
 #include <hidl/HidlSupport.h>
+#include <utils/NativeHandle.h>
 
 namespace android {
 namespace hardware {
@@ -48,8 +49,6 @@
     size_t getQuantum() const;
 
     int32_t getFlags() const;
-    void* mapGrantorDescr(uint32_t grantor_idx);
-    void unmapGrantorDescr(void* address, uint32_t grantor_idx);
 
     ::android::status_t readEmbeddedFromParcel(
             const ::android::hardware::Parcel &parcel,
@@ -63,23 +62,16 @@
 
     bool isHandleValid() const { return mHandle != nullptr; }
     size_t countGrantors() const { return mGrantors.size(); }
+    std::vector<GrantorDescriptor> getGrantors() const;
+    const sp<NativeHandle> getNativeHandle() const;
 
 private:
     ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
-    const ::native_handle_t *mHandle;
+    ::native_handle_t *mHandle;
     uint32_t mQuantum;
     uint32_t mFlags;
 };
-
-inline size_t MQDescriptor::getSize() const {
-  return mGrantors[DATAPTRPOS].extent;
-}
-
-inline size_t MQDescriptor::getQuantum() const { return mQuantum; }
-
-inline int32_t MQDescriptor::getFlags() const { return mFlags; }
-
-} // namespace hardware
-} // namespace android
+}  // namespace hardware
+}  // namespace android
 
 #endif  // FMSGQ_DESCRIPTOR_H