!77 蓝牙服务驱动

Merge pull request !77 from lrx/master
This commit is contained in:
openharmony_ci
2023-12-15 08:53:41 +00:00
committed by Gitee
5 changed files with 507 additions and 7 deletions
@@ -17,17 +17,18 @@ module_name = get_path_info(rebase_path("."), "name")
kernel_module(module_name) {
sources = [
"bluetooth_device.c",
"bluetooth_service.c",
]
ESP_SDK_PATH="//device/soc/esp/esp32/components/"
ESP_SDK_PATH = "//device/soc/esp/esp32/components/"
include_dirs = [
ESP_SDK_PATH+"bt/include/esp32/include",
ESP_SDK_PATH + "bt/include/esp32/include",
"//foundation/communication/bluetooth/interfaces/inner_api/include/c_header",
ESP_SDK_PATH+"bt/host/bluedroid/api/include/api",
ESP_SDK_PATH+"log/include",
ESP_SDK_PATH + "bt/host/bluedroid/api/include/api",
ESP_SDK_PATH + "log/include",
]
}
config("public") {
include_dirs = ["."]
include_dirs = [ "." ]
}
@@ -25,6 +25,7 @@
#include "esp_gatt_common_api.h"
#include "esp_log.h"
#include "ohos_bt_def.h"
#include "esp_system.h"
#define GattInterfaceType esp_gatt_if_t
#define BleGapParam esp_ble_gap_cb_param_t
@@ -178,7 +179,7 @@ BtError GetLocalAddr(unsigned char *mac, unsigned int len);
BtError BleStartScan(void);
BtError BleStopScan(void);
BtError BleStopScan2(void);
BtError BleGapUpdateConnParams(BleConnUpdateParams *params);
@@ -187,4 +188,5 @@ BtError BleGatSetScanParams(BleScanParams *scan_params);
BtError BleGapDisconnect(BdAddr remote_device);
uint8_t *BleResolveAdvData(uint8_t *adv_data, uint8_t type, uint8_t *length);
#endif
@@ -92,7 +92,7 @@ BtError BleStartScan(void)
return esp_ble_gap_start_scanning(ScanTime);
}
BtError BleStopScan(void)
BtError BleStopScan2(void)
{
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
return ESP_ERR_INVALID_STATE;
@@ -0,0 +1,269 @@
/*
* Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bluetooth_service.h"
#include "ohos_run.h"
#include "blegap.h"
#define GATTS_TAG "BLUETOOTH_SERVICE"
/**
* @brief Registers GATT server callbacks.
*
* @param func Indicates the pointer to the callbacks to register, as enumerated in {@link BtGattServerCallbacks}.
* @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the callbacks are registered;
* returns an error code defined in {@link BtStatus} otherwise.
* @since 6
*/
BtError BleGattsRegisterCallbacks(BtGattServerCallbacks func)
{
esp_err_t ret;
ret = esp_ble_gatts_register_callback(func.gattsCallback);
if (ret) {
ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
return;
}
ret = esp_ble_gap_register_callback(func.gapCallback);
if (ret) {
ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
return;
}
ret = esp_ble_gatts_app_register(func.profileAppId);
if (ret) {
ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
return;
}
return ret;
}
/**
* @brief Starts a service.
*
* @param serverId Indicates the ID of the GATT server.
* @param srvcHandle Indicates the handle ID of the service.
* @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the service is started;
* returns an error code defined in {@link BtStatus} otherwise.
* @since 6
*/
BtError BleGattsStartService(int serverId, int srvcHandle)
{
return esp_ble_gatts_start_service(srvcHandle);
}
/**
* @brief Stops a service.
*
* @param serverId Indicates the ID of the GATT server.
* @param srvcHandle Indicates the handle ID of the service.
* @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the service is stopped;
* returns an error code defined in {@link BtStatus} otherwise.
* @since 6
*/
BtError BleGattsStopService(int serverId, int srvcHandle)
{
return esp_ble_gatts_stop_service(srvcHandle);
}
/**
* @brief Sends an indication or notification to the client.
*
* The <b>confirm</b> field in <b>param</b> determines whether to send an indication or a notification.
*
* @param serverId Indicates the ID of the GATT server.
* @param param Indicates the pointer to the sending parameters. For details, see {@link GattsSendIndParam}.
* @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the indication or notification is sent;
* returns an error code defined in {@link BtStatus} otherwise.
* @since 6
*/
BtError BleGattsSendIndication(GattsSendParam *param)
{
if (param == NULL && param->value == NULL) {
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gatts_send_indicate(param->gattsIf, param->connId, param->attrHandle, param->valueLen, param->value,
param->needConfirm);
}
/**
* @brief This function is called to start advertising.
*
* @param advParams: pointer to User defined advParams data structure.
* @return
* - ESP_OK : success
* - other : failed
*
*/
BtError BleGattsStartAdvertising(BleAdvParams2 *advParams)
{
if (advParams == NULL) {
BT_DEBUG("BleGattsStartAdvertising param is NULL! \n");
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gap_start_advertising(advParams);
}
/**
* @brief This function is called to send a response to a request.
*
* @param[in] gattsIf: GATT server access interface
* @param[in] connId - connection identifier.
* @param[in] transId - transfer id
* @param[in] status - response status
* @param[in] rsp - response data.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
BtError BleGattsSendResponse(GattInterfaceType gattsIf, uint16_t connId, uint32_t transId, GattStatus status,
BleGattRsp *rsp)
{
return esp_ble_gatts_send_response(gattsIf, connId, transId, status, rsp);
}
/**
* @brief This function is called to override the BTA default ADV parameters.
*
* @param[in] advData: Pointer to User defined ADV data structure. This
* memory space can not be freed until callback of config_adv_data
* is received.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
BtError BleGapConfigAdvData(BleAdvData *advData)
{
if (advData == NULL) {
BT_DEBUG("BleGapConfigAdvData param is NULL! \n");
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gap_config_adv_data(advData);
}
/**
* @brief Create a service. When service creation is done, a callback
* event ESP_GATTS_CREATE_EVT is called to report status
* and service ID to the profile. The service ID obtained in
* the callback function needs to be used when adding included
* service and characteristics/descriptors into the service.
*
* @param[in] gattsIf: GATT server access interface
* @param[in] serviceId: service ID.
* @param[in] numHandle: number of handle requested for this service.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
BtError BleGattsCreateService(GattInterfaceType gattsIf, GattSrvcId *serviceId, uint16_t numHandle)
{
if (serviceId == NULL) {
BT_DEBUG("BleGattsCreateService param is NULL! \n");
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gatts_create_service(gattsIf, serviceId, numHandle);
}
/**
* @brief Retrieve attribute value
*
* @param[in] attrHandle: Attribute handle.
* @param[out] length: pointer to the attribute value length
* @param[out] value: Pointer to attribute value payload, the value cannot be modified by user
*
* @return
* - ESP_GATT_OK : success
* - other : failed
*
*/
BtError BleGattsGetAttrValue(uint16_t attrHandle, uint16_t *length, const uint8_t **value)
{
if ((length == NULL) || (value == NULL)) {
BT_DEBUG("BleGattsGetAttrValue param is NULL! \n");
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gatts_get_attr_value(attrHandle, length, value);
}
/**
* @brief This function is called to add characteristic descriptor. When
* it's done, a callback event ESP_GATTS_ADD_DESCR_EVT is called
* to report the status and an ID number for this descriptor.
*
* @param[in] serviceHandle: service handle to which this characteristic descriptor is to
* be added.
* @param[in] perm: descriptor access permission.
* @param[in] descrUuid: descriptor UUID.
* @param[in] charDescrVal : Characteristic descriptor value
* @param[in] control : attribute response control byte
* @return
* - ESP_OK : success
* - other : failed
*
*/
BtError BleGattsAddCharDescr(uint16_t serviceHandle, BtUuid *descrUuid, uint16_t perm, BleAttrValue *charDescrVal,
BleAttrControl *control)
{
if (descrUuid == NULL) {
BT_DEBUG("BleGattsAddCharDescr param is NULL! \n");
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gatts_add_char_descr(serviceHandle, descrUuid, perm, charDescrVal, control);
}
/**
* @brief This function is called to add a characteristic into a service.
*
* @param[in] serviceHandle: service handle to which this included service is to
* be added.
* @param[in] character : Characteristic.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
BtError BleGattsAddChar(uint16_t serviceHandle, GattsChar *character)
{
if ((charUuid == NULL) || (character == NULL) || (character->charVal == NULL)) {
BT_DEBUG("BleGattsAddChar param is NULL! \n");
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gatts_add_char(serviceHandle, character->charUuid, character->perm, character->property,
character->charVal, character->control);
}
/**
* @brief Update connection parameters, can only be used when connection is up.
*
* @param[in] params - connection update parameters
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
BtError BleGapUpdateConnParams(BleConnUpdateParams *params)
{
if (params == NULL) {
BT_DEBUG("BleGapUpdateConnParams param is NULL! \n");
return BT_PARAMINPUT_ERROR;
}
return esp_ble_gap_update_conn_params(params);
}
@@ -0,0 +1,228 @@
/*
* Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __BLUETOOTH_SERVICE_H__
#define __BLUETOOTH_SERVICE_H__
#include "securec.h"
#include "blegap.h"
#include "esp_gatts_api.h"
typedef esp_ble_gatts_cb_param_t BleGattsParam;
// Gap回调参数联合体
typedef esp_ble_gap_cb_param_t BleGapParam;
// Gatt服务端回调参数联合体
typedef esp_ble_gatts_cb_param_t BleGattsCbParam;
// Ble扫描参数
typedef esp_ble_scan_params_t BleScanParams;
// 设置属性值类型
typedef esp_attr_value_t BleAttrValue;
// 广播数据结构体
typedef esp_ble_adv_data_t BleAdvData;
// 广播参数
typedef esp_ble_adv_params_t BleAdvParams2;
// GATT远程读请求响应类型
typedef esp_gatt_rsp_t BleGattRsp;
// Gatt服务ID
typedef esp_gatt_srvc_id_t GattSrvcId;
// 属性自动响应标志
typedef esp_attr_control_t BleAttrControl;
// 蓝牙版本号
#define BT_VERSION 1
/// GATT服务回调函数事件
typedef enum {
OHOS_GATTS_REG_EVT = 0, /*!< When register application id, the event comes */
OHOS_GATTS_READ_EVT = 1, /*!< When gatt client request read operation, the event comes */
OHOS_GATTS_WRITE_EVT = 2, /*!< When gatt client request write operation, the event comes */
OHOS_GATTS_EXEC_WRITE_EVT = 3, /*!< When gatt client request execute write, the event comes */
OHOS_GATTS_MTU_EVT = 4, /*!< When set mtu complete, the event comes */
OHOS_GATTS_CONF_EVT = 5, /*!< When receive confirm, the event comes */
OHOS_GATTS_UNREG_EVT = 6, /*!< When unregister application id, the event comes */
OHOS_GATTS_CREATE_EVT = 7, /*!< When create service complete, the event comes */
OHOS_GATTS_ADD_INCL_SRVC_EVT = 8, /*!< When add included service complete, the event comes */
OHOS_GATTS_ADD_CHAR_EVT = 9, /*!< When add characteristic complete, the event comes */
OHOS_GATTS_ADD_CHAR_DESCR_EVT = 10, /*!< When add descriptor complete, the event comes */
OHOS_GATTS_DELETE_EVT = 11, /*!< When delete service complete, the event comes */
OHOS_GATTS_START_EVT = 12, /*!< When start service complete, the event comes */
OHOS_GATTS_STOP_EVT = 13, /*!< When stop service complete, the event comes */
OHOS_GATTS_CONNECT_EVT = 14, /*!< When gatt client connect, the event comes */
OHOS_GATTS_DISCONNECT_EVT = 15, /*!< When gatt client disconnect, the event comes */
OHOS_GATTS_OPEN_EVT = 16, /*!< When connect to peer, the event comes */
OHOS_GATTS_CANCEL_OPEN_EVT = 17, /*!< When disconnect from peer, the event comes */
OHOS_GATTS_CLOSE_EVT = 18, /*!< When gatt server close, the event comes */
OHOS_GATTS_LISTEN_EVT = 19, /*!< When gatt listen to be connected the event comes */
OHOS_GATTS_CONGEST_EVT = 20, /*!< When congest happen, the event comes */
/* following is extra event */
OHOS_GATTS_RESPONSE_EVT = 21, /*!< When gatt send response complete, the event comes */
OHOS_GATTS_CREAT_ATTR_TAB_EVT = 22, /*!< When gatt create table complete, the event comes */
OHOS_GATTS_SET_ATTR_VAL_EVT = 23, /*!< When gatt set attr value complete, the event comes */
OHOS_GATTS_SEND_SERVICE_CHANGE_EVT = 24, /*!< When gatt send service change indication complete, the event comes */
} GattsBleCallbackEvent;
/**
* @brief GATT服务端回调函数类型
* @param event : 事件类型
* @param gattc_if : GATT客户端访问接口,不同的gattc_if对应不同的profile
* @param param : 指向回调参数指针,当前是联合类型
*/
typedef void (*GattsBleCallback)(GattsBleCallbackEvent event, GattInterfaceType gattsIf, BleGattsParam *param);
typedef struct {
GapBleCallback gapCallback; // gap回调函数
GattsBleCallback gattsCallback; // gatts回调函数
uint16_t profileAppId; // 应用程序标识(UUID),用于不同的应用程序,应用程序回调函数使用
} BtGattServerCallbacks;
typedef struct {
GattInterfaceType gattsIf; // GATT服务器访问接口
uint16_t connId; // 连接id
uint16_t attrHandle; // 属性句柄
uint16_t valueLen; // 值的长度
uint8_t *value; // 值
bool needConfirm; // 是否需要确认
} GattsSendParam;
typedef struct {
BtUuid *charUuid; // Characteristic UUID.
uint16_t perm; // Characteristic value declaration attribute permission.
uint8_t property; // Characteristic Properties
BleAttrValue *charVal; // Characteristic value
BleAttrControl *control; // attribute response control byte
} GattsChar;
/**
* @brief 发现请求远程设备上的GATT服务
*
* @param clientId 客户端ID
* @param connId 连接ID
* @param filterUuid 过滤服务UUID *
* @return BtResult 成功返回 BT_SUCCESS
*/
BtError BleGattcSearchServices(int clientId, int connId, BtUuid *filterUuid);
/**
* @brief 注册Gatt服务端回调
*
* @param func BtGattServerCallbacks结构体
* @return BtResult 成功返回 BT_SUCCESS
*/
BtError BleGattsRegisterCallbacks(BtGattServerCallbacks func);
/**
* @brief 启动Gatt服务
*
* @param serverId Gatt服务id
* @param srvcHandl Gatt服务句柄
* @return BtResult 成功返回 BT_SUCCESS
* @since 6
*/
BtError BleGattsStartService(int serverId, int srvcHandle);
/**
* @brief 停止Gatt服务
*
* @param serverId Gatt服务id
* @param srvcHandl Gatt服务句柄
* @return BtResult 成功返回 BT_SUCCESS
*/
BtError BleGattsStopService(int serverId, int srvcHandle);
/**
* @brief 发送指示或通知GATT客户端
*
* @param param: GATT服务器发送数据参数
* @return BtResult 成功返回 BT_SUCCESS
*/
BtError BleGattsSendIndication(GattsSendParam* param);
/**
* @brief 发送指示或通知GATT客户端
*
* @param advParams: 广播参数
* @return BtResult 成功返回 BT_SUCCESS
*/
BtError BleGattsStartAdvertising(BleAdvParams2 *advParams);
/**
* @brief 向请求发送响应函数.
*
* @param gattsIf: GATT server access interface
* @param connId - connection identifier.
* @param transId - transfer id
* @param status - response status
* @param rsp - response data.
* @return BtResult 成功返回 BT_SUCCESS
*
*/
BtError BleGattsSendResponse(GattInterfaceType gattsIf, uint16_t connId, uint32_t transId, GattStatus status,
BleGattRsp *rsp);
/**
* @brief 覆盖BTA默认的ADV参数
*
* @param[in] advData: Pointer to User defined ADV data structure.
* @return BtResult 成功返回 BT_SUCCESS
*
*/
BtError BleGapConfigAdvData(BleAdvData *advData);
/**
* @brief 创建Gatt服务
*
* @param gattsIf: GATT server access interface
* @param serviceId: service ID.
* @param numHandle: number of handle requested for this service.
*
* @return BtResult 成功返回 BT_SUCCESS
*
*/
BtError BleGattsCreateService(GattInterfaceType gattsIf, GattSrvcId *serviceId, uint16_t numHandle);
/**
* @brief 获取属性值
*
* @param attrHandle: Attribute handle.
* @param length: pointer to the attribute value length
* @param value: Pointer to attribute value payload, the value cannot be modified by user
* @return BtResult 成功返回 BT_SUCCESS
*
*/
BtError BleGattsGetAttrValue(uint16_t attrHandle, uint16_t *length, const uint8_t **value);
/**
* @brief 添加特征描述符
*
* @param serviceHandle: service handle to which this characteristic descriptor is to
* be added.
* @param perm: descriptor access permission.
* @param descrUuid: descriptor UUID.
* @param charDescrVal : Characteristic descriptor value
* @param control : attribute response control byte
* @return BtResult 成功返回 BT_SUCCESS
*
*/
BtError BleGattsAddCharDescr(uint16_t serviceHandle, BtUuid *descrUuid, uint16_t perm, BleAttrValue *charDescrVal,
BleAttrControl *control);
/**
* @brief 将特征添加到服务中
*
* @param serviceHandle: service handle to which this included service is to
* be added.
* @param character: 特征值参数
* @return BtResult 成功返回 BT_SUCCESS
*
*/
BtError BleGattsAddChar(uint16_t serviceHandle, GattsChar* character);
#endif