1 /*
2  * Copyright (C) 2016 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 #ifndef ANDROID_INCLUDE_BLE_ADVERTISER_H
18 #define ANDROID_INCLUDE_BLE_ADVERTISER_H
19 
20 #include <base/functional/callback_forward.h>
21 #include <raw_address.h>
22 #include <stdint.h>
23 
24 #include <vector>
25 
26 #include "bt_common_types.h"
27 #include "bt_gatt_types.h"
28 
29 constexpr uint8_t kAdvertiserClientIdJni = 0xff;
30 constexpr uint8_t kAdvertiserClientIdLeAudio = 0x1;
31 
32 struct AdvertiseParameters {
33   uint16_t advertising_event_properties;
34   uint32_t min_interval;
35   uint32_t max_interval;
36   uint8_t channel_map;
37   int8_t tx_power;
38   uint8_t primary_advertising_phy;
39   uint8_t secondary_advertising_phy;
40   uint8_t scan_request_notification_enable;
41   int8_t own_address_type;
42 };
43 
44 struct PeriodicAdvertisingParameters {
45   bool enable;
46   bool include_adi;
47   uint16_t min_interval;
48   uint16_t max_interval;
49   uint16_t periodic_advertising_properties;
50 };
51 
52 /**
53  * LE Advertising related callbacks invoked from from the Bluetooth native stack
54  * All callbacks are invoked on the JNI thread
55  */
56 class AdvertisingCallbacks {
57  public:
58   virtual ~AdvertisingCallbacks() = default;
59   virtual void OnAdvertisingSetStarted(int reg_id, uint8_t advertiser_id,
60                                        int8_t tx_power, uint8_t status) = 0;
61   virtual void OnAdvertisingEnabled(uint8_t advertiser_id, bool enable,
62                                     uint8_t status) = 0;
63   virtual void OnAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) = 0;
64   virtual void OnScanResponseDataSet(uint8_t advertiser_id, uint8_t status) = 0;
65   virtual void OnAdvertisingParametersUpdated(uint8_t advertiser_id,
66                                               int8_t tx_power,
67                                               uint8_t status) = 0;
68   virtual void OnPeriodicAdvertisingParametersUpdated(uint8_t advertiser_id,
69                                                       uint8_t status) = 0;
70   virtual void OnPeriodicAdvertisingDataSet(uint8_t advertiser_id,
71                                             uint8_t status) = 0;
72   virtual void OnPeriodicAdvertisingEnabled(uint8_t advertiser_id, bool enable,
73                                             uint8_t status) = 0;
74   virtual void OnOwnAddressRead(uint8_t advertiser_id, uint8_t address_type,
75                                 RawAddress address) = 0;
76 };
77 
78 class BleAdvertiserInterface {
79  public:
80   virtual ~BleAdvertiserInterface() = default;
81 
82   /** Callback invoked when multi-adv operation has completed */
83   using StatusCallback = base::Callback<void(uint8_t /* status */)>;
84   using IdStatusCallback =
85       base::Callback<void(uint8_t /* advertiser_id */, uint8_t /* status */)>;
86   using IdTxPowerStatusCallback =
87       base::Callback<void(uint8_t /* advertiser_id */, int8_t /* tx_power */,
88                           uint8_t /* status */)>;
89   using ParametersCallback =
90       base::Callback<void(uint8_t /* status */, int8_t /* tx_power */)>;
91 
92   /** Registers an advertiser with the stack */
93   virtual void RegisterAdvertiser(IdStatusCallback) = 0;
94 
95   using GetAddressCallback =
96       base::Callback<void(uint8_t /* address_type*/, RawAddress /*address*/)>;
97   virtual void GetOwnAddress(uint8_t advertiser_id, GetAddressCallback cb) = 0;
98 
99   /* Set the parameters as per spec, user manual specified values */
100   virtual void SetParameters(uint8_t advertiser_id, AdvertiseParameters params,
101                              ParametersCallback cb) = 0;
102 
103   /* Setup the data */
104   virtual void SetData(int advertiser_id, bool set_scan_rsp,
105                        std::vector<uint8_t> data, StatusCallback cb) = 0;
106 
107   /* Enable the advertising instance */
108   virtual void Enable(uint8_t advertiser_id, bool enable, StatusCallback cb,
109                       uint16_t duration, uint8_t maxExtAdvEvents,
110                       StatusCallback timeout_cb) = 0;
111 
112   /*  Unregisters an advertiser */
113   virtual void Unregister(uint8_t advertiser_id) = 0;
114 
115   virtual void StartAdvertising(uint8_t advertiser_id, StatusCallback cb,
116                                 AdvertiseParameters params,
117                                 std::vector<uint8_t> advertise_data,
118                                 std::vector<uint8_t> scan_response_data,
119                                 int timeout_s, StatusCallback timeout_cb) = 0;
120 
121   /** Start the advertising set. This include registering, setting all
122    * parameters and data, and enabling it. |register_cb| is called when the set
123    * is advertising. |timeout_cb| is called when the timeout_s have passed.
124    * |reg_id| is the callback id assigned from upper or native layer.
125    * |client_id| is the callbacks client id for jni or native layer.
126    */
127   virtual void StartAdvertisingSet(
128       uint8_t client_id, int reg_id, IdTxPowerStatusCallback register_cb,
129       AdvertiseParameters params, std::vector<uint8_t> advertise_data,
130       std::vector<uint8_t> scan_response_data,
131       PeriodicAdvertisingParameters periodic_params,
132       std::vector<uint8_t> periodic_data, uint16_t duration,
133       uint8_t maxExtAdvEvents, IdStatusCallback timeout_cb) = 0;
134 
135   virtual void SetPeriodicAdvertisingParameters(
136       int advertiser_id, PeriodicAdvertisingParameters parameters,
137       StatusCallback cb) = 0;
138 
139   virtual void SetPeriodicAdvertisingData(int advertiser_id,
140                                           std::vector<uint8_t> data,
141                                           StatusCallback cb) = 0;
142 
143   virtual void SetPeriodicAdvertisingEnable(int advertiser_id, bool enable,
144                                             bool include_adi,
145                                             StatusCallback cb) = 0;
146   virtual void RegisterCallbacks(AdvertisingCallbacks* callbacks) = 0;
147   virtual void RegisterCallbacksNative(AdvertisingCallbacks* callbacks,
148                                        uint8_t client_id) = 0;
149 };
150 
151 #endif /* ANDROID_INCLUDE_BLE_ADVERTISER_H */
152