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 #include "pw_transfer/internal/protocol.h" 20 21 namespace pw::transfer { 22 23 class Handler; 24 25 namespace internal { 26 27 enum class TransferType : bool { kTransmit, kReceive }; 28 29 enum class TransferStream { 30 kClientRead, 31 kClientWrite, 32 kServerRead, 33 kServerWrite, 34 }; 35 36 enum class EventType { 37 // Begins a new transfer in an available context. 38 kNewClientTransfer, 39 kNewServerTransfer, 40 41 // Processes an incoming chunk for a transfer. 42 kClientChunk, 43 kServerChunk, 44 45 // Runs the timeout handler for a transfer. 46 kClientTimeout, 47 kServerTimeout, 48 49 // Terminates an ongoing transfer with a specified status, optionally sending 50 // a status chunk to the other end of the transfer. 51 kClientEndTransfer, 52 kServerEndTransfer, 53 54 // Sends a status chunk to terminate a transfer. This does not call into the 55 // transfer context's completion handler; it is for out-of-band termination. 56 kSendStatusChunk, 57 58 // Manages the list of transfer handlers for a transfer service. 59 kAddTransferHandler, 60 kRemoveTransferHandler, 61 62 // For testing only: aborts the transfer thread. 63 kTerminate, 64 }; 65 66 // Forward declarations required for events. 67 class TransferParameters; 68 class TransferThread; 69 70 struct NewTransferEvent { 71 TransferType type; 72 ProtocolVersion protocol_version; 73 uint32_t session_id; 74 uint32_t resource_id; 75 rpc::Writer* rpc_writer; 76 const TransferParameters* max_parameters; 77 chrono::SystemClock::duration timeout; 78 uint32_t max_retries; 79 uint32_t max_lifetime_retries; 80 TransferThread* transfer_thread; 81 82 union { 83 stream::Stream* stream; // In client-side transfers. 84 Handler* handler; // In server-side transfers. 85 }; 86 87 const std::byte* raw_chunk_data; 88 size_t raw_chunk_size; 89 }; 90 91 // A chunk received by a transfer client / server. 92 struct ChunkEvent { 93 // Identifier for the transfer to which the chunk belongs. 94 uint32_t context_identifier; 95 96 // If true, only match the identifier against context resource IDs. 97 bool match_resource_id; 98 99 // The raw data of the chunk. 100 const std::byte* data; 101 size_t size; 102 }; 103 104 struct EndTransferEvent { 105 uint32_t session_id; 106 Status::Code status; 107 bool send_status_chunk; 108 }; 109 110 struct SendStatusChunkEvent { 111 uint32_t session_id; 112 bool set_resource_id; 113 ProtocolVersion protocol_version; 114 Status::Code status; 115 TransferStream stream; 116 }; 117 118 struct Event { 119 EventType type; 120 121 union { 122 NewTransferEvent new_transfer; 123 ChunkEvent chunk; 124 EndTransferEvent end_transfer; 125 SendStatusChunkEvent send_status_chunk; 126 Handler* add_transfer_handler; 127 Handler* remove_transfer_handler; 128 }; 129 }; 130 131 } // namespace internal 132 } // namespace pw::transfer 133