pw_rpc: Make channel output to Server::ProcessPacket optional
This updates the RPC server to not require a channel output as an
argument to the ProcessPacket function. This output is only used to
create dynamic channels. Dynamic channels are not properly supported
yet, and some projects will never use them. Requiring a channel output
argument needlessly complicates RPC setup for these projects.
Change-Id: I2b05578ffd3aa23c925e515affe3f41683f85aa9
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/71760
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Alexei Frolov <frolv@google.com>
diff --git a/pw_rpc/server.cc b/pw_rpc/server.cc
index 5a45a36..a61d253 100644
--- a/pw_rpc/server.cc
+++ b/pw_rpc/server.cc
@@ -28,15 +28,13 @@
namespace pw::rpc {
namespace {
-using std::byte;
-
using internal::Packet;
using internal::PacketType;
} // namespace
-Status Server::ProcessPacket(std::span<const byte> packet_data,
- ChannelOutput& interface) {
+Status Server::ProcessPacket(ConstByteSpan packet_data,
+ ChannelOutput* interface) {
PW_TRY_ASSIGN(Result<Packet> result,
Endpoint::ProcessPacket(packet_data, Packet::kServer));
Packet& packet = *result;
@@ -55,14 +53,23 @@
internal::Channel* channel = GetInternalChannel(packet.channel_id());
if (channel == nullptr) {
+ if (interface == nullptr) {
+ internal::rpc_lock().unlock();
+ PW_LOG_WARN("RPC server received packet for unknown channel %u",
+ static_cast<unsigned>(packet.channel_id()));
+ PW_LOG_WARN(
+ "No ChannelOutput was provided, so a channel cannot be created");
+ return OkStatus(); // OK since the packet was handled
+ }
+
// If the requested channel doesn't exist, try to dynamically assign one.
- channel = AssignChannel(packet.channel_id(), interface);
+ channel = AssignChannel(packet.channel_id(), *interface);
if (channel == nullptr) {
internal::rpc_lock().unlock();
// If a channel can't be assigned, send a RESOURCE_EXHAUSTED error. Never
// send responses to error messages, though, to avoid infinite cycles.
if (packet.type() != PacketType::CLIENT_ERROR) {
- internal::Channel temp_channel(packet.channel_id(), &interface);
+ internal::Channel temp_channel(packet.channel_id(), interface);
temp_channel
.Send(Packet::ServerError(packet, Status::ResourceExhausted()))
.IgnoreError();