blob: 6f9c00b4ee24fc2aaef7a91a23f999b5641f6e08 [file] [log] [blame]
Chris Mantonfaedfe22019-10-01 11:26:49 -07001/*
2 * Copyright 2019 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>
Chris Mantonac2aaf92019-11-15 12:23:24 -080020#include <mutex>
Chris Mantonfaedfe22019-10-01 11:26:49 -070021#include <unordered_map>
Chris Mantonf5617872019-10-18 09:30:27 -070022#include <vector>
Chris Mantonfaedfe22019-10-01 11:26:49 -070023
Chris Mantonf5617872019-10-18 09:30:27 -070024#include "stack/include/btm_api_types.h"
Chris Mantonfaedfe22019-10-01 11:26:49 -070025
26/* Discoverable modes */
27static constexpr int kDiscoverableModeOff = 0;
28static constexpr int kLimitedDiscoverableMode = 1;
29static constexpr int kGeneralDiscoverableMode = 2;
30
Chris Mantonf5617872019-10-18 09:30:27 -070031/* Inquiry modes */
32// NOTE: The inquiry general/limited are reversed from the discoverability
33// constants
34static constexpr int kInquiryModeOff = 0;
35static constexpr int kGeneralInquiryMode = 1;
36static constexpr int kLimitedInquiryMode = 2;
37
Chris Mantonfaedfe22019-10-01 11:26:49 -070038/* Connectable modes */
39static constexpr int kConnectibleModeOff = 0;
40static constexpr int kConnectibleModeOn = 1;
41
42/* Inquiry and page scan modes */
43static constexpr int kStandardScanType = 0;
44static constexpr int kInterlacedScanType = 1;
45
46/* Inquiry result modes */
47static constexpr int kStandardInquiryResult = 0;
48static constexpr int kInquiryResultWithRssi = 1;
49static constexpr int kExtendedInquiryResult = 2;
50
51/* Inquiry filter types */
52static constexpr int kClearInquiryFilter = 0;
53static constexpr int kFilterOnDeviceClass = 1;
54static constexpr int kFilterOnAddress = 2;
55
56using DiscoverabilityState = struct {
57 int mode;
Chris Mantonfaedfe22019-10-01 11:26:49 -070058 uint16_t interval;
Chris Mantonf5617872019-10-18 09:30:27 -070059 uint16_t window;
Chris Mantonfaedfe22019-10-01 11:26:49 -070060};
Chris Mantonf5617872019-10-18 09:30:27 -070061using ConnectabilityState = DiscoverabilityState;
Chris Mantonfaedfe22019-10-01 11:26:49 -070062
63namespace bluetooth {
64namespace shim {
65
Chris Mantonac2aaf92019-11-15 12:23:24 -080066using BtmStatus = enum : uint16_t {
67 BTM_SUCCESS = 0, /* 0 Command succeeded */
68 BTM_CMD_STARTED, /* 1 Command started OK. */
69 BTM_BUSY, /* 2 Device busy with another command */
70 BTM_NO_RESOURCES, /* 3 No resources to issue command */
71 BTM_MODE_UNSUPPORTED, /* 4 Request for 1 or more unsupported modes */
72 BTM_ILLEGAL_VALUE, /* 5 Illegal parameter value */
73 BTM_WRONG_MODE, /* 6 Device in wrong mode for request */
74 BTM_UNKNOWN_ADDR, /* 7 Unknown remote BD address */
75 BTM_DEVICE_TIMEOUT, /* 8 Device timeout */
76 BTM_BAD_VALUE_RET, /* 9 A bad value was received from HCI */
77 BTM_ERR_PROCESSING, /* 10 Generic error */
78 BTM_NOT_AUTHORIZED, /* 11 Authorization failed */
79 BTM_DEV_RESET, /* 12 Device has been reset */
80 BTM_CMD_STORED, /* 13 request is stored in control block */
81 BTM_ILLEGAL_ACTION, /* 14 state machine gets illegal command */
82 BTM_DELAY_CHECK, /* 15 delay the check on encryption */
83 BTM_SCO_BAD_LENGTH, /* 16 Bad SCO over HCI data length */
84 BTM_SUCCESS_NO_SECURITY, /* 17 security passed, no security set */
85 BTM_FAILED_ON_SECURITY, /* 18 security failed */
86 BTM_REPEATED_ATTEMPTS, /* 19 repeated attempts for LE security requests */
87 BTM_MODE4_LEVEL4_NOT_SUPPORTED, /* 20 Secure Connections Only Mode can't be
88 supported */
89 BTM_DEV_BLACKLISTED /* 21 The device is Blacklisted */
90};
91
92class ReadRemoteName {
93 public:
94 bool Start(RawAddress raw_address) {
95 std::unique_lock<std::mutex> lock(mutex_);
96 if (in_progress_) {
97 return false;
98 }
99 raw_address_ = raw_address;
100 in_progress_ = true;
101 return true;
102 }
103
104 void Stop() {
105 std::unique_lock<std::mutex> lock(mutex_);
106 raw_address_ = RawAddress::kEmpty;
107 in_progress_ = false;
108 }
109
110 bool IsInProgress() const { return in_progress_; }
111
112 std::string AddressString() const { return raw_address_.ToString(); }
113
114 ReadRemoteName() : in_progress_{false}, raw_address_(RawAddress::kEmpty) {}
115
116 private:
117 bool in_progress_;
118 RawAddress raw_address_;
119 std::mutex mutex_;
120};
121
Chris Mantonfaedfe22019-10-01 11:26:49 -0700122class Btm {
123 public:
124 Btm();
125
Chris Mantonf5617872019-10-18 09:30:27 -0700126 // Callbacks
127 void OnInquiryResult(std::vector<const uint8_t> result);
128 void OnInquiryResultWithRssi(std::vector<const uint8_t> result);
129 void OnExtendedInquiryResult(std::vector<const uint8_t> result);
130 void OnInquiryComplete(uint16_t status);
Chris Mantonfaedfe22019-10-01 11:26:49 -0700131
Chris Mantonf5617872019-10-18 09:30:27 -0700132 // Inquiry API
133 bool SetInquiryFilter(uint8_t mode, uint8_t type, tBTM_INQ_FILT_COND data);
134 void SetFilterInquiryOnAddress();
135 void SetFilterInquiryOnDevice();
136 void ClearInquiryFilter();
Chris Mantonfaedfe22019-10-01 11:26:49 -0700137
Chris Mantonf5617872019-10-18 09:30:27 -0700138 bool SetStandardInquiryResultMode();
139 bool SetInquiryWithRssiResultMode();
140 bool SetExtendedInquiryResultMode();
141
142 void SetInterlacedInquiryScan();
143 void SetStandardInquiryScan();
Chris Mantonfaedfe22019-10-01 11:26:49 -0700144 bool IsInterlacedScanSupported() const;
145
Chris Mantonf5617872019-10-18 09:30:27 -0700146 bool StartInquiry(uint8_t mode, uint8_t duration, uint8_t max_responses);
147 void CancelInquiry();
Chris Mantonfaedfe22019-10-01 11:26:49 -0700148 bool IsInquiryActive() const;
Chris Mantonf5617872019-10-18 09:30:27 -0700149 bool IsGeneralInquiryActive() const;
150 bool IsLimitedInquiryActive() const;
Chris Mantonfaedfe22019-10-01 11:26:49 -0700151
Chris Mantonf5617872019-10-18 09:30:27 -0700152 bool StartPeriodicInquiry(uint8_t mode, uint8_t duration,
153 uint8_t max_responses, uint16_t max_delay,
154 uint16_t min_delay,
155 tBTM_INQ_RESULTS_CB* p_results_cb);
156 void CancelPeriodicInquiry();
Chris Mantonf85c8702019-11-04 10:00:55 -0800157 bool IsGeneralPeriodicInquiryActive() const;
158 bool IsLimitedPeriodicInquiryActive() const;
Chris Mantonf5617872019-10-18 09:30:27 -0700159
160 void SetClassicGeneralDiscoverability(uint16_t window, uint16_t interval);
161 void SetClassicLimitedDiscoverability(uint16_t window, uint16_t interval);
162 void SetClassicDiscoverabilityOff();
Chris Mantonfaedfe22019-10-01 11:26:49 -0700163 DiscoverabilityState GetClassicDiscoverabilityState() const;
Chris Mantonf5617872019-10-18 09:30:27 -0700164
165 void SetLeGeneralDiscoverability();
166 void SetLeLimitedDiscoverability();
167 void SetLeDiscoverabilityOff();
Chris Mantonfaedfe22019-10-01 11:26:49 -0700168 DiscoverabilityState GetLeDiscoverabilityState() const;
169
Chris Mantonf5617872019-10-18 09:30:27 -0700170 void SetClassicConnectibleOn();
171 void SetClassicConnectibleOff();
172 ConnectabilityState GetClassicConnectabilityState() const;
173 void SetInterlacedPageScan();
174 void SetStandardPageScan();
Chris Mantonfaedfe22019-10-01 11:26:49 -0700175
Chris Mantonf5617872019-10-18 09:30:27 -0700176 void SetLeConnectibleOn();
177 void SetLeConnectibleOff();
178 ConnectabilityState GetLeConnectabilityState() const;
Chris Mantonfaedfe22019-10-01 11:26:49 -0700179
Chris Mantonac2aaf92019-11-15 12:23:24 -0800180 bool IsLeAclConnected(const RawAddress& raw_address) const;
181
182 // Remote device name
183 BtmStatus ReadClassicRemoteDeviceName(const RawAddress& raw_address,
184 tBTM_CMPL_CB* callback);
185 BtmStatus ReadLeRemoteDeviceName(const RawAddress& raw_address,
186 tBTM_CMPL_CB* callback);
187 BtmStatus CancelAllReadRemoteDeviceName();
188
Chris Mantonfaedfe22019-10-01 11:26:49 -0700189 private:
Chris Mantonac2aaf92019-11-15 12:23:24 -0800190 ReadRemoteName le_read_remote_name_;
191 ReadRemoteName classic_read_remote_name_;
192 // TODO(cmanton) abort if there is no classic acl link up
193 bool CheckClassicAclLink(const RawAddress& raw_address) { return true; }
194 bool CheckLeAclLink(const RawAddress& raw_address) { return true; }
Chris Mantonfaedfe22019-10-01 11:26:49 -0700195};
196
197} // namespace shim
198} // namespace bluetooth