• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines advertiser, including avertise data and callbacks, and advertiser functions.
21  *
22  * @since 6
23  */
24 
25 /**
26  * @file bluetooth_ble_advertiser.h
27  *
28  * @brief Advertiser common functions.
29  *
30  * @since 6
31  */
32 
33 #ifndef BLUETOOTH_BLE_ADVERTISER_H
34 #define BLUETOOTH_BLE_ADVERTISER_H
35 
36 #include "bluetooth_def.h"
37 #include "bluetooth_types.h"
38 
39 namespace OHOS {
40 namespace Bluetooth {
41 /**
42  * @brief Represents advertise data.
43  *
44  * @since 6
45  */
46 class BLUETOOTH_API BleAdvertiserData {
47 public:
48     /**
49      * @brief A constructor used to create a <b>BleAdvertiserData</b> instance.
50      *
51      * @since 6
52      */
53     BleAdvertiserData();
54 
55     /**
56      * @brief A destructor used to delete the <b>BleAdvertiserData</b> instance.
57      *
58      * @since 6
59      */
60     ~BleAdvertiserData();
61 
62     /**
63      * @brief Add manufacture data.
64      *
65      * @param manufacturerId Manufacture Id which addad data.
66      * @param data Manufacture data
67      * @since 6
68      */
69     void AddManufacturerData(uint16_t manufacturerId, const std::string &data);
70 
71     /**
72      * @brief Add service data.
73      *
74      * @param uuid Uuid of service data.
75      * @param serviceData Service data.
76      * @since 6
77      */
78     void AddServiceData(const ParcelUuid &uuid, const std::string &serviceData);
79 
80     /**
81      * @brief Add service uuid.
82      *
83      * @param serviceUuid Service uuid.
84      * @since 6
85      */
86     void AddServiceUuid(const ParcelUuid &serviceUuid);
87 
88     /**
89      * @brief Get manufacture data.
90      *
91      * @return Returns manufacture data.
92      * @since 6
93      */
94     std::map<uint16_t, std::string> GetManufacturerData() const;
95 
96     /**
97      * @brief Get service data.
98      *
99      * @return Returns service data.
100      * @since 6
101      */
102     std::map<ParcelUuid, std::string> GetServiceData() const;
103 
104     /**
105      * @brief Get service uuids.
106      *
107      * @return Returns service uuids.
108      * @since 6
109      */
110     std::vector<ParcelUuid> GetServiceUuids() const;
111 
112     /**
113      * @brief Get advertiser flag.
114      *
115      * @return Returns advertiser flag.
116      * @since 6
117      */
118     uint8_t GetAdvFlag() const;
119 
120     /**
121      * @brief Set advertiser flag.
122      *
123      * @param flag Advertiser flag.
124      * @return Returns error flag.
125      * @since 6
126      */
127     void SetAdvFlag(uint8_t flag);
128 
129 private:
130     std::vector<ParcelUuid> serviceUuids_{};
131     std::map<uint16_t, std::string> manufacturerSpecificData_{};
132     std::map<ParcelUuid, std::string> serviceData_{};
133     uint8_t advFlag_ = BLE_ADV_FLAG_GEN_DISC;
134 };
135 
136 /**
137  * @brief Represents advertise settings.
138  *
139  * @since 6
140  */
141 class BLUETOOTH_API BleAdvertiserSettings {
142 public:
143     /**
144      * @brief A constructor used to create a <b>BleAdvertiseSettings</b> instance.
145      *
146      * @since 6
147      */
148     BleAdvertiserSettings();
149 
150     /**
151      * @brief A destructor used to delete the <b>BleAdvertiseSettings</b> instance.
152      *
153      * @since 6
154      */
155     ~BleAdvertiserSettings();
156 
157     /**
158      * @brief Check if device service is connctable.
159      *
160      * @return Returns <b>true</b> if device service is connctable;
161      *         returns <b>false</b> if device service is not connctable.
162      * @since 6
163      */
164     bool IsConnectable() const;
165 
166     /**
167      * @brief Check if advertiser is legacy mode.
168      *
169      * @return Returns <b>true</b> if advertiser is legacy mode;
170      *         returns <b>false</b> if advertiser is not legacy mode.
171      * @since 6
172      */
173     bool IsLegacyMode() const;
174 
175     /**
176      * @brief Set connectable.
177      *
178      * @param connectable Whether it is connectable.
179      * @since 6
180      */
181     void SetConnectable(bool connectable);
182 
183     /**
184      * @brief Set legacyMode.
185      *
186      * @param legacyMode Whether it is legacyMode.
187      * @since 6
188      */
189     void SetLegacyMode(bool legacyMode);
190 
191     /**
192      * @brief Get advertise interval.
193      *
194      * @return Returns advertise interval.
195      * @since 6
196      */
197     uint16_t GetInterval() const;
198 
199     /**
200      * @brief Get Tx power.
201      *
202      * @return Returns Tx power.
203      * @since 6
204      */
205     uint8_t GetTxPower() const;
206 
207     /**
208      * @brief Set advertise interval.
209      *
210      * @param interval Advertise interval.
211      * @since 6
212      */
213     void SetInterval(uint16_t interval);
214 
215     /**
216      * @brief Set Tx power.
217      *
218      * @param txPower Tx power.
219      * @since 6
220      */
221     void SetTxPower(uint8_t txPower);
222 
223     /**
224      * @brief Get primary phy.
225      *
226      * @return Returns primary phy.
227      * @since 6
228      */
229     int GetPrimaryPhy() const;
230 
231     /**
232      * @brief Set primary phy.
233      *
234      * @param primaryPhy Primary phy.
235      * @since 6
236      */
237     void SetPrimaryPhy(int primaryPhy);
238 
239     /**
240      * @brief Get second phy.
241      *
242      * @return Returns primary phy.
243      * @since 6
244      */
245     int GetSecondaryPhy() const;
246 
247     /**
248      * @brief Set second phy.
249      *
250      * @param secondaryPhy Second phy.
251      * @since 6
252      */
253     void SetSecondaryPhy(int secondaryPhy);
254 
255 private:
256     bool connectable_ = true;
257     bool legacyMode_ = true;
258     uint16_t interval_ = BLE_ADV_DEFAULT_INTERVAL;
259     uint8_t txPower_ = BLE_ADV_TX_POWER_LOW;
260     int primaryPhy_ = BLE_ADVERTISEMENT_PHY_1M;
261     int secondaryPhy_ = BLE_ADVERTISEMENT_PHY_1M;
262 };
263 
264 /**
265  * @brief Represents advertise callback.
266  *
267  * @since 6
268  */
269 class BleAdvertiseCallback {
270 public:
271     /**
272      * @brief A destructor used to delete the <b>BleAdvertiseCallback</b> instance.
273      *
274      * @since 6
275      */
276     virtual ~BleAdvertiseCallback() = default;
277 
278     /**
279      * @brief Start advertising result event callback.
280      *
281      * @param result Start advertising result.
282      * @since 6
283      */
284     virtual void OnStartResultEvent(int result) = 0;
285 };
286 
287 /**
288  * @brief Represents advertiser.
289  *
290  * @since 6
291  */
292 class BLUETOOTH_API BleAdvertiser {
293 public:
294     /**
295      * @brief A constructor used to create a <b>BleAdvertiser</b> instance.
296      *
297      * @since 6
298      */
299     explicit BleAdvertiser();
300 
301     /**
302      * @brief A destructor used to delete the <b>BleAdvertiser</b> instance.
303      *
304      * @since 6
305      */
306     ~BleAdvertiser();
307 
308     /**
309      * @brief Start advertising.
310      *
311      * @param settings Advertise settings.
312      * @param advData Advertise data.
313      * @param scanResponse Scan response.
314      * @param callback Advertise callback.
315      * @since 6
316      */
317     int StartAdvertising(const BleAdvertiserSettings &settings, const BleAdvertiserData &advData,
318         const BleAdvertiserData &scanResponse, BleAdvertiseCallback &callback);
319 
320     /**
321      * @brief Start advertising.
322      *
323      * @param settings Advertise settings.
324      * @param advData Advertise data.
325      * @param scanResponse Scan response.
326      * @param callback Advertise callback.
327      * @since 6
328      */
329     int StartAdvertising(const BleAdvertiserSettings &settings, const std::vector<uint8_t> &advData,
330         const std::vector<uint8_t> &scanResponse, BleAdvertiseCallback &callback);
331 
332     int StopAdvertising(BleAdvertiseCallback &callback);
333 
334     /**
335      * @brief Cleans up advertisers.
336      *
337      * @param callback Advertise callback.
338      * @since 6
339      */
340     void Close(BleAdvertiseCallback &callback);
341 
342 private:
343     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleAdvertiser);
344     BLUETOOTH_DECLARE_IMPL();
345 };
346 }  // namespace Bluetooth
347 }  // namespace OHOS
348 #endif