ble_advertiser.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /******************************************************************************
  2. *
  3. * Copyright 2016 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #ifndef BLE_ADVERTISER_H
  19. #define BLE_ADVERTISER_H
  20. #include <base/bind.h>
  21. #include <base/memory/weak_ptr.h>
  22. #include <vector>
  23. #include "btm_ble_api.h"
  24. #define BTM_BLE_MULTI_ADV_SUCCESS 0
  25. #define BTM_BLE_MULTI_ADV_FAILURE 1
  26. #define ADVERTISE_FAILED_TOO_MANY_ADVERTISERS 0x02
  27. using MultiAdvCb = base::Callback<void(uint8_t /* status */)>;
  28. using ParametersCb =
  29. base::Callback<void(uint8_t /* status */, int8_t /* tx_power */)>;
  30. // methods we must have defined
  31. void btm_ble_update_dmt_flag_bits(uint8_t* flag_value,
  32. const uint16_t connect_mode,
  33. const uint16_t disc_mode);
  34. void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address);
  35. // methods we expose to c code:
  36. void btm_ble_multi_adv_cleanup(void);
  37. void btm_ble_multi_adv_init();
  38. typedef struct {
  39. uint16_t advertising_event_properties;
  40. uint32_t adv_int_min;
  41. uint32_t adv_int_max;
  42. tBTM_BLE_ADV_CHNL_MAP channel_map;
  43. tBTM_BLE_AFP adv_filter_policy;
  44. int8_t tx_power;
  45. uint8_t primary_advertising_phy;
  46. uint8_t secondary_advertising_phy;
  47. uint8_t scan_request_notification_enable;
  48. } tBTM_BLE_ADV_PARAMS;
  49. typedef struct {
  50. uint8_t enable;
  51. uint16_t min_interval;
  52. uint16_t max_interval;
  53. uint16_t periodic_advertising_properties;
  54. } tBLE_PERIODIC_ADV_PARAMS;
  55. class BleAdvertiserHciInterface;
  56. class BleAdvertisingManager {
  57. public:
  58. virtual ~BleAdvertisingManager() = default;
  59. static const uint16_t advertising_prop_legacy_connectable = 0x0011;
  60. static const uint16_t advertising_prop_legacy_non_connectable = 0x0010;
  61. static void Initialize(BleAdvertiserHciInterface* interface);
  62. static void CleanUp();
  63. static bool IsInitialized();
  64. static base::WeakPtr<BleAdvertisingManager> Get();
  65. /* Register an advertising instance, status will be returned in |cb|
  66. * callback, with assigned id, if operation succeeds. Instance is freed when
  67. * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
  68. * of the operations fails.
  69. * The instance will have data set to |advertise_data|, scan response set to
  70. * |scan_response_data|, and will be enabled.
  71. */
  72. virtual void StartAdvertising(uint8_t advertiser_id, MultiAdvCb cb,
  73. tBTM_BLE_ADV_PARAMS* params,
  74. std::vector<uint8_t> advertise_data,
  75. std::vector<uint8_t> scan_response_data,
  76. int duration, MultiAdvCb timeout_cb) = 0;
  77. /* Register an advertising instance, status will be returned in |cb|
  78. * callback, with assigned id, if operation succeeds. Instance is freed when
  79. * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
  80. * of the operations fails.
  81. * The instance will have data set to |advertise_data|, scan response set to
  82. * |scan_response_data|, periodic data set to |periodic_data| and will be
  83. * enabled.
  84. */
  85. virtual void StartAdvertisingSet(
  86. base::Callback<void(uint8_t /* inst_id */, int8_t /* tx_power */,
  87. uint8_t /* status */)>
  88. cb,
  89. tBTM_BLE_ADV_PARAMS* params, std::vector<uint8_t> advertise_data,
  90. std::vector<uint8_t> scan_response_data,
  91. tBLE_PERIODIC_ADV_PARAMS* periodic_params,
  92. std::vector<uint8_t> periodic_data, uint16_t duration,
  93. uint8_t maxExtAdvEvents,
  94. base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>
  95. timeout_cb) = 0;
  96. /* Register an advertising instance, status will be returned in |cb|
  97. * callback, with assigned id, if operation succeeds. Instance is freed when
  98. * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
  99. * of the operations fails. */
  100. virtual void RegisterAdvertiser(
  101. base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>) = 0;
  102. /* This function enables/disables an advertising instance. Operation status is
  103. * returned in |cb| */
  104. virtual void Enable(uint8_t inst_id, bool enable, MultiAdvCb cb,
  105. uint16_t duration, uint8_t maxExtAdvEvents,
  106. MultiAdvCb timeout_cb) = 0;
  107. /* This function update a Multi-ADV instance with the specififed adv
  108. * parameters. */
  109. virtual void SetParameters(uint8_t inst_id, tBTM_BLE_ADV_PARAMS* p_params,
  110. ParametersCb cb) = 0;
  111. /* This function configure a Multi-ADV instance with the specified adv data or
  112. * scan response data.*/
  113. virtual void SetData(uint8_t inst_id, bool is_scan_rsp,
  114. std::vector<uint8_t> data, MultiAdvCb cb) = 0;
  115. /* This function configure instance with the specified periodic parameters */
  116. virtual void SetPeriodicAdvertisingParameters(
  117. uint8_t inst_id, tBLE_PERIODIC_ADV_PARAMS* params, MultiAdvCb cb) = 0;
  118. /* This function configure instance with the specified periodic data */
  119. virtual void SetPeriodicAdvertisingData(uint8_t inst_id,
  120. std::vector<uint8_t> data,
  121. MultiAdvCb cb) = 0;
  122. /* This function enables/disables periodic advertising on selected instance */
  123. virtual void SetPeriodicAdvertisingEnable(uint8_t inst_id, uint8_t enable,
  124. MultiAdvCb cb) = 0;
  125. /* This function disable a Multi-ADV instance */
  126. virtual void Unregister(uint8_t inst_id) = 0;
  127. /* When resolving list is used, we need to suspend and resume all advertising
  128. * instances for the time of operation. Suspend() saves current state,
  129. * Resume() resumes the advertising.
  130. */
  131. virtual void Suspend() = 0;
  132. virtual void Resume() = 0;
  133. /* This method is a member of BleAdvertiserHciInterface, and is exposed here
  134. * just for tests. It should never be called from upper layers*/
  135. virtual void OnAdvertisingSetTerminated(
  136. uint8_t status, uint8_t advertising_handle, uint16_t connection_handle,
  137. uint8_t num_completed_extended_adv_events) = 0;
  138. using GetAddressCallback =
  139. base::Callback<void(uint8_t /* address_type*/, RawAddress /*address*/)>;
  140. virtual void GetOwnAddress(uint8_t inst_id, GetAddressCallback cb) = 0;
  141. };
  142. #endif // BLE_ADVERTISER_H