Files
tubestation/dom/bluetooth/BluetoothOppManager.cpp
Eric Chou 564940f28b Bug 795410 - patch 1: Basic function prototype, r=qdot
From 2231ac9d0fb159edabb891f8b89afc176fb15c2b Mon Sep 17 00:00:00 2001
2012-09-28 18:59:12 +08:00

135 lines
3.4 KiB
C++

/* -*- 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 "base/basictypes.h"
#include "BluetoothOppManager.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothServiceUuid.h"
#include "ObexBase.h"
#include "mozilla/RefPtr.h"
#include "nsIDOMFile.h"
USING_BLUETOOTH_NAMESPACE
using namespace mozilla::ipc;
static mozilla::RefPtr<BluetoothOppManager> sInstance;
BluetoothOppManager::BluetoothOppManager() : mConnected(false)
, mConnectionId(1)
, mLastCommand(0)
{
}
BluetoothOppManager::~BluetoothOppManager()
{
}
//static
BluetoothOppManager*
BluetoothOppManager::Get()
{
MOZ_ASSERT(NS_IsMainThread());
if (sInstance == nullptr) {
sInstance = new BluetoothOppManager();
}
return sInstance;
}
bool
BluetoothOppManager::Connect(const nsAString& aDeviceObjectPath,
BluetoothReplyRunnable* aRunnable)
{
MOZ_ASSERT(NS_IsMainThread());
BluetoothService* bs = BluetoothService::Get();
if (!bs) {
NS_WARNING("BluetoothService not available!");
return false;
}
nsString serviceUuidStr =
NS_ConvertUTF8toUTF16(mozilla::dom::bluetooth::BluetoothServiceUuidStr::ObjectPush);
nsRefPtr<BluetoothReplyRunnable> runnable = aRunnable;
nsresult rv = bs->GetSocketViaService(aDeviceObjectPath,
serviceUuidStr,
BluetoothSocketType::RFCOMM,
true,
false,
this,
runnable);
runnable.forget();
return NS_FAILED(rv) ? false : true;
}
void
BluetoothOppManager::Disconnect()
{
CloseSocket();
}
bool
BluetoothOppManager::SendFile(BlobParent* aActor,
BluetoothReplyRunnable* aRunnable)
{
// will implement in another patch.
return true;
}
bool
BluetoothOppManager::StopSendingFile(BluetoothReplyRunnable* aRunnable)
{
// will implement in another patch.
return true;
}
// Virtual function of class SocketConsumer
void
BluetoothOppManager::ReceiveSocketData(UnixSocketRawData* aMessage)
{
uint8_t responseCode = aMessage->mData[0];
if (mLastCommand == ObexRequestCode::Connect) {
if (responseCode == ObexResponseCode::Success) {
mConnected = true;
}
} else if (mLastCommand == ObexRequestCode::Disconnect) {
if (responseCode == ObexResponseCode::Success) {
mConnected = false;
}
}
}
void
BluetoothOppManager::SendConnectReqeust()
{
// Section 3.3.1 "Connect", IrOBEX 1.2
// [opcode:1][length:2][version:1][flags:1][MaxPktSizeWeCanReceive:2][Headers:var]
uint8_t req[255];
int index = 7;
req[3] = 0x10; // version=1.0
req[4] = 0x00; // flag=0x00
req[5] = BluetoothOppManager::MAX_PACKET_LENGTH >> 8;
req[6] = BluetoothOppManager::MAX_PACKET_LENGTH;
index += AppendHeaderConnectionId(&req[index], mConnectionId++);
SetObexPacketInfo(req, ObexRequestCode::Connect, index);
mLastCommand = ObexRequestCode::Connect;
UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize);
SendSocketData(s);
}