blob: b36748d3601abf0dacb89b68cd05729f93d1e5e4 [file] [log] [blame]
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2014-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file defines a Bluetooth Low Energy (BLE) connection
* endpoint abstraction for the byte-streaming,
* connection-oriented CHIP over Bluetooth Low Energy (CHIPoBLE)
* Bluetooth Transport Protocol (BTP).
*
*/
#pragma once
#include <system/SystemLayer.h>
#include <system/SystemMutex.h>
#include <ble/BleRole.h>
#include <ble/BtpEngine.h>
#if CHIP_ENABLE_CHIPOBLE_TEST
#include <ble/BtpEngineTest.h>
#endif
namespace chip {
namespace Ble {
using ::chip::System::PacketBufferHandle;
enum
{
kBleCloseFlag_SuppressCallback = 0x01,
kBleCloseFlag_AbortTransmission = 0x02
};
// Forward declarations
class BleLayer;
class BleEndPointPool;
// BLEEndPoint holds a pointer to BleLayerDelegate for messages, while BleLayerDelegate functions also accepts BLEEndPoint.
class BleLayerDelegate;
#if CHIP_ENABLE_CHIPOBLE_TEST
class BtpEngineTest;
#endif
class DLL_EXPORT BLEEndPoint
{
friend class BleLayer;
friend class BleEndPointPool;
#if CHIP_ENABLE_CHIPOBLE_TEST
friend class BtpEngineTest;
#endif
public:
typedef uint64_t AlignT;
// Public data members:
enum
{
kState_Ready = 0,
kState_Connecting = 1,
kState_Aborting = 2,
kState_Connected = 3,
kState_Closing = 4,
kState_Closed = 5
} mState; // [READ-ONLY] End point connection state. Refers to state of CHIP over
// BLE transport protocol connection, not of underlying BLE connection.
// Public function pointers:
typedef void (*OnConnectCompleteFunct)(BLEEndPoint * endPoint, CHIP_ERROR err);
OnConnectCompleteFunct OnConnectComplete;
typedef void (*OnMessageReceivedFunct)(BLEEndPoint * endPoint, PacketBufferHandle && msg);
OnMessageReceivedFunct OnMessageReceived;
typedef void (*OnConnectionClosedFunct)(BLEEndPoint * endPoint, CHIP_ERROR err);
OnConnectionClosedFunct OnConnectionClosed;
#if CHIP_ENABLE_CHIPOBLE_TEST
typedef void (*OnCommandReceivedFunct)(BLEEndPoint * endPoint, PacketBufferHandle && msg);
OnCommandReceivedFunct OnCommandReceived;
inline void SetOnCommandReceivedCB(OnCommandReceivedFunct cb) { OnCommandReceived = cb; };
BtpEngineTest mBtpEngineTest;
inline void SetTxWindowSize(uint8_t size) { mRemoteReceiveWindowSize = size; };
inline void SetRxWindowSize(uint8_t size) { mReceiveWindowMaxSize = size; };
#endif
// Public functions:
CHIP_ERROR Send(PacketBufferHandle && data);
CHIP_ERROR Receive(PacketBufferHandle && data);
CHIP_ERROR StartConnect();
bool IsUnsubscribePending() const;
bool ConnectionObjectIs(BLE_CONNECTION_OBJECT connObj) { return connObj == mConnObj; }
void Close();
void Abort();
void ReleaseBleConnection();
private:
BleLayer * mBle; ///< [READ-ONLY] Pointer to the BleLayer object that owns this object.
BleLayerDelegate * mBleTransport;
uint32_t mRefCount;
void AddRef() { mRefCount++; }
void Release();
// Private data members:
enum class ConnectionStateFlag : uint8_t
{
kAutoClose = 0x01, // End point should close underlying BLE conn on BTP close.
kCapabilitiesConfReceived = 0x02, // GATT confirmation received for sent capabilities req/resp.
kCapabilitiesMsgReceived = 0x04, // Capabilities request or response message received.
kDidBeginSubscribe = 0x08, // GATT subscribe request sent; must unsubscribe on close.
kStandAloneAckInFlight = 0x10, // Stand-alone ack in flight, awaiting GATT confirmation.
kGattOperationInFlight = 0x20 // GATT write, indication, subscribe, or unsubscribe in flight,
// awaiting GATT confirmation.
};
enum class TimerStateFlag : uint8_t
{
kConnectTimerRunning = 0x01, // BTP connect completion timer running.
kReceiveConnectionTimerRunning = 0x02, // BTP receive connection completion timer running.
kAckReceivedTimerRunning = 0x04, // Ack received timer running due to unacked sent fragment.
kSendAckTimerRunning = 0x08, // Send ack timer running; indicates pending ack to send.
kUnsubscribeTimerRunning = 0x10, // Unsubscribe completion timer running.
#if CHIP_ENABLE_CHIPOBLE_TEST
kUnderTestTimerRunnung = 0x80 // running throughput Tx test
#endif
};
// BLE connection to which an end point is uniquely bound. Type BLE_CONNECTION_OBJECT is defined by the platform or
// void* by default. This object is passed back to the platform delegate with each call to send traffic over or
// modify the state of the underlying BLE connection.
BLE_CONNECTION_OBJECT mConnObj;
// Queue of outgoing messages to send when current BtpEngine transmission completes.
//
// Re-used during connection setup to cache capabilities request and response payloads; payloads are freed when
// connection is established.
PacketBufferHandle mSendQueue;
// Pending stand-alone BTP acknolwedgement. Pre-empts regular send queue or fragmented message transmission in
// progress.
PacketBufferHandle mAckToSend;
BtpEngine mBtpEngine;
BleRole mRole;
BitFlags<ConnectionStateFlag> mConnStateFlags;
BitFlags<TimerStateFlag> mTimerStateFlags;
SequenceNumber_t mLocalReceiveWindowSize;
SequenceNumber_t mRemoteReceiveWindowSize;
SequenceNumber_t mReceiveWindowMaxSize;
#if CHIP_ENABLE_CHIPOBLE_TEST
chip::System::Mutex mTxQueueMutex; // For MT-safe Tx queuing
#endif
// Private functions:
BLEEndPoint() = delete;
~BLEEndPoint() = delete;
CHIP_ERROR Init(BleLayer * bleLayer, BLE_CONNECTION_OBJECT connObj, BleRole role, bool autoClose);
bool IsConnected(uint8_t state) const;
void DoClose(uint8_t flags, CHIP_ERROR err);
// Transmit path:
CHIP_ERROR DriveSending();
CHIP_ERROR DriveStandAloneAck();
bool PrepareNextFragment(PacketBufferHandle && data, bool & sentAck);
CHIP_ERROR SendNextMessage();
CHIP_ERROR ContinueMessageSend();
CHIP_ERROR DoSendStandAloneAck();
CHIP_ERROR SendCharacteristic(PacketBufferHandle && buf);
bool SendIndication(PacketBufferHandle && buf);
bool SendWrite(PacketBufferHandle && buf);
// Receive path:
CHIP_ERROR HandleConnectComplete();
CHIP_ERROR HandleReceiveConnectionComplete();
void HandleSubscribeReceived();
void HandleSubscribeComplete();
void HandleUnsubscribeComplete();
CHIP_ERROR HandleGattSendConfirmationReceived();
CHIP_ERROR HandleHandshakeConfirmationReceived();
CHIP_ERROR HandleFragmentConfirmationReceived();
CHIP_ERROR HandleCapabilitiesRequestReceived(PacketBufferHandle && data);
CHIP_ERROR HandleCapabilitiesResponseReceived(PacketBufferHandle && data);
SequenceNumber_t AdjustRemoteReceiveWindow(SequenceNumber_t lastReceivedAck, SequenceNumber_t maxRemoteWindowSize,
SequenceNumber_t newestUnackedSentSeqNum);
// Timer control functions:
CHIP_ERROR StartConnectTimer(); // Start connect timer.
CHIP_ERROR StartReceiveConnectionTimer(); // Start receive connection timer.
CHIP_ERROR StartAckReceivedTimer(); // Start ack-received timer if it's not already running.
CHIP_ERROR RestartAckReceivedTimer(); // Restart ack-received timer.
CHIP_ERROR StartSendAckTimer(); // Start send-ack timer if it's not already running.
CHIP_ERROR StartUnsubscribeTimer();
void StopConnectTimer(); // Stop connect timer.
void StopReceiveConnectionTimer(); // Stop receive connection timer.
void StopAckReceivedTimer(); // Stop ack-received timer.
void StopSendAckTimer(); // Stop send-ack timer.
void StopUnsubscribeTimer(); // Stop unsubscribe timer.
// Timer expired callbacks:
static void HandleConnectTimeout(chip::System::Layer * systemLayer, void * appState);
static void HandleReceiveConnectionTimeout(chip::System::Layer * systemLayer, void * appState);
static void HandleAckReceivedTimeout(chip::System::Layer * systemLayer, void * appState);
static void HandleSendAckTimeout(chip::System::Layer * systemLayer, void * appState);
static void HandleUnsubscribeTimeout(chip::System::Layer * systemLayer, void * appState);
// Close functions:
void DoCloseCallback(uint8_t state, uint8_t flags, CHIP_ERROR err);
void FinalizeClose(uint8_t state, uint8_t flags, CHIP_ERROR err);
void Free();
void FreeBtpEngine();
// Mutex lock on Tx queue. Used only in BtpEngine test build for now.
#if CHIP_ENABLE_CHIPOBLE_TEST
inline void QueueTxLock() { mTxQueueMutex.Lock(); }
inline void QueueTxUnlock() { mTxQueueMutex.Unlock(); }
#else
inline void QueueTxLock() {}
inline void QueueTxUnlock() {}
#endif
void QueueTx(PacketBufferHandle && data, PacketType_t type);
};
} /* namespace Ble */
} /* namespace chip */