2013-04-04 17:52:12 -07:00
|
|
|
/* -*- 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);
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGD("%s failed. Current connected device address: %s",
|
2013-04-04 17:52:12 -07:00
|
|
|
__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);
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGD("%s failed. Current connected device address: %s",
|
2013-04-04 17:52:12 -07:00
|
|
|
__FUNCTION__, NS_ConvertUTF16toUTF8(addr).get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothSocket::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mObserver);
|
2013-04-04 17:55:55 -07:00
|
|
|
mObserver->ReceiveSocketData(this, aMessage);
|
2013-04-04 17:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothSocket::OnConnectSuccess()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mObserver);
|
2013-09-06 04:19:46 -07:00
|
|
|
mObserver->OnSocketConnectSuccess(this);
|
2013-04-04 17:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothSocket::OnConnectError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mObserver);
|
2013-09-06 04:19:46 -07:00
|
|
|
mObserver->OnSocketConnectError(this);
|
2013-04-04 17:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothSocket::OnDisconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mObserver);
|
2013-09-06 04:19:46 -07:00
|
|
|
mObserver->OnSocketDisconnect(this);
|
2013-04-04 17:52:12 -07:00
|
|
|
}
|
|
|
|
|