Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Manton | ac2aaf9 | 2019-11-15 12:23:24 -0800 | [diff] [blame] | 20 | #include <mutex> |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 21 | #include <unordered_map> |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 22 | #include <vector> |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 23 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 24 | #include "stack/include/btm_api_types.h" |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 25 | |
| 26 | /* Discoverable modes */ |
| 27 | static constexpr int kDiscoverableModeOff = 0; |
| 28 | static constexpr int kLimitedDiscoverableMode = 1; |
| 29 | static constexpr int kGeneralDiscoverableMode = 2; |
| 30 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 31 | /* Inquiry modes */ |
| 32 | // NOTE: The inquiry general/limited are reversed from the discoverability |
| 33 | // constants |
| 34 | static constexpr int kInquiryModeOff = 0; |
| 35 | static constexpr int kGeneralInquiryMode = 1; |
| 36 | static constexpr int kLimitedInquiryMode = 2; |
| 37 | |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 38 | /* Connectable modes */ |
| 39 | static constexpr int kConnectibleModeOff = 0; |
| 40 | static constexpr int kConnectibleModeOn = 1; |
| 41 | |
| 42 | /* Inquiry and page scan modes */ |
| 43 | static constexpr int kStandardScanType = 0; |
| 44 | static constexpr int kInterlacedScanType = 1; |
| 45 | |
| 46 | /* Inquiry result modes */ |
| 47 | static constexpr int kStandardInquiryResult = 0; |
| 48 | static constexpr int kInquiryResultWithRssi = 1; |
| 49 | static constexpr int kExtendedInquiryResult = 2; |
| 50 | |
| 51 | /* Inquiry filter types */ |
| 52 | static constexpr int kClearInquiryFilter = 0; |
| 53 | static constexpr int kFilterOnDeviceClass = 1; |
| 54 | static constexpr int kFilterOnAddress = 2; |
| 55 | |
| 56 | using DiscoverabilityState = struct { |
| 57 | int mode; |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 58 | uint16_t interval; |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 59 | uint16_t window; |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 60 | }; |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 61 | using ConnectabilityState = DiscoverabilityState; |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 62 | |
| 63 | namespace bluetooth { |
| 64 | namespace shim { |
| 65 | |
Chris Manton | ac2aaf9 | 2019-11-15 12:23:24 -0800 | [diff] [blame] | 66 | using 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 | |
| 92 | class 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 Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 122 | class Btm { |
| 123 | public: |
| 124 | Btm(); |
| 125 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 126 | // 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 Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 131 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 132 | // 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 Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 137 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 138 | bool SetStandardInquiryResultMode(); |
| 139 | bool SetInquiryWithRssiResultMode(); |
| 140 | bool SetExtendedInquiryResultMode(); |
| 141 | |
| 142 | void SetInterlacedInquiryScan(); |
| 143 | void SetStandardInquiryScan(); |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 144 | bool IsInterlacedScanSupported() const; |
| 145 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 146 | bool StartInquiry(uint8_t mode, uint8_t duration, uint8_t max_responses); |
| 147 | void CancelInquiry(); |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 148 | bool IsInquiryActive() const; |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 149 | bool IsGeneralInquiryActive() const; |
| 150 | bool IsLimitedInquiryActive() const; |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 151 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 152 | 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 Manton | f85c870 | 2019-11-04 10:00:55 -0800 | [diff] [blame] | 157 | bool IsGeneralPeriodicInquiryActive() const; |
| 158 | bool IsLimitedPeriodicInquiryActive() const; |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 159 | |
| 160 | void SetClassicGeneralDiscoverability(uint16_t window, uint16_t interval); |
| 161 | void SetClassicLimitedDiscoverability(uint16_t window, uint16_t interval); |
| 162 | void SetClassicDiscoverabilityOff(); |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 163 | DiscoverabilityState GetClassicDiscoverabilityState() const; |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 164 | |
| 165 | void SetLeGeneralDiscoverability(); |
| 166 | void SetLeLimitedDiscoverability(); |
| 167 | void SetLeDiscoverabilityOff(); |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 168 | DiscoverabilityState GetLeDiscoverabilityState() const; |
| 169 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 170 | void SetClassicConnectibleOn(); |
| 171 | void SetClassicConnectibleOff(); |
| 172 | ConnectabilityState GetClassicConnectabilityState() const; |
| 173 | void SetInterlacedPageScan(); |
| 174 | void SetStandardPageScan(); |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 175 | |
Chris Manton | f561787 | 2019-10-18 09:30:27 -0700 | [diff] [blame] | 176 | void SetLeConnectibleOn(); |
| 177 | void SetLeConnectibleOff(); |
| 178 | ConnectabilityState GetLeConnectabilityState() const; |
Chris Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 179 | |
Chris Manton | ac2aaf9 | 2019-11-15 12:23:24 -0800 | [diff] [blame] | 180 | 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 Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 189 | private: |
Chris Manton | ac2aaf9 | 2019-11-15 12:23:24 -0800 | [diff] [blame] | 190 | 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 Manton | faedfe2 | 2019-10-01 11:26:49 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | } // namespace shim |
| 198 | } // namespace bluetooth |