Skip to content

Commit

Permalink
Merge changes I02cc2057,Icb879814,I468ac585,Idf93afcf,If3006967, ... …
Browse files Browse the repository at this point in the history
…am: f54f80e am: a6efacd am: beb2aba

Original change: https://android-review.googlesource.com/c/platform/system/bt/+/1680525

Change-Id: Ied9619c078bedb4b1bfad9c2ed04eb8d0b3b7d5f
  • Loading branch information
Treehugger Robot authored and android-build-merge-worker-robot committed Apr 30, 2021
2 parents b8ef94c + beb2aba commit e6f5de3
Show file tree
Hide file tree
Showing 17 changed files with 1,321 additions and 9 deletions.
5 changes: 5 additions & 0 deletions gd/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ cc_defaults {
target: {
linux: {
srcs: [
":BluetoothBtaaSources_linux_generic",
":BluetoothOsSources_linux_generic",
],
},
Expand Down Expand Up @@ -574,13 +575,15 @@ genrule {
],
cmd: "$(location flatc) -I system/bt/gd -b --schema -o $(genDir) $(in) ",
srcs: [
"btaa/activity_attribution.fbs",
"common/init_flags.fbs",
"dumpsys_data.fbs",
"hci/hci_acl_manager.fbs",
"l2cap/classic/l2cap_classic_module.fbs",
"shim/dumpsys.fbs",
],
out: [
"activity_attribution.bfbs",
"init_flags.bfbs",
"dumpsys.bfbs",
"dumpsys_data.bfbs",
Expand All @@ -596,13 +599,15 @@ genrule {
],
cmd: "$(location flatc) -I system/bt/gd -o $(genDir) --cpp $(in) ",
srcs: [
"btaa/activity_attribution.fbs",
"common/init_flags.fbs",
"dumpsys_data.fbs",
"hci/hci_acl_manager.fbs",
"l2cap/classic/l2cap_classic_module.fbs",
"shim/dumpsys.fbs",
],
out: [
"activity_attribution_generated.h",
"dumpsys_data_generated.h",
"dumpsys_generated.h",
"hci_acl_manager_generated.h",
Expand Down
10 changes: 10 additions & 0 deletions gd/btaa/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ filegroup {
"host/activity_attribution.cc",
],
}

filegroup {
name: "BluetoothBtaaSources_linux_generic",
srcs: [
"linux_generic/attribution_processor.cc",
"linux_generic/cmd_evt_classification.cc",
"linux_generic/hci_processor.cc",
"linux_generic/wakelock_processor.cc",
],
}
29 changes: 29 additions & 0 deletions gd/btaa/activity_attribution.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace bluetooth.activity_attribution;

attribute "privacy";

table WakeupEntry {
wakeup_time:string;
activity:string;
address:string;
}

table DeviceActivityAggregationEntry {
address:string;
activity:string;
wakeup_count:int;
byte_count:int;
wakelock_duration_ms:int;
creation_time:string;
}

table ActivityAttributionData {
title_wakeup:string;
num_wakeup:int;
wakeup_attribution:[WakeupEntry];
title_activity:string;
num_device_activity:int;
device_activity_aggregation:[DeviceActivityAggregationEntry];
}

root_type ActivityAttributionData;
11 changes: 9 additions & 2 deletions gd/btaa/activity_attribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
namespace bluetooth {
namespace activity_attribution {

enum class Activity : uint8_t { UNKNOWN = 0, ADVERTISE, CONNECT, CONTROL, SCAN, HFP, VENDOR };
enum class Activity : uint8_t { UNKNOWN = 0, ACL, ADVERTISE, CONNECT, CONTROL, HFP, ISO, SCAN, VENDOR };

using CreationTime = std::chrono::time_point<std::chrono::system_clock>;

struct BtaaAggregationEntry {
hci::Address address;
Activity activity;
uint16_t wakeup_count;
uint32_t byte_count;
uint32_t wakelock_duration;
uint32_t wakelock_duration_ms;
CreationTime creation_time;
};

class ActivityAttributionCallback {
Expand All @@ -50,6 +53,9 @@ class ActivityAttribution : public bluetooth::Module {
~ActivityAttribution() = default;

void Capture(const hal::HciPacket& packet, hal::SnoopLogger::PacketType type);
void OnWakelockAcquired();
void OnWakelockReleased();
void OnWakeup();
void RegisterActivityAttributionCallback(ActivityAttributionCallback* callback);

static const ModuleFactory Factory;
Expand All @@ -59,6 +65,7 @@ class ActivityAttribution : public bluetooth::Module {
void ListDependencies(ModuleList* list) override;
void Start() override;
void Stop() override;
DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const override; // Module

private:
struct impl;
Expand Down
71 changes: 69 additions & 2 deletions gd/btaa/android/activity_attribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
#define LOG_TAG "btaa"

#include "btaa/activity_attribution.h"
#include "activity_attribution_generated.h"

#include <aidl/android/system/suspend/BnSuspendCallback.h>
#include <aidl/android/system/suspend/BnWakelockCallback.h>
#include <aidl/android/system/suspend/ISuspendControlService.h>
#include <android/binder_manager.h>

#include "btaa/attribution_processor.h"
#include "btaa/hci_processor.h"
#include "btaa/wakelock_processor.h"
#include "module.h"
#include "os/log.h"

Expand All @@ -39,14 +43,18 @@ namespace activity_attribution {
const ModuleFactory ActivityAttribution::Factory = ModuleFactory([]() { return new ActivityAttribution(); });

static const std::string kBtWakelockName("hal_bluetooth_lock");
static const std::string kBtWakeupReason("hs_uart_wakeup");
static const size_t kHciAclHeaderSize = 4;

struct wakelock_callback : public BnWakelockCallback {
wakelock_callback(ActivityAttribution* module) : module_(module) {}

Status notifyAcquired() override {
module_->OnWakelockAcquired();
return Status::ok();
}
Status notifyReleased() override {
module_->OnWakelockReleased();
return Status::ok();
}

Expand All @@ -57,6 +65,12 @@ struct wakeup_callback : public BnSuspendCallback {
wakeup_callback(ActivityAttribution* module) : module_(module) {}

Status notifyWakeup(bool success, const std::vector<std::string>& wakeup_reasons) override {
for (auto& wakeup_reason : wakeup_reasons) {
if (wakeup_reason.find(kBtWakeupReason) != std::string::npos) {
module_->OnWakeup();
break;
}
}
return Status::ok();
}

Expand Down Expand Up @@ -89,13 +103,40 @@ struct ActivityAttribution::impl {
}
}

void on_hci_packet(hal::HciPacket packet, hal::SnoopLogger::PacketType type, uint16_t length) {}
void on_hci_packet(hal::HciPacket packet, hal::SnoopLogger::PacketType type, uint16_t length) {
attribution_processor_.OnBtaaPackets(std::move(hci_processor_.OnHciPacket(std::move(packet), type, length)));
}

void on_wakelock_acquired() {
wakelock_processor_.OnWakelockAcquired();
}

void on_wakelock_released() {
uint32_t wakelock_duration_ms = 0;

wakelock_duration_ms = wakelock_processor_.OnWakelockReleased();
if (wakelock_duration_ms != 0) {
attribution_processor_.OnWakelockReleased(wakelock_duration_ms);
}
}

void on_wakeup() {
attribution_processor_.OnWakeup();
}

void register_callback(ActivityAttributionCallback* callback) {
callback_ = callback;
}

void Dump(
std::promise<flatbuffers::Offset<ActivityAttributionData>> promise, flatbuffers::FlatBufferBuilder* fb_builder) {
attribution_processor_.Dump(std::move(promise), fb_builder);
}

ActivityAttributionCallback* callback_;
AttributionProcessor attribution_processor_;
HciProcessor hci_processor_;
WakelockProcessor wakelock_processor_;
};

void ActivityAttribution::Capture(const hal::HciPacket& packet, hal::SnoopLogger::PacketType type) {
Expand All @@ -110,7 +151,7 @@ void ActivityAttribution::Capture(const hal::HciPacket& packet, hal::SnoopLogger
case hal::SnoopLogger::PacketType::ACL:
case hal::SnoopLogger::PacketType::SCO:
case hal::SnoopLogger::PacketType::ISO:
truncate_length = 0;
truncate_length = kHciAclHeaderSize;
break;
}

Expand All @@ -122,6 +163,18 @@ void ActivityAttribution::Capture(const hal::HciPacket& packet, hal::SnoopLogger
CallOn(pimpl_.get(), &impl::on_hci_packet, truncate_packet, type, original_length);
}

void ActivityAttribution::OnWakelockAcquired() {
CallOn(pimpl_.get(), &impl::on_wakelock_acquired);
}

void ActivityAttribution::OnWakelockReleased() {
CallOn(pimpl_.get(), &impl::on_wakelock_released);
}

void ActivityAttribution::OnWakeup() {
CallOn(pimpl_.get(), &impl::on_wakeup);
}

void ActivityAttribution::RegisterActivityAttributionCallback(ActivityAttributionCallback* callback) {
CallOn(pimpl_.get(), &impl::register_callback, callback);
}
Expand All @@ -140,5 +193,19 @@ void ActivityAttribution::Stop() {
pimpl_.reset();
}

DumpsysDataFinisher ActivityAttribution::GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) const {
ASSERT(fb_builder != nullptr);

std::promise<flatbuffers::Offset<ActivityAttributionData>> promise;
auto future = promise.get_future();
pimpl_->Dump(std::move(promise), fb_builder);

auto dumpsys_data = future.get();

return [dumpsys_data](DumpsysDataBuilder* dumpsys_builder) {
dumpsys_builder->add_activity_attribution_dumpsys_data(dumpsys_data);
};
}

} // namespace activity_attribution
} // namespace bluetooth
71 changes: 71 additions & 0 deletions gd/btaa/attribution_processor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 WakeupDescriptor {
Activity activity_;
const hci::Address address_;
WakeupDescriptor(Activity activity, const hci::Address address) : activity_(activity), address_(address) {}
virtual ~WakeupDescriptor() {}
};

class AttributionProcessor {
public:
void OnBtaaPackets(std::vector<BtaaHciPacket> btaa_packets);
void OnWakelockReleased(uint32_t duration_ms);
void OnWakeup();
void Dump(
std::promise<flatbuffers::Offset<ActivityAttributionData>> promise, flatbuffers::FlatBufferBuilder* fb_builder);

private:
bool wakeup_ = false;
std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> btaa_aggregator_;
std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> wakelock_duration_aggregator_;
common::TimestampedCircularBuffer<WakeupDescriptor> wakeup_aggregator_ =
common::TimestampedCircularBuffer<WakeupDescriptor>(kWakeupAggregatorSize);
const char* ActivityToString(Activity activity);
};

} // namespace activity_attribution
} // namespace bluetooth
36 changes: 36 additions & 0 deletions gd/btaa/cmd_evt_classification.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021 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 "btaa/activity_attribution.h"
#include "hci/hci_packets.h"

namespace bluetooth {
namespace activity_attribution {

struct CmdEvtActivityClassification {
Activity activity;
uint16_t connection_handle_pos;
uint16_t address_pos;
};

CmdEvtActivityClassification lookup_cmd(hci::OpCode opcode);
CmdEvtActivityClassification lookup_event(hci::EventCode event_code);
CmdEvtActivityClassification lookup_le_event(hci::SubeventCode subevent_code);

} // namespace activity_attribution
} // namespace bluetooth
Loading

0 comments on commit e6f5de3

Please sign in to comment.