Bug 1209469: Expose |BluetoothPinCode| in Bluetooth backend interface, r=brsun
authorThomas Zimmermann <tdz@users.sourceforge.net>
Fri, 02 Oct 2015 11:07:47 +0200
changeset 265809 c12ac1a74a8f9230306953ee7098854134259986
parent 265808 b0ead0d45e13b8cdea19455cf601ff5b19a653b3
child 265810 e09924fe7c2010427b2275554a4c599b4f7ed5ba
push id66032
push userkwierso@gmail.com
push dateFri, 02 Oct 2015 20:43:24 +0000
treeherdermozilla-inbound@03e8acfc181f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbrsun
bugs1209469
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 1209469: Expose |BluetoothPinCode| in Bluetooth backend interface, r=brsun
dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.cpp
dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.h
dom/bluetooth/bluedroid/BluetoothDaemonHelpers.cpp
dom/bluetooth/bluedroid/BluetoothDaemonHelpers.h
dom/bluetooth/bluedroid/BluetoothDaemonInterface.cpp
dom/bluetooth/bluedroid/BluetoothDaemonInterface.h
dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp
dom/bluetooth/common/BluetoothCommon.h
dom/bluetooth/common/BluetoothInterface.h
dom/bluetooth/common/BluetoothUtils.cpp
dom/bluetooth/common/BluetoothUtils.h
--- a/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.cpp
@@ -368,29 +368,26 @@ BluetoothDaemonCoreModule::CancelBondCmd
   }
   unused << pdu.forget();
   return rv;
 }
 
 nsresult
 BluetoothDaemonCoreModule::PinReplyCmd(const BluetoothAddress& aBdAddr,
                                        bool aAccept,
-                                       const nsAString& aPinCode,
+                                       const BluetoothPinCode& aPinCode,
                                        BluetoothResultHandler* aRes)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   nsAutoPtr<DaemonSocketPDU> pdu(
     new DaemonSocketPDU(SERVICE_ID, OPCODE_PIN_REPLY,
                         0));
 
-  nsresult rv = PackPDU(
-    aBdAddr,
-    aAccept,
-    PackConversion<nsAString, BluetoothPinCode>(aPinCode), *pdu);
+  nsresult rv = PackPDU(aBdAddr, aAccept, aPinCode, *pdu);
   if (NS_FAILED(rv)) {
     return rv;
   }
   rv = Send(pdu, aRes);
   if (NS_FAILED(rv)) {
     return rv;
   }
   unused << pdu.forget();
--- a/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.h
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonCoreInterface.h
@@ -113,17 +113,17 @@ public:
 
   nsresult RemoveBondCmd(const BluetoothAddress& aBdAddr,
                          BluetoothResultHandler* aRes);
 
   nsresult CancelBondCmd(const BluetoothAddress& aBdAddr,
                          BluetoothResultHandler* aRes);
 
   nsresult PinReplyCmd(const BluetoothAddress& aBdAddr, bool aAccept,
-                       const nsAString& aPinCode,
+                       const BluetoothPinCode& aPinCode,
                        BluetoothResultHandler* aRes);
 
   nsresult SspReplyCmd(const BluetoothAddress& aBdAddr,
                        BluetoothSspVariant aVariant,
                        bool aAccept, uint32_t aPasskey,
                        BluetoothResultHandler* aRes);
 
   nsresult DutModeConfigureCmd(bool aEnable, BluetoothResultHandler* aRes);
--- a/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.cpp
@@ -509,45 +509,16 @@ Convert(int32_t aIn, BluetoothGattStatus
     aOut = GATT_STATUS_UNKNOWN_ERROR;
   } else {
     aOut = sGattStatus[aIn];
   }
   return NS_OK;
 }
 
 nsresult
