Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 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 | |
Ewout van Bekkum | 7f5b305 | 2021-11-11 17:35:23 -0800 | [diff] [blame] | 15 | // clang-format off |
| 16 | #include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first. |
Carlos Chinchilla | f2b6227 | 2021-11-11 09:28:31 -0800 | [diff] [blame] | 17 | |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 18 | #include "pw_rpc/internal/channel.h" |
Ewout van Bekkum | 7f5b305 | 2021-11-11 17:35:23 -0800 | [diff] [blame] | 19 | // clang-format on |
Alexei Frolov | 5d6d392 | 2020-05-08 13:57:02 -0700 | [diff] [blame] | 20 | |
Wyatt Hepler | d421076 | 2022-04-01 15:12:27 -0700 | [diff] [blame] | 21 | #include "pw_bytes/span.h" |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 22 | #include "pw_log/log.h" |
Wyatt Hepler | d421076 | 2022-04-01 15:12:27 -0700 | [diff] [blame] | 23 | #include "pw_protobuf/decoder.h" |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 24 | #include "pw_rpc/internal/config.h" |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 25 | |
Wyatt Hepler | d421076 | 2022-04-01 15:12:27 -0700 | [diff] [blame] | 26 | namespace pw::rpc { |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 27 | namespace { |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 28 | |
Wyatt Hepler | 3d57eaa | 2022-02-01 18:31:07 -0800 | [diff] [blame] | 29 | // TODO(pwbug/615): Dynamically allocate this buffer if |
| 30 | // PW_RPC_DYNAMIC_ALLOCATION is enabled. |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 31 | std::array<std::byte, cfg::kEncodingBufferSizeBytes> encoding_buffer |
Wyatt Hepler | d421076 | 2022-04-01 15:12:27 -0700 | [diff] [blame] | 32 | PW_GUARDED_BY(internal::rpc_lock()); |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 33 | |
Wyatt Hepler | 3d57eaa | 2022-02-01 18:31:07 -0800 | [diff] [blame] | 34 | } // namespace |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 35 | |
Wyatt Hepler | d421076 | 2022-04-01 15:12:27 -0700 | [diff] [blame] | 36 | Result<uint32_t> ExtractChannelId(ConstByteSpan packet) { |
| 37 | protobuf::Decoder decoder(packet); |
| 38 | |
| 39 | while (decoder.Next().ok()) { |
| 40 | switch (static_cast<internal::RpcPacket::Fields>(decoder.FieldNumber())) { |
| 41 | case internal::RpcPacket::Fields::CHANNEL_ID: { |
| 42 | uint32_t channel_id; |
| 43 | PW_TRY(decoder.ReadUint32(&channel_id)); |
| 44 | return channel_id; |
| 45 | } |
| 46 | |
| 47 | default: |
| 48 | continue; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return Status::DataLoss(); |
| 53 | } |
| 54 | |
| 55 | namespace internal { |
| 56 | |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 57 | ByteSpan GetPayloadBuffer() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) { |
| 58 | return ByteSpan(encoding_buffer) |
| 59 | .subspan(Packet::kMinEncodedSizeWithoutPayload); |
| 60 | } |
| 61 | |
| 62 | Status Channel::Send(const Packet& packet) { |
| 63 | Result encoded = packet.Encode(encoding_buffer); |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 64 | |
| 65 | if (!encoded.ok()) { |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 66 | PW_LOG_ERROR( |
| 67 | "Failed to encode RPC packet type %u to channel %u buffer, status %u", |
| 68 | static_cast<unsigned>(packet.type()), |
| 69 | static_cast<unsigned>(id()), |
| 70 | encoded.status().code()); |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 71 | return Status::Internal(); |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 74 | Status sent = output().Send(encoded.value()); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 75 | |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 76 | if (!sent.ok()) { |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 77 | PW_LOG_DEBUG("Channel %u failed to send packet with status %u", |
| 78 | static_cast<unsigned>(id()), |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 79 | sent.code()); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 80 | |
Wyatt Hepler | 592b5a1 | 2022-02-01 17:55:21 -0800 | [diff] [blame] | 81 | return Status::Unknown(); |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 82 | } |
Wyatt Hepler | 592b5a1 | 2022-02-01 17:55:21 -0800 | [diff] [blame] | 83 | return OkStatus(); |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Wyatt Hepler | d421076 | 2022-04-01 15:12:27 -0700 | [diff] [blame] | 86 | } // namespace internal |
| 87 | } // namespace pw::rpc |