Bug 1202704: Move Bluetooth IPC unpack functions to generic HAL IPC, r=shuang
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 09 Sep 2015 13:40:26 +0200
changeset 294103 d019cd8fc5cc3ff426129a52fe74da16cf6013a0
parent 294102 64bab3289232d1e464cd058829fcdaa87f97349b
child 294104 834376f2e95eeb0e96b8eca6c251475562b5da53
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshuang
bugs1202704
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1202704: Move Bluetooth IPC unpack functions to generic HAL IPC, r=shuang
dom/bluetooth/bluedroid/BluetoothDaemonHelpers.cpp
dom/bluetooth/bluedroid/BluetoothDaemonHelpers.h
ipc/hal/DaemonSocketPDUHelpers.cpp
ipc/hal/DaemonSocketPDUHelpers.h
--- a/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.cpp
@@ -1334,28 +1334,16 @@ PackPDU(BluetoothGattWriteType aIn, Daem
   return PackPDU(PackConversion<BluetoothGattWriteType, int32_t>(aIn), aPDU);
 }
 
 //
 // Unpacking
 //
 
 nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, bool& aOut)
-{
-  return UnpackPDU(aPDU, UnpackConversion<uint8_t, bool>(aOut));
-}
-
-nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, char& aOut)
-{
-  return UnpackPDU(aPDU, UnpackConversion<uint8_t, char>(aOut));
-}
-
-nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothA2dpAudioState& aOut)
 {
   return UnpackPDU(
     aPDU, UnpackConversion<uint8_t, BluetoothA2dpAudioState>(aOut));
 }
 
 nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothA2dpConnectionState& aOut)
--- a/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.h
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.h
@@ -411,22 +411,16 @@ PackPDU(const PackReversed<BluetoothUuid
    aPDU);
 }
 
 //
 // Unpacking
 //
 
 nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, bool& aOut);
-
-nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, char& aOut);
-
-nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothA2dpAudioState& aOut);
 
 nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothA2dpConnectionState& aOut);
 
 nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothAclState& aOut);
 
@@ -525,166 +519,21 @@ nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothGattReadParam& aOut);
 
 nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothGattWriteParam& aOut);
 
 nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, BluetoothGattNotifyParam& aOut);
 