-Convert(const nsAString& aIn, BluetoothPinCode& aOut)
-{
-  if (MOZ_HAL_IPC_CONVERT_WARN_IF(
-        aIn.Length() > MOZ_ARRAY_LENGTH(aOut.mPinCode), nsAString,
-        BluetoothPinCode)) {
-    return NS_ERROR_ILLEGAL_VALUE;
-  }
-
-  NS_ConvertUTF16toUTF8 pinCodeUTF8(aIn);
-  const char* str = pinCodeUTF8.get();
-
-  nsAString::size_type i;
-
-  // Fill pin into aOut
-  for (i = 0; i < aIn.Length(); ++i, ++str) {
-    aOut.mPinCode[i] = static_cast<uint8_t>(*str);
-  }
-
-  // Clear remaining bytes in aOut
-  size_t ntrailing = (MOZ_ARRAY_LENGTH(aOut.mPinCode) - aIn.Length()) *
-                     sizeof(aOut.mPinCode[0]);
-  memset(aOut.mPinCode + aIn.Length(), 0, ntrailing);
-
-  aOut.mLength = aIn.Length();
-
-  return NS_OK;
-}
-
-nsresult
 Convert(const nsAString& aIn, BluetoothPropertyType& aOut)
 {
   if (aIn.EqualsLiteral("Name")) {
     aOut = PROPERTY_BDNAME;
   } else if (aIn.EqualsLiteral("Discoverable")) {
     aOut = PROPERTY_ADAPTER_SCAN_MODE;
   } else if (aIn.EqualsLiteral("DiscoverableTimeout")) {
     aOut = PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
--- a/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.h
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonHelpers.h
@@ -98,21 +98,16 @@ struct BluetoothAvrcpEventParamPair {
 };
 
 struct BluetoothConfigurationParameter {
   uint8_t mType;
   uint16_t mLength;
   nsAutoArrayPtr<uint8_t> mValue;
 };
 
-struct BluetoothPinCode {
-  uint8_t mPinCode[16];
-  uint8_t mLength;
-};
-
 struct BluetoothRemoteName {
   uint8_t mName[249];
 };
 
 struct BluetoothServiceName {
   uint8_t mName[256];
 };
 
@@ -191,19 +186,16 @@ Convert(uint8_t aIn, BluetoothStatus& aO
 
 nsresult
 Convert(int32_t aIn, BluetoothAttributeHandle& aOut);
 
 nsresult
 Convert(int32_t aIn, BluetoothGattStatus& aOut);
 
 nsresult
-Convert(const nsAString& aIn, BluetoothPinCode& aOut);
-
-nsresult
 Convert(const nsAString& aIn, BluetoothPropertyType& aOut);
 
 nsresult
 Convert(const nsAString& aIn, BluetoothServiceName& aOut);
 
 nsresult
 Convert(const BluetoothAttributeHandle& aIn, int32_t& aOut);
 
--- a/dom/bluetooth/bluedroid/BluetoothDaemonInterface.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonInterface.cpp
@@ -792,17 +792,17 @@ BluetoothDaemonInterface::GetConnectionS
   // NO-OP: no corresponding interface of current BlueZ
 }
 
 /* Authentication */
 
 void
 BluetoothDaemonInterface::PinReply(const BluetoothAddress& aBdAddr,
                                    bool aAccept,
-                                   const nsAString& aPinCode,
+                                   const BluetoothPinCode& aPinCode,
                                    BluetoothResultHandler* aRes)
 {
   nsresult rv = static_cast<BluetoothDaemonCoreModule*>
     (mProtocol)->PinReplyCmd(aBdAddr, aAccept, aPinCode, aRes);
   if (NS_FAILED(rv)) {
     DispatchError(aRes, rv);
   }
 }
--- a/dom/bluetooth/bluedroid/BluetoothDaemonInterface.h
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonInterface.h
@@ -97,17 +97,17 @@ public:
   /* Connection */
 
   void GetConnectionState(const BluetoothAddress& aBdAddr,
                           BluetoothResultHandler* aRes) override;
 
   /* Authentication */
 
   void PinReply(const BluetoothAddress& aBdAddr, bool aAccept,
-                const nsAString& aPinCode,
+                const BluetoothPinCode& aPinCode,
                 BluetoothResultHandler* aRes) override;
 
   void SspReply(const BluetoothAddress& aBdAddr,
                 BluetoothSspVariant aVariant,
                 bool aAccept, uint32_t aPasskey,
                 BluetoothResultHandler* aRes) override;
 
   /* DUT Mode */
--- a/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp
@@ -1342,17 +1342,24 @@ BluetoothServiceBluedroid::PinReplyInter
 
   BluetoothAddress address;
   nsresult rv = StringToAddress(aDeviceAddress, address);
   if (NS_FAILED(rv)) {
     DispatchReplyError(aRunnable, STATUS_PARM_INVALID);
     return;
   }
 
-  sBtInterface->PinReply(address, aAccept, aPinCode,
+  BluetoothPinCode pinCode;
+  rv = StringToPinCode(aPinCode, pinCode);
+  if (NS_FAILED(rv)) {
+    DispatchReplyError(aRunnable, STATUS_PARM_INVALID);
+    return;
+  }
+
+  sBtInterface->PinReply(address, aAccept, pinCode,
                          new PinReplyResultHandler(aRunnable));
 }
 
 void
 BluetoothServiceBluedroid::SetPinCodeInternal(
   const nsAString& aDeviceAddress, const nsAString& aPinCode,
   BluetoothReplyRunnable* aRunnable)
 {
--- a/dom/bluetooth/common/BluetoothCommon.h
+++ b/dom/bluetooth/common/BluetoothCommon.h
@@ -492,16 +492,21 @@ struct BluetoothUuid {
   }
 
   bool operator!=(const BluetoothUuid& aOther) const
   {
     return !(*this == aOther);
   }
 };
 
+struct BluetoothPinCode {
+  uint8_t mPinCode[16]; /* not \0-terminated */
+  uint8_t mLength;
+};
+
 struct BluetoothServiceRecord {
   BluetoothUuid mUuid;
   uint16_t mChannel;
   char mName[256];
 };
 
 struct BluetoothRemoteInfo {
   int mVerMajor;
--- a/dom/bluetooth/common/BluetoothInterface.h
+++ b/dom/bluetooth/common/BluetoothInterface.h
@@ -1080,17 +1080,17 @@ public:
   /* Connection */
 
   virtual void GetConnectionState(const BluetoothAddress& aBdAddr,
                                   BluetoothResultHandler* aRes) = 0;
 
   /* Authentication */
 
   virtual void PinReply(const BluetoothAddress& aBdAddr, bool aAccept,
-                        const nsAString& aPinCode,
+                        const BluetoothPinCode& aPinCode,
                         BluetoothResultHandler* aRes) = 0;
 
   virtual void SspReply(const BluetoothAddress& aBdAddr,
                         BluetoothSspVariant aVariant,
                         bool aAccept, uint32_t aPasskey,
                         BluetoothResultHandler* aRes) = 0;
 
   /* DUT Mode */
--- a/dom/bluetooth/common/BluetoothUtils.cpp
+++ b/dom/bluetooth/common/BluetoothUtils.cpp
@@ -56,16 +56,37 @@ StringToAddress(const nsAString& aString
                    &aAddress.mAddr[4],
                    &aAddress.mAddr[5]);
   if (res < static_cast<ssize_t>(MOZ_ARRAY_LENGTH(aAddress.mAddr))) {
     return NS_ERROR_ILLEGAL_VALUE;
   }
   return NS_OK;
 }
 
+nsresult
+StringToPinCode(const nsAString& aString, BluetoothPinCode& aPinCode)
+{
+  NS_ConvertUTF16toUTF8 stringUTF8(aString);
+
+  auto len = stringUTF8.Length();
+
+  if (len > sizeof(aPinCode.mPinCode)) {
+    BT_LOGR("Service-name string too long");
+    return NS_ERROR_ILLEGAL_VALUE;
+  }
+
+  auto str = stringUTF8.get();
+
+  memcpy(aPinCode.mPinCode, str, len);
+  memset(aPinCode.mPinCode + len, 0, sizeof(aPinCode.mPinCode) - len);
+  aPinCode.mLength = len;
+
+  return NS_OK;
+}
+
 void
 UuidToString(const BluetoothUuid& aUuid, nsAString& aString)
 {
   char uuidStr[37];
   uint32_t uuid0, uuid4;
   uint16_t uuid1, uuid2, uuid3, uuid5;
 
   memcpy(&uuid0, &aUuid.mUuid[0], sizeof(uint32_t));
--- a/dom/bluetooth/common/BluetoothUtils.h
+++ b/dom/bluetooth/common/BluetoothUtils.h
@@ -29,16 +29,23 @@ class BluetoothValue;
 
 void
 AddressToString(const BluetoothAddress& aAddress, nsAString& aString);
 
 nsresult
 StringToAddress(const nsAString& aString, BluetoothAddress& aAddress);
 
 //
+// Pin code/string conversion
+//
+
+nsresult
+StringToPinCode(const nsAString& aString, BluetoothPinCode& aPinCode);
+
+//
 // BluetoothUuid <-> uuid string conversion
 //
 
 /**
  * Convert BluetoothUuid object to xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx uuid string.
  *
  * Note: This utility function is used by gecko internal only to convert
  * BluetoothUuid created by bluetooth stack to uuid string representation.