• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 // clang-format off
16 #include "pw_rpc/internal/log_config.h"  // PW_LOG_* macros must be first.
17 
18 #include "pw_rpc/internal/fake_channel_output.h"
19 // clang-format on
20 
21 #include "pw_assert/check.h"
22 #include "pw_log/log.h"
23 #include "pw_result/result.h"
24 #include "pw_rpc/internal/packet.h"
25 
26 namespace pw::rpc::internal::test {
27 
clear()28 void FakeChannelOutput::clear() {
29   LockGuard lock(mutex_);
30   payloads_.clear();
31   packets_.clear();
32   send_status_ = OkStatus();
33   return_after_packet_count_ = -1;
34 }
35 
HandlePacket(std::span<const std::byte> buffer)36 Status FakeChannelOutput::HandlePacket(std::span<const std::byte> buffer) {
37   // If the buffer is empty, this is just releasing an unused buffer.
38   if (buffer.empty()) {
39     return OkStatus();
40   }
41 
42   if (return_after_packet_count_ == 0) {
43     return send_status_;
44   }
45   if (return_after_packet_count_ > 0 &&
46       return_after_packet_count_ == static_cast<int>(packets_.size())) {
47     // Disable behavior.
48     return_after_packet_count_ = -1;
49     return send_status_;
50   }
51 
52   Result<Packet> result = Packet::FromBuffer(buffer);
53   PW_CHECK_OK(result.status());
54 
55   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);
61   Packet& packet = packets_.back();
62 
63   CopyPayloadToBuffer(packet);
64 
65   switch (packet.type()) {
66     case PacketType::REQUEST:
67       return OkStatus();
68     case PacketType::RESPONSE:
69       total_response_packets_ += 1;
70       return OkStatus();
71     case PacketType::CLIENT_STREAM:
72       return OkStatus();
73     case PacketType::DEPRECATED_SERVER_STREAM_END:
74       PW_CRASH("Deprecated PacketType %d", static_cast<int>(packet.type()));
75     case PacketType::CLIENT_ERROR:
76       PW_LOG_WARN("FakeChannelOutput received client error: %s",
77                   packet.status().str());
78       return OkStatus();
79     case PacketType::SERVER_ERROR:
80       PW_LOG_WARN("FakeChannelOutput received server error: %s",
81                   packet.status().str());
82       return OkStatus();
83     case PacketType::DEPRECATED_CANCEL:
84     case PacketType::SERVER_STREAM:
85     case PacketType::CLIENT_STREAM_END:
86       return OkStatus();
87   }
88   PW_CRASH("Unhandled PacketType %d", static_cast<int>(result.value().type()));
89 }
90 
CopyPayloadToBuffer(Packet & packet)91 void FakeChannelOutput::CopyPayloadToBuffer(Packet& packet) {
92   const ConstByteSpan& payload = packet.payload();
93   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());
107   packet.set_payload(std::span(&payloads_[start], payload.size()));
108 }
109 
LogPackets() const110 void FakeChannelOutput::LogPackets() const {
111   LockGuard lock(mutex_);
112 
113   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 
128 }  // namespace pw::rpc::internal::test
129