1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bluetooth/bluez/BluetoothSocket.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "BluetoothSocket.h" 1.11 + 1.12 +#include "BluetoothSocketObserver.h" 1.13 +#include "BluetoothUnixSocketConnector.h" 1.14 +#include "nsThreadUtils.h" 1.15 + 1.16 +using namespace mozilla::ipc; 1.17 +USING_BLUETOOTH_NAMESPACE 1.18 + 1.19 +BluetoothSocket::BluetoothSocket(BluetoothSocketObserver* aObserver, 1.20 + BluetoothSocketType aType, 1.21 + bool aAuth, 1.22 + bool aEncrypt) 1.23 + : mObserver(aObserver) 1.24 + , mType(aType) 1.25 + , mAuth(aAuth) 1.26 + , mEncrypt(aEncrypt) 1.27 +{ 1.28 + MOZ_ASSERT(aObserver); 1.29 +} 1.30 + 1.31 +bool 1.32 +BluetoothSocket::Connect(const nsACString& aDeviceAddress, int aChannel) 1.33 +{ 1.34 + MOZ_ASSERT(NS_IsMainThread()); 1.35 + MOZ_ASSERT(!aDeviceAddress.IsEmpty()); 1.36 + 1.37 + nsAutoPtr<BluetoothUnixSocketConnector> c( 1.38 + new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt)); 1.39 + 1.40 + if (!ConnectSocket(c.forget(), aDeviceAddress.BeginReading())) { 1.41 + nsAutoString addr; 1.42 + GetAddress(addr); 1.43 + BT_LOGD("%s failed. Current connected device address: %s", 1.44 + __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get()); 1.45 + return false; 1.46 + } 1.47 + 1.48 + return true; 1.49 +} 1.50 + 1.51 +bool 1.52 +BluetoothSocket::Listen(int aChannel) 1.53 +{ 1.54 + MOZ_ASSERT(NS_IsMainThread()); 1.55 + 1.56 + nsAutoPtr<BluetoothUnixSocketConnector> c( 1.57 + new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt)); 1.58 + 1.59 + if (!ListenSocket(c.forget())) { 1.60 + nsAutoString addr; 1.61 + GetAddress(addr); 1.62 + BT_LOGD("%s failed. Current connected device address: %s", 1.63 + __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get()); 1.64 + return false; 1.65 + } 1.66 + 1.67 + return true; 1.68 +} 1.69 + 1.70 +void 1.71 +BluetoothSocket::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) 1.72 +{ 1.73 + MOZ_ASSERT(NS_IsMainThread()); 1.74 + MOZ_ASSERT(mObserver); 1.75 + mObserver->ReceiveSocketData(this, aMessage); 1.76 +} 1.77 + 1.78 +void 1.79 +BluetoothSocket::OnConnectSuccess() 1.80 +{ 1.81 + MOZ_ASSERT(NS_IsMainThread()); 1.82 + MOZ_ASSERT(mObserver); 1.83 + mObserver->OnSocketConnectSuccess(this); 1.84 +} 1.85 + 1.86 +void 1.87 +BluetoothSocket::OnConnectError() 1.88 +{ 1.89 + MOZ_ASSERT(NS_IsMainThread()); 1.90 + MOZ_ASSERT(mObserver); 1.91 + mObserver->OnSocketConnectError(this); 1.92 +} 1.93 + 1.94 +void 1.95 +BluetoothSocket::OnDisconnect() 1.96 +{ 1.97 + MOZ_ASSERT(NS_IsMainThread()); 1.98 + MOZ_ASSERT(mObserver); 1.99 + mObserver->OnSocketDisconnect(this); 1.100 +} 1.101 +