2012-09-28 15:46:37 +04: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-10 10:39:12 +04:00
|
|
|
#include "BluetoothProfileManagerBase.h"
|
2013-04-05 04:53:08 +04:00
|
|
|
#include "BluetoothSocketObserver.h"
|
2013-04-26 14:09:00 +04:00
|
|
|
#include "DeviceStorage.h"
|
2012-09-28 14:59:12 +04:00
|
|
|
#include "mozilla/dom/ipc/Blob.h"
|
2012-09-28 15:46:37 +04:00
|
|
|
#include "mozilla/ipc/UnixSocket.h"
|
2013-04-26 14:09:00 +04:00
|
|
|
#include "nsCOMArray.h"
|
2012-09-28 15:46:37 +04:00
|
|
|
|
2012-10-31 07:15:12 +04:00
|
|
|
class nsIOutputStream;
|
|
|
|
class nsIInputStream;
|
2013-06-07 11:10:24 +04:00
|
|
|
class nsIVolumeMountLock;
|
2012-10-31 07:15:12 +04:00
|
|
|
|
2012-09-28 15:46:37 +04:00
|
|
|
BEGIN_BLUETOOTH_NAMESPACE
|
|
|
|
|
2013-04-05 04:53:08 +04:00
|
|
|
class BluetoothSocket;
|
2012-12-07 16:12:57 +04:00
|
|
|
class ObexHeaderSet;
|
2013-11-01 07:21:13 +04:00
|
|
|
class SendFileBatch;
|
2012-09-28 15:46:37 +04:00
|
|
|
|
2013-04-05 04:53:08 +04:00
|
|
|
class BluetoothOppManager : public BluetoothSocketObserver
|
2013-05-10 10:39:12 +04:00
|
|
|
, public BluetoothProfileManagerBase
|
2012-09-28 15:46:37 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-11-12 07:59:01 +04:00
|
|
|
BT_DECL_PROFILE_MGR_BASE
|
|
|
|
virtual void GetName(nsACString& aName)
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("OPP");
|
|
|
|
}
|
2013-06-08 19:26:01 +04:00
|
|
|
|
2012-09-27 18:59:27 +04:00
|
|
|
static const int MAX_PACKET_LENGTH = 0xFFFE;
|
2012-09-28 15:46:37 +04:00
|
|
|
|
2013-12-06 08:20:07 +04:00
|
|
|
virtual ~BluetoothOppManager();
|
2012-09-28 15:46:37 +04:00
|
|
|
static BluetoothOppManager* Get();
|
2012-12-07 16:12:57 +04:00
|
|
|
void ClientDataHandler(mozilla::ipc::UnixSocketRawData* aMessage);
|
|
|
|
void ServerDataHandler(mozilla::ipc::UnixSocketRawData* aMessage);
|
2012-09-28 15:46:37 +04:00
|
|
|
|
2012-10-15 10:16:41 +04:00
|
|
|
bool Listen();
|
2012-09-28 15:46:37 +04:00
|
|
|
|
2013-11-01 07:21:13 +04:00
|
|
|
bool SendFile(const nsAString& aDeviceAddress, BlobParent* aActor);
|
2014-02-11 06:07:23 +04:00
|
|
|
bool SendFile(const nsAString& aDeviceAddress, nsIDOMBlob* aBlob);
|
2012-11-03 05:36:43 +04:00
|
|
|
bool StopSendingFile();
|
|
|
|
bool ConfirmReceivingFile(bool aConfirm);
|
2012-09-28 15:46:37 +04:00
|
|
|
|
2012-09-28 16:03:36 +04:00
|
|
|
void SendConnectRequest();
|
|
|
|
void SendPutHeaderRequest(const nsAString& aFileName, int aFileSize);
|
2012-12-11 09:55:37 +04:00
|
|
|
void SendPutRequest(uint8_t* aFileBody, int aFileBodyLength);
|
|
|
|
void SendPutFinalRequest();
|
2012-09-28 16:03:36 +04:00
|
|
|
void SendDisconnectRequest();
|
|
|
|
|
2012-12-07 16:12:57 +04:00
|
|
|
void ExtractPacketHeaders(const ObexHeaderSet& aHeader);
|
|
|
|
bool ExtractBlobHeaders();
|
2013-07-15 08:10:32 +04:00
|
|
|
void CheckPutFinal(uint32_t aNumRead);
|
2013-01-08 17:02:00 +04:00
|
|
|
|
2013-09-06 15:21:04 +04:00
|
|
|
// The following functions are inherited from BluetoothSocketObserver
|
2013-04-05 04:55:55 +04:00
|
|
|
void ReceiveSocketData(
|
|
|
|
BluetoothSocket* aSocket,
|
|
|
|
nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
|
2013-09-06 15:21:04 +04:00
|
|
|
virtual void OnSocketConnectSuccess(BluetoothSocket* aSocket) MOZ_OVERRIDE;
|
|
|
|
virtual void OnSocketConnectError(BluetoothSocket* aSocket) MOZ_OVERRIDE;
|
|
|
|
virtual void OnSocketDisconnect(BluetoothSocket* aSocket) MOZ_OVERRIDE;
|
2013-04-05 04:55:55 +04:00
|
|
|
|
2012-09-28 15:46:37 +04:00
|
|
|
private:
|
|
|
|
BluetoothOppManager();
|
2013-06-20 05:19:46 +04:00
|
|
|
bool Init();
|
2013-06-20 05:20:16 +04:00
|
|
|
void HandleShutdown();
|
2013-06-20 05:19:46 +04:00
|
|
|
|
2012-11-28 11:59:20 +04:00
|
|
|
void StartFileTransfer();
|
2013-04-26 14:09:00 +04:00
|
|
|
void StartSendingNextFile();
|
2012-11-28 11:59:20 +04:00
|
|
|
void FileTransferComplete();
|
|
|
|
void UpdateProgress();
|
|
|
|
void ReceivingFileConfirmation();
|
2012-11-08 05:46:31 +04:00
|
|
|
bool CreateFile();
|
|
|
|
bool WriteToFile(const uint8_t* aData, int aDataLength);
|
2012-11-03 05:36:49 +04:00
|
|
|
void DeleteReceivedFile();
|
2012-10-09 09:56:17 +04:00
|
|
|
void ReplyToConnect();
|
2013-05-24 11:59:32 +04:00
|
|
|
void ReplyToDisconnectOrAbort();
|
2012-10-19 10:16:13 +04:00
|
|
|
void ReplyToPut(bool aFinal, bool aContinue);
|
2013-07-15 08:10:32 +04:00
|
|
|
void ReplyError(uint8_t aError);
|
2012-10-31 07:15:12 +04:00
|
|
|
void AfterOppConnected();
|
2012-12-07 16:12:57 +04:00
|
|
|
void AfterFirstPut();
|
2012-10-31 07:15:12 +04:00
|
|
|
void AfterOppDisconnected();
|
2013-01-16 14:23:28 +04:00
|
|
|
void ValidateFileName();
|
2014-01-04 19:02:17 +04:00
|
|
|
bool IsReservedChar(char16_t c);
|
2013-04-26 14:09:00 +04:00
|
|
|
void ClearQueue();
|
|
|
|
void RetrieveSentFileName();
|
2013-04-26 14:09:04 +04:00
|
|
|
void NotifyAboutFileChange();
|
2013-06-07 11:10:24 +04:00
|
|
|
bool AcquireSdcardMountLock();
|
2013-07-10 07:20:13 +04:00
|
|
|
void SendObexData(uint8_t* aData, uint8_t aOpcode, int aSize);
|
2014-02-11 06:07:23 +04:00
|
|
|
void AppendBlobToSend(const nsAString& aDeviceAddress, nsIDOMBlob* aBlob);
|
2013-11-01 07:21:13 +04:00
|
|
|
void DiscardBlobsToSend();
|
|
|
|
bool ProcessNextBatch();
|
|
|
|
void ConnectInternal(const nsAString& aDeviceAddress);
|
2013-07-10 07:20:13 +04:00
|
|
|
|
2013-11-22 07:55:42 +04:00
|
|
|
/**
|
|
|
|
* Usually we won't get a full PUT packet in one operation, which means that
|
|
|
|
* a packet may be devided into several parts and BluetoothOppManager should
|
|
|
|
* be in charge of assembling.
|
|
|
|
*
|
|
|
|
* @return true if a packet has been fully received.
|
|
|
|
* false if the received length exceeds/not reaches the expected
|
|
|
|
* length.
|
|
|
|
*/
|
2013-12-09 06:53:11 +04:00
|
|
|
bool ComposePacket(uint8_t aOpCode,
|
|
|
|
mozilla::ipc::UnixSocketRawData* aMessage);
|
2013-11-22 07:55:42 +04:00
|
|
|
|
2012-12-07 16:12:57 +04:00
|
|
|
/**
|
|
|
|
* OBEX session status.
|
|
|
|
* Set when OBEX session is established.
|
|
|
|
*/
|
2012-09-27 18:59:27 +04:00
|
|
|
bool mConnected;
|
2013-12-02 06:39:58 +04:00
|
|
|
nsString mDeviceAddress;
|
2012-12-07 16:12:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote information
|
|
|
|
*/
|
2012-09-28 16:03:36 +04:00
|
|
|
uint8_t mRemoteObexVersion;
|
|
|
|
uint8_t mRemoteConnectionFlags;
|
|
|
|
int mRemoteMaxPacketLength;
|
2012-12-07 16:12:57 +04: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;
|
|
|
|
|
2013-11-22 07:55:42 +04:00
|
|
|
int mPacketLength;
|
2013-12-11 09:17:34 +04:00
|
|
|
int mPutPacketReceivedLength;
|
2012-11-08 05:46:31 +04:00
|
|
|
int mBodySegmentLength;
|
2012-10-31 07:15:12 +04:00
|
|
|
int mUpdateProgressCounter;
|
2012-12-07 16:12:57 +04:00
|
|
|
|
2013-05-22 15:08:21 +04: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 16:12:57 +04: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-28 11:59:20 +04:00
|
|
|
bool mSuccessFlag;
|
2012-12-07 16:12:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* True: Receive file (Server)
|
|
|
|
* False: Send file (Client)
|
|
|
|
*/
|
2013-04-26 14:09:04 +04:00
|
|
|
bool mIsServer;
|
2012-12-07 16:12:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set when receiving the first PUT packet and wait for
|
|
|
|
* ConfirmReceivingFile() to be called.
|
|
|
|
*/
|
|
|
|
bool mWaitingForConfirmationFlag;
|
2012-09-28 16:03:36 +04:00
|
|
|
|
2013-07-15 08:10:32 +04:00
|
|
|
nsString mFileName;
|
|
|
|
nsString mContentType;
|
|
|
|
uint32_t mFileLength;
|
|
|
|
uint32_t mSentFileLength;
|
|
|
|
bool mWaitingToSendPutFinal;
|
|
|
|
|
2012-12-13 16:12:04 +04:00
|
|
|
nsAutoArrayPtr<uint8_t> mBodySegment;
|
|
|
|
nsAutoArrayPtr<uint8_t> mReceivedDataBuffer;
|
2012-11-08 05:46:31 +04:00
|
|
|
|
2013-04-26 14:09:00 +04:00
|
|
|
int mCurrentBlobIndex;
|
2012-09-28 16:03:36 +04:00
|
|
|
nsCOMPtr<nsIDOMBlob> mBlob;
|
2013-11-01 07:21:13 +04:00
|
|
|
nsTArray<SendFileBatch> mBatches;
|
2013-04-09 20:11:00 +04: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 11:09:27 +04:00
|
|
|
nsCOMPtr<nsIThread> mReadFileThread;
|
2012-10-31 07:15:12 +04:00
|
|
|
nsCOMPtr<nsIOutputStream> mOutputStream;
|
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
2013-06-07 11:10:24 +04:00
|
|
|
nsCOMPtr<nsIVolumeMountLock> mMountLock;
|
2012-12-12 12:54:54 +04:00
|
|
|
nsRefPtr<DeviceStorageFile> mDsFile;
|
2013-04-12 14:45:39 +04:00
|
|
|
|
|
|
|
// If a connection has been established, mSocket will be the socket
|
|
|
|
// communicating with the remote socket. We maintain the invariant that if
|
2013-12-02 06:39:58 +04:00
|
|
|
// mSocket is non-null, mServerSocket must be null (and vice versa).
|
2013-04-05 04:53:08 +04:00
|
|
|
nsRefPtr<BluetoothSocket> mSocket;
|
2013-04-12 14:45:39 +04: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.
|
2013-12-02 06:39:58 +04:00
|
|
|
nsRefPtr<BluetoothSocket> mServerSocket;
|
2012-09-28 15:46:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
END_BLUETOOTH_NAMESPACE
|
|
|
|
|
|
|
|
#endif
|