• 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 #include "ohos_run.h"
16 #include "blegap.h"
17 #include "bluetooth_device.h"
18 #define NULL 0L
19 
EnableBle(void)20 BtError EnableBle(void)
21 {
22     esp_err_t ret;
23     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
24     ret = esp_bt_controller_init(&bt_cfg);
25     if (ret) {
26         ESP_LOGE(GATTC_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
27         return ret;
28     }
29 
30     ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
31     if (ret) {
32         ESP_LOGE(GATTC_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
33         return ret;
34     }
35 
36     ret = esp_bluedroid_init();
37     if (ret) {
38         ESP_LOGE(GATTC_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
39         return ret;
40     }
41 
42     ret = esp_bluedroid_enable();
43     if (ret) {
44         ESP_LOGE(GATTC_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
45         return ret;
46     }
47     return ret;
48 }
49 
DisableBle(void)50 BtError DisableBle(void)
51 {
52     if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_ENABLED) {
53         esp_bluedroid_disable();
54         esp_bt_controller_disable();
55     }
56     return BT_SUCCESS;
57 }
58 
SetLocalName(unsigned char * localName,unsigned char length)59 BtError SetLocalName(unsigned char *localName, unsigned char length)
60 {
61     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
62         printf("bluedroid is not enable! \n");
63         return ESP_ERR_INVALID_STATE;
64     }
65     if ((localName == NULL) || (length <= 0)) {
66         BT_DEBUG("SetLocalName param is NULL! \n");
67         return BT_PARAMINPUT_ERROR;
68     }
69     return esp_ble_gap_set_device_name(localName);
70 }
71 
GetLocalAddr(unsigned char * mac,unsigned int len)72 BtError GetLocalAddr(unsigned char *mac, unsigned int len)
73 {
74     int ret;
75     if ((mac == NULL) || (len <= 0)) {
76         BT_DEBUG("GetLocalAddr param is NULL! \n");
77         return BT_PARAMINPUT_ERROR;
78     }
79     ret = memcpy_s(mac, len, esp_bt_dev_get_address(), len);
80     if (!ret) {
81         printf("memcpy_s fail!!\n");
82         return ret;
83     }
84     return BT_SUCCESS;
85 }
86 
BleStartScan(void)87 BtError BleStartScan(void)
88 {
89     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
90         return ESP_ERR_INVALID_STATE;
91     }
92     return esp_ble_gap_start_scanning(ScanTime);
93 }
94 
BleStopScan(void)95 BtError BleStopScan(void)
96 {
97     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
98         return ESP_ERR_INVALID_STATE;
99     }
100     return esp_ble_gap_stop_scanning();
101 }
102 
BleGattcConnect(int clientId,VOID * func,const BdAddr * bdAddr,bool isAutoConnect,BtTransportType transport)103 BtError BleGattcConnect(int clientId, VOID *func,
104                         const BdAddr *bdAddr, bool isAutoConnect,
105                         BtTransportType transport)
106 {
107     if (bdAddr == NULL) {
108         BT_DEBUG("BleGattcConnect param is NULL! \n");
109         return BT_PARAMINPUT_ERROR;
110     }
111     return esp_ble_gattc_open(clientId, bdAddr, transport, isAutoConnect);
112 }
113 
BleGattcDisconnect(int clientId,int conn_id)114 BtError BleGattcDisconnect(int clientId, int conn_id)
115 {
116     return esp_ble_gattc_close(clientId, conn_id);
117 }
118 
BleGapDisconnect(BdAddr remote_device)119 BtError BleGapDisconnect(BdAddr remote_device)
120 {
121     uint8_t BdAddrs[OHOS_BD_ADDR_LEN];
122     int ret = 0;
123     ret = memcpy_s(BdAddrs, sizeof(BdAddrs), remote_device.addr, sizeof(remote_device.addr));
124     if (!ret) {
125         printf("memcpy_s fail!!\n");
126         return ret;
127     }
128     return esp_ble_gap_disconnect(BdAddrs);
129 }
130 
BleResolveAdvData(uint8_t * adv_data,uint8_t type,uint8_t * length)131 uint8_t *BleResolveAdvData(uint8_t *adv_data, uint8_t type, uint8_t *length)
132 {
133     if ((adv_data == NULL) || (length <= 0)) {
134         BT_DEBUG("BleResolveAdvData param is NULL! \n");
135         return BT_PARAMINPUT_ERROR;
136     }
137     return esp_ble_resolve_adv_data(adv_data, type, length);
138 }
139 
BleGattcConfigureMtuSize(int mtuSize)140 BtError BleGattcConfigureMtuSize(int mtuSize)
141 {
142     return esp_ble_gatt_set_local_mtu(mtuSize);
143 }
144 
BleGattcRegister(BtGattClientCallbacks func)145 BtError BleGattcRegister(BtGattClientCallbacks func)
146 {
147     esp_err_t ret;
148     ret = esp_ble_gap_register_callback(func.gap_callback);
149     if (ret) {
150         ESP_LOGE(GATTC_TAG, "%s gap register failed, error code = %x\n", __func__, ret);
151         return ret;
152     }
153 
154     ret = esp_ble_gattc_register_callback(func.gattc_callback);
155     if (ret) {
156         ESP_LOGE(GATTC_TAG, "%s gattc register failed, error code = %x\n", __func__, ret);
157         return ret;
158     }
159 
160     ret = esp_ble_gattc_app_register(func.PROFILE_APP_ID);
161     if (ret) {
162         ESP_LOGE(GATTC_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
163     }
164     return ret;
165 }
166 
BleGattcUnRegister(int clientId)167 BtError BleGattcUnRegister(int clientId)
168 {
169     return esp_ble_gattc_app_unregister(clientId);
170 }
171 
BleGattcSearchServices(int clientId,int conn_id,BtUuid * filter_uuid)172 BtError BleGattcSearchServices(int clientId, int conn_id, BtUuid *filter_uuid)
173 {
174     if (filter_uuid == NULL) {
175         BT_DEBUG("BleResolveAdvData param is NULL! \n");
176         return BT_PARAMINPUT_ERROR;
177     }
178     esp_bt_uuid_t remote_filter_service_uuid = {
179         .len = filter_uuid->uuidLen,
180         .uuid = {.uuid16 = (uint16_t)filter_uuid->uuid, },
181     };
182     esp_ble_gattc_search_service(clientId, conn_id, &remote_filter_service_uuid);
183 }
184 
BleGattcWriteCharacteristic(GattcWriteChar write_char,uint8_t * value,GattBleAuthReq auth_req)185 BtError BleGattcWriteCharacteristic(GattcWriteChar write_char, uint8_t *value,
186                                     GattBleAuthReq auth_req)
187 {
188     if ((value == NULL) || (write_char.value_len <= 0)) {
189         BT_DEBUG("BleGattcWriteCharacteristic param is NULL! \n");
190         return BT_PARAMINPUT_ERROR;
191     }
192     return esp_ble_gattc_write_char(write_char.gattc_if, write_char.conn_id,
193                                     write_char.handle, write_char.value_len,
194                                     value, write_char.write_type, auth_req);
195 }
196 
BleGatSetScanParams(BleScanParams * scan_params)197 BtError BleGatSetScanParams(BleScanParams *scan_params)
198 {
199     if (scan_params == NULL) {
200         BT_DEBUG("BleGatSetScanParams param is NULL! \n");
201         return BT_PARAMINPUT_ERROR;
202     }
203     return esp_ble_gap_set_scan_params(scan_params);
204 }
205 
BleGattcSendMtuReq(GattInterfaceType gattc_if,uint16_t conn_id)206 BtError BleGattcSendMtuReq(GattInterfaceType gattc_if, uint16_t conn_id)
207 {
208     return esp_ble_gattc_send_mtu_req(gattc_if, conn_id);
209 }
210 
BleGattcGetAttrCount(GattcGetAttr get_attr,uint16_t char_handle,uint16_t * count)211 BtError BleGattcGetAttrCount(GattcGetAttr get_attr,
212                              uint16_t char_handle,
213                              uint16_t *count)
214 {
215     if (count == NULL) {
216         BT_DEBUG("BleGattcGetAttrCount param is NULL! \n");
217         return BT_PARAMINPUT_ERROR;
218     }
219     return esp_ble_gattc_get_attr_count(get_attr.gattc_if, get_attr.conn_id,
220                                         get_attr.type, get_attr.start_handle,
221                                         get_attr.end_handle, char_handle, count);
222 }
223 
BleGattcGetCharByUuid(GattcGetChar get_char,BtUuids char_uuid,BleGattcCharElem * result,uint16_t * count)224 GattStatus BleGattcGetCharByUuid(GattcGetChar get_char, BtUuids char_uuid,
225                                  BleGattcCharElem *result, uint16_t *count)
226 {
227     if ((result == NULL) || (count == NULL)) {
228         BT_DEBUG("BleGattcGetCharByUuid param is NULL! \n");
229         return BT_PARAMINPUT_ERROR;
230     }
231     return esp_ble_gattc_get_char_by_uuid(get_char.gattc_if, get_char.conn_id,
232                                           get_char.start_handle, get_char.end_handle,
233                                           char_uuid, result, count);
234 }
235 
BleGattcRegisterForNotify(GattInterfaceType gattc_if,BdAddr server_bda,uint16_t handle)236 BtError BleGattcRegisterForNotify(GattInterfaceType gattc_if,
237                                   BdAddr server_bda,
238                                   uint16_t handle)
239 {
240     uint8_t BdAddrs[OHOS_BD_ADDR_LEN];
241     int ret = 0;
242     ret = memcpy_s(BdAddrs, sizeof(BdAddrs), server_bda.addr, sizeof(server_bda.addr));
243     if (!ret) {
244         printf("memcpy_s fail!!\n");
245         return ret;
246     }
247     return esp_ble_gattc_register_for_notify(gattc_if, BdAddrs, handle);
248 }
249 
BleGattcGetDescrByCharHandle(GattcGetDescr get_descr,BleGattcDescrElem * result,uint16_t * count)250 GattStatus BleGattcGetDescrByCharHandle(GattcGetDescr get_descr, BleGattcDescrElem *result,
251                                         uint16_t *count)
252 {
253     if ((result == NULL) || (count == NULL)) {
254         BT_DEBUG("BleGattcGetCharByUuid param is NULL! \n");
255         return BT_PARAMINPUT_ERROR;
256     }
257     return esp_ble_gattc_get_descr_by_char_handle(get_descr.gattc_if, get_descr.conn_id,
258                                                   get_descr.char_handle, get_descr.descr_uuid,
259                                                   result, count);
260 }
261 
BleGattcWriteCharDescr(GattcWriteChar write_char,uint8_t * value,GattAttributePermission auth_req)262 BtError BleGattcWriteCharDescr(GattcWriteChar write_char,
263                                uint8_t *value,
264                                GattAttributePermission auth_req)
265 {
266     if (value == NULL) {
267         BT_DEBUG("BleGattcWriteCharDescr param is NULL! \n");
268         return BT_PARAMINPUT_ERROR;
269     }
270     return esp_ble_gattc_write_char_descr(write_char.gattc_if, write_char.conn_id,
271                                           write_char.handle, write_char.value_len,
272                                           value, write_char.write_type, auth_req);
273 }
274