1 // Copyright 2021 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <optional> 17 18 #include "pw_bytes/span.h" 19 #include "pw_result/result.h" 20 21 namespace pw::transfer::internal { 22 23 struct Chunk { 24 // TODO(frolv): This is copied from the proto enum. Ideally, this entire class 25 // would be generated by pw_protobuf. 26 enum Type { 27 kTransferData = 0, 28 kTransferStart = 1, 29 kParametersRetransmit = 2, 30 kParametersContinue = 3, 31 kTransferCompletion = 4, 32 kTransferCompletionAck = 5, // Currently unused. 33 }; 34 35 // The initial chunk always has an offset of 0 and no data or status. 36 // 37 // TODO(frolv): Going forward, all users of transfer should set a type for 38 // all chunks. This initial chunk assumption should be removed. IsInitialChunkChunk39 constexpr bool IsInitialChunk() const { 40 return type == Type::kTransferStart || 41 (offset == 0 && data.empty() && !status.has_value()); 42 } 43 44 // The final chunk from the transmitter sets remaining_bytes to 0 in both Read 45 // and Write transfers. IsFinalTransmitChunkChunk46 constexpr bool IsFinalTransmitChunk() const { return remaining_bytes == 0u; } 47 48 uint32_t transfer_id; 49 uint32_t window_end_offset; 50 std::optional<uint32_t> pending_bytes; 51 std::optional<uint32_t> max_chunk_size_bytes; 52 std::optional<uint32_t> min_delay_microseconds; 53 uint32_t offset; 54 ConstByteSpan data; 55 std::optional<uint64_t> remaining_bytes; 56 std::optional<Status> status; 57 std::optional<Type> type; 58 }; 59 60 // Partially decodes a transfer chunk to find its transfer ID field. 61 Result<uint32_t> ExtractTransferId(ConstByteSpan message); 62 63 Status DecodeChunk(ConstByteSpan message, Chunk& chunk); 64 Result<ConstByteSpan> EncodeChunk(const Chunk& chunk, ByteSpan buffer); 65 66 } // namespace pw::transfer::internal 67