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 bool L2CA_ConnectCreditBasedRsp(const RawAddress& p_bd_addr, uint8_t id, 199 std::vector<uint16_t>& accepted_lcids, 200 uint16_t result, tL2CAP_LE_CFG_INFO* p_cfg); 201 202 /******************************************************************************* 203 * 204 * Function L2CA_DisconnectReq 205 * 206 * Description Higher layers call this function to disconnect a channel. 207 * 208 * Returns true if disconnect sent, else false 209 * 210 ******************************************************************************/ 211 bool L2CA_DisconnectReq(uint16_t cid); 212 213 bool L2CA_DisconnectLECocReq(uint16_t cid); 214 215 /******************************************************************************* 216 * 217 * Function L2CA_DataWrite 218 * 219 * Description Higher layers call this function to write data. 220 * 221 * Returns L2CAP_DW_SUCCESS, if data accepted, else false 222 * L2CAP_DW_CONGESTED, if data accepted and the channel is 223 * congested 224 * L2CAP_DW_FAILED, if error 225 * 226 ******************************************************************************/ 227 uint8_t L2CA_DataWrite(uint16_t cid, BT_HDR* p_data); 228 229 uint8_t L2CA_LECocDataWrite(uint16_t cid, BT_HDR* p_data); 230 231 // Given a local channel identifier, |lcid|, this function returns the bound 232 // remote channel identifier, |rcid|. If 233 // |lcid| is not known or is invalid, this function returns false and does not 234 // modify the value pointed at by |rcid|. |rcid| may be NULL. 235 bool L2CA_GetRemoteCid(uint16_t lcid, uint16_t* rcid); 236 237 /******************************************************************************* 238 * 239 * Function L2CA_SetIdleTimeoutByBdAddr 240 * 241 * Description Higher layers call this function to set the idle timeout for 242 * a connection. The "idle timeout" is the amount of time that 243 * a connection can remain up with no L2CAP channels on it. 244 * A timeout of zero means that the connection will be torn 245 * down immediately when the last channel is removed. 246 * A timeout of 0xFFFF means no timeout. Values are in seconds. 247 * A bd_addr is the remote BD address. If bd_addr = 248 * RawAddress::kAny, then the idle timeouts for all active 249 * l2cap links will be changed. 250 * 251 * Returns true if command succeeded, false if failed 252 * 253 * NOTE This timeout applies to all logical channels active on the 254 * ACL link. 255 ******************************************************************************/ 256 bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout, 257 tBT_TRANSPORT transport); 258 259 /******************************************************************************* 260 * 261 * Function L2CA_SetTraceLevel 262 * 263 * Description This function sets the trace level for L2CAP. If called with 264 * a value of 0xFF, it simply reads the current trace level. 265 * 266 * Returns the new (current) trace level 267 * 268 ******************************************************************************/ 269 uint8_t L2CA_SetTraceLevel(uint8_t trace_level); 270 271 /******************************************************************************* 272 * 273 * Function L2CA_FlushChannel 274 * 275 * Description This function flushes none, some or all buffers queued up 276 * for xmission for a particular CID. If called with 277 * L2CAP_FLUSH_CHANS_GET (0), it simply returns the number 278 * of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff) 279 * flushes all buffers. All other values specifies the maximum 280 * buffers to flush. 281 * 282 * Returns Number of buffers left queued for that CID 283 * 284 ******************************************************************************/ 285 uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush); 286 287 /******************************************************************************* 288 * 289 * Function L2CA_SetAclPriority 290 * 291 * Description Sets the transmission priority for an ACL channel. 292 * (For initial implementation only two values are valid. 293 * L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH). 294 * 295 * Returns true if a valid channel, else false 296 * 297 ******************************************************************************/ 298 bool L2CA_SetAclPriority(const RawAddress& bd_addr, tL2CAP_PRIORITY priority); 299 300 /******************************************************************************* 301 * 302 * Function L2CA_SetTxPriority 303 * 304 * Description Sets the transmission priority for a channel. (FCR Mode) 305 * 306 * Returns true if a valid channel, else false 307 * 308 ******************************************************************************/ 309 bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority); 310 311 /******************************************************************************* 312 * 313 * Function L2CA_SetChnlFlushability 314 * 315 * Description Higher layers call this function to set a channels 316 * flushability flags 317 * 318 * Returns true if CID found, else false 319 * 320 ******************************************************************************/ 321 bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable); 322 323 /******************************************************************************* 324 * 325 * Function L2CA_GetPeerFeatures 326 * 327 * Description Get a peers features and fixed channel map 328 * 329 * Parameters: BD address of the peer 330 * Pointers to features and channel mask storage area 331 * 332 * Return value: true if peer is connected 333 * 334 ******************************************************************************/ 335 bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat, 336 uint8_t* p_chnl_mask); 337 338 /******************************************************************************* 339 * 340 * Function L2CA_RegisterFixedChannel 341 * 342 * Description Register a fixed channel. 343 * 344 * Parameters: Fixed Channel # 345 * Channel Callbacks and config 346 * 347 * Return value: true if registered OK 348 * 349 ******************************************************************************/ 350 bool L2CA_RegisterFixedChannel(uint16_t fixed_cid, 351 tL2CAP_FIXED_CHNL_REG* p_freg); 352 353 /******************************************************************************* 354 * 355 * Function L2CA_ConnectFixedChnl 356 * 357 * Description Connect an fixed signalling channel to a remote device. 358 * 359 * Parameters: Fixed CID 360 * BD Address of remote 361 * 362 * Return value: true if connection started 363 * 364 ******************************************************************************/ 365 bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& bd_addr); 366 367 /******************************************************************************* 368 * 369 * Function L2CA_SendFixedChnlData 370 * 371 * Description Write data on a fixed signalling channel. 372 * 373 * Parameters: Fixed CID 374 * BD Address of remote 375 * Pointer to buffer of type BT_HDR 376 * 377 * Return value L2CAP_DW_SUCCESS, if data accepted 378 * L2CAP_DW_FAILED, if error 379 * 380 ******************************************************************************/ 381 uint16_t L2CA_SendFixedChnlData(uint16_t fixed_cid, const RawAddress& rem_bda, 382 BT_HDR* p_buf); 383 384 /******************************************************************************* 385 * 386 * Function L2CA_RemoveFixedChnl 387 * 388 * Description Remove a fixed channel to a remote device. 389 * 390 * Parameters: Fixed CID 391 * BD Address of remote 392 * Idle timeout to use (or 0xFFFF if don't care) 393 * 394 * Return value: true if channel removed 395 * 396 ******************************************************************************/ 397 bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda); 398 399 uint16_t L2CA_GetLeHandle(const RawAddress& rem_bda); 400 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr); 401 402 void L2CA_LeConnectionUpdate(const RawAddress& rem_bda, uint16_t min_int, 403 uint16_t max_int, uint16_t latency, 404 uint16_t timeout, uint16_t min_ce_len, 405 uint16_t max_ce_len); 406 407 // When GATT discovery is in progress, use the minimal connection interval, and 408 // reject remote connection updates, until done. 409 bool L2CA_EnableUpdateBleConnParams(const RawAddress& rem_bda, bool enable); 410 411 /******************************************************************************* 412 * 413 * Function L2CA_SetLeGattTimeout 414 * 415 * Description Higher layers call this function to set the idle timeout for 416 * a fixed channel. The "idle timeout" is the amount of time 417 * that a connection can remain up with no L2CAP channels on 418 * it. A timeout of zero means that the connection will be torn 419 * down immediately when the last channel is removed. 420 * A timeout of 0xFFFF means no timeout. Values are in seconds. 421 * A bd_addr is the remote BD address. If bd_addr = 422 * RawAddress::kAny, then the idle timeouts for all active 423 * l2cap links will be changed. 424 * 425 * Returns true if command succeeded, false if failed 426 * 427 ******************************************************************************/ 428 bool L2CA_SetLeGattTimeout(const RawAddress& rem_bda, uint16_t idle_tout); 429 430 bool L2CA_MarkLeLinkAsActive(const RawAddress& rem_bda); 431 432 bool L2CA_UpdateBleConnParams(const RawAddress& rem_bda, uint16_t min_int, 433 uint16_t max_int, uint16_t latency, 434 uint16_t timeout, uint16_t min_ce_len, 435 uint16_t max_ce_len); 436 437 /******************************************************************************* 438 * 439 * Function L2CA_EnableUpdateBleConnParams 440 * 441 * Description Update BLE connection parameters. 442 * 443 * Parameters: BD Address of remote 444 * enable flag 445 * 446 * Return value: true if update started 447 * 448 ******************************************************************************/ 449 bool L2CA_EnableUpdateBleConnParams(const RawAddress& rem_bda, bool enable); 450 451 /******************************************************************************* 452 * 453 * Function L2CA_GetBleConnRole 454 * 455 * Description This function returns the connection role. 456 * 457 * Returns link role. 458 * 459 ******************************************************************************/ 460 hci_role_t L2CA_GetBleConnRole(const RawAddress& bd_addr); 461 462 /** 463 * Check whether an ACL or LE link to the remote device is established 464 */ 465 bool L2CA_IsLinkEstablished(const RawAddress& bd_addr, tBT_TRANSPORT transport); 466 467 void L2CA_ConnectForSecurity(const RawAddress& bd_addr); 468 469 // Set bonding state to acquire/release link refcount 470 void L2CA_SetBondingState(const RawAddress& p_bd_addr, bool is_bonding); 471 472 // Indicated by shim stack manager that GD L2cap is enabled but Security is not 473 void L2CA_UseLegacySecurityModule(); 474 475 void L2CA_SwitchRoleToCentral(const RawAddress& addr); 476 477 bool L2CA_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version, 478 uint16_t* manufacturer, uint16_t* lmp_sub_version); 479 480 uint8_t* L2CA_ReadRemoteFeatures(const RawAddress& addr); 481 482 void L2CA_DisconnectLink(const RawAddress& remote); 483 484 uint16_t L2CA_GetNumLinks(); 485 486 bool L2CA_IsLeLink(uint16_t acl_handle); 487 488 void L2CA_ReadConnectionAddr(const RawAddress& pseudo_addr, 489 RawAddress& conn_addr, 490 tBLE_ADDR_TYPE* p_addr_type); 491 492 bool L2CA_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr, 493 RawAddress& conn_addr, 494 tBLE_ADDR_TYPE* p_addr_type); 495 496 } // namespace shim 497 } // namespace bluetooth 498