• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Hunan OpenValley Digital Industry Development 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 __BLEGAP_H__
16 #define __BLEGAP_H__
17 #include <stdint.h>
18 #include <string.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include "esp_bt.h"
22 #include "esp_gap_ble_api.h"
23 #include "esp_gatt_defs.h"
24 #include "esp_bt_main.h"
25 #include "esp_gatt_common_api.h"
26 #include "esp_log.h"
27 #include "ohos_bt_def.h"
28 
29 #define GattInterfaceType esp_gatt_if_t
30 #define BleGapParam esp_ble_gap_cb_param_t
31 #define BleGattcParam esp_ble_gattc_cb_param_t
32 #define BleScanParams esp_ble_scan_params_t
33 #define BT_DEBUG printf
34 
35 #define OHOS_GATT_PREP_WRITE_CANCEL 0x00
36 #define OHOS_GATT_PREP_WRITE_EXEC   0x01
37 #define OHOS_GATT_UUID_CHAR_CLIENT_CONFIG 0x2902
38 #define OHOS_GATT_UUID_CHAR_SRVR_CONFIG 0x2903
39 #define OHOS_GATT_IF_NONE    0xff
40 #define GattInterfaceType esp_gatt_if_t
41 #define BtUuids esp_bt_uuid_t
42 #define BleConnUpdateParams esp_ble_conn_update_params_t
43 #define BT_LOGE(format, ...) printf("[%s] " format "\r\n", __FUNCTION__, ##__VA_ARGS__)
44 
45 #define OHOS_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
46 #define OHOS_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
47 #define OHOS_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
48 #define OHOS_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
49 #define OHOS_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
50 #define OHOS_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00)
51 
52 /* 特征属性的定义 */
53 #define    OHOS_GATT_CHAR_PROP_BIT_BROADCAST    (1 << 0)
54 #define    OHOS_GATT_CHAR_PROP_BIT_READ   (1 << 1)
55 #define    OHOS_GATT_CHAR_PROP_BIT_WRITE_NR     (1 << 2)
56 #define    OHOS_GATT_CHAR_PROP_BIT_WRITE  (1 << 3)
57 #define    OHOS_GATT_CHAR_PROP_BIT_NOTIFY (1 << 4)
58 #define    OHOS_GATT_CHAR_PROP_BIT_INDICATE     (1 << 5)
59 #define    OHOS_GATT_CHAR_PROP_BIT_AUTH   (1 << 6)
60 #define    OHOS_GATT_CHAR_PROP_BIT_EXT_PROP     (1 << 7)
61 
62 typedef enum {
63     BT_ERROR = -1,
64     BT_SUCCESS = 0,
65     BT_USERAUTHID_ERROR = 1,
66     BT_DEVICETYPE_ERROR = 2,
67     BT_PARAMINPUT_ERROR = 3,
68 } BtError;
69 
70 typedef enum {
71     OHOS_BT_TRANSPORT_TYPE_AUTO = 0x0,
72     OHOS_BT_TRANSPORT_TYPE_LE,
73     OHOS_BT_TRANSPORT_TYPE_CLASSIC,
74 } BtTransportType;
75 
76 typedef enum {
77     OHOS_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT = 0,
78     OHOS_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT,
79     OHOS_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT,
80     OHOS_GAP_BLE_SCAN_RESULT_EVT,
81     OHOS_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT,
82     OHOS_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT,
83     OHOS_GAP_BLE_ADV_START_COMPLETE_EVT,
84     OHOS_GAP_BLE_SCAN_START_COMPLETE_EVT,
85     OHOS_GAP_BLE_AUTH_CMPL_EVT = 8,
86     OHOS_GAP_BLE_KEY_EVT,
87     OHOS_GAP_BLE_SEC_REQ_EVT,
88     OHOS_GAP_BLE_PASSKEY_NOTIF_EVT,
89     OHOS_GAP_BLE_PASSKEY_REQ_EVT,
90     OHOS_GAP_BLE_OOB_REQ_EVT,
91     OHOS_GAP_BLE_LOCAL_IR_EVT,
92     OHOS_GAP_BLE_LOCAL_ER_EVT,
93     OHOS_GAP_BLE_NC_REQ_EVT,
94     OHOS_GAP_BLE_ADV_STOP_COMPLETE_EVT,
95     OHOS_GAP_BLE_SCAN_STOP_COMPLETE_EVT,
96     OHOS_GAP_BLE_SET_STATIC_RAND_ADDR_EVT = 19,
97     OHOS_GAP_BLE_UPDATE_CONN_PARAMS_EVT,
98     OHOS_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT,
99     OHOS_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT,
100     OHOS_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT,
101     OHOS_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT,
102     OHOS_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT,
103     OHOS_GAP_BLE_READ_RSSI_COMPLETE_EVT,
104     OHOS_GAP_BLE_UPDATE_WHITELIST_COMPLETE_EVT,
105     OHOS_GAP_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_COMPLETE_EVT,
106     OHOS_GAP_BLE_SET_CHANNELS_EVT = 29,
107     OHOS_GAP_BLE_EVT_MAX,
108 } GapBleCallbackEvent;
109 
110 typedef enum {
111     OHOS_GAP_SEARCH_INQ_RES_EVT = 0, /*!< Inquiry result for a peer device. */
112     OHOS_GAP_SEARCH_INQ_CMPL_EVT = 1, /*!< Inquiry complete. */
113     OHOS_GAP_SEARCH_DISC_RES_EVT = 2, /*!< Discovery result for a peer device. */
114     OHOS_GAP_SEARCH_DISC_BLE_RES_EVT = 3, /*!< Discovery result for BL */
115     OHOS_GAP_SEARCH_DISC_CMPL_EVT = 4, /*!< Discovery complete. */
116     OHOS_GAP_SEARCH_DI_DISC_CMPL_EVT = 5, /*!< Discovery complete. */
117     OHOS_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT = 6, /*!< Search cancelled */
118     OHOS_GAP_SEARCH_INQ_DISCARD_NUM_EVT = 7, /*!< The number of pkt discarded by flow control */
119 } GapSearchEvent;
120 
121 /// The type of advertising data(not adv_type)
122 typedef enum {
123     OHOS_BLE_AD_TYPE_FLAG   = 0x01, /* relate to BTM_BLE_AD_TYPE_FLAG */
124     OHOS_BLE_AD_TYPE_16SRV_PART   = 0x02, /* relate to BTM_BLE_AD_TYPE_16SRV_PAR */
125     OHOS_BLE_AD_TYPE_16SRV_CMPL   = 0x03, /* relate to BTM_BLE_AD_TYPE_16SRV_CMPL */
126     OHOS_BLE_AD_TYPE_32SRV_PART   = 0x04, /* relate to BTM_BLE_AD_TYPE_32SRV_PART */
127     OHOS_BLE_AD_TYPE_32SRV_CMPL   = 0x05, /* relate to BTM_BLE_AD_TYPE_32SRV_CMPL */
128     OHOS_BLE_AD_TYPE_128SRV_PART  = 0x06, /* relate to BTM_BLE_AD_TYPE_128SRV_PART */
129     OHOS_BLE_AD_TYPE_128SRV_CMPL  = 0x07, /* relate to BTM_BLE_AD_TYPE_128SRV_CMPL */
130     OHOS_BLE_AD_TYPE_NAME_SHORT   = 0x08, /* relate to BTM_BLE_AD_TYPE_NAME_SHORT */
131     OHOS_BLE_AD_TYPE_NAME_CMPL    = 0x09, /* relate to BTM_BLE_AD_TYPE_NAME_CMPL */
132     OHOS_BLE_AD_TYPE_TX_PWR = 0x0A, /* relate to BTM_BLE_AD_TYPE_TX_PWR */
133     OHOS_BLE_AD_TYPE_DEV_CLASS    = 0x0D, /* relate to BTM_BLE_AD_TYPE_DEV_CLASS */
134     OHOS_BLE_AD_TYPE_SM_TK  = 0x10, /* relate to BTM_BLE_AD_TYPE_SM_TK */
135     OHOS_BLE_AD_TYPE_SM_OOB_FLAG  = 0x11, /* relate to BTM_BLE_AD_TYPE_SM_OOB_FLAG */
136     OHOS_BLE_AD_TYPE_INT_RANGE    = 0x12, /* relate to BTM_BLE_AD_TYPE_INT_RANGE */
137     OHOS_BLE_AD_TYPE_SOL_SRV_UUID = 0x14, /* relate to BTM_BLE_AD_TYPE_SOL_SRV_UUID */
138     OHOS_BLE_AD_TYPE_128SOL_SRV_UUID    = 0x15, /* relate to BTM_BLE_AD_TYPE_128SOL_SRV_UUID */
139     OHOS_BLE_AD_TYPE_SERVICE_DATA = 0x16, /* relate to BTM_BLE_AD_TYPE_SERVICE_DATA */
140     OHOS_BLE_AD_TYPE_PUBLIC_TARGET = 0x17, /* relate to BTM_BLE_AD_TYPE_PUBLIC_TARGET */
141     OHOS_BLE_AD_TYPE_RANDOM_TARGET = 0x18, /* relate to BTM_BLE_AD_TYPE_RANDOM_TARGET */
142     OHOS_BLE_AD_TYPE_APPEARANCE   = 0x19, /* relate to BTM_BLE_AD_TYPE_APPEARANCE */
143     OHOS_BLE_AD_TYPE_ADV_INT = 0x1A, /* relate to BTM_BLE_AD_TYPE_ADV_INT */
144     OHOS_BLE_AD_TYPE_LE_DEV_ADDR  = 0x1b, /* relate to BTM_BLE_AD_TYPE_LE_DEV_ADDR */
145     OHOS_BLE_AD_TYPE_LE_ROLE = 0x1c, /* relate to BTM_BLE_AD_TYPE_LE_ROLE */
146     OHOS_BLE_AD_TYPE_SPAIR_C256   = 0x1d, /* relate to BTM_BLE_AD_TYPE_SPAIR_C256 */
147     OHOS_BLE_AD_TYPE_SPAIR_R256   = 0x1e, /* relate to BTM_BLE_AD_TYPE_SPAIR_R256 */
148     OHOS_BLE_AD_TYPE_32SOL_SRV_UUID     = 0x1f, /* relate to BTM_BLE_AD_TYPE_32SOL_SRV_UUID */
149     OHOS_BLE_AD_TYPE_32SERVICE_DATA     = 0x20, /* relate to BTM_BLE_AD_TYPE_32SERVICE_DATA */
150     OHOS_BLE_AD_TYPE_128SERVICE_DATA    = 0x21, /* relate to BTM_BLE_AD_TYPE_128SERVICE_DATA */
151     OHOS_BLE_AD_TYPE_LE_SECURE_CONFIRM  = 0x22, /* relate to BTM_BLE_AD_TYPE_LE_SECURE_CONFIRM */
152     OHOS_BLE_AD_TYPE_LE_SECURE_RANDOM   = 0x23, /* relate to BTM_BLE_AD_TYPE_LE_SECURE_RANDOM */
153     OHOS_BLE_AD_TYPE_URI    = 0x24, /* relate to BTM_BLE_AD_TYPE_URI */
154     OHOS_BLE_AD_TYPE_INDOOR_POSITION    = 0x25, /* relate to BTM_BLE_AD_TYPE_INDOOR_POSITION */
155     OHOS_BLE_AD_TYPE_TRANS_DISC_DATA    = 0x26, /* relate to BTM_BLE_AD_TYPE_TRANS_DISC_DATA */
156     OHOS_BLE_AD_TYPE_LE_SUPPORT_FEATURE = 0x27, /* relate to BTM_BLE_AD_TYPE_LE_SUPPORT_FEATURE */
157     OHOS_BLE_AD_TYPE_CHAN_MAP_UPDATE    = 0x28, /* relate to BTM_BLE_AD_TYPE_CHAN_MAP_UPDATE */
158     OHOS_BLE_AD_MANUFACTURER_SPECIFIC_TYPE = 0xFF, /* relate to BTM_BLE_AD_MANUFACTURER_SPECIFIC_TYPE */
159 } BleAdvDataType;
160 
161 /**
162   * @brief Gatt写类型
163   */
164 typedef enum {
165     OHOS_GATT_WRITE_TYPE_NO_RSP = 1, /*!< Gatt write attribute need no response */
166     OHOS_GATT_WRITE_TYPE_RSP, /*!< Gatt write attribute need remote response */
167 } GattBleWriteType;
168 
169 typedef void (* GapBleCallback)(GapBleCallbackEvent event, BleGapParam *param);
170 
171 BtError EnableBle(void);
172 
173 BtError DisableBle(void);
174 
175 BtError SetLocalName(unsigned char *localName, unsigned char length);
176 
177 BtError GetLocalAddr(unsigned char *mac, unsigned int len);
178 
179 BtError BleStartScan(void);
180 
181 BtError BleStopScan(void);
182 
183 BtError BleGapUpdateConnParams(BleConnUpdateParams *params);
184 
185 BtError BleGatSetScanParams(BleScanParams *scan_params);
186 
187 BtError BleGapDisconnect(BdAddr remote_device);
188 
189 uint8_t *BleResolveAdvData(uint8_t *adv_data, uint8_t type, uint8_t *length);
190 #endif
191