LCOV - code coverage report
Current view: top level - ble - BLEEndPoint.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 0 3 0.0 %
Date: 2024-02-15 08:20:41 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2020-2021 Project CHIP Authors
       4             :  *    Copyright (c) 2014-2017 Nest Labs, Inc.
       5             :  *
       6             :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7             :  *    you may not use this file except in compliance with the License.
       8             :  *    You may obtain a copy of the License at
       9             :  *
      10             :  *        http://www.apache.org/licenses/LICENSE-2.0
      11             :  *
      12             :  *    Unless required by applicable law or agreed to in writing, software
      13             :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14             :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15             :  *    See the License for the specific language governing permissions and
      16             :  *    limitations under the License.
      17             :  */
      18             : 
      19             : /**
      20             :  *    @file
      21             :  *      This file defines a Bluetooth Low Energy (BLE) connection
      22             :  *      endpoint abstraction for the byte-streaming,
      23             :  *      connection-oriented CHIP over Bluetooth Low Energy (CHIPoBLE)
      24             :  *      Bluetooth Transport Protocol (BTP).
      25             :  *
      26             :  */
      27             : 
      28             : #pragma once
      29             : 
      30             : #include <system/SystemLayer.h>
      31             : 
      32             : #include <ble/BleRole.h>
      33             : #include <ble/BtpEngine.h>
      34             : 
      35             : #if CHIP_ENABLE_CHIPOBLE_TEST
      36             : #include <ble/BtpEngineTest.h>
      37             : #include <system/SystemMutex.h>
      38             : #endif
      39             : 
      40             : namespace chip {
      41             : namespace Ble {
      42             : 
      43             : using ::chip::System::PacketBufferHandle;
      44             : 
      45             : enum
      46             : {
      47             :     kBleCloseFlag_SuppressCallback  = 0x01,
      48             :     kBleCloseFlag_AbortTransmission = 0x02
      49             : };
      50             : 
      51             : // Forward declarations
      52             : class BleLayer;
      53             : class BleEndPointPool;
      54             : // BLEEndPoint holds a pointer to BleLayerDelegate for messages, while BleLayerDelegate functions also accepts BLEEndPoint.
      55             : class BleLayerDelegate;
      56             : #if CHIP_ENABLE_CHIPOBLE_TEST
      57             : class BtpEngineTest;
      58             : #endif
      59             : 
      60             : class DLL_EXPORT BLEEndPoint
      61             : {
      62             :     friend class BleLayer;
      63             :     friend class BleEndPointPool;
      64             : #if CHIP_ENABLE_CHIPOBLE_TEST
      65             :     friend class BtpEngineTest;
      66             : #endif
      67             : 
      68             : public:
      69             :     typedef uint64_t AlignT;
      70             : 
      71             :     // Public data members:
      72             :     enum
      73             :     {
      74             :         kState_Ready      = 0,
      75             :         kState_Connecting = 1,
      76             :         kState_Aborting   = 2,
      77             :         kState_Connected  = 3,
      78             :         kState_Closing    = 4,
      79             :         kState_Closed     = 5
      80             :     } mState; // [READ-ONLY] End point connection state. Refers to state of CHIP over
      81             :               // BLE transport protocol connection, not of underlying BLE connection.
      82             : 
      83             :     // Public function pointers:
      84             :     typedef void (*OnConnectCompleteFunct)(BLEEndPoint * endPoint, CHIP_ERROR err);
      85             :     OnConnectCompleteFunct OnConnectComplete;
      86             : 
      87             :     typedef void (*OnMessageReceivedFunct)(BLEEndPoint * endPoint, PacketBufferHandle && msg);
      88             :     OnMessageReceivedFunct OnMessageReceived;
      89             : 
      90             :     typedef void (*OnConnectionClosedFunct)(BLEEndPoint * endPoint, CHIP_ERROR err);
      91             :     OnConnectionClosedFunct OnConnectionClosed;
      92             : 
      93             : #if CHIP_ENABLE_CHIPOBLE_TEST
      94             :     typedef void (*OnCommandReceivedFunct)(BLEEndPoint * endPoint, PacketBufferHandle && msg);
      95             :     OnCommandReceivedFunct OnCommandReceived;
      96             :     inline void SetOnCommandReceivedCB(OnCommandReceivedFunct cb) { OnCommandReceived = cb; };
      97             :     BtpEngineTest mBtpEngineTest;
      98             :     inline void SetTxWindowSize(uint8_t size) { mRemoteReceiveWindowSize = size; };
      99             :     inline void SetRxWindowSize(uint8_t size) { mReceiveWindowMaxSize = size; };
     100             : #endif
     101             : 
     102             :     // Public functions:
     103             :     CHIP_ERROR Send(PacketBufferHandle && data);
     104             :     CHIP_ERROR Receive(PacketBufferHandle && data);
     105             :     CHIP_ERROR StartConnect();
     106             : 
     107             :     bool IsUnsubscribePending() const;
     108           0 :     bool ConnectionObjectIs(BLE_CONNECTION_OBJECT connObj) { return connObj == mConnObj; }
     109             :     void Close();
     110             :     void Abort();
     111             : 
     112             : private:
     113             :     BleLayer * mBle; ///< [READ-ONLY] Pointer to the BleLayer object that owns this object.
     114             :     BleLayerDelegate * mBleTransport;
     115             : 
     116             :     uint32_t mRefCount;
     117             : 
     118             :     void AddRef();
     119             :     void Release();
     120             : 
     121             :     // Private data members:
     122             :     enum class ConnectionStateFlag : uint8_t
     123             :     {
     124             :         kAutoClose                = 0x01, // End point should close underlying BLE conn on BTP close.
     125             :         kCapabilitiesConfReceived = 0x02, // GATT confirmation received for sent capabilities req/resp.
     126             :         kCapabilitiesMsgReceived  = 0x04, // Capabilities request or response message received.
     127             :         kDidBeginSubscribe        = 0x08, // GATT subscribe request sent; must unsubscribe on close.
     128             :         kStandAloneAckInFlight    = 0x10, // Stand-alone ack in flight, awaiting GATT confirmation.
     129             :         kGattOperationInFlight    = 0x20  // GATT write, indication, subscribe, or unsubscribe in flight,
     130             :                                           // awaiting GATT confirmation.
     131             :     };
     132             : 
     133             :     enum class TimerStateFlag : uint8_t
     134             :     {
     135             :         kConnectTimerRunning           = 0x01, // BTP connect completion timer running.
     136             :         kReceiveConnectionTimerRunning = 0x02, // BTP receive connection completion timer running.
     137             :         kAckReceivedTimerRunning       = 0x04, // Ack received timer running due to unacked sent fragment.
     138             :         kSendAckTimerRunning           = 0x08, // Send ack timer running; indicates pending ack to send.
     139             :         kUnsubscribeTimerRunning       = 0x10, // Unsubscribe completion timer running.
     140             : #if CHIP_ENABLE_CHIPOBLE_TEST
     141             :         kUnderTestTimerRunnung = 0x80 // running throughput Tx test
     142             : #endif
     143             :     };
     144             : 
     145             :     // BLE connection to which an end point is uniquely bound. Type BLE_CONNECTION_OBJECT is defined by the platform or
     146             :     // void* by default. This object is passed back to the platform delegate with each call to send traffic over or
     147             :     // modify the state of the underlying BLE connection.
     148             :     BLE_CONNECTION_OBJECT mConnObj;
     149             : 
     150             :     // Queue of outgoing messages to send when current BtpEngine transmission completes.
     151             :     //
     152             :     // Re-used during connection setup to cache capabilities request and response payloads; payloads are freed when
     153             :     // connection is established.
     154             :     PacketBufferHandle mSendQueue;
     155             : 
     156             :     // Pending stand-alone BTP acknowledgement. Pre-empts regular send queue or fragmented message transmission in
     157             :     // progress.
     158             :     PacketBufferHandle mAckToSend;
     159             : 
     160             :     BtpEngine mBtpEngine;
     161             :     BleRole mRole;
     162             :     BitFlags<ConnectionStateFlag> mConnStateFlags;
     163             :     BitFlags<TimerStateFlag> mTimerStateFlags;
     164             :     SequenceNumber_t mLocalReceiveWindowSize;
     165             :     SequenceNumber_t mRemoteReceiveWindowSize;
     166             :     SequenceNumber_t mReceiveWindowMaxSize;
     167             : #if CHIP_ENABLE_CHIPOBLE_TEST
     168             :     chip::System::Mutex mTxQueueMutex; // For MT-safe Tx queuing
     169             : #endif
     170             : 
     171             :     // Private functions:
     172             :     BLEEndPoint()  = delete;
     173             :     ~BLEEndPoint() = delete;
     174             : 
     175             :     CHIP_ERROR Init(BleLayer * bleLayer, BLE_CONNECTION_OBJECT connObj, BleRole role, bool autoClose);
     176             :     bool IsConnected(uint8_t state) const;
     177             :     void DoClose(uint8_t flags, CHIP_ERROR err);
     178             : 
     179             :     // Transmit path:
     180             :     CHIP_ERROR DriveSending();
     181             :     CHIP_ERROR DriveStandAloneAck();
     182             :     bool PrepareNextFragment(PacketBufferHandle && data, bool & sentAck);
     183             :     CHIP_ERROR SendNextMessage();
     184             :     CHIP_ERROR ContinueMessageSend();
     185             :     CHIP_ERROR DoSendStandAloneAck();
     186             :     CHIP_ERROR SendCharacteristic(PacketBufferHandle && buf);
     187             :     bool SendIndication(PacketBufferHandle && buf);
     188             :     bool SendWrite(PacketBufferHandle && buf);
     189             : 
     190             :     // Receive path:
     191             :     CHIP_ERROR HandleConnectComplete();
     192             :     CHIP_ERROR HandleReceiveConnectionComplete();
     193             :     void HandleSubscribeReceived();
     194             :     void HandleSubscribeComplete();
     195             :     void HandleUnsubscribeComplete();
     196             :     CHIP_ERROR HandleGattSendConfirmationReceived();
     197             :     CHIP_ERROR HandleHandshakeConfirmationReceived();
     198             :     CHIP_ERROR HandleFragmentConfirmationReceived();
     199             :     CHIP_ERROR HandleCapabilitiesRequestReceived(PacketBufferHandle && data);
     200             :     CHIP_ERROR HandleCapabilitiesResponseReceived(PacketBufferHandle && data);
     201             :     SequenceNumber_t AdjustRemoteReceiveWindow(SequenceNumber_t lastReceivedAck, SequenceNumber_t maxRemoteWindowSize,
     202             :                                                SequenceNumber_t newestUnackedSentSeqNum);
     203             : 
     204             :     // Timer control functions:
     205             :     CHIP_ERROR StartConnectTimer();           // Start connect timer.
     206             :     CHIP_ERROR StartReceiveConnectionTimer(); // Start receive connection timer.
     207             :     CHIP_ERROR StartAckReceivedTimer();       // Start ack-received timer if it's not already running.
     208             :     CHIP_ERROR RestartAckReceivedTimer();     // Restart ack-received timer.
     209             :     CHIP_ERROR StartSendAckTimer();           // Start send-ack timer if it's not already running.
     210             :     CHIP_ERROR StartUnsubscribeTimer();
     211             :     void StopConnectTimer();           // Stop connect timer.
     212             :     void StopReceiveConnectionTimer(); // Stop receive connection timer.
     213             :     void StopAckReceivedTimer();       // Stop ack-received timer.
     214             :     void StopSendAckTimer();           // Stop send-ack timer.
     215             :     void StopUnsubscribeTimer();       // Stop unsubscribe timer.
     216             : 
     217             :     // Timer expired callbacks:
     218             :     static void HandleConnectTimeout(chip::System::Layer * systemLayer, void * appState);
     219             :     static void HandleReceiveConnectionTimeout(chip::System::Layer * systemLayer, void * appState);
     220             :     static void HandleAckReceivedTimeout(chip::System::Layer * systemLayer, void * appState);
     221             :     static void HandleSendAckTimeout(chip::System::Layer * systemLayer, void * appState);
     222             :     static void HandleUnsubscribeTimeout(chip::System::Layer * systemLayer, void * appState);
     223             : 
     224             :     // Close functions:
     225             :     void DoCloseCallback(uint8_t state, uint8_t flags, CHIP_ERROR err);
     226             :     void FinalizeClose(uint8_t state, uint8_t flags, CHIP_ERROR err);
     227             :     void ReleaseBleConnection();
     228             :     void Free();
     229             :     void FreeBtpEngine();
     230             : 
     231             :     // Mutex lock on Tx queue. Used only in BtpEngine test build for now.
     232             : #if CHIP_ENABLE_CHIPOBLE_TEST
     233             :     inline void QueueTxLock() { mTxQueueMutex.Lock(); }
     234             :     inline void QueueTxUnlock() { mTxQueueMutex.Unlock(); }
     235             : #else
     236           0 :     inline void QueueTxLock() {}
     237           0 :     inline void QueueTxUnlock() {}
     238             : #endif
     239             :     void QueueTx(PacketBufferHandle && data, PacketType_t type);
     240             : };
     241             : 
     242             : } /* namespace Ble */
     243             : } /* namespace chip */

Generated by: LCOV version 1.14