• 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   std::lock_guard lock(mutex_);
30   payloads_.clear();
31   packets_.clear();
32   send_status_ = OkStatus();
33   return_after_packet_count_ = -1;
34 }
35 
HandlePacket(span<const std::byte> buffer)36 Status FakeChannelOutput::HandlePacket(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 pwpb::PacketType::REQUEST:
67       return OkStatus();
68     case pwpb::PacketType::RESPONSE:
69       total_response_packets_ += 1;
70       return OkStatus();
71     case pwpb::PacketType::CLIENT_STREAM:
72       return OkStatus();
73     case pwpb::PacketType::CLIENT_ERROR:
74       PW_LOG_WARN("FakeChannelOutput received client error: %s",
75                   packet.status().str());
76       return OkStatus();
77     case pwpb::PacketType::SERVER_ERROR:
78       PW_LOG_WARN("FakeChannelOutput received server error: %s",
79                   packet.status().str());
80       return OkStatus();
81     case pwpb::PacketType::SERVER_STREAM:
82     case pwpb::PacketType::CLIENT_STREAM_END:
83       return OkStatus();
84   }
85   PW_CRASH("Unhandled PacketType %d", static_cast<int>(result.value().type()));
86 }
87 
CopyPayloadToBuffer(Packet & packet)88 void FakeChannelOutput::CopyPayloadToBuffer(Packet& packet) {
89   const ConstByteSpan& payload = packet.payload();
90   if (payload.empty()) {
91     return;
92   }
93 
94   const size_t available_bytes = payloads_.max_size() - payloads_.size();
95   PW_CHECK_UINT_GE(available_bytes,
96                    payload.size(),
97                    "Ran out of payload buffer space. Increase "
98                    "kPayloadBufferSizeBytes (%u) or use smaller payloads.",
99                    static_cast<unsigned>(payloads_.max_size()));
100 
101   const size_t start = payloads_.size();
102   payloads_.resize(payloads_.size() + payload.size());
103   std::memcpy(&payloads_[start], payload.data(), payload.size());
104   packet.set_payload(span(&payloads_[start], payload.size()));
105 }
106 
LogPackets() const107 void FakeChannelOutput::LogPackets() const {
108   std::lock_guard lock(mutex_);
109 
110   PW_LOG_INFO("%u packets have been sent through this FakeChannelOutput",
111               static_cast<unsigned>(packets_.size()));
112 
113   for (unsigned i = 0; i < packets_.size(); ++i) {
114     PW_LOG_INFO("[packet %u/%u]", i + 1, unsigned(packets_.size()));
115     PW_LOG_INFO("        type: %u", unsigned(packets_[i].type()));
116     PW_LOG_INFO("  channel_id: %u", unsigned(packets_[i].channel_id()));
117     PW_LOG_INFO("  service_id: %08x", unsigned(packets_[i].service_id()));
118     PW_LOG_INFO("   method_id: %08x", unsigned(packets_[i].method_id()));
119     PW_LOG_INFO("     call_id: %u", unsigned(packets_[i].call_id()));
120     PW_LOG_INFO("      status: %s", packets_[i].status().str());
121     PW_LOG_INFO("     payload: %u B", unsigned(packets_[i].payload().size()));
122   }
123 }
124 
125 }  // namespace pw::rpc::internal::test
126