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);
73template <u
int32_t kNewChannelId>
75 static_assert(kNewChannelId < 128u,
76 "Channel IDs must be less than 128 to avoid needing to "
77 "re-encode the packet");
78 return internal::OverwriteChannelId(rpc_packet, kNewChannelId);
85 uint32_t new_channel_id) {
86 PW_ASSERT(new_channel_id < 128);
87 return internal::OverwriteChannelId(rpc_packet, new_channel_id);
103 size_t encode_buffer_size = cfg::kEncodingBufferSizeBytes) {
104 return encode_buffer_size > internal::Packet::kMinEncodedSizeWithoutPayload
105 ? encode_buffer_size -
106 internal::Packet::kMinEncodedSizeWithoutPayload
114 static constexpr size_t kUnlimited = std::numeric_limits<size_t>::max();
122 constexpr const char* name()
const {
return name_; }
127 virtual size_t MaximumTransmissionUnit() {
return kUnlimited; }
163 static constexpr uint32_t kUnassignedChannelId = 0;
173 template <
typename UnusedType =
void>
174 constexpr void Configure(uint32_t
id,
ChannelOutput& output) {
176 !cfg::kDynamicAllocationEnabled<UnusedType>,
177 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
178 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
179 PW_ASSERT(id_ == kUnassignedChannelId);
180 PW_ASSERT(
id != kUnassignedChannelId);
186 template <
typename T,
187 typename = std::enable_if_t<std::is_enum_v<T>>,
188 typename U = std::underlying_type_t<T>>
191 !cfg::kDynamicAllocationEnabled<T>,
192 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is enabled. "
193 "Call CloseChannel/OpenChannel on the endpoint instead.");
194 static_assert(
sizeof(U) <=
sizeof(uint32_t));
195 const U kIntId =
static_cast<U
>(id);
196 PW_ASSERT(kIntId > 0);
197 return Configure<T>(
static_cast<uint32_t
>(kIntId), output);
203 template <
typename UnusedType =
void>
206 !cfg::kDynamicAllocationEnabled<UnusedType>,
207 "set_channel_output() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
208 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
209 PW_ASSERT(id_ != kUnassignedChannelId);
213 constexpr uint32_t id()
const {
return id_; }
214 constexpr bool assigned()
const {
return id_ != kUnassignedChannelId; }
224 constexpr void Close() {
225 PW_ASSERT(id_ != kUnassignedChannelId);
226 id_ = kUnassignedChannelId;
232 size_t MaxWriteSizeBytes()
const;
236 : id_(
id), output_(output) {}
251 template <u
int32_t kId>
253 static_assert(kId != kUnassignedChannelId,
"Channel ID cannot be 0");
259 typename T =
decltype(kId),
260 typename = std::enable_if_t<std::is_enum_v<T>>,
261 typename U = std::underlying_type_t<T>>
263 constexpr U kIntId =
static_cast<U
>(kId);
264 static_assert(kIntId >= 0,
"Channel ID cannot be negative");
265 static_assert(kIntId <= std::numeric_limits<uint32_t>::max(),
266 "Channel ID must fit in a uint32");
267 return Create<static_cast<uint32_t>(kIntId)>(output);
274 template <
typename,
typename, u
int32_t>
276 friend class internal::ChannelList;
280 PW_ASSERT(
id != kUnassignedChannelId);
285 using internal::ChannelBase::Close;
286 using internal::ChannelBase::MaxWriteSizeBytes;
287 using internal::ChannelBase::Send;
Definition: channel.h:246
Definition: channel.h:110
Definition: channel.h:161
constexpr size_t MaxSafePayloadSize(size_t encode_buffer_size=cfg::kEncodingBufferSizeBytes)
Definition: channel.h:102
Status ChangeEncodedChannelId(ByteSpan rpc_packet)
Definition: channel.h:74
Result< uint32_t > ExtractChannelId(ConstByteSpan packet)
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146