2012-09-28 04:46:37 -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 "base/basictypes.h"
|
|
|
|
#include "BluetoothOppManager.h"
|
|
|
|
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "BluetoothService.h"
|
2012-10-03 02:06:40 -07:00
|
|
|
#include "BluetoothUtils.h"
|
2012-11-26 22:12:19 -08:00
|
|
|
#include "BluetoothUuid.h"
|
2012-09-27 07:59:27 -07:00
|
|
|
#include "ObexBase.h"
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2012-10-03 02:06:40 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2012-09-28 04:46:37 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-10-25 18:25:46 -07:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-12-20 02:36:58 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2012-11-26 04:51:29 -08:00
|
|
|
#include "nsCExternalHandlerService.h"
|
2012-10-25 18:25:46 -07:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
2012-10-30 20:15:12 -07:00
|
|
|
#include "nsIDOMFile.h"
|
|
|
|
#include "nsIFile.h"
|
2012-09-28 05:03:36 -07:00
|
|
|
#include "nsIInputStream.h"
|
2012-11-26 04:51:29 -08:00
|
|
|
#include "nsIMIMEService.h"
|
2012-10-30 20:15:12 -07:00
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2012-11-02 18:36:34 -07:00
|
|
|
#define TARGET_FOLDER "/sdcard/downloads/bluetooth/"
|
2012-09-28 04:46:37 -07:00
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
2012-10-25 18:25:46 -07:00
|
|
|
using namespace mozilla;
|
2012-09-28 04:46:37 -07:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
2012-10-25 18:25:46 -07:00
|
|
|
class BluetoothOppManagerObserver : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
BluetoothOppManagerObserver()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2012-12-12 00:54:54 -08:00
|
|
|
if (!obs || NS_FAILED(obs->AddObserver(this,
|
|
|
|
NS_XPCOM_SHUTDOWN_OBSERVER_ID,
|
|
|
|
false))) {
|
2012-10-25 18:25:46 -07:00
|
|
|
NS_WARNING("Failed to add shutdown observer!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Shutdown()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (!obs ||
|
|
|
|
(NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)))) {
|
|
|
|
NS_WARNING("Can't unregister observers, or already unregistered!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BluetoothOppManagerObserver()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2012-10-12 04:14:21 -07:00
|
|
|
// Sending system message "bluetooth-opp-update-progress" every 50kb
|
|
|
|
static const uint32_t kUpdateProgressBase = 50 * 1024;
|
2012-12-20 02:36:58 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The format of the header of an PUT request is
|
|
|
|
* [opcode:1][packet length:2][headerId:1][header length:2]
|
|
|
|
*/
|
|
|
|
static const uint32_t kPutRequestHeaderSize = 6;
|
|
|
|
|
2012-10-25 18:25:46 -07:00
|
|
|
StaticRefPtr<BluetoothOppManager> sInstance;
|
|
|
|
StaticRefPtr<BluetoothOppManagerObserver> sOppObserver;
|
2012-10-30 20:15:12 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME / Bug 806749
|
|
|
|
*
|
|
|
|
* Currently Bluetooth*Manager inherits mozilla::ipc::UnixSocketConsumer,
|
|
|
|
* which means that each Bluetooth*Manager can handle only one socket
|
|
|
|
* connection at a time. We need to support concurrent multiple socket
|
|
|
|
* connections, and then we will be able to have multiple file transferring
|
|
|
|
* sessions at a time.
|
|
|
|
*/
|
2012-10-05 01:11:21 -07:00
|
|
|
static uint32_t sSentFileLength = 0;
|
2012-10-03 02:06:40 -07:00
|
|
|
static nsString sFileName;
|
2012-10-16 23:48:52 -07:00
|
|
|
static uint32_t sFileLength = 0;
|
2012-10-12 04:14:21 -07:00
|
|
|
static nsString sContentType;
|
2012-10-25 18:25:46 -07:00
|
|
|
static bool sInShutdown = false;
|
2012-12-10 21:55:37 -08:00
|
|
|
static bool sWaitingToSendPutFinal = false;
|
2012-10-25 18:25:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothOppManagerObserver::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sInstance);
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
return sInstance->HandleShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "BluetoothOppManager got unexpected topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-09-28 05:03:36 -07:00
|
|
|
|
2013-02-25 02:49:18 -08:00
|
|
|
class SendSocketDataTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SendSocketDataTask(uint8_t* aStream, uint32_t aSize)
|
|
|
|
: mStream(aStream)
|
|
|
|
, mSize(aSize)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
sInstance->SendPutRequest(mStream, mSize);
|
|
|
|
sSentFileLength += mSize;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoArrayPtr<uint8_t> mStream;
|
|
|
|
uint32_t mSize;
|
|
|
|
};
|
|
|
|
|
2012-09-28 05:03:36 -07:00
|
|
|
class ReadFileTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-12-20 02:36:58 -08:00
|
|
|
ReadFileTask(nsIInputStream* aInputStream,
|
|
|
|
uint32_t aRemoteMaxPacketSize) : mInputStream(aInputStream)
|
2012-09-28 05:03:36 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-12-20 02:36:58 -08:00
|
|
|
|
|
|
|
mAvailablePacketSize = aRemoteMaxPacketSize - kPutRequestHeaderSize;
|
2012-09-28 05:03:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2012-10-30 20:15:12 -07:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-09-28 05:03:36 -07:00
|
|
|
|
|
|
|
uint32_t numRead;
|
2013-02-26 02:53:44 -08:00
|
|
|
nsAutoArrayPtr<char> buf(new char[mAvailablePacketSize]);
|
2012-09-28 05:03:36 -07:00
|
|
|
|
|
|
|
// function inputstream->Read() only works on non-main thread
|
2013-02-25 02:49:18 -08:00
|
|
|
nsresult rv = mInputStream->Read(buf, mAvailablePacketSize, &numRead);
|
2012-09-28 05:03:36 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Needs error handling here
|
2013-02-25 02:49:18 -08:00
|
|
|
NS_WARNING("Failed to read from input stream");
|
2012-09-28 05:03:36 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numRead > 0) {
|
2012-10-05 01:11:21 -07:00
|
|
|
if (sSentFileLength + numRead >= sFileLength) {
|
2012-12-10 21:55:37 -08:00
|
|
|
sWaitingToSendPutFinal = true;
|
2012-09-28 05:03:36 -07:00
|
|
|
}
|
2013-02-25 02:49:18 -08:00
|
|
|
|
|
|
|
nsRefPtr<SendSocketDataTask> task =
|
2013-02-26 02:53:44 -08:00
|
|
|
new SendSocketDataTask((uint8_t*)buf.forget(), numRead);
|
2013-02-25 02:49:18 -08:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(task))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-09-28 05:03:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2012-10-30 20:15:12 -07:00
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
2012-12-20 02:36:58 -08:00
|
|
|
uint32_t mAvailablePacketSize;
|
2012-09-28 05:03:36 -07:00
|
|
|
};
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2013-01-10 06:14:16 -08:00
|
|
|
class CloseSocketTask : public Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (!sInstance) {
|
|
|
|
NS_WARNING("BluetoothOppManager no longer exists, cannot close socket!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sInstance->GetConnectionStatus() ==
|
|
|
|
SocketConnectionStatus::SOCKET_CONNECTED) {
|
|
|
|
sInstance->CloseSocket();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-27 07:59:27 -07:00
|
|
|
BluetoothOppManager::BluetoothOppManager() : mConnected(false)
|
2012-09-28 05:03:36 -07:00
|
|
|
, mRemoteObexVersion(0)
|
|
|
|
, mRemoteConnectionFlags(0)
|
|
|
|
, mRemoteMaxPacketLength(0)
|
2012-12-07 04:12:57 -08:00
|
|
|
, mLastCommand(0)
|
2012-10-08 22:56:17 -07:00
|
|
|
, mPacketLeftLength(0)
|
2012-11-07 17:46:31 -08:00
|
|
|
, mBodySegmentLength(0)
|
2012-12-07 04:12:57 -08:00
|
|
|
, mReceivedDataBufferOffset(0)
|
|
|
|
, mAbortFlag(false)
|
|
|
|
, mNewFileFlag(false)
|
|
|
|
, mPutFinalFlag(false)
|
|
|
|
, mSendTransferCompleteFlag(false)
|
|
|
|
, mSuccessFlag(false)
|
|
|
|
, mWaitingForConfirmationFlag(false)
|
2012-09-28 04:46:37 -07:00
|
|
|
{
|
2012-10-30 23:07:06 -07:00
|
|
|
mConnectedDeviceAddress.AssignLiteral("00:00:00:00:00:00");
|
2012-11-02 18:36:19 -07:00
|
|
|
mSocketStatus = GetConnectionStatus();
|
2012-09-28 04:46:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2012-11-13 22:29:11 -08:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTED ||
|
|
|
|
GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTING) {
|
|
|
|
NS_WARNING("BluetoothOppManager has been already connected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseSocket();
|
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-26 22:12:19 -08:00
|
|
|
nsString uuid;
|
|
|
|
BluetoothUuidHelper::GetString(BluetoothServiceClass::OBJECT_PUSH, uuid);
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2012-11-13 22:29:11 -08:00
|
|
|
mRunnable = aRunnable;
|
2012-09-28 04:46:37 -07:00
|
|
|
|
|
|
|
nsresult rv = bs->GetSocketViaService(aDeviceObjectPath,
|
2012-11-26 22:12:19 -08:00
|
|
|
uuid,
|
2012-09-28 04:46:37 -07:00
|
|
|
BluetoothSocketType::RFCOMM,
|
|
|
|
true,
|
2012-10-31 02:33:50 -07:00
|
|
|
true,
|
2012-09-28 04:46:37 -07:00
|
|
|
this,
|
2012-11-13 22:29:11 -08:00
|
|
|
mRunnable);
|
2012-09-28 04:46:37 -07:00
|
|
|
|
|
|
|
return NS_FAILED(rv) ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::Disconnect()
|
|
|
|
{
|
2012-11-13 22:29:11 -08:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_DISCONNECTED) {
|
|
|
|
NS_WARNING("BluetoothOppManager has been disconnected!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
|
2012-10-25 18:25:46 -07:00
|
|
|
nsresult
|
|
|
|
BluetoothOppManager::HandleShutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
sInShutdown = true;
|
|
|
|
CloseSocket();
|
|
|
|
sInstance = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-14 23:16:41 -07:00
|
|
|
bool
|
|
|
|
BluetoothOppManager::Listen()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-11-27 23:59:20 -08:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_LISTENING) {
|
|
|
|
NS_WARNING("BluetoothOppManager has been already listening");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-14 23:16:41 -07:00
|
|
|
CloseSocket();
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-26 22:12:19 -08:00
|
|
|
nsresult rv =
|
|
|
|
bs->ListenSocketViaService(BluetoothReservedChannels::CHANNEL_OPUSH,
|
|
|
|
BluetoothSocketType::RFCOMM,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
this);
|
2012-11-02 18:36:19 -07:00
|
|
|
mSocketStatus = GetConnectionStatus();
|
|
|
|
|
2012-10-14 23:16:41 -07:00
|
|
|
return NS_FAILED(rv) ? false : true;
|
|
|
|
}
|
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
bool
|
2012-11-02 18:36:43 -07:00
|
|
|
BluetoothOppManager::SendFile(BlobParent* aActor)
|
2012-09-28 04:46:37 -07:00
|
|
|
{
|
2012-09-28 05:03:36 -07:00
|
|
|
if (mBlob) {
|
|
|
|
// Means there's a sending process. Reply error.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process of sending a file:
|
|
|
|
* - Keep blob because OPP connection has not been established yet.
|
2012-11-20 23:53:19 -08:00
|
|
|
* - Try to retrieve file name from the blob or assign one if failed to get.
|
2012-09-28 05:03:36 -07:00
|
|
|
* - Create an OPP connection by SendConnectRequest()
|
|
|
|
* - After receiving the response, start to read file and send.
|
|
|
|
*/
|
|
|
|
mBlob = aActor->GetBlob();
|
|
|
|
|
2012-11-26 04:51:29 -08:00
|
|
|
sFileName.Truncate();
|
2012-11-20 23:53:19 -08:00
|
|
|
|
2012-11-26 04:51:29 -08:00
|
|
|
nsCOMPtr<nsIDOMFile> file = do_QueryInterface(mBlob);
|
|
|
|
if (file) {
|
|
|
|
file->GetName(sFileName);
|
2012-11-13 22:29:11 -08:00
|
|
|
}
|
|
|
|
|
2012-11-26 04:51:29 -08:00
|
|
|
/**
|
|
|
|
* We try our best to get the file extention to avoid interoperability issues.
|
|
|
|
* However, once we found that we are unable to get suitable extension or
|
|
|
|
* information about the content type, sending a pre-defined file name without
|
|
|
|
* extension would be fine.
|
|
|
|
*/
|
2012-11-13 22:29:11 -08:00
|
|
|
if (sFileName.IsEmpty()) {
|
|
|
|
sFileName.AssignLiteral("Unknown");
|
|
|
|
}
|
|
|
|
|
2012-11-26 04:51:29 -08:00
|
|
|
int32_t offset = sFileName.RFindChar('/');
|
|
|
|
if (offset != kNotFound) {
|
|
|
|
sFileName = Substring(sFileName, offset + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = sFileName.RFindChar('.');
|
|
|
|
if (offset == kNotFound) {
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeSvc = do_GetService(NS_MIMESERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
if (mimeSvc) {
|
|
|
|
nsString mimeType;
|
|
|
|
mBlob->GetType(mimeType);
|
|
|
|
|
|
|
|
nsCString extension;
|
|
|
|
nsresult rv =
|
|
|
|
mimeSvc->GetPrimaryExtension(NS_LossyConvertUTF16toASCII(mimeType),
|
|
|
|
EmptyCString(),
|
|
|
|
extension);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
sFileName.AppendLiteral(".");
|
|
|
|
AppendUTF8toUTF16(extension, sFileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 05:03:36 -07:00
|
|
|
SendConnectRequest();
|
2012-11-30 01:49:08 -08:00
|
|
|
mTransferMode = false;
|
2013-02-24 20:40:31 -08:00
|
|
|
StartFileTransfer();
|
2012-09-28 05:03:36 -07:00
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-11-02 18:36:43 -07:00
|
|
|
BluetoothOppManager::StopSendingFile()
|
2012-09-28 04:46:37 -07:00
|
|
|
{
|
2012-10-03 00:09:27 -07:00
|
|
|
mAbortFlag = true;
|
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:36:43 -07:00
|
|
|
bool
|
|
|
|
BluetoothOppManager::ConfirmReceivingFile(bool aConfirm)
|
2012-10-18 23:16:13 -07:00
|
|
|
{
|
2012-11-27 23:59:20 -08:00
|
|
|
if (!mConnected) return false;
|
|
|
|
|
2012-10-18 23:16:13 -07:00
|
|
|
if (!mWaitingForConfirmationFlag) {
|
|
|
|
NS_WARNING("We are not waiting for a confirmation now.");
|
2012-11-02 18:36:43 -07:00
|
|
|
return false;
|
2012-10-18 23:16:13 -07:00
|
|
|
}
|
2012-11-07 17:46:31 -08:00
|
|
|
mWaitingForConfirmationFlag = false;
|
2012-10-18 23:16:13 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(mPacketLeftLength == 0,
|
|
|
|
"Should not be in the middle of receiving a PUT packet.");
|
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
// For the first packet of first file
|
2012-11-27 23:59:20 -08:00
|
|
|
bool success = false;
|
|
|
|
if (aConfirm) {
|
|
|
|
StartFileTransfer();
|
2012-12-07 04:12:57 -08:00
|
|
|
if (CreateFile()) {
|
|
|
|
success = WriteToFile(mBodySegment.get(), mBodySegmentLength);
|
|
|
|
}
|
2012-10-18 23:16:13 -07:00
|
|
|
}
|
2012-11-02 18:36:43 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
if (success && mPutFinalFlag) {
|
|
|
|
mSuccessFlag = true;
|
|
|
|
FileTransferComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplyToPut(mPutFinalFlag, success);
|
2012-11-02 18:36:43 -07:00
|
|
|
return true;
|
2012-10-18 23:16:13 -07:00
|
|
|
}
|
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
void
|
|
|
|
BluetoothOppManager::AfterFirstPut()
|
|
|
|
{
|
|
|
|
mUpdateProgressCounter = 1;
|
|
|
|
mPutFinalFlag = false;
|
|
|
|
mReceivedDataBufferOffset = 0;
|
|
|
|
mSendTransferCompleteFlag = false;
|
|
|
|
sSentFileLength = 0;
|
2012-12-10 21:55:37 -08:00
|
|
|
sWaitingToSendPutFinal = false;
|
2012-12-07 04:12:57 -08:00
|
|
|
mSuccessFlag = false;
|
|
|
|
}
|
|
|
|
|
2012-10-30 20:15:12 -07:00
|
|
|
void
|
|
|
|
BluetoothOppManager::AfterOppConnected()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mConnected = true;
|
|
|
|
mAbortFlag = false;
|
2012-11-07 17:46:31 -08:00
|
|
|
mWaitingForConfirmationFlag = true;
|
2012-12-07 04:12:57 -08:00
|
|
|
AfterFirstPut();
|
2012-10-30 20:15:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::AfterOppDisconnected()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mConnected = false;
|
|
|
|
mLastCommand = 0;
|
|
|
|
mBlob = nullptr;
|
2013-03-25 19:53:10 -07:00
|
|
|
mPacketLeftLength = 0;
|
2012-10-30 20:15:12 -07:00
|
|
|
|
2012-11-30 01:49:08 -08:00
|
|
|
// We can't reset mSuccessFlag here since this function may be called
|
|
|
|
// before we send system message of transfer complete
|
|
|
|
// mSuccessFlag = false;
|
|
|
|
|
2012-10-30 20:15:12 -07:00
|
|
|
if (mInputStream) {
|
|
|
|
mInputStream->Close();
|
|
|
|
mInputStream = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOutputStream) {
|
|
|
|
mOutputStream->Close();
|
|
|
|
mOutputStream = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReadFileThread) {
|
|
|
|
mReadFileThread->Shutdown();
|
|
|
|
mReadFileThread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:36:49 -07:00
|
|
|
void
|
|
|
|
BluetoothOppManager::DeleteReceivedFile()
|
|
|
|
{
|
|
|
|
nsString path;
|
|
|
|
path.AssignLiteral(TARGET_FOLDER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
nsresult rv = NS_NewLocalFile(path + sFileName, false, getter_AddRefs(f));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Couldn't find received file, nothing to delete.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOutputStream) {
|
|
|
|
mOutputStream->Close();
|
|
|
|
mOutputStream = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->Remove(false);
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:46:31 -08:00
|
|
|
bool
|
|
|
|
BluetoothOppManager::CreateFile()
|
|
|
|
{
|
|
|
|
nsString path;
|
|
|
|
path.AssignLiteral(TARGET_FOLDER);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mPacketLeftLength == 0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
nsresult rv;
|
|
|
|
rv = NS_NewLocalFile(path + sFileName, false, getter_AddRefs(f));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Couldn't new a local file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = f->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 00644);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Couldn't create the file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The function CreateUnique() may create a file with a different file
|
|
|
|
* name from the original sFileName. Therefore we have to retrieve
|
|
|
|
* the file name again.
|
|
|
|
*/
|
|
|
|
f->GetLeafName(sFileName);
|
|
|
|
|
2012-12-12 00:54:54 -08:00
|
|
|
mDsFile = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeSvc = do_GetService(NS_MIMESERVICE_CONTRACTID);
|
|
|
|
if (mimeSvc) {
|
|
|
|
nsCString mimeType;
|
|
|
|
nsresult rv = mimeSvc->GetTypeFromFile(f, mimeType);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (StringBeginsWith(mimeType, NS_LITERAL_CSTRING("image/"))) {
|
|
|
|
mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("pictures"), f);
|
|
|
|
} else if (StringBeginsWith(mimeType, NS_LITERAL_CSTRING("video/"))) {
|
|
|
|
mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("movies"), f);
|
|
|
|
} else if (StringBeginsWith(mimeType, NS_LITERAL_CSTRING("audio/"))) {
|
|
|
|
mDsFile = new DeviceStorageFile(NS_LITERAL_STRING("music"), f);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Couldn't recognize the mimetype of received file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:46:31 -08:00
|
|
|
NS_NewLocalFileOutputStream(getter_AddRefs(mOutputStream), f);
|
|
|
|
if (!mOutputStream) {
|
|
|
|
NS_WARNING("Couldn't new an output stream");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothOppManager::WriteToFile(const uint8_t* aData, int aDataLength)
|
|
|
|
{
|
|
|
|
if (!mOutputStream) {
|
|
|
|
NS_WARNING("No available output stream");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t wrote = 0;
|
|
|
|
mOutputStream->Write((const char*)aData, aDataLength, &wrote);
|
|
|
|
if (aDataLength != wrote) {
|
|
|
|
NS_WARNING("Writing to the file failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
// Virtual function of class SocketConsumer
|
|
|
|
void
|
2012-12-07 04:12:57 -08:00
|
|
|
BluetoothOppManager::ExtractPacketHeaders(const ObexHeaderSet& aHeader)
|
|
|
|
{
|
|
|
|
if (aHeader.Has(ObexHeaderId::Name)) {
|
|
|
|
aHeader.GetName(sFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHeader.Has(ObexHeaderId::Type)) {
|
|
|
|
aHeader.GetContentType(sContentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHeader.Has(ObexHeaderId::Length)) {
|
|
|
|
aHeader.GetLength(&sFileLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHeader.Has(ObexHeaderId::Body) ||
|
|
|
|
aHeader.Has(ObexHeaderId::EndOfBody)) {
|
|
|
|
uint8_t* bodyPtr;
|
|
|
|
aHeader.GetBody(&bodyPtr);
|
|
|
|
mBodySegment = bodyPtr;
|
|
|
|
|
|
|
|
aHeader.GetBodyLength(&mBodySegmentLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothOppManager::ExtractBlobHeaders()
|
|
|
|
{
|
|
|
|
nsresult rv = mBlob->GetType(sContentType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't get content type");
|
|
|
|
SendDisconnectRequest();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t fileLength;
|
|
|
|
rv = mBlob->GetSize(&fileLength);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't get file size");
|
|
|
|
SendDisconnectRequest();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently we keep the size of files which were sent/received via
|
|
|
|
// Bluetooth not exceed UINT32_MAX because the Length header in OBEX
|
|
|
|
// is only 4-byte long. Although it is possible to transfer a file
|
|
|
|
// larger than UINT32_MAX, it needs to parse another OBEX Header
|
|
|
|
// and I would like to leave it as a feature.
|
|
|
|
if (fileLength > (uint64_t)UINT32_MAX) {
|
|
|
|
NS_WARNING("The file size is too large for now");
|
|
|
|
SendDisconnectRequest();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sFileLength = fileLength;
|
|
|
|
rv = NS_NewThread(getter_AddRefs(mReadFileThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't create thread");
|
|
|
|
SendDisconnectRequest();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-16 02:23:28 -08:00
|
|
|
bool
|
|
|
|
BluetoothOppManager::IsReservedChar(PRUnichar c)
|
|
|
|
{
|
|
|
|
return (c < 0x0020 ||
|
|
|
|
c == PRUnichar('?') || c == PRUnichar('|') || c == PRUnichar('<') ||
|
|
|
|
c == PRUnichar('>') || c == PRUnichar('"') || c == PRUnichar(':') ||
|
|
|
|
c == PRUnichar('/') || c == PRUnichar('*') || c == PRUnichar('\\'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::ValidateFileName()
|
|
|
|
{
|
|
|
|
int length = sFileName.Length();
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
// Replace reserved char of fat file system with '_'
|
|
|
|
if (IsReservedChar(sFileName.CharAt(i))) {
|
|
|
|
sFileName.Replace(i, 1, PRUnichar('_'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
void
|
|
|
|
BluetoothOppManager::ServerDataHandler(UnixSocketRawData* aMessage)
|
2012-09-28 04:46:37 -07:00
|
|
|
{
|
2012-10-08 22:56:17 -07:00
|
|
|
uint8_t opCode;
|
|
|
|
int packetLength;
|
2012-09-28 05:03:36 -07:00
|
|
|
int receivedLength = aMessage->mSize;
|
2012-09-27 07:59:27 -07:00
|
|
|
|
2012-10-08 22:56:17 -07:00
|
|
|
if (mPacketLeftLength > 0) {
|
2012-12-07 04:12:57 -08:00
|
|
|
opCode = mPutFinalFlag ? ObexRequestCode::PutFinal : ObexRequestCode::Put;
|
2012-10-08 22:56:17 -07:00
|
|
|
packetLength = mPacketLeftLength;
|
|
|
|
} else {
|
|
|
|
opCode = aMessage->mData[0];
|
|
|
|
packetLength = (((int)aMessage->mData[1]) << 8) | aMessage->mData[2];
|
2012-12-07 04:12:57 -08:00
|
|
|
|
|
|
|
// When there's a Put packet right after a PutFinal packet,
|
|
|
|
// which means it's the start point of a new file.
|
|
|
|
if (mPutFinalFlag &&
|
|
|
|
(opCode == ObexRequestCode::Put || opCode == ObexRequestCode::PutFinal)) {
|
|
|
|
mNewFileFlag = true;
|
|
|
|
AfterFirstPut();
|
|
|
|
}
|
2012-10-08 22:56:17 -07:00
|
|
|
}
|
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
ObexHeaderSet pktHeaders(opCode);
|
|
|
|
if (opCode == ObexRequestCode::Connect) {
|
|
|
|
// Section 3.3.1 "Connect", IrOBEX 1.2
|
|
|
|
// [opcode:1][length:2][version:1][flags:1][MaxPktSizeWeCanReceive:2]
|
|
|
|
// [Headers:var]
|
|
|
|
ParseHeaders(&aMessage->mData[7],
|
|
|
|
receivedLength - 7,
|
|
|
|
&pktHeaders);
|
|
|
|
ReplyToConnect();
|
|
|
|
AfterOppConnected();
|
|
|
|
mTransferMode = true;
|
|
|
|
} else if (opCode == ObexRequestCode::Disconnect ||
|
|
|
|
opCode == ObexRequestCode::Abort) {
|
|
|
|
// Section 3.3.2 "Disconnect", IrOBEX 1.2
|
|
|
|
// Section 3.3.5 "Abort", IrOBEX 1.2
|
|
|
|
// [opcode:1][length:2][Headers:var]
|
|
|
|
ParseHeaders(&aMessage->mData[3],
|
|
|
|
receivedLength - 3,
|
|
|
|
&pktHeaders);
|
|
|
|
ReplyToDisconnect();
|
|
|
|
AfterOppDisconnected();
|
|
|
|
if (opCode == ObexRequestCode::Abort) {
|
|
|
|
DeleteReceivedFile();
|
|
|
|
}
|
|
|
|
FileTransferComplete();
|
|
|
|
} else if (opCode == ObexRequestCode::Put ||
|
|
|
|
opCode == ObexRequestCode::PutFinal) {
|
|
|
|
int headerStartIndex = 0;
|
2012-09-28 05:03:36 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
// The first part of each Put packet
|
|
|
|
if (mReceivedDataBufferOffset == 0) {
|
|
|
|
// Section 3.3.3 "Put", IrOBEX 1.2
|
|
|
|
// [opcode:1][length:2][Headers:var]
|
|
|
|
headerStartIndex = 3;
|
|
|
|
// The largest buffer size is 65535 because packetLength is a
|
|
|
|
// 2-byte value (0 ~ 0xFFFF).
|
|
|
|
mReceivedDataBuffer = new uint8_t[packetLength];
|
|
|
|
mPacketLeftLength = packetLength;
|
2012-09-28 05:03:36 -07:00
|
|
|
|
2012-11-13 22:29:11 -08:00
|
|
|
/*
|
2012-12-07 04:12:57 -08:00
|
|
|
* A PUT request from remote devices may be divided into multiple parts.
|
|
|
|
* In other words, one request may need to be received multiple times,
|
|
|
|
* so here we keep a variable mPacketLeftLength to indicate if current
|
|
|
|
* PUT request is done.
|
2012-11-13 22:29:11 -08:00
|
|
|
*/
|
2012-12-07 04:12:57 -08:00
|
|
|
mPutFinalFlag = (opCode == ObexRequestCode::PutFinal);
|
|
|
|
}
|
2012-10-12 04:14:21 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
memcpy(mReceivedDataBuffer.get() + mReceivedDataBufferOffset,
|
|
|
|
&aMessage->mData[headerStartIndex],
|
|
|
|
receivedLength - headerStartIndex);
|
2012-10-16 23:48:52 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
mPacketLeftLength -= receivedLength;
|
|
|
|
mReceivedDataBufferOffset += receivedLength - headerStartIndex;
|
|
|
|
if (mPacketLeftLength) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-16 23:48:52 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
// A Put packet is received completely
|
|
|
|
ParseHeaders(mReceivedDataBuffer.get(),
|
|
|
|
mReceivedDataBufferOffset,
|
|
|
|
&pktHeaders);
|
|
|
|
ExtractPacketHeaders(pktHeaders);
|
2013-01-16 02:23:28 -08:00
|
|
|
ValidateFileName();
|
2012-10-03 00:09:27 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
mReceivedDataBufferOffset = 0;
|
|
|
|
|
|
|
|
// When we cancel the transfer, delete the file and notify complemention
|
|
|
|
if (mAbortFlag) {
|
|
|
|
ReplyToPut(mPutFinalFlag, !mAbortFlag);
|
|
|
|
sSentFileLength += mBodySegmentLength;
|
|
|
|
DeleteReceivedFile();
|
|
|
|
FileTransferComplete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until get confirmation from user, then create file and write to it
|
|
|
|
if (mWaitingForConfirmationFlag) {
|
|
|
|
ReceivingFileConfirmation();
|
|
|
|
sSentFileLength += mBodySegmentLength;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already get confirmation from user, create a new file if needed and
|
|
|
|
// write to output stream
|
|
|
|
if (mNewFileFlag) {
|
|
|
|
StartFileTransfer();
|
|
|
|
if (!CreateFile()) {
|
|
|
|
ReplyToPut(mPutFinalFlag, false);
|
2012-11-13 22:29:11 -08:00
|
|
|
return;
|
2012-09-28 05:03:36 -07:00
|
|
|
}
|
2012-12-07 04:12:57 -08:00
|
|
|
mNewFileFlag = false;
|
|
|
|
}
|
2012-11-13 22:29:11 -08:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
if (!WriteToFile(mBodySegment.get(), mBodySegmentLength)) {
|
|
|
|
ReplyToPut(mPutFinalFlag, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplyToPut(mPutFinalFlag, true);
|
|
|
|
|
|
|
|
// Send progress update
|
|
|
|
sSentFileLength += mBodySegmentLength;
|
|
|
|
if (sSentFileLength > kUpdateProgressBase * mUpdateProgressCounter) {
|
|
|
|
UpdateProgress();
|
|
|
|
mUpdateProgressCounter = sSentFileLength / kUpdateProgressBase + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success to receive a file and notify completion
|
|
|
|
if (mPutFinalFlag) {
|
|
|
|
mSuccessFlag = true;
|
|
|
|
FileTransferComplete();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Unhandled ObexRequestCode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::ClientDataHandler(UnixSocketRawData* aMessage)
|
|
|
|
{
|
|
|
|
uint8_t opCode;
|
|
|
|
int packetLength;
|
|
|
|
|
|
|
|
if (mPacketLeftLength > 0) {
|
|
|
|
opCode = mPutFinalFlag ? ObexRequestCode::PutFinal : ObexRequestCode::Put;
|
|
|
|
packetLength = mPacketLeftLength;
|
|
|
|
} else {
|
|
|
|
opCode = aMessage->mData[0];
|
|
|
|
packetLength = (((int)aMessage->mData[1]) << 8) | aMessage->mData[2];
|
|
|
|
}
|
|
|
|
|
2013-02-24 20:40:31 -08:00
|
|
|
// Check response code and send out system message as finished if the reponse
|
|
|
|
// code is somehow incorrect.
|
|
|
|
uint8_t expectedOpCode = ObexResponseCode::Success;
|
|
|
|
if (mLastCommand == ObexRequestCode::Put) {
|
|
|
|
expectedOpCode = ObexResponseCode::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opCode != expectedOpCode) {
|
|
|
|
if (mLastCommand == ObexRequestCode::Put ||
|
|
|
|
mLastCommand == ObexRequestCode::Abort ||
|
|
|
|
mLastCommand == ObexRequestCode::PutFinal) {
|
|
|
|
SendDisconnectRequest();
|
2012-09-27 07:59:27 -07:00
|
|
|
}
|
2013-02-24 20:40:31 -08:00
|
|
|
nsAutoCString str;
|
|
|
|
str += "[OPP] 0x";
|
|
|
|
str.AppendInt(mLastCommand, 16);
|
|
|
|
str += " failed";
|
|
|
|
NS_WARNING(str.get());
|
|
|
|
FileTransferComplete();
|
|
|
|
return;
|
2012-12-07 04:12:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mLastCommand == ObexRequestCode::PutFinal) {
|
|
|
|
mSuccessFlag = true;
|
|
|
|
FileTransferComplete();
|
|
|
|
SendDisconnectRequest();
|
|
|
|
} else if (mLastCommand == ObexRequestCode::Abort) {
|
|
|
|
SendDisconnectRequest();
|
|
|
|
FileTransferComplete();
|
2012-09-27 07:59:27 -07:00
|
|
|
} else if (mLastCommand == ObexRequestCode::Disconnect) {
|
2012-10-30 20:15:12 -07:00
|
|
|
AfterOppDisconnected();
|
2012-12-10 21:55:37 -08:00
|
|
|
// Most devices will directly terminate connection after receiving
|
2013-01-10 06:14:16 -08:00
|
|
|
// Disconnect request, so we make a delay here. If the socket hasn't been
|
|
|
|
// disconnected, we will close it.
|
|
|
|
MessageLoop::current()->
|
|
|
|
PostDelayedTask(FROM_HERE, new CloseSocketTask(), 1000);
|
2012-12-07 04:12:57 -08:00
|
|
|
} else if (mLastCommand == ObexRequestCode::Connect) {
|
|
|
|
MOZ_ASSERT(!sFileName.IsEmpty());
|
|
|
|
MOZ_ASSERT(mBlob);
|
|
|
|
|
|
|
|
AfterOppConnected();
|
2012-10-30 20:15:12 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
// Keep remote information
|
|
|
|
mRemoteObexVersion = aMessage->mData[3];
|
|
|
|
mRemoteConnectionFlags = aMessage->mData[4];
|
|
|
|
mRemoteMaxPacketLength =
|
|
|
|
(((int)(aMessage->mData[5]) << 8) | aMessage->mData[6]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before sending content, we have to send a header including
|
|
|
|
* information such as file name, file length and content type.
|
|
|
|
*/
|
|
|
|
if (ExtractBlobHeaders()) {
|
|
|
|
sInstance->SendPutHeaderRequest(sFileName, sFileLength);
|
|
|
|
}
|
|
|
|
} else if (mLastCommand == ObexRequestCode::Put) {
|
2012-12-10 21:55:37 -08:00
|
|
|
|
|
|
|
// Send PutFinal packet when we get response
|
|
|
|
if (sWaitingToSendPutFinal) {
|
|
|
|
SendPutFinalRequest();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-13 22:29:11 -08:00
|
|
|
if (mAbortFlag) {
|
2012-10-30 20:15:12 -07:00
|
|
|
SendAbortRequest();
|
|
|
|
return;
|
|
|
|
}
|
2012-10-05 01:11:21 -07:00
|
|
|
|
2012-10-30 20:15:12 -07:00
|
|
|
if (kUpdateProgressBase * mUpdateProgressCounter < sSentFileLength) {
|
2012-11-27 23:59:20 -08:00
|
|
|
UpdateProgress();
|
2012-10-30 20:15:12 -07:00
|
|
|
mUpdateProgressCounter = sSentFileLength / kUpdateProgressBase + 1;
|
|
|
|
}
|
2012-09-28 05:03:36 -07:00
|
|
|
|
2012-11-27 23:59:20 -08:00
|
|
|
nsresult rv;
|
2012-11-13 22:29:11 -08:00
|
|
|
if (!mInputStream) {
|
2012-11-27 23:59:20 -08:00
|
|
|
rv = mBlob->GetInternalStream(getter_AddRefs(mInputStream));
|
2012-10-30 20:15:12 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't get internal stream of blob");
|
2012-11-13 22:29:11 -08:00
|
|
|
SendDisconnectRequest();
|
2012-10-30 20:15:12 -07:00
|
|
|
return;
|
2012-09-28 05:03:36 -07:00
|
|
|
}
|
|
|
|
}
|
2012-10-30 20:15:12 -07:00
|
|
|
|
2012-12-20 02:36:58 -08:00
|
|
|
nsRefPtr<ReadFileTask> task = new ReadFileTask(mInputStream,
|
|
|
|
mRemoteMaxPacketLength);
|
2012-11-27 23:59:20 -08:00
|
|
|
rv = mReadFileThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Cannot dispatch read file task!");
|
2012-11-13 22:29:11 -08:00
|
|
|
SendDisconnectRequest();
|
2012-10-30 20:15:12 -07:00
|
|
|
}
|
2012-10-08 22:56:17 -07:00
|
|
|
} else {
|
2012-12-07 04:12:57 -08:00
|
|
|
NS_WARNING("Unhandled ObexRequestCode");
|
|
|
|
}
|
|
|
|
}
|
2012-10-16 23:48:52 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
// Virtual function of class SocketConsumer
|
|
|
|
void
|
2013-02-01 04:28:15 -08:00
|
|
|
BluetoothOppManager::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
|
2012-12-07 04:12:57 -08:00
|
|
|
{
|
|
|
|
if (mLastCommand) {
|
|
|
|
ClientDataHandler(aMessage);
|
|
|
|
return;
|
2012-09-27 07:59:27 -07:00
|
|
|
}
|
2012-12-07 04:12:57 -08:00
|
|
|
|
|
|
|
ServerDataHandler(aMessage);
|
2012-09-27 07:59:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-28 05:03:36 -07:00
|
|
|
BluetoothOppManager::SendConnectRequest()
|
2012-09-27 07:59:27 -07:00
|
|
|
{
|
2012-09-28 05:03:36 -07:00
|
|
|
if (mConnected) return;
|
|
|
|
|
2012-09-27 07:59:27 -07:00
|
|
|
// Section 3.3.1 "Connect", IrOBEX 1.2
|
2012-10-08 22:56:17 -07:00
|
|
|
// [opcode:1][length:2][version:1][flags:1][MaxPktSizeWeCanReceive:2]
|
|
|
|
// [Headers:var]
|
2012-09-27 07:59:27 -07:00
|
|
|
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;
|
2012-11-02 18:36:34 -07:00
|
|
|
req[6] = (uint8_t)BluetoothOppManager::MAX_PACKET_LENGTH;
|
2012-09-27 07:59:27 -07:00
|
|
|
|
|
|
|
SetObexPacketInfo(req, ObexRequestCode::Connect, index);
|
|
|
|
mLastCommand = ObexRequestCode::Connect;
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
2012-09-28 04:46:37 -07:00
|
|
|
}
|
|
|
|
|
2012-09-28 05:03:36 -07:00
|
|
|
void
|
2012-10-08 22:56:17 -07:00
|
|
|
BluetoothOppManager::SendPutHeaderRequest(const nsAString& aFileName,
|
|
|
|
int aFileSize)
|
2012-09-28 05:03:36 -07:00
|
|
|
{
|
|
|
|
uint8_t* req = new uint8_t[mRemoteMaxPacketLength];
|
|
|
|
|
2012-11-02 18:36:34 -07:00
|
|
|
int len = aFileName.Length();
|
2012-09-28 05:03:36 -07:00
|
|
|
uint8_t* fileName = new uint8_t[(len + 1) * 2];
|
2012-11-02 18:36:34 -07:00
|
|
|
const PRUnichar* fileNamePtr = aFileName.BeginReading();
|
|
|
|
|
2012-09-28 05:03:36 -07:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
fileName[i * 2] = (uint8_t)(fileNamePtr[i] >> 8);
|
|
|
|
fileName[i * 2 + 1] = (uint8_t)fileNamePtr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName[len * 2] = 0x00;
|
|
|
|
fileName[len * 2 + 1] = 0x00;
|
|
|
|
|
|
|
|
int index = 3;
|
|
|
|
index += AppendHeaderName(&req[index], (char*)fileName, (len + 1) * 2);
|
|
|
|
index += AppendHeaderLength(&req[index], aFileSize);
|
|
|
|
|
|
|
|
SetObexPacketInfo(req, ObexRequestCode::Put, index);
|
|
|
|
mLastCommand = ObexRequestCode::Put;
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
|
|
|
|
delete [] fileName;
|
|
|
|
delete [] req;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::SendPutRequest(uint8_t* aFileBody,
|
2012-12-10 21:55:37 -08:00
|
|
|
int aFileBodyLength)
|
2012-09-28 05:03:36 -07:00
|
|
|
{
|
2012-12-20 02:36:58 -08:00
|
|
|
int packetLeftSpace = mRemoteMaxPacketLength - kPutRequestHeaderSize;
|
2012-09-28 05:03:36 -07:00
|
|
|
|
|
|
|
if (!mConnected) return;
|
|
|
|
if (aFileBodyLength > packetLeftSpace) {
|
|
|
|
NS_WARNING("Not allowed such a small MaxPacketLength value");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-08 22:56:17 -07:00
|
|
|
// Section 3.3.3 "Put", IrOBEX 1.2
|
2012-09-28 05:03:36 -07:00
|
|
|
// [opcode:1][length:2][Headers:var]
|
|
|
|
uint8_t* req = new uint8_t[mRemoteMaxPacketLength];
|
|
|
|
|
2012-12-20 02:36:58 -08:00
|
|
|
int index = 3;
|
2012-09-28 05:03:36 -07:00
|
|
|
index += AppendHeaderBody(&req[index], aFileBody, aFileBodyLength);
|
|
|
|
|
2012-12-10 21:55:37 -08:00
|
|
|
SetObexPacketInfo(req, ObexRequestCode::Put, index);
|
|
|
|
mLastCommand = ObexRequestCode::Put;
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
|
|
|
|
delete [] req;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::SendPutFinalRequest()
|
|
|
|
{
|
|
|
|
if (!mConnected) return;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Section 2.2.9, "End-of-Body", IrObex 1.2
|
|
|
|
* End-of-Body is used to identify the last chunk of the object body.
|
|
|
|
* For most platforms, a PutFinal packet is sent with an zero length
|
|
|
|
* End-of-Body header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// [opcode:1][length:2]
|
|
|
|
int index = 3;
|
|
|
|
uint8_t* req = new uint8_t[mRemoteMaxPacketLength];
|
|
|
|
index += AppendHeaderEndOfBody(&req[index]);
|
|
|
|
SetObexPacketInfo(req, ObexRequestCode::PutFinal, index);
|
|
|
|
mLastCommand = ObexRequestCode::PutFinal;
|
2012-09-28 05:03:36 -07:00
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
|
2012-12-10 21:55:37 -08:00
|
|
|
sWaitingToSendPutFinal = false;
|
|
|
|
|
2012-09-28 05:03:36 -07:00
|
|
|
delete [] req;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::SendDisconnectRequest()
|
|
|
|
{
|
2012-10-08 22:56:17 -07:00
|
|
|
// Section 3.3.2 "Disconnect", IrOBEX 1.2
|
2012-09-28 05:03:36 -07:00
|
|
|
// [opcode:1][length:2][Headers:var]
|
|
|
|
uint8_t req[255];
|
|
|
|
int index = 3;
|
|
|
|
|
|
|
|
SetObexPacketInfo(req, ObexRequestCode::Disconnect, index);
|
|
|
|
mLastCommand = ObexRequestCode::Disconnect;
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
}
|
2012-10-03 00:09:27 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::SendAbortRequest()
|
|
|
|
{
|
|
|
|
// Section 3.3.5 "Abort", IrOBEX 1.2
|
|
|
|
// [opcode:1][length:2][Headers:var]
|
|
|
|
uint8_t req[255];
|
|
|
|
int index = 3;
|
|
|
|
|
|
|
|
SetObexPacketInfo(req, ObexRequestCode::Abort, index);
|
|
|
|
mLastCommand = ObexRequestCode::Abort;
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
}
|
2012-10-03 02:06:40 -07:00
|
|
|
|
2013-01-08 05:02:00 -08:00
|
|
|
bool
|
|
|
|
BluetoothOppManager::IsTransferring()
|
|
|
|
{
|
|
|
|
return (mConnected && !mSendTransferCompleteFlag);
|
|
|
|
}
|
|
|
|
|
2012-10-08 22:56:17 -07:00
|
|
|
void
|
|
|
|
BluetoothOppManager::ReplyToConnect()
|
|
|
|
{
|
|
|
|
if (mConnected) return;
|
|
|
|
mConnected = true;
|
|
|
|
|
|
|
|
// 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;
|
2012-11-02 18:36:34 -07:00
|
|
|
req[6] = (uint8_t)BluetoothOppManager::MAX_PACKET_LENGTH;
|
2012-10-08 22:56:17 -07:00
|
|
|
|
|
|
|
SetObexPacketInfo(req, ObexResponseCode::Success, index);
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::ReplyToDisconnect()
|
|
|
|
{
|
|
|
|
if (!mConnected) return;
|
|
|
|
mConnected = false;
|
|
|
|
|
|
|
|
// Section 3.3.2 "Disconnect", IrOBEX 1.2
|
|
|
|
// [opcode:1][length:2][Headers:var]
|
|
|
|
uint8_t req[255];
|
|
|
|
int index = 3;
|
|
|
|
|
|
|
|
SetObexPacketInfo(req, ObexResponseCode::Success, index);
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-18 23:16:13 -07:00
|
|
|
BluetoothOppManager::ReplyToPut(bool aFinal, bool aContinue)
|
2012-10-08 22:56:17 -07:00
|
|
|
{
|
|
|
|
if (!mConnected) return;
|
|
|
|
|
|
|
|
// Section 3.3.2 "Disconnect", IrOBEX 1.2
|
|
|
|
// [opcode:1][length:2][Headers:var]
|
|
|
|
uint8_t req[255];
|
|
|
|
int index = 3;
|
|
|
|
|
2012-10-18 23:16:13 -07:00
|
|
|
if (aContinue) {
|
|
|
|
if (aFinal) {
|
|
|
|
SetObexPacketInfo(req, ObexResponseCode::Success, index);
|
|
|
|
} else {
|
|
|
|
SetObexPacketInfo(req, ObexResponseCode::Continue, index);
|
|
|
|
}
|
2012-10-08 22:56:17 -07:00
|
|
|
} else {
|
2012-10-18 23:16:13 -07:00
|
|
|
if (aFinal) {
|
|
|
|
SetObexPacketInfo(req, ObexResponseCode::Unauthorized, index);
|
|
|
|
} else {
|
2012-11-02 18:36:34 -07:00
|
|
|
SetObexPacketInfo(req,
|
|
|
|
ObexResponseCode::Unauthorized & (~FINAL_BIT),
|
|
|
|
index);
|
2012-10-18 23:16:13 -07:00
|
|
|
}
|
2012-10-08 22:56:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
UnixSocketRawData* s = new UnixSocketRawData(index);
|
|
|
|
memcpy(s->mData, req, s->mSize);
|
|
|
|
SendSocketData(s);
|
|
|
|
}
|
|
|
|
|
2012-10-03 02:06:40 -07:00
|
|
|
void
|
2012-11-27 23:59:20 -08:00
|
|
|
BluetoothOppManager::FileTransferComplete()
|
2012-10-03 02:06:40 -07:00
|
|
|
{
|
2012-12-07 04:12:57 -08:00
|
|
|
if (mSendTransferCompleteFlag) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-03 02:06:40 -07:00
|
|
|
nsString type, name;
|
|
|
|
BluetoothValue v;
|
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
type.AssignLiteral("bluetooth-opp-transfer-complete");
|
|
|
|
|
2012-10-12 04:14:21 -07:00
|
|
|
name.AssignLiteral("address");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mConnectedDeviceAddress;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
2012-10-03 02:06:40 -07:00
|
|
|
name.AssignLiteral("success");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mSuccessFlag;
|
2012-10-03 02:06:40 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("received");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mTransferMode;
|
2012-10-03 02:06:40 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
2012-10-12 04:14:21 -07:00
|
|
|
name.AssignLiteral("fileName");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sFileName;
|
2012-10-03 02:06:40 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
2012-10-12 04:14:21 -07:00
|
|
|
name.AssignLiteral("fileLength");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sSentFileLength;
|
2012-10-03 02:06:40 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
2012-10-12 04:14:21 -07:00
|
|
|
name.AssignLiteral("contentType");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sContentType;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
2012-10-03 02:06:40 -07:00
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
|
|
|
NS_WARNING("Failed to broadcast [bluetooth-opp-transfer-complete]");
|
|
|
|
return;
|
|
|
|
}
|
2012-12-07 04:12:57 -08:00
|
|
|
|
|
|
|
mSendTransferCompleteFlag = true;
|
2012-10-03 02:06:40 -07:00
|
|
|
}
|
2012-10-05 01:11:21 -07:00
|
|
|
|
|
|
|
void
|
2012-11-27 23:59:20 -08:00
|
|
|
BluetoothOppManager::StartFileTransfer()
|
2012-10-12 04:14:21 -07:00
|
|
|
{
|
|
|
|
nsString type, name;
|
|
|
|
BluetoothValue v;
|
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
type.AssignLiteral("bluetooth-opp-transfer-start");
|
|
|
|
|
|
|
|
name.AssignLiteral("address");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mConnectedDeviceAddress;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("received");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mTransferMode;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("fileName");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sFileName;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("fileLength");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sFileLength;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("contentType");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sContentType;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
|
|
|
NS_WARNING("Failed to broadcast [bluetooth-opp-transfer-start]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-27 23:59:20 -08:00
|
|
|
BluetoothOppManager::UpdateProgress()
|
2012-10-05 01:11:21 -07:00
|
|
|
{
|
|
|
|
nsString type, name;
|
|
|
|
BluetoothValue v;
|
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
type.AssignLiteral("bluetooth-opp-update-progress");
|
|
|
|
|
2012-10-12 04:14:21 -07:00
|
|
|
name.AssignLiteral("address");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mConnectedDeviceAddress;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("received");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mTransferMode;
|
2012-10-12 04:14:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("processedLength");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sSentFileLength;
|
2012-10-05 01:11:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
2012-10-12 04:14:21 -07:00
|
|
|
name.AssignLiteral("fileLength");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sFileLength;
|
2012-10-05 01:11:21 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
|
|
|
NS_WARNING("Failed to broadcast [bluetooth-opp-update-progress]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-10-10 22:49:11 -07:00
|
|
|
|
2012-10-16 23:46:53 -07:00
|
|
|
void
|
2012-11-27 23:59:20 -08:00
|
|
|
BluetoothOppManager::ReceivingFileConfirmation()
|
2012-10-16 23:46:53 -07:00
|
|
|
{
|
|
|
|
nsString type, name;
|
|
|
|
BluetoothValue v;
|
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
type.AssignLiteral("bluetooth-opp-receiving-file-confirmation");
|
|
|
|
|
|
|
|
name.AssignLiteral("address");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = mConnectedDeviceAddress;
|
2012-10-16 23:46:53 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("fileName");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sFileName;
|
2012-10-16 23:46:53 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("fileLength");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sFileLength;
|
2012-10-16 23:46:53 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("contentType");
|
2012-11-27 23:59:20 -08:00
|
|
|
v = sContentType;
|
2012-10-16 23:46:53 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
2012-11-02 18:36:34 -07:00
|
|
|
NS_WARNING("Failed to send [bluetooth-opp-receiving-file-confirmation]");
|
2012-10-16 23:46:53 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-10-16 23:48:52 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::OnConnectSuccess()
|
|
|
|
{
|
2012-11-13 22:29:11 -08:00
|
|
|
if (mRunnable) {
|
|
|
|
BluetoothReply* reply = new BluetoothReply(BluetoothReplySuccess(true));
|
|
|
|
mRunnable->SetReply(reply);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mRunnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
|
|
|
mRunnable.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-30 23:07:06 -07:00
|
|
|
// Cache device address since we can't get socket address when a remote
|
|
|
|
// device disconnect with us.
|
|
|
|
GetSocketAddr(mConnectedDeviceAddress);
|
2012-11-02 18:36:19 -07:00
|
|
|
|
|
|
|
mSocketStatus = GetConnectionStatus();
|
2012-10-16 23:48:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::OnConnectError()
|
|
|
|
{
|
2012-11-13 22:29:11 -08:00
|
|
|
if (mRunnable) {
|
|
|
|
nsString errorStr;
|
|
|
|
errorStr.AssignLiteral("Failed to connect with a bluetooth opp manager!");
|
|
|
|
BluetoothReply* reply = new BluetoothReply(BluetoothReplyError(errorStr));
|
|
|
|
mRunnable->SetReply(reply);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mRunnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
|
|
|
mRunnable.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:36:19 -07:00
|
|
|
CloseSocket();
|
|
|
|
mSocketStatus = GetConnectionStatus();
|
|
|
|
Listen();
|
2012-10-16 23:48:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothOppManager::OnDisconnect()
|
|
|
|
{
|
2012-12-07 04:12:57 -08:00
|
|
|
/**
|
|
|
|
* It is valid for a bluetooth device which is transfering file via OPP
|
|
|
|
* closing socket without sending OBEX disconnect request first. So we
|
|
|
|
* delete the broken file when we failed to receive a file from the remote,
|
|
|
|
* and notify the transfer has been completed (but failed). We also call
|
|
|
|
* AfterOppDisconnected here to ensure all variables will be cleaned.
|
|
|
|
*/
|
2012-11-27 23:59:20 -08:00
|
|
|
if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
|
2012-12-12 00:54:54 -08:00
|
|
|
if (mTransferMode) {
|
|
|
|
if (!mSuccessFlag) {
|
2012-12-07 04:12:57 -08:00
|
|
|
DeleteReceivedFile();
|
2012-12-12 00:54:54 -08:00
|
|
|
} else if (mDsFile) {
|
|
|
|
nsString data;
|
|
|
|
CopyASCIItoUTF16("modified", data);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
2013-01-09 07:03:28 -08:00
|
|
|
obs->NotifyObservers(mDsFile, "file-watcher-notify", data.get());
|
2012-12-12 00:54:54 -08:00
|
|
|
}
|
2012-12-07 04:12:57 -08:00
|
|
|
}
|
2012-12-12 00:54:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSuccessFlag) {
|
2012-12-07 04:12:57 -08:00
|
|
|
FileTransferComplete();
|
2012-11-27 23:59:20 -08:00
|
|
|
}
|
2012-12-12 00:54:54 -08:00
|
|
|
|
2012-11-27 23:59:20 -08:00
|
|
|
Listen();
|
|
|
|
} else if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTING) {
|
|
|
|
NS_WARNING("BluetoothOppManager got unexpected socket status!");
|
|
|
|
}
|
|
|
|
|
2012-10-30 23:07:06 -07:00
|
|
|
AfterOppDisconnected();
|
2012-11-27 23:59:20 -08:00
|
|
|
mConnectedDeviceAddress.AssignLiteral("00:00:00:00:00:00");
|
2012-11-30 01:49:08 -08:00
|
|
|
mSuccessFlag = false;
|
2012-10-16 23:48:52 -07:00
|
|
|
}
|