1 // Copyright 2024 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 <limits> 17 18 #include "pw_function/function.h" 19 #include "pw_transfer/internal/context.h" 20 21 namespace pw::transfer::internal { 22 23 class ClientContext final : public Context { 24 public: ClientContext()25 constexpr ClientContext() 26 : handle_id_(0), 27 transfer_size_bytes_(std::numeric_limits<size_t>::max()), 28 on_completion_(nullptr) {} 29 set_on_completion(Function<void (Status)> && on_completion)30 void set_on_completion(Function<void(Status)>&& on_completion) { 31 on_completion_ = std::move(on_completion); 32 } 33 handle_id()34 constexpr uint32_t handle_id() const { return handle_id_; } set_handle_id(uint32_t handle_id)35 constexpr void set_handle_id(uint32_t handle_id) { handle_id_ = handle_id; } 36 set_transfer_size_bytes(size_t transfer_size_bytes)37 constexpr void set_transfer_size_bytes(size_t transfer_size_bytes) { 38 transfer_size_bytes_ = transfer_size_bytes; 39 } 40 41 private: 42 Status FinalCleanup(Status status) override; 43 TransferSizeBytes()44 size_t TransferSizeBytes() const override { return transfer_size_bytes_; } 45 46 // Seeks the reader to the offset, taking into account the client side reader 47 // needs to be shifted back for the initial offset. 48 Status SeekReader(uint32_t offset) override; 49 50 // Transfer clients assign a unique handle_id to all active transfer sessions. 51 // Unlike session or transfer IDs, this value is local to the client, not 52 // requiring any coordination with the transfer server, allowing users of the 53 // client to manage their ongoing transfers. 54 uint32_t handle_id_; 55 56 size_t transfer_size_bytes_; 57 Function<void(Status)> on_completion_; 58 }; 59 60 } // namespace pw::transfer::internal 61