• 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 #ifndef OHOS_BT_GAP_H
16 #define OHOS_BT_GAP_H
17 
18 #include "ohos_bt_def.h"
19 #include <stdbool.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define OHOS_BD_NAME_LEN 32
26 typedef struct {
27     BdAddr addr;
28     unsigned char deviceName[OHOS_BD_NAME_LEN]; /* Name of the device record, must be in UTF-8 */
29 } PairedDeviceInfo;
30 
31 /* Bluetooth stack state */
32 typedef enum {
33     OHOS_GAP_STATE_TURNING_ON = 0x0,
34     OHOS_GAP_STATE_TURN_ON,
35     OHOS_GAP_STATE_TURNING_OFF,
36     OHOS_GAP_STATE_TURN_OFF
37 } BtStackState;
38 
39 /* Bluetooth ACL connection state */
40 typedef enum {
41     OHOS_GAP_ACL_STATE_CONNECTED,
42     OHOS_GAP_ACL_STATE_DISCONNECTED,
43     OHOS_GAP_ACL_STATE_LE_CONNECTED,
44     OHOS_GAP_ACL_STATE_LE_DISCONNECTED
45 } GapAclState;
46 
47 /* bondable mode */
48 typedef enum {
49     OHOS_GAP_BONDABLE_MODE_OFF = 0x00,
50     OHOS_GAP_BONDABLE_MODE_ON = 0x01
51 } GapBtBondableMode;
52 
53 typedef enum {
54     OHOS_GAP_ACCESS_UNKNOWN,
55     OHOS_GAP_ACCESS_ALLOWED,
56     OHOS_GAP_ACCESS_FORBIDDEN,
57 } GapBtPermissionType;
58 
59 typedef enum {
60     OHOS_GAP_SCAN_MODE_NONE = 0x00,
61     OHOS_GAP_SCAN_MODE_CONNECTABLE,
62     OHOS_GAP_SCAN_MODE_GENERAL_DISCOVERABLE,
63     OHOS_GAP_SCAN_MODE_LIMITED_DISCOVERABLE,
64     OHOS_GAP_SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE,
65     OHOS_GAP_SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE
66 } GapBtScanMode;
67 
68 typedef enum {
69     OHOS_PROFILE_HEADSET = 0x0,
70     OHOS_PROFILE_A2DP,
71     OHOS_PROFILE_OPP,
72     OHOS_PROFILE_HID,
73     OHOS_PROFILE_PANU,
74     OHOS_PROFILE_NAP,
75     OHOS_PROFILE_HFP
76 } GapBtProfileType;
77 
78 typedef enum {
79     OHOS_GAP_PAIR_NONE = 0x01,
80     OHOS_GAP_PAIR_PAIRING = 0x02,
81     OHOS_GAP_PAIR_PAIRED = 0x03
82 } GapBtPairedState;
83 
84 /**
85  * @brief Enable ble.
86  *
87  * @return Returns <b>true</b> if the operation is accepted;
88  *         returns <b>false</b> if the operation is rejected.
89  */
90 bool EnableBle(void);
91 
92 /**
93  * @brief Disable ble.
94  *
95  * @return Returns <b>true</b> if the operation is accepted;
96  *         returns <b>false</b> if the operation is rejected.
97  */
98 bool DisableBle(void);
99 
100 /**
101  * @brief Enable classic.
102  *
103  * @return Returns <b>true</b> if the operation is accepted;
104  *         returns <b>false</b> if the operation is rejected.
105  */
106 bool EnableBt(void);
107 
108 /**
109  * @brief Disable classic.
110  *
111  * @return Returns <b>true</b> if the operation is accepted;
112  *         returns <b>false</b> if the operation is rejected.
113  */
114 bool DisableBt(void);
115 
116 /**
117  * @brief Get classic enable/disable state.
118  *
119  * @return Returns classic enable/disable state, {@link BtStackState}.
120  */
121 int GetBtState();
122 
123 /**
124  * @brief Get ble enable/disable state.
125  *
126  * @return Returns <b>true</b> if ble is enabled;
127  *         returns <b>false</b> if ble is not enabled.
128  */
129 bool IsBleEnabled();
130 
131 /**
132  * @brief Get local host bluetooth address.
133  *
134  * @return Returns <b>true</b> if the operation is accepted;
135  *         returns <b>false</b> if the operation is rejected.
136  */
137 bool GetLocalAddr(unsigned char *mac, unsigned int len);
138 
139 /**
140  * @brief Set local device name.
141  *
142  * @param localName Device name.
143  * @param length localName length, The maximum length of the name is {@link OHOS_BD_NAME_LEN}.
144  * @return Returns <b>true</b> if the operation is successful;
145  *         returns <b>false</b> if the operation fails.
146  */
147 bool SetLocalName(unsigned char *localName, unsigned char length);
148 
149 /**
150  * @brief Set device scan mode.
151  * @param mode Scan mode, see details {@link GapBtScanMode}.
152  * @param duration Scan time.
153  * @return Returns <b>true</b> if the operation is successful;
154  *         returns <b>false</b> if the operation fails.
155  */
156 bool SetBtScanMode(int mode, int duration);
157 
158 /**
159  * @brief Check device pair request reply.
160  *
161  * @param bdAddr device address.
162  * @param transport Transport type, details see {@link BtTransportId}
163  * @param accept Set gap accept flag.
164  * @return Returns <b>true</b> if the operation is successful;
165  *         returns <b>false</b> if the operation fails.
166  */
167 bool PairRequestReply(const BdAddr *bdAddr, int transport, bool accept);
168 
169 /**
170  * @brief Set device pairing confirmation.
171  *
172  * @param bdAddr device address.
173  * @param transport Transport type, details see {@link BtTransportId}
174  * @param accept Set gap accept flag.
175  * @return Returns <b>true</b> if the operation is successful;
176  *         returns <b>false</b> if the operation fails.
177  */
178 bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int transport, bool accept);
179 
180 /**
181  * @brief Gap state changed callback.
182  *
183  * @param transport Transport type when state change, details see {@link BtTransportId}
184  * @param status Change to the new state, details see {@link BtStackState}.
185  */
186 typedef void (*GapStateChangedCallback)(const int transport, const int status);
187 
188 /**
189  * @brief ACL state changed callback.
190  *
191  * @param bdAddr device address.
192  * @param state details of acl state changed.
193  * @param reason clarify detals of REASON CODE from stack - If possible, suggest using the standard HCI error code.
194  */
195 typedef void (*GapAclStateChangedCallback)(const BdAddr *bdAddr, GapAclState state, unsigned int reason);
196 
197 /**
198  * @brief Discovery state changed callback.
199  *
200  * @param status DISCOVERY_STARTED/DISCOVERYING/DISCOVERY_STOPED
201  */
202 typedef void (*GapDiscoveryStateChangedCallback)(int status);
203 
204 /**
205  * @brief Discovery state changed observer.
206  *
207  * @param status Device discovery status.
208  */
209 typedef void (*GapDiscoveryResultCallback)(const BdAddr *bdAddr);
210 
211 /**
212  * @brief Pair request observer.
213  *
214  * @param bdAddr Remote device address.
215  * @param transport Transport type, details see {@link BtTransportId}
216  */
217 typedef void (*GapPairRequestedCallback)(const BdAddr *bdAddr, int transport);
218 
219 /**
220  * @brief Pair confirmed observer.
221  *
222  * @param bdAddr Remote device address.
223  * @param transport Transport type, details see {@link BtTransportId}
224  * @param reqType Pair type.
225  * @param number Paired passkey.
226  */
227 typedef void (*GapPairConfirmedCallback)(const BdAddr *bdAddr, int transport, int reqType, int number);
228 
229 /**
230  * @brief Scan mode changed observer.
231  *
232  * @param mode Device scan mode.
233  */
234 typedef void (*GapScanModeChangedCallback)(int mode);
235 
236 /**
237  * @brief Device name changed observer.
238  *
239  * @param deviceName Device name.
240  */
241 typedef void (*GapLocalDeviceNameChangedCallback)(const unsigned char *deviceName, unsigned char length);
242 
243 /**
244  * @brief Device address changed observer.
245  *
246  * @param bdAddr Device address.
247  */
248 typedef void (*GapLocalDeviceAddrChangedCallback)(const BdAddr *bdAddr);
249 
250 /**
251  * @brief Pair status changed observer.
252  *
253  * @param bdAddr Device address.
254  * @param status Remote device pair status.
255  */
256 typedef void (*GapPairStatusChangedCallback)(const BdAddr *bdAddr, int status);
257 
258 /**
259  * @brief Remote uuid changed observer.
260  *
261  * @param bdAddr Device address.
262  * @param uuid Remote device uuids.
263  */
264 typedef void (*GapRemoteUuidChangedCallback)(const BdAddr *bdAddr, BtUuid uuid);
265 
266 /**
267  * @brief Remote name changed observer.
268  *
269  * @param bdAddr Device address.
270  * @param deviceName Remote device name.
271  */
272 typedef void (*GapRemoteNameChangedCallback)(const BdAddr *bdAddr,
273     const unsigned char *deviceName, unsigned char length);
274 
275 /**
276  * @brief Remote alias changed observer.
277  *
278  * @param bdAddr Device address.
279  * @param alias Remote device alias.
280  */
281 typedef void (*GapRemoteAliasChangedCallback)(const BdAddr *bdAddr, const unsigned char *alias, unsigned char length);
282 
283 /**
284  * @brief Remote cod changed observer.
285  *
286  * @param bdAddr Device address.
287  * @param cod Remote device cod.
288  */
289 typedef void (*GapRemoteCodChangedCallback)(const BdAddr *bdAddr, int cod);
290 
291 /**
292  * @brief Remote battery level changed observer.
293  *
294  * @param bdAddr Device address.
295  * @param cod Remote device battery Level.
296  */
297 typedef void (*GapRemoteBatteryLevelChangedCallback)(const BdAddr *bdAddr, int batteryLevel);
298 
299 /**
300  * @brief Remote rssi event observer.
301  *
302  * @param bdAddr Device address.
303  * @param rssi Remote device rssi.
304  * @param status Read status.
305  */
306 typedef void (*GapReadRemoteRssiEventCallback)(const BdAddr *bdAddr, int rssi, int status);
307 
308 /**
309  * @brief Query application whether to accept the connection.
310  *
311  * @param bdAddr Device address.
312  * @param res Application decision, 0 is reject, 1 is accept.
313  */
314 typedef void (*GapIsAcceptConnOnSafeModeCallback)(const BdAddr *bdAddr, bool *res);
315 
316 /**
317  * @brief Defines callbacks for the GAP
318  */
319 typedef struct {
320     GapStateChangedCallback stateChangeCallback;
321     GapAclStateChangedCallback aclStateChangedCallbak;
322     GapScanModeChangedCallback scanModeChangedCallback;
323     GapPairStatusChangedCallback pairStatusChangedCallback;
324     GapLocalDeviceNameChangedCallback deviceNameChangedCallback;
325     GapLocalDeviceAddrChangedCallback deviceAddrChangedCallback;
326     GapPairRequestedCallback pairRequestedCallback;
327     GapPairConfirmedCallback pairConfiremedCallback;
328     GapDiscoveryStateChangedCallback discoveryStateChangedCallback;
329     GapDiscoveryResultCallback discoveryResultCallback;
330     GapRemoteUuidChangedCallback remoteUuidChangedCallback;
331     GapRemoteNameChangedCallback remoteNameChangedCallback;
332     GapRemoteAliasChangedCallback remoteAliasChangedCallback;
333     GapRemoteCodChangedCallback remoteCodChangedCallback;
334     GapRemoteBatteryLevelChangedCallback remoteBatteryLevelChangedCallback;
335     GapReadRemoteRssiEventCallback readRemoteRssiEventCallback;
336     GapIsAcceptConnOnSafeModeCallback isAcceptConnOnSafeModeCallback;
337 } BtGapCallBacks;
338 
339 /**
340  * @brief Registers GAP callbacks.
341  *
342  * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the callbacks are registered;
343  * returns an error code defined in {@link BtStatus} otherwise.
344  */
345 int GapRegisterCallbacks(BtGapCallBacks *func);
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 #endif