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