Bug 1216195 - use mozilla/Endian.h facilities in bluetooth code; r=btian
authorNathan Froyd <froydnj@mozilla.com>
Fri, 18 Sep 2015 19:36:05 -0400
changeset 268749 38f2959a97b20c3c871aef81d78297d4460a64af
parent 268748 1e8eb9ccc07d897a65495f992e09c2341f20e987
child 268750 0f85afd327f240b748878444fcb9c7f2334e702a
push id66933
push usernfroyd@mozilla.com
push dateWed, 21 Oct 2015 16:09:55 +0000
treeherdermozilla-inbound@38f2959a97b2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbtian
bugs1216195
milestone44.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 1216195 - use mozilla/Endian.h facilities in bluetooth code; r=btian We have a number of functions in mozilla/Endian.h for doing endianness conversions, and these functions should be used instead of hand-rolled functions. Using those functions in the bluetooth code makes the code clearer and more portable (ReadLittleEndianUint16 won't do the right thing if we're running on a big-endian processor, for instance). Endian.h is also careful to avoid assumptions about memory alignment, which utility functions like ReadLittleEndianUint16 don't do.
dom/bluetooth/bluedroid/BluetoothMapSmsManager.cpp
dom/bluetooth/bluedroid/BluetoothPbapManager.cpp
dom/bluetooth/bluedroid/BluetoothSocketMessageWatcher.cpp
dom/bluetooth/common/BluetoothUtils.cpp
dom/bluetooth/common/BluetoothUtils.h
--- a/dom/bluetooth/bluedroid/BluetoothMapSmsManager.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothMapSmsManager.cpp
@@ -10,16 +10,17 @@
 #include "BluetoothService.h"
 #include "BluetoothSocket.h"
 #include "BluetoothUtils.h"
 #include "BluetoothUuid.h"
 #include "ObexBase.h"
 
 #include "mozilla/dom/BluetoothMapParametersBinding.h"
 #include "mozilla/dom/ipc/BlobParent.h"
+#include "mozilla/Endian.h"
 #include "mozilla/dom/File.h"
 
 #include "mozilla/RefPtr.h"
 #include "mozilla/Services.h"
 #include "mozilla/StaticPtr.h"
 #include "nsAutoPtr.h"
 #include "nsIInputStream.h"
 #include "nsIObserver.h"
