1/******************************************************************************
2 *
3 *  Copyright (C) 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
19#ifndef BLE_ADVERTISER_H
20#define BLE_ADVERTISER_H
21
22#include <base/bind.h>
23#include <vector>
24#include "btm_ble_api.h"
25
26#define BTM_BLE_MULTI_ADV_SUCCESS 0
27#define BTM_BLE_MULTI_ADV_FAILURE 1
28#define ADVERTISE_FAILED_TOO_MANY_ADVERTISERS 0x02
29
30using MultiAdvCb = base::Callback<void(uint8_t /* status */)>;
31using ParametersCb =
32    base::Callback<void(uint8_t /* status */, int8_t /* tx_power */)>;
33
34// methods we must have defined
35void btm_ble_update_dmt_flag_bits(uint8_t* flag_value,
36                                  const uint16_t connect_mode,
37                                  const uint16_t disc_mode);
38void btm_acl_update_conn_addr(uint8_t conn_handle, BD_ADDR address);
39
40// methods we expose to c code:
41void btm_ble_multi_adv_cleanup(void);
42void btm_ble_multi_adv_init();
43
44typedef struct {
45  uint16_t advertising_event_properties;
46  uint32_t adv_int_min;
47  uint32_t adv_int_max;
48  tBTM_BLE_ADV_CHNL_MAP channel_map;
49  tBTM_BLE_AFP adv_filter_policy;
50  int8_t tx_power;
51  uint8_t primary_advertising_phy;
52  uint8_t secondary_advertising_phy;
53  uint8_t scan_request_notification_enable;
54} tBTM_BLE_ADV_PARAMS;
55
56typedef struct {
57  uint8_t enable;
58  uint16_t min_interval;
59  uint16_t max_interval;
60  uint16_t periodic_advertising_properties;
61} tBLE_PERIODIC_ADV_PARAMS;
62
63class BleAdvertiserHciInterface;
64
65class BleAdvertisingManager {
66 public:
67  virtual ~BleAdvertisingManager() = default;
68
69  static const uint16_t advertising_prop_legacy_connectable = 0x0011;
70  static const uint16_t advertising_prop_legacy_non_connectable = 0x0010;
71
72  static void Initialize(BleAdvertiserHciInterface* interface);
73  static void CleanUp();
74  static BleAdvertisingManager* Get();
75
76  /* Register an advertising instance, status will be returned in |cb|
77   * callback, with assigned id, if operation succeeds. Instance is freed when
78   * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
79   * of the operations fails.
80   * The instance will have data set to |advertise_data|, scan response set to
81   * |scan_response_data|, and will be enabled.
82   */
83  virtual void StartAdvertising(uint8_t advertiser_id, MultiAdvCb cb,
84                                tBTM_BLE_ADV_PARAMS* params,
85                                std::vector<uint8_t> advertise_data,
86                                std::vector<uint8_t> scan_response_data,
87                                int duration, MultiAdvCb timeout_cb) = 0;
88
89  /* Register an advertising instance, status will be returned in |cb|
90   * callback, with assigned id, if operation succeeds. Instance is freed when
91   * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
92   * of the operations fails.
93   * The instance will have data set to |advertise_data|, scan response set to
94   * |scan_response_data|, periodic data set to |periodic_data| and will be
95   * enabled.
96   */
97  virtual void StartAdvertisingSet(
98      base::Callback<void(uint8_t /* inst_id */, int8_t /* tx_power */,
99                          uint8_t /* status */)>
100          cb,
101      tBTM_BLE_ADV_PARAMS* params, std::vector<uint8_t> advertise_data,
102      std::vector<uint8_t> scan_response_data,
103      tBLE_PERIODIC_ADV_PARAMS* periodic_params,
104      std::vector<uint8_t> periodic_data, uint16_t duration,
105      uint8_t maxExtAdvEvents,
106      base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>
107          timeout_cb) = 0;
108
109  /* Register an advertising instance, status will be returned in |cb|
110  * callback, with assigned id, if operation succeeds. Instance is freed when
111  * advertising is disabled by calling |BTM_BleDisableAdvInstance|, or when any
112  * of the operations fails. */
113  virtual void RegisterAdvertiser(
114      base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>) = 0;
115
116  /* This function enables/disables an advertising instance. Operation status is
117   * returned in |cb| */
118  virtual void Enable(uint8_t inst_id, bool enable, MultiAdvCb cb,
119                      uint16_t duration, uint8_t maxExtAdvEvents,
120                      MultiAdvCb timeout_cb) = 0;
121
122  /* This function update a Multi-ADV instance with the specififed adv
123   * parameters. */
124  virtual void SetParameters(uint8_t inst_id, tBTM_BLE_ADV_PARAMS* p_params,
125                             ParametersCb cb) = 0;
126
127  /* This function configure a Multi-ADV instance with the specified adv data or
128   * scan response data.*/
129  virtual void SetData(uint8_t inst_id, bool is_scan_rsp,
130                       std::vector<uint8_t> data, MultiAdvCb cb) = 0;
131
132  /* This function configure instance with the specified periodic parameters */
133  virtual void SetPeriodicAdvertisingParameters(
134      uint8_t inst_id, tBLE_PERIODIC_ADV_PARAMS* params, MultiAdvCb cb) = 0;
135
136  /* This function configure instance with the specified periodic data */
137  virtual void SetPeriodicAdvertisingData(uint8_t inst_id,
138                                          std::vector<uint8_t> data,
139                                          MultiAdvCb cb) = 0;
140
141  /* This function enables/disables periodic advertising on selected instance */
142  virtual void SetPeriodicAdvertisingEnable(uint8_t inst_id, uint8_t enable,
143                                            MultiAdvCb cb) = 0;
144
145  /*  This function disable a Multi-ADV instance */
146  virtual void Unregister(uint8_t inst_id) = 0;
147
148  /* This method is a member of BleAdvertiserHciInterface, and is exposed here
149   * just for tests. It should never be called from upper layers*/
150  virtual void OnAdvertisingSetTerminated(
151      uint8_t status, uint8_t advertising_handle, uint16_t connection_handle,
152      uint8_t num_completed_extended_adv_events) = 0;
153
154  using GetAddressCallback =
155      base::Callback<void(uint8_t /* address_type*/, bt_bdaddr_t /*address*/)>;
156  virtual void GetOwnAddress(uint8_t inst_id, GetAddressCallback cb) = 0;
157};
158
159#endif  // BLE_ADVERTISER_H
160