Bug 1102703 - Porting bluetooth interface of android lollipop (core). r=btian, r=tzimmermann, r=shawnjohnjr
☠☠ backed out by b4926227268f ☠ ☠
authorBruce Sun <brsun@mozilla.com>
Thu, 08 Jan 2015 11:47:12 +0800
changeset 222670 9f067e4973ebec2447d6caa7dadea196c5d2ca39
parent 222669 db035be5bc6ccbb71556e4fe7e9f19750daa1a58
child 222671 ece26b235282103462be87d39e3bdbc973c06d32
push id28071
push userkwierso@gmail.com
push dateFri, 09 Jan 2015 00:54:55 +0000
treeherdermozilla-central@ccfafae7330f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbtian, tzimmermann, shawnjohnjr
bugs1102703
milestone37.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 1102703 - Porting bluetooth interface of android lollipop (core). r=btian, r=tzimmermann, r=shawnjohnjr
dom/bluetooth/bluedroid/BluetoothHALHelpers.cpp
dom/bluetooth/bluedroid/BluetoothHALHelpers.h
dom/bluetooth/bluedroid/BluetoothHALInterface.cpp
--- a/dom/bluetooth/bluedroid/BluetoothHALHelpers.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothHALHelpers.cpp
@@ -206,16 +206,49 @@ Convert(const btrc_player_settings_t& aI
   aOut.mNumAttr = aIn.num_attr;
   memcpy(aOut.mIds, aIn.attr_ids, aIn.num_attr);
   memcpy(aOut.mValues, aIn.attr_values, aIn.num_attr);
 
   return NS_OK;
 }
 #endif // ANDROID_VERSION >= 18
 
