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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_bluetooth_bluetoothoppmanager_h__
|
|
|
|
#define mozilla_dom_bluetooth_bluetoothoppmanager_h__
|
|
|
|
|
|
|
|
#include "BluetoothCommon.h"
|
2013-05-09 23:39:12 -07:00
|
|
|
#include "BluetoothProfileManagerBase.h"
|
2013-04-04 17:53:08 -07:00
|
|
|
#include "BluetoothSocketObserver.h"
|
2013-04-26 03:09:00 -07:00
|
|
|
#include "DeviceStorage.h"
|
2012-09-28 03:59:12 -07:00
|
|
|
#include "mozilla/dom/ipc/Blob.h"
|
2012-09-28 04:46:37 -07:00
|
|
|
#include "mozilla/ipc/UnixSocket.h"
|
2013-04-26 03:09:00 -07:00
|
|
|
#include "nsCOMArray.h"
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2012-10-30 20:15:12 -07:00
|
|
|
class nsIOutputStream;
|
|
|
|
class nsIInputStream;
|
2013-06-07 00:10:24 -07:00
|
|
|
class nsIVolumeMountLock;
|
2012-10-30 20:15:12 -07:00
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
BEGIN_BLUETOOTH_NAMESPACE
|
|
|
|
|
2013-04-04 17:53:08 -07:00
|
|
|
class BluetoothSocket;
|
2012-12-07 04:12:57 -08:00
|
|
|
class ObexHeaderSet;
|
2013-10-31 20:21:13 -07:00
|
|
|
class SendFileBatch;
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2013-04-04 17:53:08 -07:00
|
|
|
class BluetoothOppManager : public BluetoothSocketObserver
|
2013-05-09 23:39:12 -07:00
|
|
|
, public BluetoothProfileManagerBase
|
2012-09-28 04:46:37 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-06-08 08:26:01 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
2013-06-19 18:19:46 -07:00
|
|
|
NS_DECL_NSIOBSERVER
|
2013-06-08 08:26:01 -07:00
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
/*
|
|
|
|
* Channel of reserved services are fixed values, please check
|
|
|
|
* function add_reserved_service_records() in
|
|
|
|
* external/bluetooth/bluez/src/adapter.c for more information.
|
|
|
|
*/
|
|
|
|
static const int DEFAULT_OPP_CHANNEL = 10;
|
2012-09-27 07:59:27 -07:00
|
|
|
static const int MAX_PACKET_LENGTH = 0xFFFE;
|
2012-09-28 04:46:37 -07:00
|
|
|
|
|
|
|
~BluetoothOppManager();
|
|
|
|
static BluetoothOppManager* Get();
|
2012-12-07 04:12:57 -08:00
|
|
|
void ClientDataHandler(mozilla::ipc::UnixSocketRawData* aMessage);
|
|
|
|
void ServerDataHandler(mozilla::ipc::UnixSocketRawData* aMessage);
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2012-10-14 23:16:41 -07:00
|
|
|
bool Listen();
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2013-10-31 20:21:13 -07:00
|
|
|
bool SendFile(const nsAString& aDeviceAddress, BlobParent* aActor);
|
2012-11-02 18:36:43 -07:00
|
|
|
bool StopSendingFile();
|
|
|
|
bool ConfirmReceivingFile(bool aConfirm);
|
2012-09-28 04:46:37 -07:00
|
|
|
|
2012-09-28 05:03:36 -07:00
|
|
|
void SendConnectRequest();
|
|
|
|
void SendPutHeaderRequest(const nsAString& aFileName, int aFileSize);
|
2012-12-10 21:55:37 -08:00
|
|
|
void SendPutRequest(uint8_t* aFileBody, int aFileBodyLength);
|
|
|
|
void SendPutFinalRequest();
|
2012-09-28 05:03:36 -07:00
|
|
|
void SendDisconnectRequest();
|
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
void ExtractPacketHeaders(const ObexHeaderSet& aHeader);
|
|
|
|
bool ExtractBlobHeaders();
|
2013-07-14 21:10:32 -07:00
|
|
|
void CheckPutFinal(uint32_t aNumRead);
|
2013-01-08 05:02:00 -08:00
|
|
|
|
2013-09-06 04:21:04 -07:00
|
|
|
// The following functions are inherited from BluetoothSocketObserver
|
2013-04-04 17:55:55 -07:00
|
|
|
void ReceiveSocketData(
|
|
|
|
BluetoothSocket* aSocket,
|
|
|
|
nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
|
2013-09-06 04:21:04 -07:00
|
|
|
virtual void OnSocketConnectSuccess(BluetoothSocket* aSocket) MOZ_OVERRIDE;
|
|
|
|
virtual void OnSocketConnectError(BluetoothSocket* aSocket) MOZ_OVERRIDE;
|
|
|
|
virtual void OnSocketDisconnect(BluetoothSocket* aSocket) MOZ_OVERRIDE;
|
2013-04-04 17:55:55 -07:00
|
|
|
|
2013-09-06 04:21:04 -07:00
|
|
|
// The following functions are inherited from BluetoothProfileManagerBase
|
2013-05-09 23:39:12 -07:00
|
|
|
virtual void OnGetServiceChannel(const nsAString& aDeviceAddress,
|
|
|
|
const nsAString& aServiceUuid,
|
|
|
|
int aChannel) MOZ_OVERRIDE;
|
2013-05-22 04:08:21 -07:00
|
|
|
virtual void OnUpdateSdpRecords(const nsAString& aDeviceAddress) MOZ_OVERRIDE;
|
2013-06-08 08:26:01 -07:00
|
|
|
virtual void GetAddress(nsAString& aDeviceAddress) MOZ_OVERRIDE;
|
2013-07-29 02:32:34 -07:00
|
|
|
virtual bool IsConnected() MOZ_OVERRIDE;
|
2013-04-04 17:53:08 -07:00
|
|
|
|
2013-09-13 15:51:00 -07:00
|
|
|
virtual void GetName(nsACString& aName)
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("OPP");
|
|
|
|
}
|
|
|
|
|
2013-09-06 04:21:04 -07:00
|
|
|
virtual void Connect(const nsAString& aDeviceAddress,
|
|
|
|
BluetoothProfileController* aController) MOZ_OVERRIDE;
|
|
|
|
virtual void Disconnect(BluetoothProfileController* aController) MOZ_OVERRIDE;
|
|
|
|
virtual void OnConnect(const nsAString& aErrorStr) MOZ_OVERRIDE;
|
|
|
|
virtual void OnDisconnect(const nsAString& aErrorStr) MOZ_OVERRIDE;
|
|
|
|
|
2012-09-28 04:46:37 -07:00
|
|
|
private:
|
|
|
|
BluetoothOppManager();
|
2013-06-19 18:19:46 -07:00
|
|
|
bool Init();
|
2013-06-19 18:20:16 -07:00
|
|
|
void HandleShutdown();
|
2013-06-19 18:19:46 -07:00
|
|
|
|
2012-11-27 23:59:20 -08:00
|
|
|
void StartFileTransfer();
|
2013-04-26 03:09:00 -07:00
|
|
|
void StartSendingNextFile();
|
2012-11-27 23:59:20 -08:00
|
|
|
void FileTransferComplete();
|
|
|
|
void UpdateProgress();
|
|
|
|
void ReceivingFileConfirmation();
|
2012-11-07 17:46:31 -08:00
|
|
|
bool CreateFile();
|
|
|
|
bool WriteToFile(const uint8_t* aData, int aDataLength);
|
2012-11-02 18:36:49 -07:00
|
|
|
void DeleteReceivedFile();
|
2012-10-08 22:56:17 -07:00
|
|
|
void ReplyToConnect();
|
2013-05-24 00:59:32 -07:00
|
|
|
void ReplyToDisconnectOrAbort();
|
2012-10-18 23:16:13 -07:00
|
|
|
void ReplyToPut(bool aFinal, bool aContinue);
|
2013-07-14 21:10:32 -07:00
|
|
|
void ReplyError(uint8_t aError);
|
2012-10-30 20:15:12 -07:00
|
|
|
void AfterOppConnected();
|
2012-12-07 04:12:57 -08:00
|
|
|
void AfterFirstPut();
|
2012-10-30 20:15:12 -07:00
|
|
|
void AfterOppDisconnected();
|
2013-01-16 02:23:28 -08:00
|
|
|
void ValidateFileName();
|
|
|
|
bool IsReservedChar(PRUnichar c);
|
2013-04-26 03:09:00 -07:00
|
|
|
void ClearQueue();
|
|
|
|
void RetrieveSentFileName();
|
2013-04-26 03:09:04 -07:00
|
|
|
void NotifyAboutFileChange();
|
2013-06-07 00:10:24 -07:00
|
|
|
bool AcquireSdcardMountLock();
|
2013-07-09 20:20:13 -07:00
|
|
|
void SendObexData(uint8_t* aData, uint8_t aOpcode, int aSize);
|
2013-10-31 20:21:13 -07:00
|
|
|
void AppendBlobToSend(const nsAString& aDeviceAddress, BlobParent* aActor);
|
|
|
|
void DiscardBlobsToSend();
|
|
|
|
bool ProcessNextBatch();
|
|
|
|
void ConnectInternal(const nsAString& aDeviceAddress);
|
2013-07-09 20:20:13 -07:00
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
/**
|
|
|
|
* OBEX session status.
|
|
|
|
* Set when OBEX session is established.
|
|
|
|
*/
|
2012-09-27 07:59:27 -07:00
|
|
|
bool mConnected;
|
2012-12-07 04:12:57 -08:00
|
|
|
nsString mConnectedDeviceAddress;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote information
|
|
|
|
*/
|
2012-09-28 05:03:36 -07:00
|
|
|
uint8_t mRemoteObexVersion;
|
|
|
|
uint8_t mRemoteConnectionFlags;
|
|
|
|
int mRemoteMaxPacketLength;
|
2012-12-07 04:12:57 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For sending files, we decide our next action based on current command and
|
|
|
|
* previous one.
|
|
|
|
* For receiving files, we don't need previous command and it is set to 0
|
|
|
|
* as a default value.
|
|
|
|
*/
|
|
|
|
int mLastCommand;
|
|
|
|
|
2012-10-08 22:56:17 -07:00
|
|
|
int mPacketLeftLength;
|
2012-11-07 17:46:31 -08:00
|
|
|
int mBodySegmentLength;
|
|
|
|
int mReceivedDataBufferOffset;
|
2012-10-30 20:15:12 -07:00
|
|
|
int mUpdateProgressCounter;
|
2012-12-07 04:12:57 -08:00
|
|
|
|
2013-05-22 04:08:21 -07:00
|
|
|
/**
|
|
|
|
* When it is true and the target service on target device couldn't be found,
|
|
|
|
* refreshing SDP records is necessary.
|
|
|
|
*/
|
|
|
|
bool mNeedsUpdatingSdpRecords;
|
|
|
|
|
2012-12-07 04:12:57 -08:00
|
|
|
/**
|
|
|
|
* Set when StopSendingFile() is called.
|
|
|
|
*/
|
|
|
|
bool mAbortFlag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set when receiving the first PUT packet of a new file
|
|
|
|
*/
|
|
|
|
bool mNewFileFlag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set when receiving a PutFinal packet
|
|
|
|
*/
|
|
|
|
bool mPutFinalFlag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set when FileTransferComplete() is called
|
|
|
|
*/
|
|
|
|
bool mSendTransferCompleteFlag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set when a transfer is successfully completed.
|
|
|
|
*/
|
2012-11-27 23:59:20 -08:00
|
|
|
bool mSuccessFlag;
|
2012-12-07 04:12:57 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* True: Receive file (Server)
|
|
|
|
* False: Send file (Client)
|
|
|
|
*/
|
2013-04-26 03:09:04 -07:00
|
|
|
bool mIsServer;
|
2012-12-07 04:12:57 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set when receiving the first PUT packet and wait for
|
|
|
|
* ConfirmReceivingFile() to be called.
|
|
|
|
*/
|
|
|
|
bool mWaitingForConfirmationFlag;
|
2012-09-28 05:03:36 -07:00
|
|
|
|
2013-07-14 21:10:32 -07:00
|
|
|
nsString mFileName;
|
|
|
|
nsString mContentType;
|
|
|
|
uint32_t mFileLength;
|
|
|
|
uint32_t mSentFileLength;
|
|
|
|
bool mWaitingToSendPutFinal;
|
|
|
|
|
2012-12-13 04:12:04 -08:00
|
|
|
nsAutoArrayPtr<uint8_t> mBodySegment;
|
|
|
|
nsAutoArrayPtr<uint8_t> mReceivedDataBuffer;
|
2012-11-07 17:46:31 -08:00
|
|
|
|
2013-04-26 03:09:00 -07:00
|
|
|
int mCurrentBlobIndex;
|
2012-09-28 05:03:36 -07:00
|
|
|
nsCOMPtr<nsIDOMBlob> mBlob;
|
2013-10-31 20:21:13 -07:00
|
|
|
nsTArray<SendFileBatch> mBatches;
|
2013-04-09 09:11:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A seperate member thread is required because our read calls can block
|
|
|
|
* execution, which is not allowed to happen on the IOThread.
|
|
|
|
*/
|
2012-10-03 00:09:27 -07:00
|
|
|
nsCOMPtr<nsIThread> mReadFileThread;
|
2012-10-30 20:15:12 -07:00
|
|
|
nsCOMPtr<nsIOutputStream> mOutputStream;
|
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
2013-06-07 00:10:24 -07:00
|
|
|
nsCOMPtr<nsIVolumeMountLock> mMountLock;
|
2012-12-12 00:54:54 -08:00
|
|
|
nsRefPtr<DeviceStorageFile> mDsFile;
|
2013-04-12 03:45:39 -07:00
|
|
|
|
|
|
|
// If a connection has been established, mSocket will be the socket
|
|
|
|
// communicating with the remote socket. We maintain the invariant that if
|
|
|
|
// mSocket is non-null, mRfcommSocket and mL2capSocket must be null (and vice
|
|
|
|
// versa).
|
2013-04-04 17:53:08 -07:00
|
|
|
nsRefPtr<BluetoothSocket> mSocket;
|
2013-04-12 03:45:39 -07:00
|
|
|
|
|
|
|
// Server sockets. Once an inbound connection is established, it will hand
|
|
|
|
// over the ownership to mSocket, and get a new server socket while Listen()
|
|
|
|
// is called.
|
|
|
|
nsRefPtr<BluetoothSocket> mRfcommSocket;
|
|
|
|
nsRefPtr<BluetoothSocket> mL2capSocket;
|
2012-09-28 04:46:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
END_BLUETOOTH_NAMESPACE
|
|
|
|
|
|
|
|
#endif
|