xref: /aosp_15_r20/external/pigweed/pw_transfer/public/pw_transfer/internal/server_context.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_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  private:
39   // Ends the transfer with the given status, calling the handler's Finalize
40   // method. No chunks are sent.
41   //
42   // Returns DATA_LOSS if the finalize call fails.
43   //
44   // Precondition: Transfer context is active.
45   Status FinalCleanup(Status status) override;
46 
TransferSizeBytes()47   size_t TransferSizeBytes() const override { return handler_->ResourceSize(); }
48 
49   // Seeks the reader to the given offset. Does not incorporate any initial
50   // offset
51   Status SeekReader(uint32_t offset) override;
52 
53   Handler* handler_;
54 };
55 
56 }  // namespace pw::transfer::internal
57