Bug 851046 - BluetoothSocket. r=mrbkap, a=tef+
authorKyle Machulis <kyle@nonpolynomial.com>
Wed, 03 Apr 2013 19:04:26 -0700
changeset 119007 a3eb01854dcb499cd4108ce43604792a25068dea
parent 119006 15321131e53b287bf27a4c555d341e02ce1d6248
child 119008 52b929f9c1a45ccc65df3a468ff4e7373517024a
push id645
push userryanvm@gmail.com
push dateTue, 09 Apr 2013 16:07:57 +0000
reviewersmrbkap, tef
bugs851046
milestone18.0
Bug 851046 - BluetoothSocket. r=mrbkap, a=tef+
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
@@ -18,16 +18,17 @@
 #define USING_BLUETOOTH_NAMESPACE \
   using namespace mozilla::dom::bluetooth;
 
 #define LOCAL_AGENT_PATH  "/B2G/bluetooth/agent"
 #define REMOTE_AGENT_PATH "/B2G/bluetooth/remote_device_agent"
 
 // 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
@@ -52,16 +52,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 += BluetoothRilListener.cpp
 endif
 
 XPIDLSRCS = \
   nsIDOMNavigatorBluetooth.idl \
--- 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.
    *