blob: e68a879f03b48cbb91d76bce4be133b8b1857c79 [file] [log] [blame]
Wyatt Hepler5ba80642021-06-18 12:56:17 -07001// 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
15#include "pw_rpc_private/fake_channel_output.h"
16
17#include "pw_assert/check.h"
18#include "pw_result/result.h"
19#include "pw_rpc/internal/packet.h"
20
21namespace pw::rpc::internal::test {
22
23void FakeChannelOutput::clear() {
24 ClearResponses();
25 total_responses_ = 0;
26 last_status_ = Status::Unknown();
27 done_ = false;
28}
29
30Status FakeChannelOutput::SendAndReleaseBuffer(
31 std::span<const std::byte> buffer) {
32 PW_CHECK(!done_);
33 PW_CHECK_PTR_EQ(buffer.data(), packet_buffer_.data());
34
35 if (buffer.empty()) {
36 return OkStatus();
37 }
38
39 Result<Packet> result = Packet::FromBuffer(buffer);
40 PW_CHECK_OK(result.status());
41
42 last_status_ = result.value().status();
43
44 switch (result.value().type()) {
45 case PacketType::RESPONSE:
46 // Server streaming RPCs don't have a payload in their response packet.
47 if (!server_streaming_) {
48 ProcessResponse(result.value().payload());
49 }
50 done_ = true;
51 break;
52 case PacketType::SERVER_STREAM:
53 ProcessResponse(result.value().payload());
54 break;
55 default:
56 PW_CRASH("Unhandled PacketType %d",
57 static_cast<int>(result.value().type()));
58 }
59 return OkStatus();
60}
61
62} // namespace pw::rpc::internal::test