xref: /aosp_15_r20/external/pigweed/pw_transfer/public/pw_transfer/internal/event.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 "pw_bytes/span.h"
17 #include "pw_chrono/system_clock.h"
18 #include "pw_function/function.h"
19 #include "pw_rpc/raw/server_reader_writer.h"
20 #include "pw_rpc/writer.h"
21 #include "pw_stream/stream.h"
22 #include "pw_transfer/internal/protocol.h"
23 
24 namespace pw::transfer {
25 
26 class Handler;
27 
28 namespace internal {
29 
30 enum class TransferType : bool { kTransmit, kReceive };
31 
32 enum class TransferStream {
33   kClientRead,
34   kClientWrite,
35   kServerRead,
36   kServerWrite,
37 };
38 
39 enum class IdentifierType {
40   Session,
41   Resource,
42   Handle,
43 };
44 
45 enum class EventType {
46   // Begins a new transfer in an available context.
47   kNewClientTransfer,
48   kNewServerTransfer,
49 
50   // Processes an incoming chunk for a transfer.
51   kClientChunk,
52   kServerChunk,
53 
54   // Runs the timeout handler for a transfer.
55   kClientTimeout,
56   kServerTimeout,
57 
58   // Terminates an ongoing transfer with a specified status, optionally sending
59   // a status chunk to the other end of the transfer.
60   kClientEndTransfer,
61   kServerEndTransfer,
62 
63   // Sends a status chunk to terminate a transfer. This does not call into the
64   // transfer context's completion handler; it is for out-of-band termination.
65   kSendStatusChunk,
66 
67   // Changes parameters of an ongoing client transfer.
68   kUpdateClientTransfer,
69 
70   // Manages the list of transfer handlers for a transfer service.
71   kAddTransferHandler,
72   kRemoveTransferHandler,
73 
74   // Updates one of the transfer thread's RPC streams.
75   kSetStream,
76 
77   // For testing only: aborts the transfer thread.
78   kTerminate,
79 
80   // Gets the status of a resource, if there is a handler registered for it.
81   kGetResourceStatus,
82 };
83 
84 // Forward declarations required for events.
85 class TransferParameters;
86 class TransferThread;
87 
88 struct NewTransferEvent {
89   TransferType type;
90   ProtocolVersion protocol_version;
91   uint32_t session_id;
92   uint32_t resource_id;
93   uint32_t handle_id;
94   rpc::Writer* rpc_writer;
95   const TransferParameters* max_parameters;
96   chrono::SystemClock::duration timeout;
97   chrono::SystemClock::duration initial_timeout;
98   uint32_t max_retries;
99   uint32_t max_lifetime_retries;
100   TransferThread* transfer_thread;
101 
102   union {
103     stream::Stream* stream;  // In client-side transfers.
104     Handler* handler;        // In server-side transfers.
105   };
106 
107   const std::byte* raw_chunk_data;
108   size_t raw_chunk_size;
109 
110   uint64_t initial_offset;
111 };
112 
113 // A chunk received by a transfer client / server.
114 struct ChunkEvent {
115   // Identifier for the transfer to which the chunk belongs.
116   uint32_t context_identifier;
117 
118   // If true, only match the identifier against context resource IDs.
119   bool match_resource_id;
120 
121   // The raw data of the chunk.
122   const std::byte* data;
123   size_t size;
124 };
125 
126 struct EndTransferEvent {
127   IdentifierType id_type;
128   uint32_t id;
129   Status::Code status;
130   bool send_status_chunk;
131 };
132 
133 struct SendStatusChunkEvent {
134   uint32_t session_id;
135   ProtocolVersion protocol_version;
136   Status::Code status;
137   TransferStream stream;
138 };
139 
140 struct SetStreamEvent {
141   TransferStream stream;
142 };
143 
144 struct UpdateTransferEvent {
145   uint32_t handle_id;
146   uint32_t transfer_size_bytes;
147 };
148 
149 struct ResourceStatus {
150   uint32_t resource_id;
151   uint64_t readable_offset;
152   uint64_t writeable_offset;
153   uint64_t read_checksum;
154   uint64_t write_checksum;
155 };
156 
157 using ResourceStatusCallback = Callback<void(Status, const ResourceStatus&)>;
158 
159 struct GetResourceStatusEvent {
160   uint32_t resource_id;
161 };
162 
163 struct Event {
164   EventType type;
165 
166   union {
167     NewTransferEvent new_transfer;
168     ChunkEvent chunk;
169     EndTransferEvent end_transfer;
170     SendStatusChunkEvent send_status_chunk;
171     UpdateTransferEvent update_transfer;
172     Handler* add_transfer_handler;
173     Handler* remove_transfer_handler;
174     SetStreamEvent set_stream;
175     GetResourceStatusEvent resource_status;
176   };
177 };
178 
179 }  // namespace internal
180 }  // namespace pw::transfer
181