diff options
author | Wyatt Hepler <hepler@google.com> | 2023-02-23 19:20:46 +0000 |
---|---|---|
committer | CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-02-23 19:20:46 +0000 |
commit | adb36282e3b03dec101bf5529fd9bb12fd707068 (patch) | |
tree | 2cda520894af7986fcb3d65efcbfd6e8e86daea5 /pw_rpc/public | |
parent | 8788fb08e2595a4e9c9606997887897f18f45bea (diff) | |
download | pigweed-adb36282e3b03dec101bf5529fd9bb12fd707068.tar.gz |
pw_rpc: Consolidate functions that encode to the payload buffer
Create a EncodeToPayloadBuffer() function template that is used by
both Nanopb and pwpb. Place it in a pw_rpc/internal/encoding_buffer.h
header.
Change-Id: I454ef814743c48a0cf50acf98636a57cfc3afc3d
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/129573
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
Diffstat (limited to 'pw_rpc/public')
-rw-r--r-- | pw_rpc/public/pw_rpc/internal/channel.h | 7 | ||||
-rw-r--r-- | pw_rpc/public/pw_rpc/internal/encoding_buffer.h | 42 |
2 files changed, 42 insertions, 7 deletions
diff --git a/pw_rpc/public/pw_rpc/internal/channel.h b/pw_rpc/public/pw_rpc/internal/channel.h index a74f4bb5d..a3161d05d 100644 --- a/pw_rpc/public/pw_rpc/internal/channel.h +++ b/pw_rpc/public/pw_rpc/internal/channel.h @@ -13,20 +13,13 @@ // the License. #pragma once -#include "pw_assert/assert.h" -#include "pw_bytes/span.h" #include "pw_rpc/channel.h" #include "pw_rpc/internal/lock.h" #include "pw_rpc/internal/packet.h" -#include "pw_span/span.h" #include "pw_status/status.h" namespace pw::rpc::internal { -// Returns a portion of the encoding buffer that may be used to encode an -// outgoing payload. -ByteSpan GetPayloadBuffer() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()); - class Channel : public rpc::Channel { public: Channel() = delete; diff --git a/pw_rpc/public/pw_rpc/internal/encoding_buffer.h b/pw_rpc/public/pw_rpc/internal/encoding_buffer.h new file mode 100644 index 000000000..3c81bbf86 --- /dev/null +++ b/pw_rpc/public/pw_rpc/internal/encoding_buffer.h @@ -0,0 +1,42 @@ +// Copyright 2023 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +// Definitions for the static and dynamic versions of the pw_rpc encoding +// buffer. Both version are compiled rot, but only one is instantiated, +// depending on the PW_RPC_DYNAMIC_ALLOCATION config option. +#pragma once + +#include "pw_bytes/span.h" +#include "pw_rpc/internal/lock.h" +#include "pw_status/status_with_size.h" + +namespace pw::rpc::internal { + +ByteSpan GetPayloadBuffer() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()); + +// Successful calls to EncodeToPayloadBuffer MUST send the returned buffer, +// without releasing the RPC lock. +template <typename Proto, typename Encoder> +static Result<ByteSpan> EncodeToPayloadBuffer(Proto& payload, + const Encoder& encoder) + PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) { + ByteSpan buffer = GetPayloadBuffer(); + StatusWithSize result = encoder.Encode(payload, buffer); + if (!result.ok()) { + return result.status(); + } + return buffer.first(result.size()); +} + +} // namespace pw::rpc::internal |