1 // Copyright 2020 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/internal/channel.h" 16 17 #include "pw_log/log.h" 18 #include "pw_rpc/internal/packet.h" 19 20 namespace pw::rpc::internal { 21 22 using std::byte; 23 payload(const Packet & packet) const24std::span<byte> Channel::OutputBuffer::payload(const Packet& packet) const { 25 const size_t reserved_size = packet.MinEncodedSizeBytes(); 26 return reserved_size <= buffer_.size() ? buffer_.subspan(reserved_size) 27 : std::span<byte>(); 28 } 29 Send(OutputBuffer & buffer,const internal::Packet & packet)30Status Channel::Send(OutputBuffer& buffer, const internal::Packet& packet) { 31 Result encoded = packet.Encode(buffer.buffer_); 32 33 if (!encoded.ok()) { 34 PW_LOG_ERROR("Failed to encode RPC response packet to channel %u buffer", 35 static_cast<unsigned>(id())); 36 output().DiscardBuffer(buffer.buffer_); 37 buffer.buffer_ = {}; 38 return Status::Internal(); 39 } 40 41 buffer.buffer_ = {}; 42 return output().SendAndReleaseBuffer(encoded.value()); 43 } 44 45 } // namespace pw::rpc::internal 46