20#include "pw_assert/assert.h"
21#include "pw_bytes/span.h"
22#include "pw_result/result.h"
23#include "pw_rpc/internal/config.h"
24#include "pw_rpc/internal/lock.h"
25#include "pw_rpc/internal/packet.h"
26#include "pw_span/span.h"
27#include "pw_status/status.h"
33template <
typename,
typename, u
int32_t>
40Status OverwriteChannelId(ByteSpan rpc_packet, uint32_t channel_id_under_128);
74template <u
int32_t kNewChannelId>
76 static_assert(kNewChannelId < 128u,
77 "Channel IDs must be less than 128 to avoid needing to "
78 "re-encode the packet");
79 return internal::OverwriteChannelId(rpc_packet, kNewChannelId);
86 uint32_t new_channel_id) {
87 PW_ASSERT(new_channel_id < 128);
88 return internal::OverwriteChannelId(rpc_packet, new_channel_id);
97constexpr size_t MaxSafePayloadSize(
98 size_t encode_buffer_size = cfg::kEncodingBufferSizeBytes) {
99 return encode_buffer_size > internal::Packet::kMinEncodedSizeWithoutPayload
100 ? encode_buffer_size -
101 internal::Packet::kMinEncodedSizeWithoutPayload
109 static constexpr size_t kUnlimited = std::numeric_limits<size_t>::max();
117 constexpr const char* name()
const {
return name_; }
122 virtual size_t MaximumTransmissionUnit() {
return kUnlimited; }
143 virtual Status Send(span<const std::byte> buffer)
144 PW_EXCLUSIVE_LOCKS_REQUIRED(internal::rpc_lock()) = 0;
156 static constexpr uint32_t kUnassignedChannelId = 0;
166 template <
typename UnusedType =
void>
167 constexpr void Configure(uint32_t
id,
ChannelOutput& output) {
169 !cfg::kDynamicAllocationEnabled<UnusedType>,
170 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
171 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
172 PW_ASSERT(id_ == kUnassignedChannelId);
173 PW_ASSERT(
id != kUnassignedChannelId);
179 template <
typename T,
180 typename = std::enable_if_t<std::is_enum_v<T>>,
181 typename U = std::underlying_type_t<T>>
184 !cfg::kDynamicAllocationEnabled<T>,
185 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is enabled. "
186 "Call CloseChannel/OpenChannel on the endpoint instead.");
187 static_assert(
sizeof(U) <=
sizeof(uint32_t));
188 const U kIntId =
static_cast<U
>(id);
189 PW_ASSERT(kIntId > 0);
190 return Configure<T>(
static_cast<uint32_t
>(kIntId), output);
196 template <
typename UnusedType =
void>
199 !cfg::kDynamicAllocationEnabled<UnusedType>,
200 "set_channel_output() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
201 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
202 PW_ASSERT(id_ != kUnassignedChannelId);
206 constexpr uint32_t id()
const {
return id_; }
207 constexpr bool assigned()
const {
return id_ != kUnassignedChannelId; }
215 Status Send(
const Packet& packet) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
217 constexpr void Close() {
218 PW_ASSERT(id_ != kUnassignedChannelId);
219 id_ = kUnassignedChannelId;
225 : id_(
id), output_(output) {}
240 template <u
int32_t kId>
242 static_assert(kId != kUnassignedChannelId,
"Channel ID cannot be 0");
248 typename T =
decltype(kId),
249 typename = std::enable_if_t<std::is_enum_v<T>>,
250 typename U = std::underlying_type_t<T>>
252 constexpr U kIntId =
static_cast<U
>(kId);
253 static_assert(kIntId >= 0,
"Channel ID cannot be negative");
254 static_assert(kIntId <= std::numeric_limits<uint32_t>::max(),
255 "Channel ID must fit in a uint32");
256 return Create<static_cast<uint32_t>(kIntId)>(output);
263 template <
typename,
typename, u
int32_t>
265 friend class internal::ChannelList;
269 PW_ASSERT(
id != kUnassignedChannelId);
274 using internal::ChannelBase::Close;
275 using internal::ChannelBase::Send;
Definition: channel.h:235
Definition: channel.h:105
Definition: channel.h:154
Status ChangeEncodedChannelId(ByteSpan rpc_packet)
Definition: channel.h:75
Result< uint32_t > ExtractChannelId(ConstByteSpan packet)