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 <set> 20 #include <unordered_map> 21 #include <vector> 22 23 #include "stack/include/bt_hdr.h" 24 #include "stack/include/l2c_api.h" 25 #include "types/ble_address_with_type.h" 26 #include "types/hci_role.h" 27 #include "types/raw_address.h" 28 29 namespace bluetooth { 30 namespace shim { 31 32 /******************************************************************************* 33 * 34 * Function L2CA_Register 35 * 36 * Description Other layers call this function to register for L2CAP 37 * services. 38 * 39 * Returns PSM to use or zero if error. Typically, the PSM returned 40 * is the same as was passed in, but for an outgoing-only 41 * connection to a dynamic PSM, a "virtual" PSM is returned 42 * and should be used in the calls to L2CA_ConnectReq() and 43 * BTM_SetSecurityLevel(). 44 * 45 ******************************************************************************/ 46 uint16_t L2CA_Register(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, 47 bool enable_snoop, tL2CAP_ERTM_INFO* p_ertm_info, 48 uint16_t my_mtu, uint16_t required_remote_mtu, 49 uint16_t sec_level); 50 51 /******************************************************************************* 52 * 53 * Function L2CA_Deregister 54 * 55 * Description Other layers call this function to deregister for L2CAP 56 * services. 57 * 58 * Returns void 59 * 60 ******************************************************************************/ 61 void L2CA_Deregister(uint16_t psm); 62 63 /******************************************************************************* 64 * 65 * Function L2CA_AllocateLePSM 66 * 67 * Description Other layers call this function to find an unused LE PSM for 68 * L2CAP services. 69 * 70 * Returns LE_PSM to use if success. Otherwise returns 0. 71 * 72 ******************************************************************************/ 73 uint16_t L2CA_AllocateLePSM(void); 74 75 /******************************************************************************* 76 * 77 * Function L2CA_FreeLePSM 78 * 79 * Description Free an assigned LE PSM. 80 * 81 * Returns void 82 * 83 ******************************************************************************/ 84 void L2CA_FreeLePSM(uint16_t psm); 85 86 /******************************************************************************* 87 * 88 * Function L2CA_ConnectReq 89 * 90 * Description Higher layers call this function to create an L2CAP 91 * connection. 92 * Note that the connection is not established at this time, 93 * but connection establishment gets started. The callback 94 * will be invoked when connection establishes or fails. 95 * 96 * Returns the CID of the connection, or 0 if it failed to start 97 * 98 ******************************************************************************/ 99 uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& p_bd_addr); 100 101 /******************************************************************************* 102 * 103 * Function L2CA_RegisterLECoc 104 * 105 * Description Other layers call this function to register for L2CAP 106 * Connection Oriented Channel. 107 * 108 * Returns PSM to use or zero if error. Typically, the PSM returned 109 * is the same as was passed in, but for an outgoing-only 110 * connection to a dynamic PSM, a "virtual" PSM is returned 111 * and should be used in the calls to L2CA_ConnectLECocReq() 112 * and BTM_SetSecurityLevel(). 113 * 114 ******************************************************************************/ 115 uint16_t L2CA_RegisterLECoc(uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, 116 uint16_t sec_level, tL2CAP_LE_CFG_INFO cfg); 117 118 /******************************************************************************* 119 * 120 * Function L2CA_DeregisterLECoc 121 * 122 * Description Other layers call this function to deregister for L2CAP 123 * Connection Oriented Channel. 124 * 125 * Returns void 126 * 127 ******************************************************************************/ 128 void L2CA_DeregisterLECoc(uint16_t psm); 129 130 /******************************************************************************* 131 * 132 * Function L2CA_ConnectLECocReq 133 * 134 * Description Higher layers call this function to create an L2CAP LE COC. 135 * Note that the connection is not established at this time, 136 * but connection establishment gets started. The callback 137 * will be invoked when connection establishes or fails. 138 * 139 * Returns the CID of the connection, or 0 if it failed to start 140 * 141 ******************************************************************************/ 142 uint16_t L2CA_ConnectLECocReq(uint16_t psm, const RawAddress& p_bd_addr, 143 tL2CAP_LE_CFG_INFO* p_cfg); 144 145 /******************************************************************************* 146 * 147 * Function L2CA_GetPeerLECocConfig 148 * 149 * Description Get peers configuration for LE Connection Oriented Channel. 150 * 151 * Return value: true if peer is connected 152 * 153 ******************************************************************************/ 154 bool L2CA_GetPeerLECocConfig(uint16_t lcid, tL2CAP_LE_CFG_INFO* peer_cfg); 155 156 /******************************************************************************* 157 * 158 * Function L2CA_ReconfigCreditBasedConnsReq 159 * 160 * Description Start reconfigure procedure on Credit Based Connection Oriented 161 * Channels. 162 * 163 * Return value: true if peer is connected 164 * 165 ******************************************************************************/ 166 167 bool L2CA_ReconfigCreditBasedConnsReq(const RawAddress& bd_addr, 168 std::vector<uint16_t>& lcids, 169 tL2CAP_LE_CFG_INFO* p_cfg); 170 171 /******************************************************************************* 172 * 173 * Function L2CA_ConnectCreditBasedReq 174 * 175 * Description With this function L2CAP will initiate setup of up to 5 credit 176 * based connections for given psm using provided configuration. 177 * L2CAP will notify user on the connection result, by calling 178 * pL2CA_CreditBasedConnectCfm_Cb for each cid with a result. 179 * 180 * Return value: vector of allocated local cids for the connection 181 * 182 ******************************************************************************/ 183 184 extern std::vector<uint16_t> L2CA_ConnectCreditBasedReq( 185 uint16_t psm, const RawAddress& p_bd_addr, tL2CAP_LE_CFG_INFO* p_cfg); 186 187 /******************************************************************************* 188 * 189 * Function L2CA_ConnectCreditBasedRsp 190 * 191 * Description Response for the pL2CA_CreditBasedConnectInd_Cb which is the 192 * indication for peer requesting credit based connection. 193 * 194 * Return value: true if peer is connected 195 * 196 ******************************************************************************/ 197 198 extern bool L2CA_ConnectCreditBasedRsp(const RawAddress& p_bd_addr, uint8_t id, 199 std::vector<uint16_t>& accepted_lcids, 200 uint16_t result, 201 tL2CAP_LE_CFG_INFO* p_cfg); 202 203 /******************************************************************************* 204 * 205 * Function L2CA_DisconnectReq 206 * 207 * Description Higher layers call this function to disconnect a channel. 208 * 209 * Returns true if disconnect sent, else false 210 * 211 ******************************************************************************/ 212 bool L2CA_DisconnectReq(uint16_t cid); 213 214 bool L2CA_DisconnectLECocReq(uint16_t cid); 215 216 /******************************************************************************* 217 * 218 * Function L2CA_DataWrite 219 * 220 * Description Higher layers call this function to write data. 221 * 222 * Returns L2CAP_DW_SUCCESS, if data accepted, else false 223 * L2CAP_DW_CONGESTED, if data accepted and the channel is 224 * congested 225 * L2CAP_DW_FAILED, if error 226 * 227 ******************************************************************************/ 228 uint8_t L2CA_DataWrite(uint16_t cid, BT_HDR* p_data); 229 230 uint8_t L2CA_LECocDataWrite(uint16_t cid, BT_HDR* p_data); 231 232 // Given a local channel identifier, |lcid|, this function returns the bound 233 // remote channel identifier, |rcid|. If 234 // |lcid| is not known or is invalid, this function returns false and does not 235 // modify the value pointed at by |rcid|. |rcid| may be NULL. 236 bool L2CA_GetRemoteCid(uint16_t lcid, uint16_t* rcid); 237 238 /******************************************************************************* 239 * 240 * Function L2CA_SetIdleTimeoutByBdAddr 241 * 242 * Description Higher layers call this function to set the idle timeout for 243 * a connection. The "idle timeout" is the amount of time that 244 * a connection can remain up with no L2CAP channels on it. 245 * A timeout of zero means that the connection will be torn 246 * down immediately when the last channel is removed. 247 * A timeout of 0xFFFF means no timeout. Values are in seconds. 248 * A bd_addr is the remote BD address. If bd_addr = 249 * RawAddress::kAny, then the idle timeouts for all active 250 * l2cap links will be changed. 251 * 252 * Returns true if command succeeded, false if failed 253 * 254 * NOTE This timeout applies to all logical channels active on the 255 * ACL link. 256 ******************************************************************************/ 257 bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout, 258 tBT_TRANSPORT transport); 259 260 /******************************************************************************* 261 * 262 * Function L2CA_SetTraceLevel 263 * 264 * Description This function sets the trace level for L2CAP. If called with 265 * a value of 0xFF, it simply reads the current trace level. 266 * 267 * Returns the new (current) trace level 268 * 269 ******************************************************************************/ 270 uint8_t L2CA_SetTraceLevel(uint8_t trace_level); 271 272 /******************************************************************************* 273 * 274 * Function L2CA_FlushChannel 275 * 276 * Description This function flushes none, some or all buffers queued up 277 * for xmission for a particular CID. If called with 278 * L2CAP_FLUSH_CHANS_GET (0), it simply returns the number 279 * of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff) 280 * flushes all buffers. All other values specifies the maximum 281 * buffers to flush. 282 * 283 * Returns Number of buffers left queued for that CID 284 * 285 ******************************************************************************/ 286 uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush); 287 288 /******************************************************************************* 289 * 290 * Function L2CA_SetAclPriority 291 * 292 * Description Sets the transmission priority for an ACL channel. 293 * (For initial implementation only two values are valid. 294 * L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH). 295 * 296 * Returns true if a valid channel, else false 297 * 298 ******************************************************************************/ 299 bool L2CA_SetAclPriority(const RawAddress& bd_addr, tL2CAP_PRIORITY priority); 300 301 /******************************************************************************* 302 * 303 * Function L2CA_SetTxPriority 304 * 305 * Description Sets the transmission priority for a channel. (FCR Mode) 306 * 307 * Returns true if a valid channel, else false 308 * 309 ******************************************************************************/ 310 bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority); 311 312 /******************************************************************************* 313 * 314 * Function L2CA_SetChnlFlushability 315 * 316 * Description Higher layers call this function to set a channels 317 * flushability flags 318 * 319 * Returns true if CID found, else false 320 * 321 ******************************************************************************/ 322 bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable); 323 324 /******************************************************************************* 325 * 326 * Function L2CA_GetPeerFeatures 327 * 328 * Description Get a peers features and fixed channel map 329 * 330 * Parameters: BD address of the peer 331 * Pointers to features and channel mask storage area 332 * 333 * Return value: true if peer is connected 334 * 335 ******************************************************************************/ 336 bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat, 337 uint8_t* p_chnl_mask); 338 339 /******************************************************************************* 340 * 341 * Function L2CA_RegisterFixedChannel 342 * 343 * Description Register a fixed channel. 344 * 345 * Parameters: Fixed Channel # 346 * Channel Callbacks and config 347 * 348 * Return value: true if registered OK 349 * 350 ******************************************************************************/ 351 bool L2CA_RegisterFixedChannel(uint16_t fixed_cid, 352 tL2CAP_FIXED_CHNL_REG* p_freg); 353 354 /******************************************************************************* 355 * 356 * Function L2CA_ConnectFixedChnl 357 * 358 * Description Connect an fixed signalling channel to a remote device. 359 * 360 * Parameters: Fixed CID 361 * BD Address of remote 362 * 363 * Return value: true if connection started 364 * 365 ******************************************************************************/ 366 bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr); 367 368 /******************************************************************************* 369 * 370 * Function L2CA_SendFixedChnlData 371 * 372 * Description Write data on a fixed signalling channel. 373 * 374 * Parameters: Fixed CID 375 * BD Address of remote 376 * Pointer to buffer of type BT_HDR 377 * 378 * Return value L2CAP_DW_SUCCESS, if data accepted 379 * L2CAP_DW_FAILED, if error 380 * 381 ******************************************************************************/ 382 uint16_t L2CA_SendFixedChnlData(uint16_t fixed_cid, const RawAddress& rem_bda, 383 BT_HDR* p_buf); 384 385 /******************************************************************************* 386 * 387 * Function L2CA_RemoveFixedChnl 388 * 389 * Description Remove a fixed channel to a remote device. 390 * 391 * Parameters: Fixed CID 392 * BD Address of remote 393 * Idle timeout to use (or 0xFFFF if don't care) 394 * 395 * Return value: true if channel removed 396 * 397 ******************************************************************************/ 398 bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda); 399 400 uint16_t L2CA_GetLeHandle(const RawAddress& rem_bda); 401 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr); 402 403 void L2CA_LeConnectionUpdate(const RawAddress& rem_bda, uint16_t min_int, 404 uint16_t max_int, uint16_t latency, 405 uint16_t timeout, uint16_t min_ce_len, 406 uint16_t max_ce_len); 407 408 // When GATT discovery is in progress, use the minimal connection interval, and 409 // reject remote connection updates, until done. 410 bool L2CA_EnableUpdateBleConnParams(const RawAddress& rem_bda, bool enable); 411 412 /******************************************************************************* 413 * 414 * Function L2CA_SetLeGattTimeout 415 * 416 * Description Higher layers call this function to set the idle timeout for 417 * a fixed channel. The "idle timeout" is the amount of time 418 * that a connection can remain up with no L2CAP channels on 419 * it. A timeout of zero means that the connection will be torn 420 * down immediately when the last channel is removed. 421 * A timeout of 0xFFFF means no timeout. Values are in seconds. 422 * A bd_addr is the remote BD address. If bd_addr = 423 * RawAddress::kAny, then the idle timeouts for all active 424 * l2cap links will be changed. 425 * 426 * Returns true if command succeeded, false if failed 427 * 428 ******************************************************************************/ 429 bool L2CA_SetLeGattTimeout(const RawAddress& rem_bda, uint16_t idle_tout); 430 431 bool L2CA_MarkLeLinkAsActive(const RawAddress& rem_bda); 432 433 bool L2CA_UpdateBleConnParams(const RawAddress& rem_bda, uint16_t min_int, 434 uint16_t max_int, uint16_t latency, 435 uint16_t timeout, uint16_t min_ce_len, 436 uint16_t max_ce_len); 437 438 /******************************************************************************* 439 * 440 * Function L2CA_EnableUpdateBleConnParams 441 * 442 * Description Update BLE connection parameters. 443 * 444 * Parameters: BD Address of remote 445 * enable flag 446 * 447 * Return value: true if update started 448 * 449 ******************************************************************************/ 450 bool L2CA_EnableUpdateBleConnParams(const RawAddress& rem_bda, bool enable); 451 452 /******************************************************************************* 453 * 454 * Function L2CA_GetBleConnRole 455 * 456 * Description This function returns the connection role. 457 * 458 * Returns link role. 459 * 460 ******************************************************************************/ 461 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr); 462 463 /** 464 * Check whether an ACL or LE link to the remote device is established 465 */ 466 bool L2CA_IsLinkEstablished(const RawAddress& bd_addr, tBT_TRANSPORT transport); 467 468 void L2CA_ConnectForSecurity(const RawAddress& bd_addr); 469 470 // Set bonding state to acquire/release link refcount 471 void L2CA_SetBondingState(const RawAddress& p_bd_addr, bool is_bonding); 472 473 // Indicated by shim stack manager that GD L2cap is enabled but Security is not 474 void L2CA_UseLegacySecurityModule(); 475 476 void L2CA_SwitchRoleToCentral(const RawAddress& addr); 477 478 bool L2CA_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version, 479 uint16_t* manufacturer, uint16_t* lmp_sub_version); 480 481 uint8_t* L2CA_ReadRemoteFeatures(const RawAddress& addr); 482 483 void L2CA_DisconnectLink(const RawAddress& remote); 484 485 uint16_t L2CA_GetNumLinks(); 486 487 bool L2CA_IsLeLink(uint16_t acl_handle); 488 489 void L2CA_ReadConnectionAddr(const RawAddress& pseudo_addr, 490 RawAddress& conn_addr, 491 tBLE_ADDR_TYPE* p_addr_type); 492 493 bool L2CA_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr, 494 RawAddress& conn_addr, 495 tBLE_ADDR_TYPE* p_addr_type); 496 497 } // namespace shim 498 } // namespace bluetooth 499