blob: 530a03a6d6447b67205718792b309abce60afb07 [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
Ewout van Bekkum7f5b3052021-11-11 17:35:23 -080015// clang-format off
16#include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first.
17
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070018#include "pw_rpc/internal/fake_channel_output.h"
Ewout van Bekkum7f5b3052021-11-11 17:35:23 -080019// clang-format on
Wyatt Hepler5ba80642021-06-18 12:56:17 -070020
21#include "pw_assert/check.h"
Wyatt Heplerb5aeefc2021-09-23 18:50:12 -070022#include "pw_log/log.h"
Wyatt Hepler5ba80642021-06-18 12:56:17 -070023#include "pw_result/result.h"
24#include "pw_rpc/internal/packet.h"
25
26namespace pw::rpc::internal::test {
27
28void FakeChannelOutput::clear() {
Wyatt Heplere7385e02022-02-28 12:18:19 -080029 LockGuard lock(mutex_);
Wyatt Heplerb5aeefc2021-09-23 18:50:12 -070030 payloads_.clear();
31 packets_.clear();
Carlos Chinchilla54891ae2021-09-22 11:26:43 -070032 send_status_ = OkStatus();
33 return_after_packet_count_ = -1;
Wyatt Hepler5ba80642021-06-18 12:56:17 -070034}
35
Wyatt Hepler9c7e57d2022-02-08 09:50:02 -080036Status FakeChannelOutput::HandlePacket(std::span<const std::byte> buffer) {
Wyatt Hepler59b37f72021-06-15 16:23:44 -070037 // If the buffer is empty, this is just releasing an unused buffer.
Wyatt Hepler5ba80642021-06-18 12:56:17 -070038 if (buffer.empty()) {
39 return OkStatus();
40 }
41
Carlos Chinchilla54891ae2021-09-22 11:26:43 -070042 if (return_after_packet_count_ == 0) {
43 return send_status_;
44 }
45 if (return_after_packet_count_ > 0 &&
Wyatt Hepler9c7e57d2022-02-08 09:50:02 -080046 return_after_packet_count_ == static_cast<int>(packets_.size())) {
Carlos Chinchilla54891ae2021-09-22 11:26:43 -070047 // Disable behavior.
48 return_after_packet_count_ = -1;
49 return send_status_;
50 }
Wyatt Hepler59b37f72021-06-15 16:23:44 -070051
Wyatt Hepler5ba80642021-06-18 12:56:17 -070052 Result<Packet> result = Packet::FromBuffer(buffer);
53 PW_CHECK_OK(result.status());
54
Wyatt Heplerb5aeefc2021-09-23 18:50:12 -070055 PW_CHECK(!packets_.full(),
56 "Attempted to store more than %u packets. Increase the kMaxPackets "
57 "template arg to store more packets.",
58 static_cast<unsigned>(packets_.size()));
59
60 packets_.push_back(*result);
Wyatt Heplerb1c47992022-01-06 11:05:50 -080061 Packet& packet = packets_.back();
Wyatt Heplerb5aeefc2021-09-23 18:50:12 -070062
Wyatt Heplerb1c47992022-01-06 11:05:50 -080063 CopyPayloadToBuffer(packet);
Wyatt Hepler5ba80642021-06-18 12:56:17 -070064
Wyatt Heplerb1c47992022-01-06 11:05:50 -080065 switch (packet.type()) {
Wyatt Hepler82db4b12021-09-23 09:10:12 -070066 case PacketType::REQUEST:
67 return OkStatus();
Wyatt Hepler5ba80642021-06-18 12:56:17 -070068 case PacketType::RESPONSE:
Wyatt Hepler82db4b12021-09-23 09:10:12 -070069 total_response_packets_ += 1;
70 return OkStatus();
71 case PacketType::CLIENT_STREAM:
72 return OkStatus();
73 case PacketType::DEPRECATED_SERVER_STREAM_END:
Wyatt Heplerb1c47992022-01-06 11:05:50 -080074 PW_CRASH("Deprecated PacketType %d", static_cast<int>(packet.type()));
Wyatt Hepler82db4b12021-09-23 09:10:12 -070075 case PacketType::CLIENT_ERROR:
76 PW_LOG_WARN("FakeChannelOutput received client error: %s",
Wyatt Heplerb1c47992022-01-06 11:05:50 -080077 packet.status().str());
Wyatt Hepler82db4b12021-09-23 09:10:12 -070078 return OkStatus();
79 case PacketType::SERVER_ERROR:
80 PW_LOG_WARN("FakeChannelOutput received server error: %s",
Wyatt Heplerb1c47992022-01-06 11:05:50 -080081 packet.status().str());
Wyatt Hepler82db4b12021-09-23 09:10:12 -070082 return OkStatus();
Wyatt Hepler17169152021-10-20 18:46:08 -070083 case PacketType::DEPRECATED_CANCEL:
Wyatt Hepler82db4b12021-09-23 09:10:12 -070084 case PacketType::SERVER_STREAM:
85 case PacketType::CLIENT_STREAM_END:
86 return OkStatus();
Wyatt Hepler5ba80642021-06-18 12:56:17 -070087 }
Wyatt Hepler82db4b12021-09-23 09:10:12 -070088 PW_CRASH("Unhandled PacketType %d", static_cast<int>(result.value().type()));
Wyatt Hepler5ba80642021-06-18 12:56:17 -070089}
90
Wyatt Heplerb1c47992022-01-06 11:05:50 -080091void FakeChannelOutput::CopyPayloadToBuffer(Packet& packet) {
92 const ConstByteSpan& payload = packet.payload();
Wyatt Heplerb5aeefc2021-09-23 18:50:12 -070093 if (payload.empty()) {
94 return;
95 }
96
97 const size_t available_bytes = payloads_.max_size() - payloads_.size();
98 PW_CHECK_UINT_GE(available_bytes,
99 payload.size(),
100 "Ran out of payload buffer space. Increase "
101 "kPayloadBufferSizeBytes (%u) or use smaller payloads.",
102 static_cast<unsigned>(payloads_.max_size()));
103
104 const size_t start = payloads_.size();
105 payloads_.resize(payloads_.size() + payload.size());
106 std::memcpy(&payloads_[start], payload.data(), payload.size());
Wyatt Heplerb1c47992022-01-06 11:05:50 -0800107 packet.set_payload(std::span(&payloads_[start], payload.size()));
Wyatt Heplerb5aeefc2021-09-23 18:50:12 -0700108}
109
Wyatt Heplera2154152021-10-21 08:09:30 -0700110void FakeChannelOutput::LogPackets() const {
Wyatt Heplere7385e02022-02-28 12:18:19 -0800111 LockGuard lock(mutex_);
Wyatt Hepler9c7e57d2022-02-08 09:50:02 -0800112
Wyatt Heplera2154152021-10-21 08:09:30 -0700113 PW_LOG_INFO("%u packets have been sent through this FakeChannelOutput",
114 static_cast<unsigned>(packets_.size()));
115
116 for (unsigned i = 0; i < packets_.size(); ++i) {
117 PW_LOG_INFO("[packet %u/%u]", i + 1, unsigned(packets_.size()));
118 PW_LOG_INFO(" type: %u", unsigned(packets_[i].type()));
119 PW_LOG_INFO(" channel_id: %u", unsigned(packets_[i].channel_id()));
120 PW_LOG_INFO(" service_id: %08x", unsigned(packets_[i].service_id()));
121 PW_LOG_INFO(" method_id: %08x", unsigned(packets_[i].method_id()));
122 PW_LOG_INFO(" call_id: %u", unsigned(packets_[i].call_id()));
123 PW_LOG_INFO(" status: %s", packets_[i].status().str());
124 PW_LOG_INFO(" payload: %u B", unsigned(packets_[i].payload().size()));
125 }
126}
127
Wyatt Hepler5ba80642021-06-18 12:56:17 -0700128} // namespace pw::rpc::internal::test