• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/internal/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 
49 }  // namespace internal
50 
ExtractChannelId(ConstByteSpan packet)51 Result<uint32_t> ExtractChannelId(ConstByteSpan packet) {
52   protobuf::Decoder decoder(packet);
53 
54   while (decoder.Next().ok()) {
55     if (static_cast<Fields>(decoder.FieldNumber()) != Fields::kChannelId) {
56       continue;
57     }
58     uint32_t channel_id;
59     PW_TRY(decoder.ReadUint32(&channel_id));
60     return channel_id;
61   }
62 
63   return Status::DataLoss();
64 }
65 
66 namespace internal {
67 
Send(const Packet & packet)68 Status Channel::Send(const Packet& packet) {
69   ByteSpan buffer = encoding_buffer.GetPacketBuffer(packet.payload().size());
70   Result encoded = packet.Encode(buffer);
71 
72   if (!encoded.ok()) {
73     encoding_buffer.Release();
74     PW_LOG_ERROR(
75         "Failed to encode RPC packet type %u to channel %u buffer, status %u",
76         static_cast<unsigned>(packet.type()),
77         static_cast<unsigned>(id()),
78         encoded.status().code());
79     return Status::Internal();
80   }
81 
82   Status sent = output().Send(encoded.value());
83   encoding_buffer.Release();
84 
85   if (!sent.ok()) {
86     PW_LOG_DEBUG("Channel %u failed to send packet with status %u",
87                  static_cast<unsigned>(id()),
88                  sent.code());
89 
90     return Status::Unknown();
91   }
92   return OkStatus();
93 }
94 
95 }  // namespace internal
96 }  // namespace pw::rpc
97