• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <hardware/bt_le_audio.h>
20 
21 #include <cstdint>
22 #include <memory>
23 #include <unordered_map>
24 
25 #include "le_audio_types.h"
26 #include "types/raw_address.h"
27 
28 namespace le_audio {
29 
30 enum ConnectionStatus : int32_t {
31   UNKNOWN = 0,
32   SUCCESS = 1,
33   FAILED = 2,
34 };
35 
36 /* android.bluetooth.leaudio.ContextType */
37 enum class LeAudioMetricsContextType : int32_t {
38   INVALID = 0,
39   UNSPECIFIED = 1,
40   COMMUNICATION = 2,
41   MEDIA = 3,
42   INSTRUCTIONAL = 4,
43   ATTENTION_SEEKING = 5,
44   IMMEDIATE_ALERT = 6,
45   MAN_MACHINE = 7,
46   EMERGENCY_ALERT = 8,
47   RINGTONE = 9,
48   TV = 10,
49   LIVE = 11,
50   GAME = 12,
51   RFU = 13,
52 };
53 
54 class GroupMetrics {
55  public:
GroupMetrics()56   GroupMetrics() {}
57 
~GroupMetrics()58   virtual ~GroupMetrics() {}
59 
60   virtual void AddStateChangedEvent(const RawAddress& address,
61                                     bluetooth::le_audio::ConnectionState state,
62                                     ConnectionStatus status) = 0;
63 
64   virtual void AddStreamStartedEvent(
65       le_audio::types::LeAudioContextType context_type) = 0;
66 
67   virtual void AddStreamEndedEvent() = 0;
68 
69   virtual void SetGroupSize(int32_t group_size) = 0;
70 
71   virtual bool IsClosed() = 0;
72 
73   virtual void WriteStats() = 0;
74 
75   virtual void Flush() = 0;
76 };
77 
78 class MetricsCollector {
79  public:
80   static MetricsCollector* Get();
81 
82   /**
83    * Update the size of given group which will be used in the
84    * LogMetricBluetoothLeAudioConnectionStateChanged()
85    *
86    * @param group_id ID of target group
87    * @param group_size Size of target group
88    */
89   void OnGroupSizeUpdate(int32_t group_id, int32_t group_size);
90 
91   /**
92    * When there is a change in Bluetooth LE Audio connection state
93    *
94    * @param group_id Group ID of the associated device.
95    * @param address Address of the associated device.
96    * @param state New LE Audio connetion state.
97    * @param status status or reason of the state transition. Ignored at
98    * CONNECTING states.
99    */
100   void OnConnectionStateChanged(int32_t group_id, const RawAddress& address,
101                                 bluetooth::le_audio::ConnectionState state,
102                                 ConnectionStatus status);
103 
104   /**
105    * When there is a change in LE Audio stream started
106    *
107    * @param group_id Group ID of the associated stream.
108    */
109   void OnStreamStarted(int32_t group_id,
110                        le_audio::types::LeAudioContextType context_type);
111 
112   /**
113    * When there is a change in LE Audio stream started
114    *
115    * @param group_id Group ID of the associated stream.
116    */
117   void OnStreamEnded(int32_t group_id);
118 
119   /**
120    * Flush all log to statsd
121    *
122    * @param group_id Group ID of the associated stream.
123    */
124   void Flush();
125 
126  protected:
MetricsCollector()127   MetricsCollector() {}
128 
129  private:
130   static MetricsCollector* instance;
131 
132   std::unordered_map<int32_t, std::unique_ptr<GroupMetrics>> opened_groups_;
133   std::unordered_map<int32_t, int32_t> group_size_table_;
134 };
135 
136 }  // namespace le_audio
137