aboutsummaryrefslogtreecommitdiff
path: root/pw_log_rpc/log_filter_service.cc
blob: b56f3968e2e5808012aa9465c836f0ab9d4834fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#include "pw_log_rpc/log_filter_service.h"

#include "pw_log/log.h"
#include "pw_log/proto/log.pwpb.h"
#include "pw_log_rpc/log_filter.h"
#include "pw_protobuf/decoder.h"

namespace pw::log_rpc {
StatusWithSize FilterService::SetFilter(ConstByteSpan request, ByteSpan) {
  protobuf::Decoder decoder(request);
  PW_TRY_WITH_SIZE(decoder.Next());
  if (static_cast<log::SetFilterRequest::Fields>(decoder.FieldNumber()) !=
      log::SetFilterRequest::Fields::FILTER_ID) {
    return StatusWithSize::InvalidArgument();
  }
  ConstByteSpan filter_id;
  PW_TRY_WITH_SIZE(decoder.ReadBytes(&filter_id));
  Result<Filter*> filter = filter_map_.GetFilterFromId(filter_id);
  if (!filter.ok()) {
    return StatusWithSize::NotFound();
  }

  PW_TRY_WITH_SIZE(decoder.Next());
  ConstByteSpan filter_buffer;
  if (static_cast<log::SetFilterRequest::Fields>(decoder.FieldNumber()) !=
      log::SetFilterRequest::Fields::FILTER) {
    return StatusWithSize::InvalidArgument();
  }
  PW_TRY_WITH_SIZE(decoder.ReadBytes(&filter_buffer));
  PW_TRY_WITH_SIZE(filter.value()->UpdateRulesFromProto(filter_buffer));
  return StatusWithSize();
}

StatusWithSize FilterService::GetFilter(ConstByteSpan request,
                                        ByteSpan response) {
  protobuf::Decoder decoder(request);
  PW_TRY_WITH_SIZE(decoder.Next());
  if (static_cast<log::GetFilterRequest::Fields>(decoder.FieldNumber()) !=
      log::GetFilterRequest::Fields::FILTER_ID) {
    return StatusWithSize::InvalidArgument();
  }
  ConstByteSpan filter_id;
  PW_TRY_WITH_SIZE(decoder.ReadBytes(&filter_id));
  Result<Filter*> filter = filter_map_.GetFilterFromId(filter_id);
  if (!filter.ok()) {
    return StatusWithSize::NotFound();
  }

  log::Filter::MemoryEncoder encoder(response);
  for (auto& rule : (*filter)->rules()) {
    log::FilterRule::StreamEncoder rule_encoder = encoder.GetRuleEncoder();
    rule_encoder.WriteLevelGreaterThanOrEqual(rule.level_greater_than_or_equal)
        .IgnoreError();
    rule_encoder.WriteModuleEquals(rule.module_equals).IgnoreError();
    rule_encoder.WriteAnyFlagsSet(rule.any_flags_set).IgnoreError();
    rule_encoder.WriteAction(static_cast<log::FilterRule::Action>(rule.action))
        .IgnoreError();
    PW_TRY_WITH_SIZE(rule_encoder.status());
  }
  PW_TRY_WITH_SIZE(encoder.status());

  return StatusWithSize(encoder.size());
}

StatusWithSize FilterService::ListFilterIds(ConstByteSpan, ByteSpan response) {
  log::FilterIdListResponse::MemoryEncoder encoder(response);
  for (auto& filter : filter_map_.filters()) {
    PW_TRY_WITH_SIZE(encoder.WriteFilterId(filter.id()));
  }
  return StatusWithSize(encoder.size());
}

}  // namespace pw::log_rpc