-/* |UnpackConversion| is a helper for convering unpacked values. Pass
- * an instance of this structure to |UnpackPDU| to read a value from
- * the PDU in the input type and convert it to the output type.
- */
-template<typename Tin, typename Tout>
-struct UnpackConversion {
-  UnpackConversion(Tout& aOut)
-  : mOut(aOut)
-  { }
-
-  Tout& mOut;
-};
-
-template<typename Tin, typename Tout>
-inline nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, const UnpackConversion<Tin, Tout>& aOut)
-{
-  Tin in;
-  nsresult rv = UnpackPDU(aPDU, in);
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
-  return Convert(in, aOut.mOut);
-}
-
-/* |UnpackArray| is a helper for unpacking arrays. Pass an instance
- * of this structure as the second argument to |UnpackPDU| to unpack
- * an array.
- */
-template <typename T>
-struct UnpackArray
-{
-  UnpackArray(T* aData, size_t aLength)
-  : mData(aData)
-  , mLength(aLength)
-  { }
-
-  UnpackArray(nsAutoArrayPtr<T>& aData, size_t aLength)
-  : mData(nullptr)
-  , mLength(aLength)
-  {
-    aData = new T[mLength];
-    mData = aData.get();
-  }
-
-  UnpackArray(nsAutoArrayPtr<T>& aData, size_t aSize, size_t aElemSize)
-  : mData(nullptr)
-  , mLength(aSize / aElemSize)
-  {
-    aData = new T[mLength];
-    mData = aData.get();
-  }
-
-  T* mData;
-  size_t mLength;
-};
-
-template<typename T>
-inline nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, const UnpackArray<T>& aOut)
-{
-  for (size_t i = 0; i < aOut.mLength; ++i) {
-    nsresult rv = UnpackPDU(aPDU, aOut.mData[i]);
-    if (NS_FAILED(rv)) {
-      return rv;
-    }
-  }
-  return NS_OK;
-}
-
-template<typename T>
-inline nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, UnpackArray<T>& aOut)
-{
-  for (size_t i = 0; i < aOut.mLength; ++i) {
-    nsresult rv = UnpackPDU(aPDU, aOut.mData[i]);
-    if (NS_FAILED(rv)) {
-      return rv;
-    }
-  }
-  return NS_OK;
-}
-
-template<>
-inline nsresult
-UnpackPDU<uint8_t>(DaemonSocketPDU& aPDU, const UnpackArray<uint8_t>& aOut)
-{
-  /* Read raw bytes in one pass */
-  return aPDU.Read(aOut.mData, aOut.mLength);
-}
-
-template<typename T>
-inline nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, nsTArray<T>& aOut)
-{
-  for (typename nsTArray<T>::size_type i = 0; i < aOut.Length(); ++i) {
-    nsresult rv = UnpackPDU(aPDU, aOut[i]);
-    if (NS_FAILED(rv)) {
-      return rv;
-    }
-  }
-  return NS_OK;
-}
-
-/* |UnpackReversed| is a helper for unpacking data in reversed order. Pass an
- * instance of this structure as the second argument to |UnpackPDU| to unpack
- * data in reversed order.
- */
-template<typename T>
-struct UnpackReversed
-{
-  UnpackReversed(T& aValue)
-    : mValue(&aValue)
-  { }
-
-  UnpackReversed(T&& aValue)
-    : mValue(&aValue)
-  { }
-
-  T* mValue;
-};
-
-/* No general rules to unpack data in reversed order. Signal a link error if
- * the type |T| of |UnpackReversed| is not defined explicitly.
- */
-template<typename T>
-nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, const UnpackReversed<T>& aOut);
-
-template<typename U>
-inline nsresult
-UnpackPDU(DaemonSocketPDU& aPDU, const UnpackReversed<UnpackArray<U>>& aOut)
-{
-  for (size_t i = 0; i < aOut.mValue->mLength; ++i) {
-    nsresult rv = UnpackPDU(aPDU,
-                            aOut.mValue->mData[aOut.mValue->mLength - i - 1]);
-    if (NS_FAILED(rv)) {
-      return rv;
-    }
-  }
-  return NS_OK;
-}
-
 /* This implementation of |UnpackPDU| unpacks |BluetoothUuid| in reversed
  * order. (ex. reversed GATT UUID, see bug 1171866)
  */
-template<>
 inline nsresult
-UnpackPDU<BluetoothUuid>(DaemonSocketPDU& aPDU,
-                         const UnpackReversed<BluetoothUuid>& aOut)
+UnpackPDU(DaemonSocketPDU& aPDU, const UnpackReversed<BluetoothUuid>& aOut)
 {
   return UnpackPDU(
     aPDU,
     UnpackReversed<UnpackArray<uint8_t>>(
       UnpackArray<uint8_t>(aOut.mValue->mUuid, sizeof(aOut.mValue->mUuid))));
 }
 
 //
--- a/ipc/hal/DaemonSocketPDUHelpers.cpp
+++ b/ipc/hal/DaemonSocketPDUHelpers.cpp
@@ -185,16 +185,28 @@ PackPDU(const DaemonSocketPDUHeader& aIn
   return NS_OK;
 }
 
 //
 // Unpacking
 //
 
 nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, bool& aOut)
+{
+  return UnpackPDU(aPDU, UnpackConversion<uint8_t, bool>(aOut));
+}
+
+nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, char& aOut)
+{
+  return UnpackPDU(aPDU, UnpackConversion<uint8_t, char>(aOut));
+}
+
+nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, nsDependentCString& aOut)
 {
   // We get a pointer to the first character in the PDU, a length
   // of 1 ensures we consume the \0 byte. With 'str' pointing to
   // the string in the PDU, we can copy the actual bytes.
 
   const char* str = reinterpret_cast<const char*>(aPDU.Consume(1));
   if (NS_WARN_IF(!str)) {
--- a/ipc/hal/DaemonSocketPDUHelpers.h
+++ b/ipc/hal/DaemonSocketPDUHelpers.h
@@ -497,16 +497,22 @@ PackPDU(const T1& aIn1, const T2& aIn2, 
 // Unpacking
 //
 
 // introduce link errors on non-handled data types
 template <typename T>
 nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, T& aOut);
 
+nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, bool& aOut);
+
+nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, char& aOut);
+
 inline nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, int8_t& aOut)
 {
   return aPDU.Read(aOut);
 }
 
 inline nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, uint8_t& aOut)
