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