• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: ohos 蓝牙gap相关宏与接口定义
15  */
16 #ifndef OHOS_BT_GAP_H
17 #define OHOS_BT_GAP_H
18 
19 #include "ohos_bt_def.h"
20 
21 #define SERVICE_LIMITED_DISCOVERABILITY 0x002000
22 #define SERVICE_POSITIONING 0x010000
23 #define SERVICE_NETWORKING 0x020000
24 #define SERVICE_RENDER 0x040000
25 #define SERVICE_CAPTURE 0x080000
26 #define SERVICE_OBJECT_TRANSFER 0x100000
27 #define SERVICE_AUDIO 0x200000
28 #define SERVICE_TELEPHONY 0x400000
29 #define SERVICE_INFORMATION 0x800000
30 
31 #define OHOS_BD_NAME_LEN 32
32 typedef struct {
33     BdAddr addr;
34     unsigned char deviceName[OHOS_BD_NAME_LEN]; /* Name of the device record, must be in UTF-8 */
35 } PairedDeviceInfo;
36 
37 /* Bluetooth stack state */
38 typedef enum {
39     OHOS_GAP_STATE_TURNING_ON = 0x0,
40     OHOS_GAP_STATE_TURN_ON,
41     OHOS_GAP_STATE_TURNING_OFF,
42     OHOS_GAP_STATE_TURN_OFF
43 } BtStackState;
44 
45 /* Bluetooth ACL connection state */
46 typedef enum {
47     OHOS_GAP_ACL_STATE_CONNECTED,
48     OHOS_GAP_ACL_STATE_DISCONNECTED,
49     OHOS_GAP_ACL_STATE_LE_CONNECTED,
50     OHOS_GAP_ACL_STATE_LE_DISCONNECTED
51 } GapAclState;
52 
53 /* bondable mode */
54 typedef enum {
55     OHOS_GAP_BONDABLE_MODE_OFF = 0x00,
56     OHOS_GAP_BONDABLE_MODE_ON = 0x01
57 } GapBtBondableMode;
58 
59 typedef enum {
60     OHOS_GAP_ACCESS_UNKNOWN,
61     OHOS_GAP_ACCESS_ALLOWED,
62     OHOS_GAP_ACCESS_FORBIDDEN,
63 } GapBtPermissionType;
64 
65 typedef enum {
66     OHOS_GAP_SCAN_MODE_NONE = 0x00,
67     OHOS_GAP_SCAN_MODE_CONNECTABLE,
68     OHOS_GAP_SCAN_MODE_GENERAL_DISCOVERABLE,
69     OHOS_GAP_SCAN_MODE_LIMITED_DISCOVERABLE,
70     OHOS_GAP_SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE,
71     OHOS_GAP_SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE
72 } GapBtScanMode;
73 
74 typedef enum {
75     OHOS_PROFILE_HEADSET = 0x0,
76     OHOS_PROFILE_A2DP,
77     OHOS_PROFILE_OPP,
78     OHOS_PROFILE_HID,
79     OHOS_PROFILE_PANU,
80     OHOS_PROFILE_NAP,
81     OHOS_PROFILE_HFP
82 } GapBtProfileType;
83 
84 typedef enum {
85     OHOS_GAP_PAIR_NONE = 0x01,
86     OHOS_GAP_PAIR_PAIRING = 0x02,
87     OHOS_GAP_PAIR_PAIRED = 0x03
88 } GapBtPairedState;
89 
90 bool EnableBle(void);
91 
92 bool DisableBle(void);
93 
94 bool EnableBt(void);
95 
96 bool DisableBt(void);
97 
98 /**
99  *  @brief Get local host bluetooth address
100  *  @return @c Local host bluetooth address
101  */
102 BdAddr* GetLocalAddress(void);
103 
104 /**
105  *  @brief Get local host bluetooth name
106  *  @param localName actual receiving device name
107            length - localName length, initail set length to zero, and call this func to set real length
108  *  @return Local host bluetooth name
109  */
110 bool GetLocalName(unsigned char *localName, unsigned char *length);
111 
112 /**
113  * @brief Set local device name.
114  * @param localName Device name.
115           length device name length
116  * @return Returns <b>true</b> if the operation is successful;
117  *         returns <b>false</b> if the operation fails.
118  */
119 bool SetLocalName(unsigned char *localName, unsigned char length);
120 
121 /**
122  * @brief Factory reset bluetooth service.
123  * @return Returns <b>true</b> if the operation is successful;
124  *         returns <b>false</b> if the operation fails.
125  */
126 bool BluetoothFactoryReset(void);
127 
128 /**
129  * @brief Set device scan mode.
130  * @param mode Scan mode.
131  * @param duration Scan time, see details {@link GapBtScanMode}
132  * @return special mode
133  */
134 int GetBtScanMode(void);
135 
136 /**
137  * @brief Set device scan mode.
138  * @param mode Scan mode, see details {@link GapBtScanMode}.
139  * @param duration Scan time.
140  * @return Returns <b>true</b> if the operation is successful;
141  *         returns <b>false</b> if the operation fails.
142  */
143 bool SetBtScanMode(int mode, int duration);
144 
145 /*
146  * @brief Get paired devices.
147  * @param pairList - 按照maxPairNums申请的设备列表数组
148           maxPairNums - 指定需要获取的设备列表最大个数
149           realPairNums - 实际的配对设备列表个数
150  * @return Returns <b>true</b> if the operation is successful;
151  *         returns <b>false</b> if the operation fails.
152  */
153 bool GetPariedDevicesNum(unsigned int *number);
154 
155 bool GetPairedDevicesList(PairedDeviceInfo *devInfo, int *number);
156 
157 bool GetRemoteDevName(PairedDeviceInfo *devInfo);
158 
159 /**
160  * @brief Get device pair state.
161  * @param device Remote device.
162  * @return Returns device pair state. see detail {@link GapBtPairedState}
163  */
164 int GetPairState(void);
165 
166 /**
167  * @brief Remove pair.
168  * @param BdAddr Remote device address.
169  * @return Returns <b>true</b> if the operation is successful;
170  *         returns <b>false</b> if the operation fails.
171  */
172 bool RemovePair(const BdAddr addr);
173 
174 /**
175  * @brief Remove all pairs.
176  * @return Returns <b>true</b> if the operation is successful;
177  *         returns <b>false</b> if the operation fails.
178  */
179 bool RemoveAllPairs(void);
180 
181 /**
182  * @brief Read remote device rssi value.
183  *
184  * @param bdAddr device address.
185  * @param transport Transport type, details see {@link BtTransportId}
186  * @return Returns <b>true</b> if the operation is successful;
187  *         returns <b>false</b> if the operation fails.
188  */
189 bool ReadRemoteRssiValue(const BdAddr *bdAddr, int transport);
190 
191 /**
192  * @brief Check if device acl connected.
193  * @param addr device address.
194  * @return Returns <b>true</b> if device acl connected;
195  *         returns <b>false</b> if device does not acl connect.
196  */
197 bool IsAclConnected(BdAddr *addr);
198 
199 /**
200  * @brief disconnect remote device all profile.
201  * @param addr device address.
202  * @return Returns <b>true</b> if device acl connected;
203  *         returns <b>false</b> if device does not acl connect.
204  */
205 bool DisconnectRemoteDevice(BdAddr *addr);
206 
207 /*
208  * @brief connect remote device acl profile.
209  * @param: remote device address
210  * @return Returns <b>true</b> if device acl connected;
211  *         returns <b>false</b> if device does not acl connect.
212  */
213 bool ConnectRemoteDevice(BdAddr *addr);
214 
215 /**
216  * @brief Adapter state change function. 开关蓝牙的callback上报
217  * @param transport Transport type when state change.
218  *        OHOS_BT_TRANSPORT_BR_EDR : classic;
219  *        OHOS_BT_TRANSPORT_LE : ble.
220  *        detail see {@link BtTransportId}
221  * @param state Change to the new state
222  *        details see {@link BtStackState}.
223  */
224 typedef void (*GapStateChangedCallback)(const int transport, const int status);
225 
226 /**
227  * @brief ACL state change callback
228  * @param BtGapAclState - details of acl state changed
229  * @param reason - clarify detals of REASON CODE from stack - If possible, suggest using the standard HCI error code.
230  * @return void
231  */
232 typedef void (*GapAclStateChangedCallback)(const BdAddr *bdAddr, GapAclState state, unsigned int reason);
233 
234 /**
235  * @brief Discovery state changed observer.
236  * @param status DISCOVERY_STARTED/DISCOVERYING/DISCOVERY_STOPED
237  * @return void
238  */
239 typedef void (*GapDiscoveryStateChangedCallback)(int status);
240 
241 /**
242  * @brief Discovery state changed observer.
243  * @param status Device discovery status.
244  * @return void
245  */
246 typedef void (*GapDiscoveryResultCallback)(const BdAddr *bdAddr);
247 
248 /**
249  * @brief Pair request observer.
250  * @param bdAddr Remote device address.
251  * @return void
252  */
253 typedef void (*GapPairRequestedCallback)(const BdAddr *bdAddr);
254 
255 /**
256  * @brief Pair confirmed observer.
257  * @param BdAddr Remote device address.
258  * @param reqType Pair type.
259  * @param number Paired passkey.
260  * @return void
261  */
262 typedef void (*GapPairConfirmedCallback)(const BdAddr *bdAddr, int reqType, int number);
263 
264 /**
265  * @brief Scan mode changed observer.
266  * @param mode Device scan mode.
267  * @return void
268  */
269 typedef void (*GapScanModeChangedCallback)(int mode);
270 
271 /**
272  * @brief Device name changed observer.
273  * @param deviceName Device name.
274  * @return void
275  */
276 typedef void (*GapLocalDeviceNameChangedCallback)(const unsigned char *deviceName, unsigned char length);
277 
278 /**
279  * @brief Device address changed observer.
280  * @param bdAddr Device address.
281  * @return void
282  */
283 typedef void (*GapLocalDeviceAddrChangedCallback)(const BdAddr *bdAddr);
284 
285 /**
286  * @brief Pair status changed observer.
287  * @param bdAddr Device address.
288  * @param status Remote device pair status.
289  * @return void
290  */
291 typedef void (*GapPairStatusChangedCallback)(const BdAddr *bdAddr, int status);
292 
293 /**
294  * @brief Remote uuid changed observer.
295  * @param bdAddr Device address.
296  * @param uuid Remote device uuids.
297  * @return void
298  */
299 typedef void (*GapRemoteUuidChangedCallback)(const BdAddr *bdAddr, BtUuid uuid);
300 
301 /**
302  * @brief Remote name changed observer.
303  * @param bdAddr Device address.
304  * @param deviceName Remote device name.
305  * @return void
306  */
307 typedef void (*GapRemoteNameChangedCallback)(const BdAddr *bdAddr,
308     const unsigned char *deviceName, unsigned char length);
309 
310 /**
311  * @brief Remote alias changed observer.
312  * @param bdAddr Device address.
313  * @param alias Remote device alias.
314  * @return void
315  */
316 typedef void (*GapRemoteAliasChangedCallback)(const BdAddr *bdAddr, const unsigned char *alias, unsigned char length);
317 
318 /**
319  * @brief Remote cod changed observer.
320  * @param bdAddr Device address.
321  * @param cod Remote device cod.
322  * @return void
323  */
324 typedef void (*GapRemoteCodChangedCallback)(const BdAddr *bdAddr, int cod);
325 
326 /**
327  * @brief Remote battery level changed observer.
328  * @param bdAddr Device address.
329  * @param cod Remote device battery Level.
330  * @return void
331  */
332 typedef void (*GapRemoteBatteryLevelChangedCallback)(const BdAddr *bdAddr, int batteryLevel);
333 
334 /**
335  * @brief Remote rssi event observer.
336  * @param bdAddr Device address.
337  * @param rssi Remote device rssi.
338  * @param status Read status.
339  * @return void
340  */
341 typedef void (*GapReadRemoteRssiEventCallback)(const BdAddr *bdAddr, int rssi, int status);
342 
343 /**
344  * @brief Query application whether to accept the connection.
345  * @param bdAddr Device address.
346  * @param res Application decision, 0 is reject, 1 is accept.
347  * @return void
348  */
349 typedef void (*GapIsAcceptConnOnSafeModeCallback)(const BdAddr *bdAddr, bool *res);
350 
351 /**
352  * @brief GAP回调函数接口定义
353  */
354 typedef struct {
355     GapStateChangedCallback stateChangeCallback;
356     GapAclStateChangedCallback aclStateChangedCallbak;
357     GapScanModeChangedCallback scanModeChangedCallback;
358     GapPairStatusChangedCallback pairStatusChangedCallback;
359     GapLocalDeviceNameChangedCallback deviceNameChangedCallback;
360     GapLocalDeviceAddrChangedCallback deviceAddrChangedCallback;
361     GapPairRequestedCallback pairRequestedCallback;
362     GapPairConfirmedCallback pairConfiremedCallback;
363     GapDiscoveryStateChangedCallback discoveryStateChangedCallback;
364     GapDiscoveryResultCallback discoveryResultCallback;
365     GapRemoteUuidChangedCallback remoteUuidChangedCallback;
366     GapRemoteNameChangedCallback remoteNameChangedCallback;
367     GapRemoteAliasChangedCallback remoteAliasChangedCallback;
368     GapRemoteCodChangedCallback remoteCodChangedCallback;
369     GapRemoteBatteryLevelChangedCallback remoteBatteryLevelChangedCallback;
370     GapReadRemoteRssiEventCallback readRemoteRssiEventCallback;
371     GapIsAcceptConnOnSafeModeCallback isAcceptConnOnSafeModeCallback;
372 } BtGapCallBacks;
373 
374 /**
375  * @brief 注册GAP回调函数。
376  * @param func 回调函数原型,参考{@link BtGapCallBacks}。
377  * @return 成功返回{@link OHOS_BT_STATUS_SUCCESS},失败返回错误码,具体错误码含义参考{@link BtStatus}定义
378  */
379 int GapRegisterCallbacks(BtGapCallBacks *func);
380 
381 #endif