summaryrefslogtreecommitdiff
path: root/system/main/shim/acl_api.cc
blob: 09b1823f2c7fd8cfbd3f66092f7555a122e6d348 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * 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.
 */

#include "main/shim/acl_api.h"

#include <cstddef>
#include <cstdint>
#include <future>

#include "gd/hci/acl_manager.h"
#include "main/shim/dumpsys.h"
#include "main/shim/helpers.h"
#include "main/shim/stack.h"
#include "osi/include/allocator.h"
#include "stack/include/bt_hdr.h"
#include "types/ble_address_with_type.h"
#include "types/raw_address.h"

void bluetooth::shim::ACL_CreateClassicConnection(
    const RawAddress& raw_address) {
  auto address = ToGdAddress(raw_address);
  Stack::GetInstance()->GetAcl()->CreateClassicConnection(address);
}

void bluetooth::shim::ACL_CancelClassicConnection(
    const RawAddress& raw_address) {
  auto address = ToGdAddress(raw_address);
  Stack::GetInstance()->GetAcl()->CancelClassicConnection(address);
}

bool bluetooth::shim::ACL_AcceptLeConnectionFrom(
    const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct) {
  std::promise<bool> promise;
  auto future = promise.get_future();
  Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(
      ToAddressWithTypeFromLegacy(legacy_address_with_type), is_direct,
      std::move(promise));
  return future.get();
}

void bluetooth::shim::ACL_IgnoreLeConnectionFrom(
    const tBLE_BD_ADDR& legacy_address_with_type) {
  Stack::GetInstance()->GetAcl()->IgnoreLeConnectionFrom(
      ToAddressWithTypeFromLegacy(legacy_address_with_type));
}

void bluetooth::shim::ACL_WriteData(uint16_t handle, BT_HDR* p_buf) {
  std::unique_ptr<bluetooth::packet::RawBuilder> packet = MakeUniquePacket(
      p_buf->data + p_buf->offset + HCI_DATA_PREAMBLE_SIZE,
      p_buf->len - HCI_DATA_PREAMBLE_SIZE, IsPacketFlushable(p_buf));
  Stack::GetInstance()->GetAcl()->WriteData(handle, std::move(packet));
  osi_free(p_buf);
}

void bluetooth::shim::ACL_ConfigureLePrivacy(bool is_le_privacy_enabled) {
  hci::LeAddressManager::AddressPolicy address_policy =
      is_le_privacy_enabled
          ? hci::LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS
          : hci::LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS;
  hci::AddressWithType empty_address_with_type(
      hci::Address{}, hci::AddressType::RANDOM_DEVICE_ADDRESS);
  /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
  auto minimum_rotation_time = std::chrono::minutes(7);
  auto maximum_rotation_time = std::chrono::minutes(15);

  Stack::GetInstance()
      ->GetStackManager()
      ->GetInstance<bluetooth::hci::AclManager>()
      ->SetPrivacyPolicyForInitiatorAddress(
          address_policy, empty_address_with_type, minimum_rotation_time,
          maximum_rotation_time);
}

void bluetooth::shim::ACL_Disconnect(uint16_t handle, bool is_classic,
                                     tHCI_STATUS reason, std::string comment) {
  (is_classic)
      ? Stack::GetInstance()->GetAcl()->DisconnectClassic(handle, reason,
                                                          comment)
      : Stack::GetInstance()->GetAcl()->DisconnectLe(handle, reason, comment);
}

void bluetooth::shim::ACL_Shutdown() {
  Stack::GetInstance()->GetAcl()->Shutdown();
}

void bluetooth::shim::ACL_IgnoreAllLeConnections() {
  return Stack::GetInstance()->GetAcl()->ClearAcceptList();
}

void bluetooth::shim::ACL_ReadConnectionAddress(const RawAddress& pseudo_addr,
                                                RawAddress& conn_addr,
                                                tBLE_ADDR_TYPE* p_addr_type) {
  auto local_address =
      Stack::GetInstance()->GetAcl()->GetConnectionLocalAddress(pseudo_addr);
  conn_addr = ToRawAddress(local_address.GetAddress());
  *p_addr_type = static_cast<tBLE_ADDR_TYPE>(local_address.GetAddressType());
}

void bluetooth::shim::ACL_AddToAddressResolution(
    const tBLE_BD_ADDR& legacy_address_with_type, const Octet16& peer_irk,
    const Octet16& local_irk) {
  Stack::GetInstance()->GetAcl()->AddToAddressResolution(
      ToAddressWithType(legacy_address_with_type.bda,
                        legacy_address_with_type.type),
      peer_irk, local_irk);
}

void bluetooth::shim::ACL_RemoveFromAddressResolution(
    const tBLE_BD_ADDR& legacy_address_with_type) {
  Stack::GetInstance()->GetAcl()->RemoveFromAddressResolution(ToAddressWithType(
      legacy_address_with_type.bda, legacy_address_with_type.type));
}

void bluetooth::shim::ACL_ClearAddressResolution() {
  Stack::GetInstance()->GetAcl()->ClearAddressResolution();
}

void bluetooth::shim::ACL_ClearAcceptList() {
  Stack::GetInstance()->GetAcl()->ClearAcceptList();
}