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_assert/assert.h" 17 #include "pw_containers/intrusive_list.h" 18 #include "pw_result/result.h" 19 #include "pw_rpc/raw/server_reader_writer.h" 20 #include "pw_transfer/handler.h" 21 #include "pw_transfer/internal/context.h" 22 23 namespace pw::transfer::internal { 24 25 // Transfer context for use within the transfer service (server-side). Stores a 26 // pointer to a transfer handler when active to stream the transfer data. 27 class ServerContext final : public Context { 28 public: ServerContext()29 constexpr ServerContext() : handler_(nullptr) {} 30 31 // Sets the handler. The handler isn't set by Context::Initialize() since 32 // ClientContexts don't track it. set_handler(Handler & handler)33 void set_handler(Handler& handler) { handler_ = &handler; } 34 35 // Returns the pointer to the current handler. handler()36 const Handler* handler() { return handler_; } 37 38 // In server-side transfer contexts, a session ID always exists. id()39 constexpr uint32_t id() const { return session_id(); } 40 41 private: 42 // Ends the transfer with the given status, calling the handler's Finalize 43 // method. No chunks are sent. 44 // 45 // Returns DATA_LOSS if the finalize call fails. 46 // 47 // Precondition: Transfer context is active. 48 Status FinalCleanup(Status status) override; 49 50 Handler* handler_; 51 }; 52 53 } // namespace pw::transfer::internal 54