1 // Copyright 2023 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
15 #include "pw_system/transfer_service.h"
16
17 #include "pw_system/file_manager.h"
18
19 namespace pw::system {
20 namespace {
21 // The maximum number of concurrent transfers the thread should support as
22 // either a client or a server. These can be set to 0 (if only using one or
23 // the other).
24 constexpr size_t kMaxConcurrentClientTransfers = 5;
25 constexpr size_t kMaxConcurrentServerTransfers = 3;
26
27 // The maximum payload size that can be transmitted by the system's
28 // transport stack. This would typically be defined within some transport
29 // header.
30 constexpr size_t kMaxTransmissionUnit = 512;
31
32 // The maximum amount of data that should be sent within a single transfer
33 // packet. By necessity, this should be less than the max transmission unit.
34 //
35 // pw_transfer requires some additional per-packet overhead, so the actual
36 // amount of data it sends may be lower than this.
37 constexpr size_t kMaxTransferChunkSizeBytes = 480;
38
39 // In a write transfer, the maximum number of bytes to receive at one time
40 // (potentially across multiple chunks), unless specified otherwise by the
41 // transfer handler's stream::Writer.
42 constexpr size_t kDefaultMaxBytesToReceive = 1024;
43
44 // Buffers for storing and encoding chunks (see documentation above).
45 std::array<std::byte, kMaxTransferChunkSizeBytes> chunk_buffer;
46 std::array<std::byte, kMaxTransmissionUnit> encode_buffer;
47
48 transfer::Thread<kMaxConcurrentClientTransfers, kMaxConcurrentServerTransfers>
49 transfer_thread(chunk_buffer, encode_buffer);
50
51 transfer::TransferService transfer_service(transfer_thread,
52 kDefaultMaxBytesToReceive);
53 } // namespace
54
RegisterTransferService(rpc::Server & rpc_server)55 void RegisterTransferService(rpc::Server& rpc_server) {
56 rpc_server.RegisterService(transfer_service);
57 }
58
InitTransferService()59 void InitTransferService() {
60 // the handlers need to be registered after the transfer thread has started
61 for (auto handler : GetFileManager().GetTransferHandlers()) {
62 transfer_service.RegisterHandler(*handler);
63 }
64 }
65
GetTransferThread()66 transfer::TransferThread& GetTransferThread() { return transfer_thread; }
67
68 } // namespace pw::system
69