xiaozhengsheng 6df0f7d96e 初始版本
2025-08-19 09:49:41 +08:00

166 lines
5.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef BLE_NUS_H__
#define BLE_NUS_H__
#include <stdint.h>
#include <stdbool.h>
#include "sdk_config.h"
#include "ble.h"
#include "ble_srv_common.h"
#include "nrf_sdh_ble.h"
#ifdef __cplusplus
extern "C" {
#endif
//定义串口透传服务实例该实例完成2件事情
//1定义了static类型串口透传服务结构体变量为串口透传服务结构体分配了内存
//2注册了BLE事件监视者这使得串口透传程序模块可以接收BLE协议栈的事件从而可以在ble_uarts_on_ble_evt()事件回调函数中处理自己感兴趣的事件
#define BLE_NUS_DEF(_name) \
static ble_nus_t _name; \
NRF_SDH_BLE_OBSERVER(_name ## _obs, \
BLE_NUS_BLE_OBSERVER_PRIO, \
ble_nus_on_ble_evt, \
&_name)
//定义串口透传服务128位UUID基数
#define NUS_BASE_UUID {{0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
//定义串口透传服务16位UUID
#define BLE_UUID_NUS_SERVICE 0x1000 //串口透传服务16位UUID
#define BLE_UUID_NUS_TX_CHARACTERISTIC 0x1002 //TX特征16位UUID
#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x1001 //RX特征16位UUID
//定义操作码长度
#define OPCODE_LENGTH 1
//定义句柄长度
#define HANDLE_LENGTH 2
//定义最大传输数据长度(字节数)
#if defined(NRF_SDH_BLE_GATT_MAX_MTU_SIZE) && (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 0)
#define BLE_NUS_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
#else
#define BLE_NUS_MAX_DATA_LEN (BLE_GATT_MTU_SIZE_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH)
#warning NRF_SDH_BLE_GATT_MAX_MTU_SIZE is not defined.
#endif
//定义串口透传事件类型,这是用户自己定义的,供应用程序使用
typedef enum
{
BLE_NUS_EVT_RX_DATA, //接收到新的数据
BLE_NUS_EVT_TX_RDY, //准备就绪,可以发送新数据
BLE_NUS_EVT_COMM_STARTED, /**< Notification has been enabled. */
BLE_NUS_EVT_COMM_STOPPED, /**< Notification has been disabled. */
} ble_nus_evt_type_t;
/* Forward declaration of the ble_nus_t type. */
typedef struct ble_nus_s ble_nus_t;
//串口透传服务BLE_NUS_EVT_RX_DATA事件数据结构体该结构体用于当BLE_NUS_EVT_RX_DATA产生时将接收的数据信息传递给处理函数
typedef struct
{
uint8_t const * p_data; //指向存放接收数据的缓存
uint16_t length; //接收的数据长度
} ble_nus_evt_rx_data_t;
/**@brief Nordic UART Service client context structure.
*
* @details This structure contains state context related to hosts.
*/
typedef struct
{
bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/
} ble_nus_client_context_t;
//串口透传服务事件结构体
typedef struct
{
ble_nus_evt_type_t type; //事件类型
ble_nus_t * p_nus; //指向串口透传实例的指针
uint16_t conn_handle; //连接句柄
// ble_nus_client_context_t * p_link_ctx; /**< A pointer to the link context. */
union
{
ble_nus_evt_rx_data_t rx_data; //BLE_NUS_EVT_RX_DATA事件数据
} params;
} ble_nus_evt_t;
//定义函数指针类型ble_uarts_data_handler_t
typedef void (* ble_nus_data_handler_t) (ble_nus_evt_t * p_evt);
//串口服务初始化结构体
typedef struct
{
ble_nus_data_handler_t data_handler; //处理接收数据的事件句柄
} ble_nus_init_t;
//串口透传服务结构体,包含所需要的信息
struct ble_nus_s
{
uint8_t uuid_type; //UUID类型
uint16_t service_handle; //串口透传服务句柄(由协议栈提供)
ble_gatts_char_handles_t tx_handles; //TX特征句柄
ble_gatts_char_handles_t rx_handles; //RX特征句柄
// blcm_link_ctx_storage_t * const p_link_ctx_storage; /**< Pointer to link context storage with handles of all current connections and its context. */
ble_nus_data_handler_t data_handler; //处理接收数据的事件句柄
};
/**@brief Function for initializing the Nordic UART Service.
*
* @param[out] p_nus Nordic UART Service structure. This structure must be supplied
* by the application. It is initialized by this function and will
* later be used to identify this particular service instance.
* @param[in] p_nus_init Information needed to initialize the service.
*
* @retval NRF_SUCCESS If the service was successfully initialized. Otherwise, an error code is returned.
* @retval NRF_ERROR_NULL If either of the pointers p_nus or p_nus_init is NULL.
*/
uint32_t ble_nus_init(ble_nus_t * p_nus, ble_nus_init_t const * p_nus_init);
/**@brief Function for handling the Nordic UART Service's BLE events.
*
* @details The Nordic UART Service expects the application to call this function each time an
* event is received from the SoftDevice. This function processes the event if it
* is relevant and calls the Nordic UART Service event handler of the
* application if necessary.
*
* @param[in] p_ble_evt Event received from the SoftDevice.
* @param[in] p_context Nordic UART Service structure.
*/
void ble_nus_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
/**@brief Function for sending a data to the peer.
*
* @details This function sends the input string as an RX characteristic notification to the
* peer.
*
* @param[in] p_nus Pointer to the Nordic UART Service structure.
* @param[in] p_data String to be sent.
* @param[in,out] p_length Pointer Length of the string. Amount of sent bytes.
* @param[in] conn_handle Connection Handle of the destination client.
*
* @retval NRF_SUCCESS If the string was sent successfully. Otherwise, an error code is returned.
*/
uint32_t ble_nus_data_send(ble_nus_t * p_nus,
uint8_t * p_data,
uint16_t * p_length,
uint16_t conn_handle);
#ifdef __cplusplus
}
#endif
#endif // BLE_NUS_H__
/** @} */