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 WakeupDescriptor { 47 Activity activity_; 48 const hci::Address address_; WakeupDescriptorWakeupDescriptor49 WakeupDescriptor(Activity activity, const hci::Address address) : activity_(activity), address_(address) {} ~WakeupDescriptorWakeupDescriptor50 virtual ~WakeupDescriptor() {} 51 }; 52 53 class AttributionProcessor { 54 public: 55 void OnBtaaPackets(std::vector<BtaaHciPacket> btaa_packets); 56 void OnWakelockReleased(uint32_t duration_ms); 57 void OnWakeup(); 58 void Dump( 59 std::promise<flatbuffers::Offset<ActivityAttributionData>> promise, flatbuffers::FlatBufferBuilder* fb_builder); 60 61 private: 62 bool wakeup_ = false; 63 std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> btaa_aggregator_; 64 std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> wakelock_duration_aggregator_; 65 common::TimestampedCircularBuffer<WakeupDescriptor> wakeup_aggregator_ = 66 common::TimestampedCircularBuffer<WakeupDescriptor>(kWakeupAggregatorSize); 67 const char* ActivityToString(Activity activity); 68 }; 69 70 } // namespace activity_attribution 71 } // namespace bluetooth 72