Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 765582bb authored by Michael Sun's avatar Michael Sun Committed by Rahul Sabnis
Browse files

btaa: introduce btaa module into gd

Introduce the btaa module skeleton implementation in gd.

Tag: #feature
Bug: 172501038
Test: mmma -j system/bt
Change-Id: I7fa28e8ef5eb25ff08b96ab7b43a2526359450f8
parent c919bb02
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -136,6 +136,8 @@ cc_test {
        "android.hardware.bluetooth.a2dp@1.0",
        "android.hardware.bluetooth.audio@2.0",
        "android.hardware.bluetooth.audio@2.1",
        "android.system.suspend.control-V1-ndk",
        "libbinder_ndk",
        "libfmq",
        "libhidlbase",
        "liblog",
+5 −1
Original line number Diff line number Diff line
@@ -115,20 +115,24 @@ cc_defaults {
        },
        host: {
            srcs: [
                ":BluetoothBtaaSources_host",
                ":BluetoothHalSources_hci_rootcanal",
                ":BluetoothOsSources_host",
            ],
        },
        android: {
            srcs: [
                ":BluetoothBtaaSources_android",
                ":BluetoothHalSources_hci_android_hidl",
                ":BluetoothOsSources_android",
            ],
            shared_libs: [
                "android.hardware.bluetooth@1.0",
                "android.system.suspend.control-V1-ndk",
                "libbinder_ndk",
                "libcutils",
                "libhidlbase",
                "libutils",
                "libcutils",
            ],
        },
    },
+13 −0
Original line number Diff line number Diff line
filegroup {
    name: "BluetoothBtaaSources_android",
    srcs: [
        "android/activity_attribution.cc",
    ],
}

filegroup {
    name: "BluetoothBtaaSources_host",
    srcs: [
        "host/activity_attribution.cc",
    ],
}
+58 −0
Original line number Diff line number Diff line
/*
 * 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 "hci/address.h"
#include "module.h"

namespace bluetooth {
namespace activity_attribution {

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

class ActivityAttributionCallback {
 public:
  virtual ~ActivityAttributionCallback() = default;

  // Callback when Blutooth woke up the system
  virtual void OnWakeup(const Activity activity, const hci::Address& address) = 0;
};

class ActivityAttribution : public bluetooth::Module {
 public:
  ActivityAttribution() = default;
  ~ActivityAttribution() = default;

  void RegisterActivityAttributionCallback(ActivityAttributionCallback* callback);

  static const ModuleFactory Factory;

 protected:
  std::string ToString() const override;
  void ListDependencies(ModuleList* list) override;
  void Start() override;
  void Stop() override;

 private:
  struct impl;
  std::unique_ptr<impl> pimpl_;

  DISALLOW_COPY_AND_ASSIGN(ActivityAttribution);
};

}  // namespace activity_attribution
}  // namespace bluetooth
+123 −0
Original line number Diff line number Diff line
/*
 * 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.
 */

#define LOG_TAG "btaa"

#include "btaa/activity_attribution.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 "module.h"
#include "os/log.h"

using aidl::android::system::suspend::BnSuspendCallback;
using aidl::android::system::suspend::BnWakelockCallback;
using aidl::android::system::suspend::ISuspendCallback;
using aidl::android::system::suspend::ISuspendControlService;
using Status = ::ndk::ScopedAStatus;
using namespace ndk;

namespace bluetooth {
namespace activity_attribution {

const ModuleFactory ActivityAttribution::Factory = ModuleFactory([]() {
  return new ActivityAttribution();
});

static const std::string kBtWakelockName("hal_bluetooth_lock");

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

  Status notifyAcquired() override {
    return Status::ok();
  }
  Status notifyReleased() override {
    return Status::ok();
  }

  ActivityAttribution* module_;
};

struct wakeup_callback : public BnSuspendCallback {
  wakeup_callback(ActivityAttribution* module) : module_(module) {}

  Status notifyWakeup(bool success, const std::vector<std::string>& wakeup_reasons) override {
    return Status::ok();
  }

  ActivityAttribution* module_;
};

struct ActivityAttribution::impl {
  impl(ActivityAttribution* module) {
    bool is_registered = false;

    auto control_service =
        ISuspendControlService::fromBinder(SpAIBinder(
          AServiceManager_getService("suspend_control")));
    if (!control_service) {
      LOG_ERROR("Fail to obtain suspend_control");
      return;
    }

    Status register_callback_status =
        control_service->registerCallback(SharedRefBase::make<wakeup_callback>(module),
                                          &is_registered);
    if (!is_registered || !register_callback_status.isOk()) {
      LOG_ERROR("Fail to register wakeup callback");
      return;
    }

    register_callback_status = control_service->registerWakelockCallback(
        SharedRefBase::make<wakelock_callback>(module), kBtWakelockName, &is_registered);
    if (!is_registered || !register_callback_status.isOk()) {
      LOG_ERROR("Fail to register wakelock callback");
      return;
    }
  }

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

  ActivityAttributionCallback* callback_;
};

void ActivityAttribution::RegisterActivityAttributionCallback(
  ActivityAttributionCallback* callback) {
  CallOn(pimpl_.get(), &impl::register_callback, callback);
}

std::string ActivityAttribution::ToString() const {
  return "Btaa Module";
}

void ActivityAttribution::ListDependencies(ModuleList* list) {}

void ActivityAttribution::Start() {
  pimpl_ = std::make_unique<impl>(this);
}

void ActivityAttribution::Stop() {
  pimpl_.reset();
}

}  // namespace activity_attribution
}  // namespace bluetooth
Loading