Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 3 | * Copyright (c) 2020-2021 Project CHIP Authors |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 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 | * Implements utility methods for working with some complex BDX messages. |
| 22 | */ |
| 23 | |
| 24 | #include <protocols/bdx/BdxMessages.h> |
| 25 | |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 26 | #include <lib/support/BufferReader.h> |
| 27 | #include <lib/support/BufferWriter.h> |
| 28 | #include <lib/support/CodeUtils.h> |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 29 | |
| 30 | #include <limits> |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 31 | #include <utility> |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 32 | |
| 33 | namespace { |
Carol Yang | 1fd382d | 2022-03-02 20:36:27 -0800 | [diff] [blame] | 34 | constexpr uint8_t kVersionMask = 0x0F; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 35 | } // namespace |
| 36 | |
| 37 | using namespace chip; |
Trevor Holbrook | 775e37f | 2021-02-05 07:23:29 -0800 | [diff] [blame] | 38 | using namespace chip::bdx; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 39 | using namespace chip::Encoding::LittleEndian; |
| 40 | |
| 41 | // WARNING: this function should never return early, since MessageSize() relies on it to calculate |
| 42 | // the size of the message (even if the message is incomplete or filled out incorrectly). |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 43 | BufferWriter & TransferInit::WriteToBuffer(BufferWriter & aBuffer) const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 44 | { |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 45 | const BitFlags<TransferControlFlags> proposedTransferCtl(Version & kVersionMask, TransferCtlOptions); |
| 46 | const bool widerange = |
| 47 | (StartOffset > std::numeric_limits<uint32_t>::max()) || (MaxLength > std::numeric_limits<uint32_t>::max()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 48 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 49 | BitFlags<RangeControlFlags> rangeCtlFlags; |
| 50 | rangeCtlFlags.Set(RangeControlFlags::kDefLen, MaxLength > 0); |
| 51 | rangeCtlFlags.Set(RangeControlFlags::kStartOffset, StartOffset > 0); |
| 52 | rangeCtlFlags.Set(RangeControlFlags::kWiderange, widerange); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 53 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 54 | aBuffer.Put(proposedTransferCtl.Raw()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 55 | aBuffer.Put(rangeCtlFlags.Raw()); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 56 | aBuffer.Put16(MaxBlockSize); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 57 | |
| 58 | if (StartOffset > 0) |
| 59 | { |
| 60 | if (widerange) |
| 61 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 62 | aBuffer.Put64(StartOffset); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 63 | } |
| 64 | else |
| 65 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 66 | aBuffer.Put32(static_cast<uint32_t>(StartOffset)); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | if (MaxLength > 0) |
| 71 | { |
| 72 | if (widerange) |
| 73 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 74 | aBuffer.Put64(MaxLength); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 75 | } |
| 76 | else |
| 77 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 78 | aBuffer.Put32(static_cast<uint32_t>(MaxLength)); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 82 | aBuffer.Put16(FileDesLength); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 83 | if (FileDesignator != nullptr) |
| 84 | { |
| 85 | aBuffer.Put(FileDesignator, static_cast<size_t>(FileDesLength)); |
| 86 | } |
| 87 | |
| 88 | if (Metadata != nullptr) |
| 89 | { |
| 90 | aBuffer.Put(Metadata, static_cast<size_t>(MetadataLength)); |
| 91 | } |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 92 | return aBuffer; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 95 | CHIP_ERROR TransferInit::Parse(System::PacketBufferHandle aBuffer) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 96 | { |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 97 | uint8_t proposedTransferCtl; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 98 | uint32_t tmpUint32Value = 0; // Used for reading non-wide length and offset fields |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 99 | uint8_t * bufStart = aBuffer->Start(); |
| 100 | Reader bufReader(bufStart, aBuffer->DataLength()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 101 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 102 | ReturnErrorOnFailure( |
| 103 | bufReader.Read8(&proposedTransferCtl).Read8(mRangeCtlFlags.RawStorage()).Read16(&MaxBlockSize).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 104 | |
| 105 | Version = proposedTransferCtl & kVersionMask; |
| 106 | TransferCtlOptions.SetRaw(static_cast<uint8_t>(proposedTransferCtl & ~kVersionMask)); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 107 | |
| 108 | StartOffset = 0; |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 109 | if (mRangeCtlFlags.Has(RangeControlFlags::kStartOffset)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 110 | { |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 111 | if (mRangeCtlFlags.Has(RangeControlFlags::kWiderange)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 112 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 113 | ReturnErrorOnFailure(bufReader.Read64(&StartOffset).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 117 | ReturnErrorOnFailure(bufReader.Read32(&tmpUint32Value).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 118 | StartOffset = tmpUint32Value; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | MaxLength = 0; |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 123 | if (mRangeCtlFlags.Has(RangeControlFlags::kDefLen)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 124 | { |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 125 | if (mRangeCtlFlags.Has(RangeControlFlags::kWiderange)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 126 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 127 | ReturnErrorOnFailure(bufReader.Read64(&MaxLength).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 131 | ReturnErrorOnFailure(bufReader.Read32(&tmpUint32Value).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 132 | MaxLength = tmpUint32Value; |
| 133 | } |
| 134 | } |
| 135 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 136 | ReturnErrorOnFailure(bufReader.Read16(&FileDesLength).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 137 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 138 | VerifyOrReturnError(bufReader.HasAtLeast(FileDesLength), CHIP_ERROR_MESSAGE_INCOMPLETE); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 139 | FileDesignator = &bufStart[bufReader.OctetsRead()]; |
| 140 | |
| 141 | // Rest of message is metadata (could be empty) |
| 142 | Metadata = nullptr; |
| 143 | MetadataLength = 0; |
| 144 | if (bufReader.Remaining() > FileDesLength) |
| 145 | { |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 146 | uint16_t metadataStartIndex = static_cast<uint16_t>(bufReader.OctetsRead() + FileDesLength); |
| 147 | Metadata = &bufStart[metadataStartIndex]; |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 148 | MetadataLength = static_cast<uint16_t>(aBuffer->DataLength() - metadataStartIndex); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Kevin Schoedel | bd11f98 | 2021-01-26 13:34:29 -0500 | [diff] [blame] | 151 | // Retain ownership of the packet buffer so that the FileDesignator and Metadata pointers remain valid. |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 152 | Buffer = std::move(aBuffer); |
| 153 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 154 | return CHIP_NO_ERROR; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 157 | size_t TransferInit::MessageSize() const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 158 | { |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 159 | BufferWriter emptyBuf(nullptr, 0); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 160 | return WriteToBuffer(emptyBuf).Needed(); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 163 | #if CHIP_AUTOMATION_LOGGING |
| 164 | void TransferInit::LogMessage(bdx::MessageType messageType) const |
| 165 | { |
| 166 | char fd[kMaxFileDesignatorLen]; |
Boris Zbarsky | 2b52b36 | 2021-11-17 16:59:07 -0500 | [diff] [blame] | 167 | snprintf(fd, sizeof(fd), "%.*s", static_cast<int>(FileDesLength), FileDesignator); |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 168 | |
| 169 | switch (messageType) |
| 170 | { |
| 171 | case MessageType::SendInit: |
| 172 | ChipLogAutomation("SendInit"); |
| 173 | break; |
| 174 | case MessageType::ReceiveInit: |
| 175 | ChipLogAutomation("ReceiveInit"); |
| 176 | break; |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | ChipLogAutomation(" Proposed Transfer Control: 0x%X", static_cast<unsigned>(TransferCtlOptions.Raw() | Version)); |
| 182 | ChipLogAutomation(" Range Control: 0x%X", static_cast<unsigned>(mRangeCtlFlags.Raw())); |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 183 | ChipLogAutomation(" Proposed Max Block Size: %u", MaxBlockSize); |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 184 | ChipLogAutomation(" Start Offset: 0x" ChipLogFormatX64, ChipLogValueX64(StartOffset)); |
| 185 | ChipLogAutomation(" Proposed Max Length: 0x" ChipLogFormatX64, ChipLogValueX64(MaxLength)); |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 186 | ChipLogAutomation(" File Designator Length: %u", FileDesLength); |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 187 | ChipLogAutomation(" File Designator: %s", fd); |
| 188 | } |
| 189 | #endif // CHIP_AUTOMATION_LOGGING |
| 190 | |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 191 | bool TransferInit::operator==(const TransferInit & another) const |
| 192 | { |
| 193 | if ((MetadataLength != another.MetadataLength) || (FileDesLength != another.FileDesLength)) |
| 194 | { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | bool fileDesMatches = true; |
| 199 | if (FileDesLength > 0) |
| 200 | { |
| 201 | fileDesMatches = (memcmp(FileDesignator, another.FileDesignator, FileDesLength) == 0); |
| 202 | } |
| 203 | |
| 204 | bool metadataMatches = true; |
| 205 | if (MetadataLength > 0) |
| 206 | { |
| 207 | metadataMatches = (memcmp(Metadata, another.Metadata, MetadataLength) == 0); |
| 208 | } |
| 209 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 210 | return ((Version == another.Version) && (TransferCtlOptions == another.TransferCtlOptions) && |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 211 | (StartOffset == another.StartOffset) && (MaxLength == another.MaxLength) && (MaxBlockSize == another.MaxBlockSize) && |
| 212 | fileDesMatches && metadataMatches); |
| 213 | } |
| 214 | |
| 215 | // WARNING: this function should never return early, since MessageSize() relies on it to calculate |
| 216 | // the size of the message (even if the message is incomplete or filled out incorrectly). |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 217 | Encoding::LittleEndian::BufferWriter & SendAccept::WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 218 | { |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 219 | const BitFlags<TransferControlFlags> transferCtl(Version & kVersionMask, TransferCtlFlags); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 220 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 221 | aBuffer.Put(transferCtl.Raw()); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 222 | aBuffer.Put16(MaxBlockSize); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 223 | |
| 224 | if (Metadata != nullptr) |
| 225 | { |
| 226 | aBuffer.Put(Metadata, static_cast<size_t>(MetadataLength)); |
| 227 | } |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 228 | return aBuffer; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 231 | CHIP_ERROR SendAccept::Parse(System::PacketBufferHandle aBuffer) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 232 | { |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 233 | uint8_t transferCtl = 0; |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 234 | uint8_t * bufStart = aBuffer->Start(); |
| 235 | Reader bufReader(bufStart, aBuffer->DataLength()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 236 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 237 | ReturnErrorOnFailure(bufReader.Read8(&transferCtl).Read16(&MaxBlockSize).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 238 | |
| 239 | Version = transferCtl & kVersionMask; |
| 240 | |
| 241 | // Only one of these values should be set. It is up to the caller to verify this. |
| 242 | TransferCtlFlags.SetRaw(static_cast<uint8_t>(transferCtl & ~kVersionMask)); |
| 243 | |
| 244 | // Rest of message is metadata (could be empty) |
| 245 | Metadata = nullptr; |
| 246 | MetadataLength = 0; |
| 247 | if (bufReader.Remaining() > 0) |
| 248 | { |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 249 | Metadata = &bufStart[bufReader.OctetsRead()]; |
| 250 | MetadataLength = bufReader.Remaining(); |
| 251 | } |
| 252 | |
Kevin Schoedel | bd11f98 | 2021-01-26 13:34:29 -0500 | [diff] [blame] | 253 | // Retain ownership of the packet buffer so that the Metadata pointer remains valid. |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 254 | Buffer = std::move(aBuffer); |
| 255 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 256 | return CHIP_NO_ERROR; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 259 | size_t SendAccept::MessageSize() const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 260 | { |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 261 | BufferWriter emptyBuf(nullptr, 0); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 262 | return WriteToBuffer(emptyBuf).Needed(); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 265 | #if CHIP_AUTOMATION_LOGGING |
| 266 | void SendAccept::LogMessage(bdx::MessageType messageType) const |
| 267 | { |
| 268 | (void) messageType; |
| 269 | ChipLogAutomation("SendAccept"); |
| 270 | ChipLogAutomation(" Transfer Control: 0x%X", static_cast<unsigned>(TransferCtlFlags.Raw() | Version)); |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 271 | ChipLogAutomation(" Max Block Size: %u", MaxBlockSize); |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 272 | } |
| 273 | #endif // CHIP_AUTOMATION_LOGGING |
| 274 | |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 275 | bool SendAccept::operator==(const SendAccept & another) const |
| 276 | { |
| 277 | if (MetadataLength != another.MetadataLength) |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | bool metadataMatches = true; |
| 283 | if (MetadataLength > 0) |
| 284 | { |
| 285 | metadataMatches = (memcmp(Metadata, another.Metadata, MetadataLength) == 0); |
| 286 | } |
| 287 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 288 | return ((Version == another.Version) && (TransferCtlFlags == another.TransferCtlFlags) && |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 289 | (MaxBlockSize == another.MaxBlockSize) && metadataMatches); |
| 290 | } |
| 291 | |
| 292 | // WARNING: this function should never return early, since MessageSize() relies on it to calculate |
| 293 | // the size of the message (even if the message is incomplete or filled out incorrectly). |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 294 | Encoding::LittleEndian::BufferWriter & ReceiveAccept::WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 295 | { |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 296 | const BitFlags<TransferControlFlags> transferCtlFlags(Version & kVersionMask, TransferCtlFlags); |
| 297 | const bool widerange = (StartOffset > std::numeric_limits<uint32_t>::max()) || (Length > std::numeric_limits<uint32_t>::max()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 298 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 299 | BitFlags<RangeControlFlags> rangeCtlFlags; |
| 300 | rangeCtlFlags.Set(RangeControlFlags::kDefLen, Length > 0); |
| 301 | rangeCtlFlags.Set(RangeControlFlags::kStartOffset, StartOffset > 0); |
| 302 | rangeCtlFlags.Set(RangeControlFlags::kWiderange, widerange); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 303 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 304 | aBuffer.Put(transferCtlFlags.Raw()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 305 | aBuffer.Put(rangeCtlFlags.Raw()); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 306 | aBuffer.Put16(MaxBlockSize); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 307 | |
| 308 | if (StartOffset > 0) |
| 309 | { |
| 310 | if (widerange) |
| 311 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 312 | aBuffer.Put64(StartOffset); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 313 | } |
| 314 | else |
| 315 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 316 | aBuffer.Put32(static_cast<uint32_t>(StartOffset)); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | if (Length > 0) |
| 321 | { |
| 322 | if (widerange) |
| 323 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 324 | aBuffer.Put64(Length); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 328 | aBuffer.Put32(static_cast<uint32_t>(Length)); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | if (Metadata != nullptr) |
| 333 | { |
| 334 | aBuffer.Put(Metadata, static_cast<size_t>(MetadataLength)); |
| 335 | } |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 336 | return aBuffer; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 339 | CHIP_ERROR ReceiveAccept::Parse(System::PacketBufferHandle aBuffer) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 340 | { |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 341 | uint8_t transferCtl = 0; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 342 | uint32_t tmpUint32Value = 0; // Used for reading non-wide length and offset fields |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 343 | uint8_t * bufStart = aBuffer->Start(); |
| 344 | Reader bufReader(bufStart, aBuffer->DataLength()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 345 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 346 | ReturnErrorOnFailure(bufReader.Read8(&transferCtl).Read8(mRangeCtlFlags.RawStorage()).Read16(&MaxBlockSize).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 347 | |
| 348 | Version = transferCtl & kVersionMask; |
| 349 | |
| 350 | // Only one of these values should be set. It is up to the caller to verify this. |
| 351 | TransferCtlFlags.SetRaw(static_cast<uint8_t>(transferCtl & ~kVersionMask)); |
| 352 | |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 353 | StartOffset = 0; |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 354 | if (mRangeCtlFlags.Has(RangeControlFlags::kStartOffset)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 355 | { |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 356 | if (mRangeCtlFlags.Has(RangeControlFlags::kWiderange)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 357 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 358 | ReturnErrorOnFailure(bufReader.Read64(&StartOffset).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 359 | } |
| 360 | else |
| 361 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 362 | ReturnErrorOnFailure(bufReader.Read32(&tmpUint32Value).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 363 | StartOffset = tmpUint32Value; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | Length = 0; |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 368 | if (mRangeCtlFlags.Has(RangeControlFlags::kDefLen)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 369 | { |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 370 | if (mRangeCtlFlags.Has(RangeControlFlags::kWiderange)) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 371 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 372 | ReturnErrorOnFailure(bufReader.Read64(&Length).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 373 | } |
| 374 | else |
| 375 | { |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 376 | ReturnErrorOnFailure(bufReader.Read32(&tmpUint32Value).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 377 | Length = tmpUint32Value; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // Rest of message is metadata (could be empty) |
| 382 | Metadata = nullptr; |
| 383 | MetadataLength = 0; |
| 384 | if (bufReader.Remaining() > 0) |
| 385 | { |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 386 | Metadata = &bufStart[bufReader.OctetsRead()]; |
| 387 | MetadataLength = bufReader.Remaining(); |
| 388 | } |
| 389 | |
Kevin Schoedel | bd11f98 | 2021-01-26 13:34:29 -0500 | [diff] [blame] | 390 | // Retain ownership of the packet buffer so that the Metadata pointer remains valid. |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 391 | Buffer = std::move(aBuffer); |
| 392 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 393 | return CHIP_NO_ERROR; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 396 | size_t ReceiveAccept::MessageSize() const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 397 | { |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 398 | BufferWriter emptyBuf(nullptr, 0); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 399 | return WriteToBuffer(emptyBuf).Needed(); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 402 | #if CHIP_AUTOMATION_LOGGING |
| 403 | void ReceiveAccept::LogMessage(bdx::MessageType messageType) const |
| 404 | { |
| 405 | (void) messageType; |
| 406 | ChipLogAutomation("ReceiveAccept"); |
| 407 | ChipLogAutomation(" Transfer Control: 0x%X", TransferCtlFlags.Raw() | Version); |
| 408 | ChipLogAutomation(" Range Control: 0x%X", mRangeCtlFlags.Raw()); |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 409 | ChipLogAutomation(" Max Block Size: %u", MaxBlockSize); |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 410 | ChipLogAutomation(" Length: 0x" ChipLogFormatX64, ChipLogValueX64(Length)); |
| 411 | } |
| 412 | #endif // CHIP_AUTOMATION_LOGGING |
| 413 | |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 414 | bool ReceiveAccept::operator==(const ReceiveAccept & another) const |
| 415 | { |
| 416 | if (MetadataLength != another.MetadataLength) |
| 417 | { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | bool metadataMatches = true; |
| 422 | if (MetadataLength > 0) |
| 423 | { |
| 424 | metadataMatches = (memcmp(Metadata, another.Metadata, MetadataLength) == 0); |
| 425 | } |
| 426 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 427 | return ((Version == another.Version) && (TransferCtlFlags == another.TransferCtlFlags) && |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 428 | (StartOffset == another.StartOffset) && (MaxBlockSize == another.MaxBlockSize) && (Length == another.Length) && |
| 429 | metadataMatches); |
| 430 | } |
| 431 | |
| 432 | // WARNING: this function should never return early, since MessageSize() relies on it to calculate |
| 433 | // the size of the message (even if the message is incomplete or filled out incorrectly). |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 434 | Encoding::LittleEndian::BufferWriter & CounterMessage::WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 435 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 436 | return aBuffer.Put32(BlockCounter); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 439 | CHIP_ERROR CounterMessage::Parse(System::PacketBufferHandle aBuffer) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 440 | { |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 441 | uint8_t * bufStart = aBuffer->Start(); |
| 442 | Reader bufReader(bufStart, aBuffer->DataLength()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 443 | return bufReader.Read32(&BlockCounter).StatusCode(); |
| 444 | } |
| 445 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 446 | size_t CounterMessage::MessageSize() const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 447 | { |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 448 | BufferWriter emptyBuf(nullptr, 0); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 449 | return WriteToBuffer(emptyBuf).Needed(); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | bool CounterMessage::operator==(const CounterMessage & another) const |
| 453 | { |
| 454 | return (BlockCounter == another.BlockCounter); |
| 455 | } |
| 456 | |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 457 | #if CHIP_AUTOMATION_LOGGING |
| 458 | void CounterMessage::LogMessage(bdx::MessageType messageType) const |
| 459 | { |
| 460 | switch (messageType) |
| 461 | { |
| 462 | case MessageType::BlockQuery: |
| 463 | ChipLogAutomation("BlockQuery"); |
| 464 | break; |
| 465 | case MessageType::BlockAck: |
| 466 | ChipLogAutomation("BlockAck"); |
| 467 | break; |
| 468 | case MessageType::BlockAckEOF: |
| 469 | ChipLogAutomation("BlockAckEOF"); |
| 470 | break; |
| 471 | default: |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | ChipLogAutomation(" Block Counter: %" PRIu32, BlockCounter); |
| 476 | } |
| 477 | #endif // CHIP_AUTOMATION_LOGGING |
| 478 | |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 479 | // WARNING: this function should never return early, since MessageSize() relies on it to calculate |
| 480 | // the size of the message (even if the message is incomplete or filled out incorrectly). |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 481 | Encoding::LittleEndian::BufferWriter & DataBlock::WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 482 | { |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 483 | aBuffer.Put32(BlockCounter); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 484 | if (Data != nullptr) |
| 485 | { |
| 486 | aBuffer.Put(Data, DataLength); |
| 487 | } |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 488 | return aBuffer; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 491 | CHIP_ERROR DataBlock::Parse(System::PacketBufferHandle aBuffer) |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 492 | { |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 493 | uint8_t * bufStart = aBuffer->Start(); |
| 494 | Reader bufReader(bufStart, aBuffer->DataLength()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 495 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 496 | ReturnErrorOnFailure(bufReader.Read32(&BlockCounter).StatusCode()); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 497 | |
| 498 | // Rest of message is data |
| 499 | Data = nullptr; |
| 500 | DataLength = 0; |
| 501 | if (bufReader.Remaining() > 0) |
| 502 | { |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 503 | Data = &bufStart[bufReader.OctetsRead()]; |
| 504 | DataLength = bufReader.Remaining(); |
| 505 | } |
| 506 | |
Kevin Schoedel | bd11f98 | 2021-01-26 13:34:29 -0500 | [diff] [blame] | 507 | // Retain ownership of the packet buffer so that the Data pointer remains valid. |
Kevin Schoedel | b5a5d89 | 2020-12-03 19:01:10 -0500 | [diff] [blame] | 508 | Buffer = std::move(aBuffer); |
| 509 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 510 | return CHIP_NO_ERROR; |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Trevor Holbrook | 4d875fb | 2021-02-05 11:57:48 -0800 | [diff] [blame] | 513 | size_t DataBlock::MessageSize() const |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 514 | { |
Kevin Schoedel | 2d48e75 | 2021-02-08 09:29:17 -0500 | [diff] [blame] | 515 | BufferWriter emptyBuf(nullptr, 0); |
Rob Walker | cc92b63 | 2020-11-21 13:21:56 -0800 | [diff] [blame] | 516 | return WriteToBuffer(emptyBuf).Needed(); |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 519 | #if CHIP_AUTOMATION_LOGGING |
| 520 | void DataBlock::LogMessage(bdx::MessageType messageType) const |
| 521 | { |
| 522 | switch (messageType) |
| 523 | { |
| 524 | case MessageType::Block: |
| 525 | ChipLogAutomation("Block"); |
| 526 | break; |
| 527 | case MessageType::BlockEOF: |
| 528 | ChipLogAutomation("BlockEOF"); |
| 529 | break; |
| 530 | default: |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | ChipLogAutomation(" Block Counter: %" PRIu32, BlockCounter); |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 535 | ChipLogAutomation(" Data Length: %u", static_cast<unsigned int>(DataLength)); |
Carol Yang | fde6a17 | 2021-11-08 17:30:19 -0800 | [diff] [blame] | 536 | } |
| 537 | #endif // CHIP_AUTOMATION_LOGGING |
| 538 | |
Trevor Holbrook | 934f89c | 2020-11-18 15:34:14 -0800 | [diff] [blame] | 539 | bool DataBlock::operator==(const DataBlock & another) const |
| 540 | { |
| 541 | if (DataLength != another.DataLength) |
| 542 | { |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | bool dataMatches = true; |
| 547 | if (DataLength > 0) |
| 548 | { |
| 549 | dataMatches = memcmp(Data, another.Data, DataLength) == 0; |
| 550 | } |
| 551 | |
| 552 | return ((BlockCounter == another.BlockCounter) && dataMatches); |
| 553 | } |
Shubham Patil | 6c928f4 | 2021-12-13 21:21:50 +0530 | [diff] [blame] | 554 | |
| 555 | // WARNING: this function should never return early, since MessageSize() relies on it to calculate |
| 556 | // the size of the message (even if the message is incomplete or filled out incorrectly). |
| 557 | Encoding::LittleEndian::BufferWriter & BlockQueryWithSkip::WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const |
| 558 | { |
| 559 | aBuffer.Put32(BlockCounter); |
| 560 | aBuffer.Put64(BytesToSkip); |
| 561 | return aBuffer; |
| 562 | } |
| 563 | |
| 564 | CHIP_ERROR BlockQueryWithSkip::Parse(System::PacketBufferHandle aBuffer) |
| 565 | { |
Shubham Patil | 6c928f4 | 2021-12-13 21:21:50 +0530 | [diff] [blame] | 566 | uint8_t * bufStart = aBuffer->Start(); |
| 567 | Reader bufReader(bufStart, aBuffer->DataLength()); |
Shubham Patil | 6c928f4 | 2021-12-13 21:21:50 +0530 | [diff] [blame] | 568 | |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 569 | return bufReader.Read32(&BlockCounter).Read64(&BytesToSkip).StatusCode(); |
Shubham Patil | 6c928f4 | 2021-12-13 21:21:50 +0530 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | size_t BlockQueryWithSkip::MessageSize() const |
| 573 | { |
| 574 | BufferWriter emptyBuf(nullptr, 0); |
| 575 | return WriteToBuffer(emptyBuf).Needed(); |
| 576 | } |
| 577 | |
| 578 | bool BlockQueryWithSkip::operator==(const BlockQueryWithSkip & another) const |
| 579 | { |
| 580 | return (BlockCounter == another.BlockCounter && BytesToSkip == another.BytesToSkip); |
| 581 | } |
| 582 | |
| 583 | #if CHIP_AUTOMATION_LOGGING |
| 584 | void BlockQueryWithSkip::LogMessage(bdx::MessageType messageType) const |
| 585 | { |
| 586 | ChipLogAutomation("BlockQueryWithSkip"); |
| 587 | ChipLogAutomation(" Block Counter: %" PRIu32, BlockCounter); |
Boris Zbarsky | 75de58d | 2023-11-17 09:43:20 -0500 | [diff] [blame] | 588 | ChipLogAutomation(" Bytes To Skip: 0x" ChipLogFormatX64, ChipLogValueX64(BytesToSkip)); |
Shubham Patil | 6c928f4 | 2021-12-13 21:21:50 +0530 | [diff] [blame] | 589 | } |
| 590 | #endif // CHIP_AUTOMATION_LOGGING |