xref: /aosp_15_r20/external/pigweed/pw_rpc/channel.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 // clang-format off
16 #include "pw_rpc/internal/log_config.h"  // PW_LOG_* macros must be first.
17 
18 #include "pw_rpc/channel.h"
19 // clang-format on
20 
21 #include "pw_assert/check.h"
22 #include "pw_bytes/span.h"
23 #include "pw_log/log.h"
24 #include "pw_protobuf/decoder.h"
25 #include "pw_protobuf/find.h"
26 #include "pw_rpc/internal/config.h"
27 #include "pw_rpc/internal/encoding_buffer.h"
28 #include "pw_rpc/internal/packet.pwpb.h"
29 
30 using pw::rpc::internal::pwpb::RpcPacket::Fields;
31 
32 namespace pw::rpc {
33 namespace internal {
34 
OverwriteChannelId(ByteSpan rpc_packet,uint32_t channel_id_under_128)35 Status OverwriteChannelId(ByteSpan rpc_packet, uint32_t channel_id_under_128) {
36   Result<ConstByteSpan> raw_field =
37       protobuf::FindRaw(rpc_packet, Fields::kChannelId);
38   if (!raw_field.ok()) {
39     return Status::DataLoss();  // Unexpected packet format
40   }
41   if (raw_field->size() != 1u) {
42     return Status::OutOfRange();
43   }
44   const_cast<std::byte*>(raw_field->data())[0] =
45       static_cast<std::byte>(channel_id_under_128);
46   return OkStatus();
47 }
48 
Send(const Packet & packet)49 Status ChannelBase::Send(const Packet& packet) {
50   static constexpr bool kLogAllOutgoingPackets = false;
51   if constexpr (kLogAllOutgoingPackets) {
52     PW_LOG_INFO("pw_rpc channel sending RPC packet type %u for %u:%08x/%08x",
53                 static_cast<unsigned>(packet.type()),
54                 static_cast<unsigned>(packet.channel_id()),
55                 static_cast<unsigned>(packet.service_id()),
56                 static_cast<unsigned>(packet.method_id()));
57   }
58 
59   ByteSpan buffer = encoding_buffer.GetPacketBuffer(packet.payload().size());
60   Result encoded = packet.Encode(buffer);
61 
62   if (!encoded.ok()) {
63     encoding_buffer.Release();
64     PW_LOG_ERROR(
65         "Failed to encode RPC packet type %u to channel %u buffer, status %u",
66         static_cast<unsigned>(packet.type()),
67         static_cast<unsigned>(id()),
68         encoded.status().code());
69     return Status::Internal();
70   }
71 
72   PW_CHECK_NOTNULL(output_);
73   Status sent = output_->Send(encoded.value());
74   encoding_buffer.Release();
75 
76   if (!sent.ok()) {
77     PW_LOG_DEBUG("Channel %u failed to send packet with status %u",
78                  static_cast<unsigned>(id()),
79                  sent.code());
80 
81     return Status::Unknown();
82   }
83   return OkStatus();
84 }
85 
86 }  // namespace internal
87 
ExtractChannelId(ConstByteSpan packet)88 Result<uint32_t> ExtractChannelId(ConstByteSpan packet) {
89   protobuf::Decoder decoder(packet);
90 
91   while (decoder.Next().ok()) {
92     if (static_cast<Fields>(decoder.FieldNumber()) != Fields::kChannelId) {
93       continue;
94     }
95     uint32_t channel_id;
96     PW_TRY(decoder.ReadUint32(&channel_id));
97     return channel_id;
98   }
99 
100   return Status::DataLoss();
101 }
102 
103 }  // namespace pw::rpc
104