aboutsummaryrefslogtreecommitdiff
path: root/system/gd/btaa/attribution_processor.h
blob: e28e6cb2168cd63188ca0579db2880fcde970e82 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright 2020 The Android Open Source Project
 *
 * 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
 *
 *      http://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.
 */

#pragma once

#include <cstdint>
#include <unordered_map>

#include "hci_processor.h"

namespace bluetooth {
namespace activity_attribution {

static constexpr size_t kWakeupAggregatorSize = 200;

struct AddressActivityKey {
  hci::Address address;
  Activity activity;

  bool operator==(const AddressActivityKey& other) const {
    return (address == other.address && activity == other.activity);
  }
};

struct AddressActivityKeyHasher {
  std::size_t operator()(const AddressActivityKey& key) const {
    return (
        (std::hash<std::string>()(key.address.ToString()) ^
         (std::hash<unsigned char>()(static_cast<unsigned char>(key.activity)))));
  }
};

struct AppActivityKey {
  std::string app;
  Activity activity;

  bool operator==(const AppActivityKey& other) const {
    return (app == other.app && activity == other.activity);
  }
};

struct AppActivityKeyHasher {
  std::size_t operator()(const AppActivityKey& key) const {
    return (
        (std::hash<std::string>()(key.app) ^ (std::hash<unsigned char>()(static_cast<unsigned char>(key.activity)))));
  }
};

struct DeviceWakeupDescriptor {
  Activity activity_;
  const hci::Address address_;
  DeviceWakeupDescriptor(Activity activity, const hci::Address address) : activity_(activity), address_(address) {}
  virtual ~DeviceWakeupDescriptor() {}
};

struct AppWakeupDescriptor {
  Activity activity_;
  std::string package_info_;
  AppWakeupDescriptor(Activity activity, std::string package_info) : activity_(activity), package_info_(package_info) {}
  virtual ~AppWakeupDescriptor() {}
};

class AttributionProcessor {
 public:
  void OnBtaaPackets(std::vector<BtaaHciPacket> btaa_packets);
  void OnWakelockReleased(uint32_t duration_ms);
  void OnWakeup();
  void NotifyActivityAttributionInfo(int uid, const std::string& package_name, const std::string& device_address);
  void Dump(
      std::promise<flatbuffers::Offset<ActivityAttributionData>> promise, flatbuffers::FlatBufferBuilder* fb_builder);

  using ClockType = std::chrono::time_point<std::chrono::system_clock>;
  using NowFunc = ClockType (*)();

  // by default, we use the std::chrono::system_clock::now implementation to
  // get the current timestamp
  AttributionProcessor() : now_func_(std::chrono::system_clock::now) {}
  // in other cases, we may need to use different implementation
  // e.g., for testing purposes
  AttributionProcessor(NowFunc func) : now_func_(func) {}

 private:
  // this function is added for testing support in
  // OnWakelockReleased
  NowFunc now_func_ = std::chrono::system_clock::now;
  bool wakeup_ = false;
  std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> btaa_aggregator_;
  std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> wakelock_duration_aggregator_;
  std::unordered_map<std::string, std::string> address_app_map_;
  std::unordered_map<AppActivityKey, BtaaAggregationEntry, AppActivityKeyHasher> app_activity_aggregator_;
  common::TimestampedCircularBuffer<DeviceWakeupDescriptor> device_wakeup_aggregator_ =
      common::TimestampedCircularBuffer<DeviceWakeupDescriptor>(kWakeupAggregatorSize);
  common::TimestampedCircularBuffer<AppWakeupDescriptor> app_wakeup_aggregator_ =
      common::TimestampedCircularBuffer<AppWakeupDescriptor>(kWakeupAggregatorSize);
  const char* ActivityToString(Activity activity);
};

}  // namespace activity_attribution
}  // namespace bluetooth