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