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 types and an object for the chip over
22 : * Bluetooth Low Energy (CHIPoBLE) byte-stream, connection-oriented
23 : * adaptation of chip for point-to-point Bluetooth Low Energy
24 : * (BLE) links.
25 : *
26 : */
27 :
28 : #pragma once
29 :
30 : #ifndef _CHIP_BLE_BLE_H
31 : #error "Please include <ble/Ble.h> instead!"
32 : #endif
33 :
34 : #include <cstdint>
35 : #include <cstring>
36 :
37 : #include <lib/core/CHIPError.h>
38 : #include <system/SystemPacketBuffer.h>
39 :
40 : namespace chip {
41 : namespace Ble {
42 :
43 : inline constexpr size_t kTransferProtocolHeaderFlagsSize = 1; // Size in bytes of encoded BTP fragment header flag bits
44 : inline constexpr size_t kTransferProtocolSequenceNumSize = 1; // Size in bytes of encoded BTP sequence number
45 : inline constexpr size_t kTransferProtocolAckSize = 1; // Size in bytes of encoded BTP fragment acknowledgement number
46 : inline constexpr size_t kTransferProtocolMsgLenSize = 2; // Size in byte of encoded BTP total fragmented message length
47 :
48 : constexpr size_t kTransferProtocolMaxHeaderSize =
49 : kTransferProtocolHeaderFlagsSize + kTransferProtocolAckSize + kTransferProtocolSequenceNumSize + kTransferProtocolMsgLenSize;
50 : constexpr size_t kTransferProtocolMidFragmentMaxHeaderSize =
51 : kTransferProtocolHeaderFlagsSize + kTransferProtocolAckSize + kTransferProtocolSequenceNumSize;
52 : constexpr size_t kTransferProtocolStandaloneAckHeaderSize =
53 : kTransferProtocolHeaderFlagsSize + kTransferProtocolAckSize + kTransferProtocolSequenceNumSize;
54 :
55 : using ::chip::System::PacketBufferHandle;
56 :
57 : typedef uint8_t SequenceNumber_t; // If type changed from uint8_t, adjust assumptions in BtpEngine::IsValidAck and
58 : // BLEEndPoint::AdjustReceiveWindow.
59 :
60 : // Public data members:
61 : typedef enum
62 : {
63 : kType_Data = 0, // Default 0 for data
64 : kType_Control = 1,
65 : } PacketType_t; // BTP packet types
66 :
67 : class BtpEngine
68 : {
69 : public:
70 : // Public data members:
71 : typedef enum
72 : {
73 : kState_Idle = 0,
74 : kState_InProgress = 1,
75 : kState_Complete = 2,
76 : kState_Error = 3
77 : } State_t; // [READ-ONLY] Current state
78 :
79 : // Masks for BTP fragment header flag bits.
80 : enum class HeaderFlags : uint8_t
81 : {
82 : kStartMessage = 0x01,
83 : kContinueMessage = 0x02,
84 : kEndMessage = 0x04,
85 : kFragmentAck = 0x08,
86 : };
87 :
88 : static const uint16_t sDefaultFragmentSize;
89 : static const uint16_t sMaxFragmentSize;
90 :
91 : // Public functions:
92 : CHIP_ERROR Init(void * an_app_state, bool expect_first_ack);
93 :
94 12 : inline void SetTxFragmentSize(uint16_t size) { mTxFragmentSize = size; }
95 12 : inline void SetRxFragmentSize(uint16_t size) { mRxFragmentSize = size; }
96 :
97 12 : uint16_t GetRxFragmentSize() const { return mRxFragmentSize; }
98 12 : uint16_t GetTxFragmentSize() const { return mTxFragmentSize; }
99 :
100 : SequenceNumber_t GetAndIncrementNextTxSeqNum();
101 : SequenceNumber_t GetAndRecordRxAckSeqNum();
102 :
103 : inline SequenceNumber_t GetLastReceivedSequenceNumber() const { return mRxNewestUnackedSeqNum; }
104 0 : inline SequenceNumber_t GetNewestUnackedSentSequenceNumber() const { return mTxNewestUnackedSeqNum; }
105 :
106 0 : inline bool ExpectingAck() const { return mExpectingAck; }
107 :
108 2 : inline State_t RxState() { return mRxState; }
109 12 : inline State_t TxState() { return mTxState; }
110 :
111 : bool HasUnackedData() const;
112 :
113 : CHIP_ERROR HandleCharacteristicReceived(System::PacketBufferHandle && data, SequenceNumber_t & receivedAck,
114 : bool & didReceiveAck);
115 : bool HandleCharacteristicSend(System::PacketBufferHandle data, bool send_ack);
116 : CHIP_ERROR EncodeStandAloneAck(const PacketBufferHandle & data);
117 :
118 : PacketBufferHandle TakeRxPacket();
119 : PacketBufferHandle BorrowRxPacket() { return mRxBuf.Retain(); }
120 12 : void ClearRxPacket() { (void) TakeRxPacket(); }
121 : PacketBufferHandle TakeTxPacket();
122 1 : PacketBufferHandle BorrowTxPacket() { return mTxBuf.Retain(); }
123 12 : void ClearTxPacket() { (void) TakeTxPacket(); }
124 :
125 : void LogState() const;
126 : void LogStateDebug() const;
127 :
128 : private:
129 : // Private data members:
130 : State_t mRxState;
131 : uint16_t mRxLength;
132 : void * mAppState;
133 : System::PacketBufferHandle mRxBuf;
134 : SequenceNumber_t mRxNextSeqNum;
135 : SequenceNumber_t mRxNewestUnackedSeqNum;
136 : SequenceNumber_t mRxOldestUnackedSeqNum;
137 : uint16_t mRxFragmentSize;
138 :
139 : State_t mTxState;
140 : uint16_t mTxLength;
141 : System::PacketBufferHandle mTxBuf;
142 : SequenceNumber_t mTxNextSeqNum;
143 : SequenceNumber_t mTxNewestUnackedSeqNum;
144 : SequenceNumber_t mTxOldestUnackedSeqNum;
145 : bool mExpectingAck;
146 : uint16_t mTxFragmentSize;
147 :
148 : uint16_t mRxCharCount;
149 : uint16_t mRxPacketCount;
150 : uint16_t mTxCharCount;
151 : uint16_t mTxPacketCount;
152 :
153 : // Private functions:
154 : bool IsValidAck(SequenceNumber_t ack_num) const;
155 : CHIP_ERROR HandleAckReceived(SequenceNumber_t ack_num);
156 : };
157 :
158 : } /* namespace Ble */
159 : } /* namespace chip */
|