xref: /aosp_15_r20/external/pigweed/pw_transfer/server_context.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 
15 #define PW_LOG_MODULE_NAME "TRN"
16 #define PW_LOG_LEVEL PW_TRANSFER_CONFIG_LOG_LEVEL
17 
18 #include "pw_transfer/internal/server_context.h"
19 
20 #include "pw_assert/check.h"
21 #include "pw_log/log.h"
22 #include "pw_status/try.h"
23 #include "pw_transfer/internal/chunk.h"
24 #include "pw_transfer/internal/config.h"
25 #include "pw_transfer/transfer.pwpb.h"
26 #include "pw_varint/varint.h"
27 
28 namespace pw::transfer::internal {
29 
FinalCleanup(const Status status)30 Status ServerContext::FinalCleanup(const Status status) {
31   PW_DCHECK(active());
32 
33   // If no handler is set, then the Prepare call failed. Nothing to do.
34   if (handler_ == nullptr) {
35     return OkStatus();
36   }
37 
38   Handler& handler = *handler_;
39   handler_ = nullptr;
40 
41   if (type() == TransferType::kTransmit) {
42     handler.FinalizeRead(status);
43     return OkStatus();
44   }
45 
46   if (Status finalized = handler.FinalizeWrite(status); !finalized.ok()) {
47     PW_LOG_ERROR(
48         "FinalizeWrite() for transfer %u failed with status %u; aborting with "
49         "DATA_LOSS",
50         static_cast<unsigned>(handler.id()),
51         static_cast<int>(finalized.code()));
52     return Status::DataLoss();
53   }
54 
55   return OkStatus();
56 }
57 
SeekReader(uint32_t offset)58 Status ServerContext::SeekReader(uint32_t offset) {
59   return reader().Seek(offset);
60 }
61 
62 }  // namespace pw::transfer::internal
63