1 /*
2  * Copyright 2020 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 "hal/snoop_logger.h"
20 #include "hci/address.h"
21 #include "module.h"
22 
23 namespace bluetooth {
24 namespace activity_attribution {
25 
26 enum class Activity : uint8_t { UNKNOWN = 0, ACL, ADVERTISE, CONNECT, CONTROL, HFP, ISO, SCAN, VENDOR };
27 
28 using CreationTime = std::chrono::time_point<std::chrono::system_clock>;
29 
30 struct BtaaAggregationEntry {
31   hci::Address address;
32   Activity activity;
33   uint16_t wakeup_count;
34   uint32_t byte_count;
35   uint32_t wakelock_duration_ms;
36   CreationTime creation_time;
37 };
38 
39 class ActivityAttributionCallback {
40  public:
41   virtual ~ActivityAttributionCallback() = default;
42 
43   // Callback when Bluetooth woke up the system
44   virtual void OnWakeup(const Activity activity, const hci::Address& address) = 0;
45 
46   // Callback when Bluetooth activity logs are ready to be moved
47   virtual void OnActivityLogsReady(const std::vector<BtaaAggregationEntry> logs) = 0;
48 };
49 
50 class ActivityAttribution : public bluetooth::Module {
51  public:
52   ActivityAttribution() = default;
53   ~ActivityAttribution() = default;
54 
55   void Capture(const hal::HciPacket& packet, hal::SnoopLogger::PacketType type);
56   void OnWakelockAcquired();
57   void OnWakelockReleased();
58   void OnWakeup();
59   void RegisterActivityAttributionCallback(ActivityAttributionCallback* callback);
60 
61   static const ModuleFactory Factory;
62 
63  protected:
64   std::string ToString() const override;
65   void ListDependencies(ModuleList* list) override;
66   void Start() override;
67   void Stop() override;
68   DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const override;  // Module
69 
70  private:
71   struct impl;
72   std::unique_ptr<impl> pimpl_;
73 
74   DISALLOW_COPY_AND_ASSIGN(ActivityAttribution);
75 };
76 
77 }  // namespace activity_attribution
78 }  // namespace bluetooth
79