+#if ANDROID_VERSION >= 21
+nsresult
+Convert(const bt_activity_energy_info& aIn, BluetoothActivityEnergyInfo& aOut)
+{
+  nsresult rv = Convert(aIn.status, aOut.mStatus);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  rv = Convert(aIn.ctrl_state, aOut.mStackState);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  rv = Convert(aIn.tx_time, aOut.mTxTime);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  rv = Convert(aIn.rx_time, aOut.mRxTime);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  rv = Convert(aIn.idle_time, aOut.mIdleTime);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  rv = Convert(aIn.energy_used, aOut.mEnergyUsed);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+
+  return NS_OK;
+}
+#endif // ANDROID_VERSION >= 21
+
 nsresult
 Convert(const bt_property_t& aIn, BluetoothProperty& aOut)
 {
   /* type conversion */
 
   nsresult rv = Convert(aIn.type, aOut.mType);
   if (NS_FAILED(rv)) {
     return rv;
--- a/dom/bluetooth/bluedroid/BluetoothHALHelpers.h
+++ b/dom/bluetooth/bluedroid/BluetoothHALHelpers.h
@@ -760,16 +760,36 @@ Convert(btrc_remote_features_t aIn, unsi
   /* The input type's name is misleading. The converted value is
    * actually a bitmask.
    */
   aOut = static_cast<unsigned long>(aIn);
   return NS_OK;
 }
 #endif // ANDROID_VERSION >= 19
 
+#if ANDROID_VERSION >= 21
+inline nsresult
+Convert(BluetoothTransport aIn, int& aOut)
+{
+  static const int sTransport[] = {
+    CONVERT(TRANSPORT_AUTO, 0),
+    CONVERT(TRANSPORT_BREDR, 1),
+    CONVERT(TRANSPORT_LE, 2)
+  };
+  if (NS_WARN_IF(aIn >= MOZ_ARRAY_LENGTH(sTransport))) {
+    return NS_ERROR_ILLEGAL_VALUE;
+  }
+  aOut = sTransport[aIn];
+  return NS_OK;
+}
+
+nsresult
+Convert(const bt_activity_energy_info& aIn, BluetoothActivityEnergyInfo& aOut);
+#endif // ANDROID_VERSION >= 21
+
 /* |ConvertArray| is a helper for converting arrays. Pass an
  * instance of this structure as the first argument to |Convert|
  * to convert an array. The output type has to support the array
  * subscript operator.
  */
 template <typename T>
 struct ConvertArray
 {
--- a/dom/bluetooth/bluedroid/BluetoothHALInterface.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothHALInterface.cpp
@@ -313,18 +313,67 @@ struct BluetoothCallback
 
   static void
   LeTestMode(bt_status_t aStatus, uint16_t aNumPackets)
   {
     LeTestModeNotification::Dispatch(
       &BluetoothNotificationHandler::LeTestModeNotification,
       aStatus, aNumPackets);
   }
+
+#if ANDROID_VERSION >= 21
+  static void
+  EnergyInfo(bt_activity_energy_info* aEnergyInfo)
+  {
+    if (NS_WARN_IF(!aEnergyInfo)) {
+      return;
+    }
+
+    EnergyInfoNotification::Dispatch(
+      &BluetoothNotificationHandler::EnergyInfoNotification,
+      *aEnergyInfo);
+  }
+#endif
 };
 
+#if ANDROID_VERSION >= 21
+struct BluetoothOsCallout
+{
+  static bool
+  SetWakeAlarm(uint64_t aDelayMilliseconds,
+               bool aShouldWake,
+               void (* aAlarmCallback)(void*),
+               void* aData)
+  {
+    // FIXME: need to be implemented in later patches
+    // HAL wants to manage an wake_alarm but Gecko cannot fulfill it for now.
+    // Simply pass the request until a proper implementation has been added.
+    return true;
+  }
+
+  static int
+  AcquireWakeLock(const char* aLockName)
+  {
+    // FIXME: need to be implemented in later patches
+    // HAL wants to manage an wake_lock but Gecko cannot fulfill it for now.
+    // Simply pass the request until a proper implementation has been added.
+    return BT_STATUS_SUCCESS;
+  }
+
+  static int
+  ReleaseWakeLock(const char* aLockName)
+  {
+    // FIXME: need to be implemented in later patches
+    // HAL wants to manage an wake_lock but Gecko cannot fulfill it for now.
+    // Simply pass the request until a proper implementation has been added.
+    return BT_STATUS_SUCCESS;
+  }
+};
+#endif
+
 // Interface
 //
 
 /* returns the container structure of a variable; _t is the container's
  * type, _v the name of the variable, and _m is _v's field within _t
  */
 #define container(_t, _v, _m) \
   ( (_t*)( ((const unsigned char*)(_v)) - offsetof(_t, _m) ) )
@@ -409,24 +458,45 @@ BluetoothHALInterface::Init(
     BluetoothCallback::DiscoveryStateChanged,
     BluetoothCallback::PinRequest,
     BluetoothCallback::SspRequest,
     BluetoothCallback::BondStateChanged,
     BluetoothCallback::AclStateChanged,
     BluetoothCallback::ThreadEvt,
     BluetoothCallback::DutModeRecv,
 #if ANDROID_VERSION >= 18
-    BluetoothCallback::LeTestMode
+    BluetoothCallback::LeTestMode,
+#endif
+#if ANDROID_VERSION >= 21
+    BluetoothCallback::EnergyInfo
 #endif
   };
 
+#if ANDROID_VERSION >= 21
+  static bt_os_callouts_t sBluetoothOsCallouts = {
+    sizeof(sBluetoothOsCallouts),
+    BluetoothOsCallout::SetWakeAlarm,
+    BluetoothOsCallout::AcquireWakeLock,
+    BluetoothOsCallout::ReleaseWakeLock
+  };
+#endif
+
   sNotificationHandler = aNotificationHandler;
 
   int status = mInterface->init(&sBluetoothCallbacks);
 
+#if ANDROID_VERSION >= 21
+  if (status == BT_STATUS_SUCCESS) {
+    status = mInterface->set_os_callouts(&sBluetoothOsCallouts);
+    if (status != BT_STATUS_SUCCESS) {
+      mInterface->cleanup();
+    }
+  }
+#endif
+
   if (aRes) {
     DispatchBluetoothHALResult(aRes, &BluetoothResultHandler::Init,
                                ConvertDefault(status, STATUS_FAIL));
   }
 }
 
 void
 BluetoothHALInterface::Cleanup(BluetoothResultHandler* aRes)
@@ -671,18 +741,26 @@ BluetoothHALInterface::CancelDiscovery(B
 void
 BluetoothHALInterface::CreateBond(const nsAString& aBdAddr,
                                   BluetoothTransport aTransport,
                                   BluetoothResultHandler* aRes)
 {
   bt_bdaddr_t bdAddr;
   int status;
 
+#if ANDROID_VERSION >= 21
+  int transport = 0; /* TRANSPORT_AUTO */
+
+  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr)) &&
+      NS_SUCCEEDED(Convert(aTransport, transport))) {
+    status = mInterface->create_bond(&bdAddr, transport);
+#else
   if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr))) {
     status = mInterface->create_bond(&bdAddr);
+#endif
   } else {
     status = BT_STATUS_PARM_INVALID;
   }
 
   if (aRes) {
     DispatchBluetoothHALResult(aRes,
                                &BluetoothResultHandler::CreateBond,
                                ConvertDefault(status, STATUS_FAIL));
@@ -730,19 +808,29 @@ BluetoothHALInterface::CancelBond(const 
 }
 
 /* Connection */
 
 void
 BluetoothHALInterface::GetConnectionState(const nsAString& aBdAddr,
                                           BluetoothResultHandler* aRes)
 {
-  // TODO: to be implemented
+  int status;
+
+#if ANDROID_VERSION >= 21
+  bt_bdaddr_t bdAddr;
 
-  int status = BT_STATUS_UNSUPPORTED;
+  if (NS_SUCCEEDED(Convert(aBdAddr, bdAddr))) {
+    status = mInterface->get_connection_state(&bdAddr);
+  } else {
+    status = BT_STATUS_PARM_INVALID;
+  }
+#else
+  status = BT_STATUS_UNSUPPORTED;
+#endif
 
   if (aRes) {
     DispatchBluetoothHALResult(aRes,
                                &BluetoothResultHandler::GetConnectionState,
                                ConvertDefault(status, STATUS_FAIL));
   }
 }
 
@@ -853,19 +941,21 @@ BluetoothHALInterface::LeTestMode(uint16
                                ConvertDefault(status, STATUS_FAIL));
   }
 }
 
 /* Energy Information */
 void
 BluetoothHALInterface::ReadEnergyInfo(BluetoothResultHandler* aRes)
 {
-  // TODO: to be implemented
-
+#if ANDROID_VERSION >= 21
+  int status = mInterface->read_energy_info();
+#else
   int status = BT_STATUS_UNSUPPORTED;
+#endif
 
   if (aRes) {
     DispatchBluetoothHALResult(aRes,
                                &BluetoothResultHandler::ReadEnergyInfo,
                                ConvertDefault(status, STATUS_FAIL));
   }
 }