blob: ea74168ccf64f3241122801acf6d94b3770b3f32 [file] [log] [blame]
Alexei Frolov5d6d3922020-05-08 13:57:02 -07001// 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 Bekkum7f5b3052021-11-11 17:35:23 -080015// clang-format off
16#include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first.
Carlos Chinchillaf2b62272021-11-11 09:28:31 -080017
Wyatt Hepler671946e2020-06-09 14:39:33 -070018#include "pw_rpc/internal/channel.h"
Ewout van Bekkum7f5b3052021-11-11 17:35:23 -080019// clang-format on
Alexei Frolov5d6d3922020-05-08 13:57:02 -070020
Wyatt Heplerd4210762022-04-01 15:12:27 -070021#include "pw_bytes/span.h"
Wyatt Hepler671946e2020-06-09 14:39:33 -070022#include "pw_log/log.h"
Wyatt Heplerd4210762022-04-01 15:12:27 -070023#include "pw_protobuf/decoder.h"
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080024#include "pw_rpc/internal/config.h"
Wyatt Hepler671946e2020-06-09 14:39:33 -070025
Wyatt Heplerd4210762022-04-01 15:12:27 -070026namespace pw::rpc {
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080027namespace {
Wyatt Hepler671946e2020-06-09 14:39:33 -070028
Wyatt Hepler3d57eaa2022-02-01 18:31:07 -080029// TODO(pwbug/615): Dynamically allocate this buffer if
30// PW_RPC_DYNAMIC_ALLOCATION is enabled.
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080031std::array<std::byte, cfg::kEncodingBufferSizeBytes> encoding_buffer
Wyatt Heplerd4210762022-04-01 15:12:27 -070032 PW_GUARDED_BY(internal::rpc_lock());
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080033
Wyatt Hepler3d57eaa2022-02-01 18:31:07 -080034} // namespace
Wyatt Hepler671946e2020-06-09 14:39:33 -070035
Wyatt Heplerd4210762022-04-01 15:12:27 -070036Result<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
55namespace internal {
56
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080057ByteSpan GetPayloadBuffer() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
58 return ByteSpan(encoding_buffer)
59 .subspan(Packet::kMinEncodedSizeWithoutPayload);
60}
61
62Status Channel::Send(const Packet& packet) {
63 Result encoded = packet.Encode(encoding_buffer);
Wyatt Hepler671946e2020-06-09 14:39:33 -070064
65 if (!encoded.ok()) {
Wyatt Hepler82db4b12021-09-23 09:10:12 -070066 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 Heplerd78f7c62020-09-28 14:27:32 -070071 return Status::Internal();
Wyatt Hepler671946e2020-06-09 14:39:33 -070072 }
73
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080074 Status sent = output().Send(encoded.value());
Wyatt Hepler82db4b12021-09-23 09:10:12 -070075
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080076 if (!sent.ok()) {
Wyatt Hepler82db4b12021-09-23 09:10:12 -070077 PW_LOG_DEBUG("Channel %u failed to send packet with status %u",
78 static_cast<unsigned>(id()),
Wyatt Hepleraf16dbb2022-01-27 20:15:15 -080079 sent.code());
Wyatt Hepler82db4b12021-09-23 09:10:12 -070080
Wyatt Hepler592b5a12022-02-01 17:55:21 -080081 return Status::Unknown();
Wyatt Hepler82db4b12021-09-23 09:10:12 -070082 }
Wyatt Hepler592b5a12022-02-01 17:55:21 -080083 return OkStatus();
Wyatt Hepler671946e2020-06-09 14:39:33 -070084}
85
Wyatt Heplerd4210762022-04-01 15:12:27 -070086} // namespace internal
87} // namespace pw::rpc