Bug 851046: Patch 2 - New Class: BluetoothSocket; r=mrbkap
authorEric Chou <echou@mozilla.com>
Thu, 04 Apr 2013 17:25:44 -0700
changeset 134498 8439b019d59b324176c7c536b42d441b0c973bac
parent 134497 b76be1ee53501aecaaffd0fb214803c2c602638c
child 134499 9b22106f5492e10522296e22104076024ce935d8
push idunknown
push userunknown
push dateunknown
reviewersmrbkap
bugs851046
milestone23.0a1
Bug 851046: Patch 2 - New Class: BluetoothSocket; r=mrbkap
dom/bluetooth/BluetoothCommon.h
dom/bluetooth/BluetoothSocket.cpp
dom/bluetooth/BluetoothSocket.h
dom/bluetooth/BluetoothSocketObserver.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
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth/BluetoothSocketObserver.h
@@ -0,0 +1,28 @@
+/* -*- 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_BluetoothSocketObserver_h
+#define mozilla_dom_bluetooth_BluetoothSocketObserver_h
+
+#include "BluetoothCommon.h"
+#include <mozilla/ipc/UnixSocket.h>
+
+using namespace mozilla::ipc;
+
+BEGIN_BLUETOOTH_NAMESPACE
+
+class BluetoothSocketObserver
+{
+public:
+  virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) = 0;
+  virtual void OnConnectSuccess() = 0;
+  virtual void OnConnectError() = 0;
+  virtual void OnDisconnect() = 0;
+};
+
+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.
    *