@@ -582,16 +588,159 @@ struct UnpackString0
 };
 
 /* This implementation of |UnpackPDU| unpacks a 0-terminated C string
  * and converts it to wide-character encoding.
  */
 nsresult
 UnpackPDU(DaemonSocketPDU& aPDU, const UnpackString0& aOut);
 
+/* |UnpackConversion| is a helper for convering unpacked values. Pass
+ * an instance of this structure to |UnpackPDU| to read a value from
+ * the PDU in the input type and convert it to the output type.
+ */
+template<typename Tin, typename Tout>
+struct UnpackConversion {
+  UnpackConversion(Tout& aOut)
+    : mOut(aOut)
+  { }
+
+  Tout& mOut;
+};
+
+template<typename Tin, typename Tout>
+inline nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, const UnpackConversion<Tin, Tout>& aOut)
+{
+  Tin in;
+  nsresult rv = UnpackPDU(aPDU, in);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  return Convert(in, aOut.mOut);
+}
+
+/* |UnpackArray| is a helper for unpacking arrays. Pass an instance
+ * of this structure as the second argument to |UnpackPDU| to unpack
+ * an array.
+ */
+template <typename T>
+struct UnpackArray
+{
+  UnpackArray(T* aData, size_t aLength)
+    : mData(aData)
+    , mLength(aLength)
+  { }
+
+  UnpackArray(nsAutoArrayPtr<T>& aData, size_t aLength)
+    : mData(nullptr)
+    , mLength(aLength)
+  {
+    aData = new T[mLength];
+    mData = aData.get();
+  }
+
+  UnpackArray(nsAutoArrayPtr<T>& aData, size_t aSize, size_t aElemSize)
+    : mData(nullptr)
+    , mLength(aSize / aElemSize)
+  {
+    aData = new T[mLength];
+    mData = aData.get();
+  }
+
+  T* mData;
+  size_t mLength;
+};
+
+template<typename T>
+inline nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, const UnpackArray<T>& aOut)
+{
+  for (size_t i = 0; i < aOut.mLength; ++i) {
+    nsresult rv = UnpackPDU(aPDU, aOut.mData[i]);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+  }
+  return NS_OK;
+}
+
+template<typename T>
+inline nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, UnpackArray<T>& aOut)
+{
+  for (size_t i = 0; i < aOut.mLength; ++i) {
+    nsresult rv = UnpackPDU(aPDU, aOut.mData[i]);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+  }
+  return NS_OK;
+}
+
+template<>
+inline nsresult
+UnpackPDU<uint8_t>(DaemonSocketPDU& aPDU, const UnpackArray<uint8_t>& aOut)
+{
+  /* Read raw bytes in one pass */
+  return aPDU.Read(aOut.mData, aOut.mLength);
+}
+
+template<typename T>
+inline nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, nsTArray<T>& aOut)
+{
+  for (typename nsTArray<T>::size_type i = 0; i < aOut.Length(); ++i) {
+    nsresult rv = UnpackPDU(aPDU, aOut[i]);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+  }
+  return NS_OK;
+}
+
+/* |UnpackReversed| is a helper for unpacking data in reversed order. Pass an
+ * instance of this structure as the second argument to |UnpackPDU| to unpack
+ * data in reversed order.
+ */
+template<typename T>
+struct UnpackReversed
+{
+  UnpackReversed(T& aValue)
+    : mValue(&aValue)
+  { }
+
+  UnpackReversed(T&& aValue)
+    : mValue(&aValue)
+  { }
+
+  T* mValue;
+};
+
+/* No general rules to unpack data in reversed order. Signal a link error if
+ * the type |T| of |UnpackReversed| is not defined explicitly.
+ */
+template<typename T>
+nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, const UnpackReversed<T>& aOut);
+
+template<typename U>
+inline nsresult
+UnpackPDU(DaemonSocketPDU& aPDU, const UnpackReversed<UnpackArray<U>>& aOut)
+{
+  for (size_t i = 0; i < aOut.mValue->mLength; ++i) {
+    nsresult rv = UnpackPDU(aPDU,
+                            aOut.mValue->mData[aOut.mValue->mLength - i - 1]);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+  }
+  return NS_OK;
+}
+
 //
 // Init operators
 //
 
 //
 // Below are general-purpose init operators for Bluetooth. The classes
 // of type |ConstantInitOp[1..3]| initialize results or notifications
 // with constant values.