Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 1 | // Copyright 2021 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 | |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 15 | #include "pw_rpc/internal/fake_channel_output.h" |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 16 | |
| 17 | #include "pw_assert/check.h" |
| 18 | #include "pw_result/result.h" |
| 19 | #include "pw_rpc/internal/packet.h" |
| 20 | |
| 21 | namespace pw::rpc::internal::test { |
| 22 | |
| 23 | void FakeChannelOutput::clear() { |
| 24 | ClearResponses(); |
| 25 | total_responses_ = 0; |
| 26 | last_status_ = Status::Unknown(); |
| 27 | done_ = false; |
| 28 | } |
| 29 | |
| 30 | Status FakeChannelOutput::SendAndReleaseBuffer( |
| 31 | std::span<const std::byte> buffer) { |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 32 | PW_CHECK_PTR_EQ(buffer.data(), packet_buffer_.data()); |
| 33 | |
Wyatt Hepler | 59b37f7 | 2021-06-15 16:23:44 -0700 | [diff] [blame] | 34 | // If the buffer is empty, this is just releasing an unused buffer. |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 35 | if (buffer.empty()) { |
| 36 | return OkStatus(); |
| 37 | } |
| 38 | |
Wyatt Hepler | 59b37f7 | 2021-06-15 16:23:44 -0700 | [diff] [blame] | 39 | PW_CHECK(!done_); |
| 40 | |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 41 | Result<Packet> result = Packet::FromBuffer(buffer); |
| 42 | PW_CHECK_OK(result.status()); |
| 43 | |
| 44 | last_status_ = result.value().status(); |
| 45 | |
| 46 | switch (result.value().type()) { |
| 47 | case PacketType::RESPONSE: |
| 48 | // Server streaming RPCs don't have a payload in their response packet. |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 49 | if (!HasServerStream(method_type_)) { |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 50 | ProcessResponse(result.value().payload()); |
| 51 | } |
| 52 | done_ = true; |
| 53 | break; |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 54 | case PacketType::SERVER_ERROR: |
| 55 | PW_CRASH("Server error: %s", result.value().status().str()); |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 56 | case PacketType::SERVER_STREAM: |
| 57 | ProcessResponse(result.value().payload()); |
| 58 | break; |
| 59 | default: |
| 60 | PW_CRASH("Unhandled PacketType %d", |
| 61 | static_cast<int>(result.value().type())); |
| 62 | } |
| 63 | return OkStatus(); |
| 64 | } |
| 65 | |
| 66 | } // namespace pw::rpc::internal::test |