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);
59template <u
int32_t kNewChannelId>
61 static_assert(kNewChannelId < 128u,
62 "Channel IDs must be less than 128 to avoid needing to "
63 "re-encode the packet");
64 return internal::OverwriteChannelId(rpc_packet, kNewChannelId);
71 uint32_t new_channel_id) {
72 PW_ASSERT(new_channel_id < 128);
73 return internal::OverwriteChannelId(rpc_packet, new_channel_id);
89 size_t encode_buffer_size = cfg::kEncodingBufferSizeBytes) {
90 return encode_buffer_size > internal::Packet::kMinEncodedSizeWithoutPayload
91 ? encode_buffer_size -
92 internal::Packet::kMinEncodedSizeWithoutPayload
100 static constexpr size_t kUnlimited = std::numeric_limits<size_t>::max();
108 constexpr const char* name()
const {
return name_; }
113 virtual size_t MaximumTransmissionUnit() {
return kUnlimited; }
149 static constexpr uint32_t kUnassignedChannelId = 0;
159 template <
typename UnusedType =
void>
160 constexpr void Configure(uint32_t
id,
ChannelOutput& output) {
162 !cfg::kDynamicAllocationEnabled<UnusedType>,
163 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
164 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
165 PW_ASSERT(id_ == kUnassignedChannelId);
166 PW_ASSERT(
id != kUnassignedChannelId);
172 template <
typename T,
173 typename = std::enable_if_t<std::is_enum_v<T>>,
174 typename U = std::underlying_type_t<T>>
177 !cfg::kDynamicAllocationEnabled<T>,
178 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is enabled. "
179 "Call CloseChannel/OpenChannel on the endpoint instead.");
180 static_assert(
sizeof(U) <=
sizeof(uint32_t));
181 const U kIntId =
static_cast<U
>(id);
182 PW_ASSERT(kIntId > 0);
183 return Configure<T>(
static_cast<uint32_t
>(kIntId), output);
189 template <
typename UnusedType =
void>
192 !cfg::kDynamicAllocationEnabled<UnusedType>,
193 "set_channel_output() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
194 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
195 PW_ASSERT(id_ != kUnassignedChannelId);
199 constexpr uint32_t id()
const {
return id_; }
200 constexpr bool assigned()
const {
return id_ != kUnassignedChannelId; }
210 constexpr void Close() {
211 PW_ASSERT(id_ != kUnassignedChannelId);
212 id_ = kUnassignedChannelId;
218 size_t MaxWriteSizeBytes()
const;
222 : id_(
id), output_(output) {}
237 template <u
int32_t kId>
239 static_assert(kId != kUnassignedChannelId,
"Channel ID cannot be 0");
245 typename T =
decltype(kId),
246 typename = std::enable_if_t<std::is_enum_v<T>>,
247 typename U = std::underlying_type_t<T>>
249 constexpr U kIntId =
static_cast<U
>(kId);
250 static_assert(kIntId >= 0,
"Channel ID cannot be negative");
251 static_assert(kIntId <= std::numeric_limits<uint32_t>::max(),
252 "Channel ID must fit in a uint32");
253 return Create<static_cast<uint32_t>(kIntId)>(output);
260 template <
typename,
typename, u
int32_t>
262 friend class internal::ChannelList;
266 PW_ASSERT(
id != kUnassignedChannelId);
271 using internal::ChannelBase::Close;
272 using internal::ChannelBase::MaxWriteSizeBytes;
273 using internal::ChannelBase::Send;
Definition: channel.h:232
Definition: channel.h:147
constexpr size_t MaxSafePayloadSize(size_t encode_buffer_size=cfg::kEncodingBufferSizeBytes)
Definition: channel.h:88
Status ChangeEncodedChannelId(ByteSpan rpc_packet)
Definition: channel.h:60
Result< uint32_t > ExtractChannelId(ConstByteSpan packet)
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146