20#include "pw_assert/assert.h"
21#include "pw_bytes/span.h"
22#include "pw_result/result.h"
23#include "pw_rpc/internal/lock.h"
24#include "pw_rpc/internal/packet.h"
25#include "pw_span/span.h"
26#include "pw_status/status.h"
32template <
typename,
typename, u
int32_t>
39Status 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);
96constexpr size_t MaxSafePayloadSize(
97 size_t encode_buffer_size = cfg::kEncodingBufferSizeBytes) {
98 return encode_buffer_size > internal::Packet::kMinEncodedSizeWithoutPayload
99 ? encode_buffer_size -
100 internal::Packet::kMinEncodedSizeWithoutPayload
108 static constexpr size_t kUnlimited = std::numeric_limits<size_t>::max();
116 constexpr const char* name()
const {
return name_; }
121 virtual size_t MaximumTransmissionUnit() {
return kUnlimited; }
142 virtual Status Send(span<const std::byte> buffer)
143 PW_EXCLUSIVE_LOCKS_REQUIRED(internal::rpc_lock()) = 0;
155 static constexpr uint32_t kUnassignedChannelId = 0;
165 template <
typename UnusedType =
void>
166 constexpr void Configure(uint32_t
id,
ChannelOutput& output) {
168 !cfg::kDynamicAllocationEnabled<UnusedType>,
169 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
170 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
171 PW_ASSERT(id_ == kUnassignedChannelId);
172 PW_ASSERT(
id != kUnassignedChannelId);
178 template <
typename T,
179 typename = std::enable_if_t<std::is_enum_v<T>>,
180 typename U = std::underlying_type_t<T>>
183 !cfg::kDynamicAllocationEnabled<T>,
184 "Configure() may not be used if PW_RPC_DYNAMIC_ALLOCATION is enabled. "
185 "Call CloseChannel/OpenChannel on the endpoint instead.");
186 static_assert(
sizeof(U) <=
sizeof(uint32_t));
187 const U kIntId =
static_cast<U
>(id);
188 PW_ASSERT(kIntId > 0);
189 return Configure<T>(
static_cast<uint32_t
>(kIntId), output);
195 template <
typename UnusedType =
void>
198 !cfg::kDynamicAllocationEnabled<UnusedType>,
199 "set_channel_output() may not be used if PW_RPC_DYNAMIC_ALLOCATION is "
200 "enabled. Call CloseChannel/OpenChannel on the endpoint instead.");
201 PW_ASSERT(id_ != kUnassignedChannelId);
205 constexpr uint32_t id()
const {
return id_; }
206 constexpr bool assigned()
const {
return id_ != kUnassignedChannelId; }
214 Status Send(
const Packet& packet) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
216 constexpr void Close() {
217 PW_ASSERT(id_ != kUnassignedChannelId);
218 id_ = kUnassignedChannelId;
224 : id_(
id), output_(output) {}
239 template <u
int32_t kId>
241 static_assert(kId != kUnassignedChannelId,
"Channel ID cannot be 0");
247 typename T =
decltype(kId),
248 typename = std::enable_if_t<std::is_enum_v<T>>,
249 typename U = std::underlying_type_t<T>>
251 constexpr U kIntId =
static_cast<U
>(kId);
252 static_assert(kIntId >= 0,
"Channel ID cannot be negative");
253 static_assert(kIntId <= std::numeric_limits<uint32_t>::max(),
254 "Channel ID must fit in a uint32");
255 return Create<static_cast<uint32_t>(kIntId)>(output);
262 template <
typename,
typename, u
int32_t>
264 friend class internal::ChannelList;
268 PW_ASSERT(
id != kUnassignedChannelId);
273 using internal::ChannelBase::Close;
274 using internal::ChannelBase::Send;
Definition: channel.h:234
Definition: channel.h:104
Definition: channel.h:153
Status ChangeEncodedChannelId(ByteSpan rpc_packet)
Definition: channel.h:74
Result< uint32_t > ExtractChannelId(ConstByteSpan packet)