Bug 851046: Patch 2 - New Class: BluetoothSocket; r=mrbkap
authorEric Chou <echou@mozilla.com>
Thu, 04 Apr 2013 17:52:12 -0700
changeset 127741 c3f5fa4c5fc9db888175510c7ecf5da96f4e47e8
parent 127740 bf47d3b3d6c1b0a6f30c628fb3a08eaf3efe54a7
child 127742 fc1c1298bd85163181a984dd4061c61dc595a799
push id24512
push userryanvm@gmail.com
push dateFri, 05 Apr 2013 20:13:49 +0000
treeherdermozilla-central@139b6ba547fa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs851046
milestone23.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 851046: Patch 2 - New Class: BluetoothSocket; r=mrbkap
dom/bluetooth/BluetoothCommon.h
dom/bluetooth/BluetoothSocket.cpp
dom/bluetooth/BluetoothSocket.h
dom/bluetooth/Makefile.in
ipc/unixsocket/UnixSocket.h
--- a/dom/bluetooth/BluetoothCommon.h
+++ b/dom/bluetooth/BluetoothCommon.h
@@ -48,16 +48,17 @@ extern bool gBluetoothDebugFlag;
 
 #define KEY_LOCAL_AGENT  "/B2G/bluetooth/agent"
 #define KEY_REMOTE_AGENT "/B2G/bluetooth/remote_device_agent"
 #define KEY_MANAGER      "/B2G/bluetooth/manager"
 #define KEY_ADAPTER      "/B2G/bluetooth/adapter"
 
 // Bluetooth address format: xx:xx:xx:xx:xx:xx (or xx_xx_xx_xx_xx_xx)
 #define BLUETOOTH_ADDRESS_LENGTH 17
+#define BLUETOOTH_ADDRESS_NONE   "00:00:00:00:00:00"
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 enum BluetoothSocketType {
   RFCOMM = 1,
   SCO = 2,
   L2CAP = 3
 };
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth/BluetoothSocket.cpp
@@ -0,0 +1,98 @@
+/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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 "BluetoothSocket.h"
+
+#include "BluetoothSocketObserver.h"
+#include "BluetoothUnixSocketConnector.h"
+#include "nsThreadUtils.h"
+
+using namespace mozilla::ipc;
+USING_BLUETOOTH_NAMESPACE
+
+BluetoothSocket::BluetoothSocket(BluetoothSocketObserver* aObserver,
+                                 BluetoothSocketType aType,
+                                 bool aAuth,
+                                 bool aEncrypt)
+  : mObserver(aObserver)
+  , mType(aType)
+  , mAuth(aAuth)
+  , mEncrypt(aEncrypt)
+{
+  MOZ_ASSERT(aObserver);
+}
+
+bool
+BluetoothSocket::Connect(const nsACString& aDeviceAddress, int aChannel)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(!aDeviceAddress.IsEmpty());
+
+  nsAutoPtr<BluetoothUnixSocketConnector> c(
+    new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt));
+
+  if (!ConnectSocket(c.forget(), aDeviceAddress.BeginReading())) {
+    nsAutoString addr;
+    GetAddress(addr);
+    BT_LOG("%s failed. Current connected device address: %s",
+           __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get());
+    return false;
+  }
+
+  return true;
+}
+
+bool
+BluetoothSocket::Listen(int aChannel)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  nsAutoPtr<BluetoothUnixSocketConnector> c(
+    new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt));
+
+  if (!ListenSocket(c.forget())) {
+    nsAutoString addr;
+    GetAddress(addr);
+    BT_LOG("%s failed. Current connected device address: %s",
+           __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get());
+    return false;
+  }
+
+  return true;
+}
+
+void
+BluetoothSocket::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(mObserver);
+  mObserver->ReceiveSocketData(aMessage);
+}
+
+void
+BluetoothSocket::OnConnectSuccess()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(mObserver);
+  mObserver->OnConnectSuccess();
+}
+
+void
+BluetoothSocket::OnConnectError()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(mObserver);
+  mObserver->OnConnectError();
+}
+
+void
+BluetoothSocket::OnDisconnect()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(mObserver);
+  mObserver->OnDisconnect();
+}
+
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth/BluetoothSocket.h
@@ -0,0 +1,52 @@
+/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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/. */
+
+#ifndef mozilla_dom_bluetooth_BluetoothSocket_h
+#define mozilla_dom_bluetooth_BluetoothSocket_h
+
+#include "BluetoothCommon.h"
+#include "mozilla/ipc/UnixSocket.h"
+
+BEGIN_BLUETOOTH_NAMESPACE
+
+class BluetoothSocketObserver;
+
+class BluetoothSocket : public mozilla::ipc::UnixSocketConsumer
+{
+public:
+  BluetoothSocket(BluetoothSocketObserver* aObserver,
+                  BluetoothSocketType aType,
+                  bool aAuth,
+                  bool aEncrypt);
+
+  bool Connect(const nsACString& aDeviceAddress, int aChannel);
+  bool Listen(int aChannel);
+  inline void Disconnect()
+  {
+    CloseSocket();
+  }
+
+  virtual void OnConnectSuccess() MOZ_OVERRIDE;
+  virtual void OnConnectError() MOZ_OVERRIDE;
+  virtual void OnDisconnect() MOZ_OVERRIDE;
+  virtual void ReceiveSocketData(
+    nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
+
+  inline void GetAddress(nsAString& aDeviceAddress)
+  {
+    GetSocketAddr(aDeviceAddress);
+  }
+
+private:
+  BluetoothSocketObserver* mObserver;
+  BluetoothSocketType mType;
+  bool mAuth;
+  bool mEncrypt;
+};
+
+END_BLUETOOTH_NAMESPACE
+
+#endif
--- a/dom/bluetooth/Makefile.in
+++ b/dom/bluetooth/Makefile.in
@@ -49,16 +49,17 @@ CPPSRCS += \
   BluetoothParent.cpp \
   BluetoothServiceChildProcess.cpp \
   BluetoothUnixSocketConnector.cpp \
   BluetoothHfpManager.cpp \
   BluetoothOppManager.cpp \
   ObexBase.cpp \
   BluetoothScoManager.cpp \
   BluetoothUuid.cpp \
+  BluetoothSocket.cpp \
   $(NULL)
 
 ifdef MOZ_B2G_RIL
 CPPSRCS += BluetoothTelephonyListener.cpp
 endif
 
 ifeq (gonk,$(MOZ_WIDGET_TOOLKIT))
 VPATH += \
--- a/ipc/unixsocket/UnixSocket.h
+++ b/ipc/unixsocket/UnixSocket.h
@@ -137,17 +137,17 @@ enum SocketConnectionStatus {
 
 class UnixSocketConsumer : public RefCounted<UnixSocketConsumer>
 {
 public:
   UnixSocketConsumer();
 
   virtual ~UnixSocketConsumer();
 
-  SocketConnectionStatus GetConnectionStatus()
+  SocketConnectionStatus GetConnectionStatus() const
   {
     return mConnectionStatus;
   }
 
   /**
    * Function to be called whenever data is received. This is only called on the
    * main thread.
    *