@@ -274,17 +275,17 @@ BluetoothMapSmsManager::MasDataHandler(U
 
       // "Establishing an OBEX Session"
       // The OBEX header target shall equal to MAS obex target UUID.
       if (!CompareHeaderTarget(pktHeaders)) {
         ReplyError(ObexResponseCode::BadRequest);
         return;
       }
 
-      mRemoteMaxPacketLength = ((static_cast<int>(data[5]) << 8) | data[6]);
+      mRemoteMaxPacketLength = BigEndian::readUint16(&data[5]);
 
       if (mRemoteMaxPacketLength < 255) {
         BT_LOGR("Remote maximum packet length %d", mRemoteMaxPacketLength);
         mRemoteMaxPacketLength = 0;
         ReplyError(ObexResponseCode::BadRequest);
         return;
       }
 
@@ -708,36 +709,30 @@ BluetoothMapSmsManager::HandleSmsMmsFold
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   uint8_t buf[64];
   uint16_t maxListCount = 0;
 
   if (aHeader.GetAppParameter(Map::AppParametersTagId::MaxListCount,
                               buf, 64)) {
-    maxListCount = *((uint16_t *)buf);
-    // convert big endian to little endian
-    maxListCount = (maxListCount >> 8) | (maxListCount << 8);
+    maxListCount = BigEndian::readUint16(buf);
   }
 
   uint16_t startOffset = 0;
   if (aHeader.GetAppParameter(Map::AppParametersTagId::StartOffset,
                               buf, 64)) {
-    startOffset = *((uint16_t *)buf);
-    // convert big endian to little endian
-    startOffset = (startOffset >> 8) | (startOffset << 8);
+    startOffset = BigEndian::readUint16(buf);
   }
 
   // Folder listing size
   int foldersize = mCurrentFolder->GetSubFolderCount();
 
-  // Convert little endian to big endian
   uint8_t folderListingSizeValue[2];
-  folderListingSizeValue[0] = (foldersize & 0xFF00) >> 8;
-  folderListingSizeValue[1] = (foldersize & 0x00FF);
+  BigEndian::writeUint16(&folderListingSizeValue[0], foldersize);
 
   // Section 3.3.4 "GetResponse", IrOBEX 1.2
   // [opcode:1][length:2][FolderListingSize:4][Headers:var] where
   // Application Parameter [FolderListingSize:4] = [tagId:1][length:1][value: 2]
   uint8_t appParameter[4];
   AppendAppParameter(appParameter, sizeof(appParameter),
                      (uint8_t)Map::AppParametersTagId::FolderListingSize,
                      folderListingSizeValue, sizeof(folderListingSizeValue));
@@ -781,46 +776,40 @@ BluetoothMapSmsManager::AppendBtNamedVal
     return;
   }
 
   /*
    * Follow MAP 6.3.1 Application Parameter Header
    */
   switch (aTagId) {
     case Map::AppParametersTagId::MaxListCount: {
-      uint16_t maxListCount = *((uint16_t *)buf);
-      // convert big endian to little endian
-      maxListCount = (maxListCount >> 8) | (maxListCount << 8);
+      uint16_t maxListCount = BigEndian::readUint16(buf);
       BT_LOGR("max list count: %d", maxListCount);
       AppendNamedValue(aValues, "maxListCount",
                        static_cast<uint32_t>(maxListCount));
       break;
     }
     case Map::AppParametersTagId::StartOffset: {
-      uint16_t startOffset = *((uint16_t *)buf);
-      // convert big endian to little endian
-      startOffset = (startOffset >> 8) | (startOffset << 8);
+      uint16_t startOffset = BigEndian::readUint16(buf);
       BT_LOGR("start offset : %d", startOffset);
       AppendNamedValue(aValues, "startOffset",
                        static_cast<uint32_t>(startOffset));
       break;
     }
     case Map::AppParametersTagId::SubjectLength: {
       uint8_t subLength = *((uint8_t *)buf);
       BT_LOGR("msg subLength : %d", subLength);
       AppendNamedValue(aValues, "subLength", static_cast<uint32_t>(subLength));
       break;
     }
     case Map::AppParametersTagId::ParameterMask: {
       /* Table 6.5, MAP 6.3.1. ParameterMask is Bit 16-31 Reserved for future
        * use. The reserved bits shall be set to 0 by MCE and discarded by MSE.
-       * convert big endian to little endian
        */
-      uint32_t parameterMask = (buf[3] << 0) | (buf[2] << 8) |
-                               (buf[1] << 16) | (buf[0] << 24);
+      uint32_t parameterMask = BigEndian::readUint32(buf);
       BT_LOGR("msg parameterMask : %d", parameterMask);
       AppendNamedValue(aValues, "parameterMask", parameterMask);
       break;
     }
     case Map::AppParametersTagId::FilterMessageType: {
       /* Follow MAP 1.2, 6.3.1
        * 0000xxx1 = "SMS_GSM"
        * 0000xx1x = "SMS_CDMA"
--- a/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp
@@ -8,16 +8,17 @@
 #include "BluetoothPbapManager.h"
 
 #include "BluetoothService.h"
 #include "BluetoothSocket.h"
 #include "BluetoothUtils.h"
 #include "BluetoothUuid.h"
 
 #include "mozilla/dom/BluetoothPbapParametersBinding.h"
+#include "mozilla/Endian.h"
 #include "mozilla/dom/File.h"
 #include "mozilla/dom/ipc/BlobParent.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/Services.h"
 #include "mozilla/StaticPtr.h"
 #include "nsAutoPtr.h"
 #include "nsIInputStream.h"
 #include "nsIObserver.h"
@@ -532,29 +533,29 @@ BluetoothPbapManager::AppendNamedValueBy
     case AppParameterTag::SearchValue:
       // Section 5.3.4.3 "SearchValue {<text string>}", PBAP 1.2
       // The UTF-8 character set shall be used for <text string>.
 
       // Store UTF-8 string with nsCString to follow MDN:Internal_strings
       AppendNamedValue(aValues, "searchText", nsCString((char *) buf));
       break;
     case AppParameterTag::MaxListCount: {
-      uint16_t maxListCount = ReadLittleEndianUInt16(buf);
+      uint16_t maxListCount = BigEndian::readUint16(buf);
       AppendNamedValue(aValues, "maxListCount",
                        static_cast<uint32_t>(maxListCount));
 
       // Section 5 "Phone Book Access Profile Functions", PBAP 1.2
       // Replying 'PhonebookSize' is mandatory if 'MaxListCount' parameter
       // is present in the request with a value of 0, else it is excluded.
       mPhonebookSizeRequired = !maxListCount;
       break;
     }
     case AppParameterTag::ListStartOffset:
       AppendNamedValue(aValues, "listStartOffset",
-                       static_cast<uint32_t>(ReadLittleEndianUInt16(buf)));
+                       static_cast<uint32_t>(BigEndian::readUint16(buf)));
       break;
     case AppParameterTag::PropertySelector:
       AppendNamedValue(aValues, "propSelector", PackPropertiesMask(buf, 64));
       break;
     case AppParameterTag::Format:
       AppendNamedValue(aValues, "format", static_cast<bool>(buf[0]));
       break;
     case AppParameterTag::vCardSelector: {
@@ -724,19 +725,17 @@ BluetoothPbapManager::PackPropertiesMask
 {
   InfallibleTArray<uint32_t> propSelector;
 
   // Table 5.1 "Property Mask", PBAP 1.2
   // PropertyMask is a 64-bit mask that indicates the properties contained in
   // the requested vCard objects. We only support bit 0~31 since the rest are
   // reserved for future use or vendor specific properties.
 
-  // convert big endian to little endian
-  uint32_t x = (aData[7] << 0)  | (aData[6] << 8) |
-               (aData[5] << 16) | (aData[4] << 24);
+  uint32_t x = BigEndian::readUint32(&aData[4]);
 
   uint32_t count = 0;
   while (x) {
     if (x & 1) {
       propSelector.AppendElement(count);
     }
 
     ++count;
@@ -854,27 +853,28 @@ BluetoothPbapManager::ReplyToGet(uint16_
 
   // ---- Part 1: [response code:1][length:2] ---- //
   // [response code:1][length:2] will be set in |SendObexData|.
   // Reserve index for them here
   unsigned int index = kObexRespHeaderSize;
 
   if (mPhonebookSizeRequired) {
     // ---- Part 2: [headerId:1][length:2][PhonebookSize:4] ---- //
-    uint16_t pbSizeBigEndian = ConvertEndiannessUInt16(aPhonebookSize);
+    uint8_t phonebookSize[2];
+    BigEndian::writeUint16(&phonebookSize[0], aPhonebookSize);
 
     // Section 6.2.1 "Application Parameters Header", PBAP 1.2
     // appParameters: [headerId:1][length:2][PhonebookSize:4], where
     //                [PhonebookSize:4] = [tagId:1][length:1][value:2]
     uint8_t appParameters[4];
     AppendAppParameter(appParameters,
                        sizeof(appParameters),
                        static_cast<uint8_t>(AppParameterTag::PhonebookSize),
-                       (uint8_t*) &pbSizeBigEndian,
-                       sizeof(pbSizeBigEndian));
+                       phonebookSize,
+                       sizeof(phonebookSize));
 
     index += AppendHeaderAppParameters(&res[index],
                                        mRemoteMaxPacketLength,
                                        appParameters,
                                        sizeof(appParameters));
 
     // ---- Part 3: [headerId:1][length:2][EndOfBody:0] ---- //
     opcode = ObexResponseCode::Success;
--- a/dom/bluetooth/bluedroid/BluetoothSocketMessageWatcher.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothSocketMessageWatcher.cpp
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "BluetoothSocketMessageWatcher.h"
 #include <errno.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include "BluetoothInterface.h"
+#include "mozilla/Endian.h"
 #include "nsClassHashtable.h"
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 //
 // SocketMessageWatcherWrapper
 //
 
@@ -238,28 +239,24 @@ SocketMessageWatcher::RecvMsg2()
 
   return STATUS_SUCCESS;
 }
 
 int16_t
 SocketMessageWatcher::ReadInt16(unsigned long aOffset) const
 {
   /* little-endian buffer */
-  return (static_cast<int16_t>(mBuf[aOffset + 1]) << 8) |
-          static_cast<int16_t>(mBuf[aOffset]);
+  return LittleEndian::readInt16(&mBuf[aOffset]);
 }
 
 int32_t
 SocketMessageWatcher::ReadInt32(unsigned long aOffset) const
 {
   /* little-endian buffer */
-  return (static_cast<int32_t>(mBuf[aOffset + 3]) << 24) |
-         (static_cast<int32_t>(mBuf[aOffset + 2]) << 16) |
-         (static_cast<int32_t>(mBuf[aOffset + 1]) << 8) |
-          static_cast<int32_t>(mBuf[aOffset]);
+  return LittleEndian::readInt32(&mBuf[aOffset]);
 }
 
 void
 SocketMessageWatcher::ReadBdAddress(unsigned long aOffset,
                                     BluetoothAddress& aBdAddress) const
 {
   aBdAddress = BluetoothAddress(mBuf[aOffset + 0],
                                 mBuf[aOffset + 1],
--- a/dom/bluetooth/common/BluetoothUtils.cpp
+++ b/dom/bluetooth/common/BluetoothUtils.cpp
@@ -599,21 +599,9 @@ InsertNamedValue(InfallibleTArray<Blueto
                  const BluetoothValue& aValue)
 {
   nsString name;
   name.AssignASCII(aName);
 
   aArray.InsertElementAt(aIndex, BluetoothNamedValue(name, aValue));
 }
 
-uint16_t
-ConvertEndiannessUInt16(uint16_t aValue)
-{
-  return (aValue >> 8) | (aValue << 8);
-}
-
-uint16_t
-ReadLittleEndianUInt16(const uint8_t* aBuf)
-{
-  return ConvertEndiannessUInt16(*((uint16_t *) aBuf));
-}
-
 END_BLUETOOTH_NAMESPACE
--- a/dom/bluetooth/common/BluetoothUtils.h
+++ b/dom/bluetooth/common/BluetoothUtils.h
@@ -267,25 +267,11 @@ void AppendNamedValue(InfallibleTArray<B
 /**
  * Wrap literal name and value into a BluetoothNamedValue and
  * insert it to the array.
  */
 void InsertNamedValue(InfallibleTArray<BluetoothNamedValue>& aArray,
                       uint8_t aIndex, const char* aName,
                       const BluetoothValue& aValue);
 
-//
-// Big/Little endianness conversion
-//
-
-/**
- * Convert a big/little endian uint16_t value to little/big endian one.
- */
-uint16_t ConvertEndiannessUInt16(uint16_t aValue);
-
-/**
- * Read an uint16_t from array and convert it to little endian one.
- */
-uint16_t ReadLittleEndianUInt16(const uint8_t* aBuf);
-
 END_BLUETOOTH_NAMESPACE
 
 #endif // mozilla_dom_bluetooth_BluetoothUtils_h