1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include "pw_bluetooth_sapphire/internal/host/common/inspect.h"
17 #include "pw_bluetooth_sapphire/internal/host/common/metrics.h"
18 
19 namespace bt::gap {
20 
21 // Represents the shared metric counters updated across all peers
22 class PeerMetrics {
23  public:
24   static constexpr const char* kInspectNodeName = "metrics";
25 
26   PeerMetrics() = default;
27 
28   // Attach metrics node to |parent| peer cache inspect node.
29   void AttachInspect(inspect::Node& parent);
30 
31   // Log LE bonding success.
LogLeBondSuccessEvent()32   void LogLeBondSuccessEvent() { le_bond_success_.Add(); }
33 
34   // Log LE bonding failure.
LogLeBondFailureEvent()35   void LogLeBondFailureEvent() { le_bond_failure_.Add(); }
36 
37   // Log LE connection event.
LogLeConnection()38   void LogLeConnection() { le_connections_.Add(); }
39 
40   // Log LE disconnection event.
LogLeDisconnection()41   void LogLeDisconnection() { le_disconnections_.Add(); }
42 
43   // Log BrEdr bonding success.
LogBrEdrBondSuccessEvent()44   void LogBrEdrBondSuccessEvent() { bredr_bond_success_.Add(); }
45 
46   // Log BrEdr bonding failure.
LogBrEdrBondFailureEvent()47   void LogBrEdrBondFailureEvent() { bredr_bond_failure_.Add(); }
48 
49   // Log BrEdr connection event.
LogBrEdrConnection()50   void LogBrEdrConnection() { bredr_connections_.Add(); }
51 
52   // Log BrEdr disconnection event.
LogBrEdrDisconnection()53   void LogBrEdrDisconnection() { bredr_disconnections_.Add(); }
54 
55  private:
56   inspect::Node metrics_node_;
57   inspect::Node metrics_le_node_;
58   inspect::Node metrics_bredr_node_;
59 
60   UintMetricCounter le_bond_success_;
61   UintMetricCounter le_bond_failure_;
62   UintMetricCounter le_connections_;
63   UintMetricCounter le_disconnections_;
64   UintMetricCounter bredr_bond_success_;
65   UintMetricCounter bredr_bond_failure_;
66   UintMetricCounter bredr_connections_;
67   UintMetricCounter bredr_disconnections_;
68 
69   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(PeerMetrics);
70 };
71 
72 }  // namespace bt::gap
73