• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
20 #include <unordered_map>
21 
22 #include "hci_processor.h"
23 
24 namespace bluetooth {
25 namespace activity_attribution {
26 
27 static constexpr size_t kWakeupAggregatorSize = 200;
28 
29 struct AddressActivityKey {
30   hci::Address address;
31   Activity activity;
32 
33   bool operator==(const AddressActivityKey& other) const {
34     return (address == other.address && activity == other.activity);
35   }
36 };
37 
38 struct AddressActivityKeyHasher {
operatorAddressActivityKeyHasher39   std::size_t operator()(const AddressActivityKey& key) const {
40     return (
41         (std::hash<std::string>()(key.address.ToString()) ^
42          (std::hash<unsigned char>()(static_cast<unsigned char>(key.activity)))));
43   }
44 };
45 
46 struct AppActivityKey {
47   std::string app;
48   Activity activity;
49 
50   bool operator==(const AppActivityKey& other) const {
51     return (app == other.app && activity == other.activity);
52   }
53 };
54 
55 struct AppActivityKeyHasher {
operatorAppActivityKeyHasher56   std::size_t operator()(const AppActivityKey& key) const {
57     return (
58         (std::hash<std::string>()(key.app) ^ (std::hash<unsigned char>()(static_cast<unsigned char>(key.activity)))));
59   }
60 };
61 
62 struct DeviceWakeupDescriptor {
63   Activity activity_;
64   const hci::Address address_;
DeviceWakeupDescriptorDeviceWakeupDescriptor65   DeviceWakeupDescriptor(Activity activity, const hci::Address address) : activity_(activity), address_(address) {}
~DeviceWakeupDescriptorDeviceWakeupDescriptor66   virtual ~DeviceWakeupDescriptor() {}
67 };
68 
69 struct AppWakeupDescriptor {
70   Activity activity_;
71   std::string package_info_;
AppWakeupDescriptorAppWakeupDescriptor72   AppWakeupDescriptor(Activity activity, std::string package_info) : activity_(activity), package_info_(package_info) {}
~AppWakeupDescriptorAppWakeupDescriptor73   virtual ~AppWakeupDescriptor() {}
74 };
75 
76 class AttributionProcessor {
77  public:
78   void OnBtaaPackets(std::vector<BtaaHciPacket> btaa_packets);
79   void OnWakelockReleased(uint32_t duration_ms);
80   void OnWakeup();
81   void NotifyActivityAttributionInfo(int uid, const std::string& package_name, const std::string& device_address);
82   void Dump(
83       std::promise<flatbuffers::Offset<ActivityAttributionData>> promise, flatbuffers::FlatBufferBuilder* fb_builder);
84 
85   using ClockType = std::chrono::time_point<std::chrono::system_clock>;
86   using NowFunc = ClockType (*)();
87 
88   // by default, we use the std::chrono::system_clock::now implementation to
89   // get the current timestamp
AttributionProcessor()90   AttributionProcessor() : now_func_(std::chrono::system_clock::now) {}
91   // in other cases, we may need to use different implementation
92   // e.g., for testing purposes
AttributionProcessor(NowFunc func)93   AttributionProcessor(NowFunc func) : now_func_(func) {}
94 
95  private:
96   // this function is added for testing support in
97   // OnWakelockReleased
98   NowFunc now_func_ = std::chrono::system_clock::now;
99   bool wakeup_ = false;
100   std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> btaa_aggregator_;
101   std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> wakelock_duration_aggregator_;
102   std::unordered_map<std::string, std::string> address_app_map_;
103   std::unordered_map<AppActivityKey, BtaaAggregationEntry, AppActivityKeyHasher> app_activity_aggregator_;
104   common::TimestampedCircularBuffer<DeviceWakeupDescriptor> device_wakeup_aggregator_ =
105       common::TimestampedCircularBuffer<DeviceWakeupDescriptor>(kWakeupAggregatorSize);
106   common::TimestampedCircularBuffer<AppWakeupDescriptor> app_wakeup_aggregator_ =
107       common::TimestampedCircularBuffer<AppWakeupDescriptor>(kWakeupAggregatorSize);
108   const char* ActivityToString(Activity activity);
109 };
110 
111 }  // namespace activity_attribution
112 }  // namespace bluetooth
113