1 // Copyright 2022 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 "pw_chrono/system_clock.h" 17 #include "pw_rpc/writer.h" 18 #include "pw_stream/stream.h" 19 20 namespace pw::transfer::internal { 21 22 enum class TransferType : bool { kTransmit, kReceive }; 23 24 enum class TransferStream { 25 kClientRead, 26 kClientWrite, 27 kServerRead, 28 kServerWrite, 29 }; 30 31 enum class EventType { 32 // Begins a new transfer in an available context. 33 kNewClientTransfer, 34 kNewServerTransfer, 35 36 // Processes an incoming chunk for a transfer. 37 kClientChunk, 38 kServerChunk, 39 40 // Runs the timeout handler for a transfer. 41 kClientTimeout, 42 kServerTimeout, 43 44 // Sends a status chunk to terminate a transfer. This does not call into the 45 // transfer context's completion handler; it is for out-of-band termination. 46 kSendStatusChunk, 47 48 // Updates one of the transfer thread's RPC streams. 49 kSetTransferStream, 50 51 // Manages the list of transfer handlers for a transfer service. 52 kAddTransferHandler, 53 kRemoveTransferHandler, 54 55 // For testing only: aborts the transfer thread. 56 kTerminate, 57 }; 58 59 // Forward declarations required for events. 60 class Handler; 61 class TransferParameters; 62 class TransferThread; 63 64 struct NewTransferEvent { 65 TransferType type; 66 uint32_t transfer_id; 67 uint32_t handler_id; 68 rpc::Writer* rpc_writer; 69 const TransferParameters* max_parameters; 70 chrono::SystemClock::duration timeout; 71 uint32_t max_retries; 72 TransferThread* transfer_thread; 73 74 union { 75 stream::Stream* stream; // In client-side transfers. 76 Handler* handler; // In server-side transfers. 77 }; 78 }; 79 80 struct ChunkEvent { 81 uint32_t transfer_id; 82 const std::byte* data; 83 size_t size; 84 }; 85 86 struct SendStatusChunkEvent { 87 uint32_t transfer_id; 88 Status::Code status; 89 TransferStream stream; 90 }; 91 92 struct Event { 93 EventType type; 94 95 union { 96 NewTransferEvent new_transfer; 97 ChunkEvent chunk; 98 SendStatusChunkEvent send_status_chunk; 99 TransferStream set_transfer_stream; 100 Handler* add_transfer_handler; 101 Handler* remove_transfer_handler; 102 }; 103 }; 104 105 } // namespace pw::transfer::internal 106