pw_rpc: Replace ChannelOutput ID with a name
It is helpful for ChannelOutputs to have a human-readable description
for logging. Replace the ID field, which easily confused with the
channel ID, with a name.
Change-Id: I27a82b303988e718624cb7bbbc2f4bcb5667ac76
diff --git a/pw_rpc/public/pw_rpc/channel.h b/pw_rpc/public/pw_rpc/channel.h
index f0f890f..ec5d5bd 100644
--- a/pw_rpc/public/pw_rpc/channel.h
+++ b/pw_rpc/public/pw_rpc/channel.h
@@ -28,20 +28,22 @@
class ChannelOutput {
public:
- constexpr ChannelOutput(uint32_t id) : id_(id) {}
+ // Creates a channel output with the provided name. The name is used for
+ // logging only.
+ constexpr ChannelOutput(const char* name) : name_(name) {}
virtual ~ChannelOutput() = default;
+ constexpr const char* name() const { return name_; }
+
// Acquire a buffer into which to write an outgoing RPC packet.
virtual span<std::byte> AcquireBuffer() = 0;
// Sends the contents of the buffer from AcquireBuffer().
virtual void SendAndReleaseBuffer(size_t size) = 0;
- uint32_t id() const { return id_; }
-
private:
- uint32_t id_;
+ const char* name_;
};
class Channel {
diff --git a/pw_rpc/pw_rpc_private/test_utils.h b/pw_rpc/pw_rpc_private/test_utils.h
index 764d2c7..321eb3f 100644
--- a/pw_rpc/pw_rpc_private/test_utils.h
+++ b/pw_rpc/pw_rpc_private/test_utils.h
@@ -25,7 +25,8 @@
template <size_t buffer_size>
class TestOutput : public ChannelOutput {
public:
- constexpr TestOutput(uint32_t id) : ChannelOutput(id), sent_packet_{} {}
+ constexpr TestOutput(const char* name = "TestOutput")
+ : ChannelOutput(name), sent_packet_{} {}
span<std::byte> AcquireBuffer() override { return buffer_; }
@@ -56,8 +57,7 @@
static constexpr uint32_t kServiceId = service_id;
ServerContextForTest(const internal::Method& method)
- : output_(32),
- channel_(Channel::Create<kChannelId>(&output_)),
+ : channel_(Channel::Create<kChannelId>(&output_)),
service_(kServiceId),
context_(channel_, service_, method) {}
diff --git a/pw_rpc/server.cc b/pw_rpc/server.cc
index 879cae4..7ad7cf0 100644
--- a/pw_rpc/server.cc
+++ b/pw_rpc/server.cc
@@ -37,8 +37,8 @@
if (packet.channel_id() == Channel::kUnassignedChannelId ||
packet.service_id() == 0 || packet.method_id() == 0) {
// Malformed packet; don't even try to process it.
- PW_LOG_ERROR("Received incomplete RPC packet on interface %u",
- unsigned(interface.id()));
+ PW_LOG_ERROR("Received incomplete RPC packet on interface %s",
+ interface.name());
return;
}
diff --git a/pw_rpc/server_test.cc b/pw_rpc/server_test.cc
index 389c9ca..6b5905d1 100644
--- a/pw_rpc/server_test.cc
+++ b/pw_rpc/server_test.cc
@@ -61,8 +61,7 @@
byte(0x82), byte(0x02), byte(0xff), byte(0xff)};
BasicServer()
- : output_(1),
- channels_{
+ : channels_{
Channel::Create<1>(&output_),
Channel::Create<2>(&output_),
Channel(), // available for assignment
@@ -158,7 +157,7 @@
}
TEST_F(BasicServer, ProcessPacket_UnassignedChannel_AssignsToAvalableSlot) {
- TestOutput<128> unassigned_output(2);
+ TestOutput<128> unassigned_output;
server_.ProcessPacket(
EncodeRequest(PacketType::RPC, /*channel_id=*/99, 42, 27),
unassigned_output);