blob: 0fc56dcf2655e8de830ac34659035abcc28d93f3 [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
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070015#include "pw_rpc/internal/fake_channel_output.h"
Wyatt Hepler5ba80642021-06-18 12:56:17 -070016
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) {
Wyatt Hepler5ba80642021-06-18 12:56:17 -070032 PW_CHECK_PTR_EQ(buffer.data(), packet_buffer_.data());
33
Wyatt Hepler59b37f72021-06-15 16:23:44 -070034 // If the buffer is empty, this is just releasing an unused buffer.
Wyatt Hepler5ba80642021-06-18 12:56:17 -070035 if (buffer.empty()) {
36 return OkStatus();
37 }
38
Wyatt Hepler59b37f72021-06-15 16:23:44 -070039 PW_CHECK(!done_);
40
Wyatt Hepler5ba80642021-06-18 12:56:17 -070041 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 Heplerfa6edcc2021-08-20 08:30:08 -070049 if (!HasServerStream(method_type_)) {
Wyatt Hepler5ba80642021-06-18 12:56:17 -070050 ProcessResponse(result.value().payload());
51 }
52 done_ = true;
53 break;
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070054 case PacketType::SERVER_ERROR:
55 PW_CRASH("Server error: %s", result.value().status().str());
Wyatt Hepler5ba80642021-06-18 12:56:17 -070056 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