Skip to content

Commit

Permalink
pw_metric: Move protos to .proto namespace
Browse files Browse the repository at this point in the history
Change-Id: I08aa92edcfa92d467af2da4ae444c4b861826a19
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/101169
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Medha Kini <medhakini@google.com>
  • Loading branch information
Medha Kini authored and CQ Bot Account committed Jul 8, 2022
1 parent 99ad662 commit ab0e417
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
26 changes: 14 additions & 12 deletions pw_metric/metric_service_nanopb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ namespace {

class MetricWriter {
public:
MetricWriter(
MetricService::ServerWriter<pw_metric_MetricResponse>& response_writer)
: response_(pw_metric_MetricResponse_init_zero),
MetricWriter(MetricService::ServerWriter<pw_metric_proto_MetricResponse>&
response_writer)
: response_(pw_metric_proto_MetricResponse_init_zero),
response_writer_(response_writer) {}

// TODO(keir): Figure out a pw_rpc mechanism to fill a streaming packet based
Expand All @@ -38,11 +38,12 @@ class MetricWriter {
void Write(const Metric& metric, const Vector<Token>& path) {
// Nanopb doesn't offer an easy way to do bounds checking, so use span's
// type deduction magic to figure out the max size.
span<pw_metric_Metric> metrics(response_.metrics);
span<pw_metric_proto_Metric> metrics(response_.metrics);
PW_CHECK_INT_LT(response_.metrics_count, metrics.size());

// Grab the next available Metric slot to write to in the response.
pw_metric_Metric& proto_metric = response_.metrics[response_.metrics_count];
pw_metric_proto_Metric& proto_metric =
response_.metrics[response_.metrics_count];

// Copy the path.
span<Token> proto_path(proto_metric.token_path);
Expand All @@ -53,10 +54,10 @@ class MetricWriter {
// Copy the metric value.
if (metric.is_float()) {
proto_metric.value.as_float = metric.as_float();
proto_metric.which_value = pw_metric_Metric_as_float_tag;
proto_metric.which_value = pw_metric_proto_Metric_as_float_tag;
} else {
proto_metric.value.as_int = metric.as_int();
proto_metric.which_value = pw_metric_Metric_as_int_tag;
proto_metric.which_value = pw_metric_proto_Metric_as_int_tag;
}

// Move write head to the next slot.
Expand All @@ -73,14 +74,14 @@ class MetricWriter {
if (response_.metrics_count) {
response_writer_.Write(response_)
.IgnoreError(); // TODO(pwbug/387): Handle Status properly
response_ = pw_metric_MetricResponse_init_zero;
response_ = pw_metric_proto_MetricResponse_init_zero;
}
}

private:
pw_metric_MetricResponse response_;
pw_metric_proto_MetricResponse response_;
// This RPC stream writer handle must be valid for the metric writer lifetime.
MetricService::ServerWriter<pw_metric_MetricResponse>& response_writer_;
MetricService::ServerWriter<pw_metric_proto_MetricResponse>& response_writer_;
};

// Walk a metric tree recursively; passing metrics with their path (names) to a
Expand Down Expand Up @@ -129,8 +130,9 @@ class MetricWalker {

} // namespace

void MetricService::Get(const pw_metric_MetricRequest& /* request */,
ServerWriter<pw_metric_MetricResponse>& response) {
void MetricService::Get(
const pw_metric_proto_MetricRequest& /* request */,
ServerWriter<pw_metric_proto_MetricResponse>& response) {
// For now, ignore the request and just stream all the metrics back.
MetricWriter writer(response);
MetricWalker walker(writer);
Expand Down
2 changes: 1 addition & 1 deletion pw_metric/metric_service_nanopb_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ TEST(MetricService, NestedGroupsWithBatches) {
}

bool TokenPathsMatch(uint32_t expected_token_path[5],
const pw_metric_Metric& metric) {
const pw_metric_proto_Metric& metric) {
// Calculate length of expected token & compare.
int expected_length = 0;
while (expected_token_path[expected_length]) {
Expand Down
6 changes: 3 additions & 3 deletions pw_metric/public/pw_metric/metric_service_nanopb.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ namespace pw::metric {
// future, we may switch to offering an async version where the Get() method
// returns immediately, and someone else is responsible for pumping the queue.
class MetricService final
: public pw_rpc::nanopb::MetricService::Service<MetricService> {
: public proto::pw_rpc::nanopb::MetricService::Service<MetricService> {
public:
MetricService(const IntrusiveList<Metric>& metrics,
const IntrusiveList<Group>& groups)
: metrics_(metrics), groups_(groups) {}

void Get(const pw_metric_MetricRequest& request,
ServerWriter<pw_metric_MetricResponse>& response);
void Get(const pw_metric_proto_MetricRequest& request,
ServerWriter<pw_metric_proto_MetricResponse>& response);

private:
const IntrusiveList<Metric>& metrics_;
Expand Down
4 changes: 2 additions & 2 deletions pw_metric/pw_metric_proto/metric_service.options
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
// the License.

// TODO(keir): Figure out appropriate options.
pw.metric.Metric.token_path max_count:4
pw.metric.MetricResponse.metrics max_count:10
pw.metric.proto.Metric.token_path max_count:4
pw.metric.proto.MetricResponse.metrics max_count:10

2 changes: 1 addition & 1 deletion pw_metric/pw_metric_proto/metric_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// the License.
syntax = "proto3";

package pw.metric;
package pw.metric.proto;

// A metric, described by the name (path + name), and the value.
//
Expand Down

0 comments on commit ab0e417

Please sign in to comment.