You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
3.4 KiB

#ifndef ZDATACHANNEL_H__
#define ZDATACHANNEL_H__
#include <stdbool.h>
#include <stdint.h>
#include "ble.h"
#include "ble_link_ctx_manager.h"
#include "ble_srv_common.h"
#include "nrf_sdh_ble.h"
#include "sdk_config.h"
#ifdef __cplusplus
extern "C" {
#endif
/**@brief Macro for defining a ble_nus instance.
*
* @param _name Name of the instance.
* @param[in] _nus_max_clients Maximum number of NUS clients connected at a time.
* @hideinitializer
*/
#define ZDATACHANNEL_DEF(_name, _ble_observer_prio, _nus_max_clients) \
static zdatachannel_t _name; \
NRF_SDH_BLE_OBSERVER(_name##_obs, _ble_observer_prio, zdatachannel_on_ble_evt, &_name)
#define OPCODE_LENGTH 1
#define HANDLE_LENGTH 2
/**@brief Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
#if defined(NRF_SDH_BLE_GATT_MAX_MTU_SIZE) && (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 0)
#define ZDATACHANNEL_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
#else
#define ZDATACHANNEL_MAX_DATA_LEN (BLE_GATT_MTU_SIZE_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH)
#warning NRF_SDH_BLE_GATT_MAX_MTU_SIZE is not defined.
#endif
/**@brief Nordic UART Service event types. */
typedef enum {
ZDATACHANNEL_EVT_RX_DATA, /**< Data received. */
} zdatachannel_evt_type_t;
typedef struct zdatachannel_s zdatachannel_t;
typedef struct {
uint8_t const* p_data; /**< A pointer to the buffer with received data. */
uint16_t length; /**< Length of received data. */
} zdatachannel_evt_rx_data_t;
typedef struct {
zdatachannel_evt_type_t type; /**< Event type. */
union {
zdatachannel_evt_rx_data_t rx_data; /**< @ref ZDATACHANNEL_EVT_RX_DATA event data. */
} params;
} zdatachannel_evt_t;
typedef void (*zdatachannel_data_handler_t)(zdatachannel_evt_t* p_evt);
typedef struct {
zdatachannel_data_handler_t data_handler;
} zdatachannel_init_t;
struct zdatachannel_s {
uint8_t uuid_type; /**< UUID type for Nordic UART Service Base UUID. */
uint16_t service_handle; /**< Handle of Nordic UART Service (as provided by the SoftDevice). */
ble_gatts_char_handles_t cmd_tx_handles; /**< 指令发送句柄 */
ble_gatts_char_handles_t cmd_rx_handles; /**< 指令接收句柄 */
ble_gatts_char_handles_t datablock_tx_handles; /**< 数据块发送句柄 */
zdatachannel_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
// 状态
int16_t conn_handle;
bool cmd_tx_channel_is_notification_enabled;
bool datablock_tx_channel_is_notification_enabled;
};
uint32_t zdatachannel_init(zdatachannel_t* p_nus, zdatachannel_init_t const* p_nus_init);
void zdatachannel_on_ble_evt(ble_evt_t const* p_ble_evt, void* p_context);
bool zdatachannel_is_connected();
uint32_t zdatachannel_data_send(uint8_t* p_data, uint16_t* p_length);
static inline uint32_t zdatachannel_data_send2(uint8_t* p_data, uint16_t p_length){
return zdatachannel_data_send(p_data, &p_length);
}
uint32_t zdatachannel_block_data_send(uint8_t* p_data, uint16_t* p_length);
static inline uint32_t zdatachannel_block_data_send2(uint8_t* p_data, uint16_t p_length){
return zdatachannel_block_data_send(p_data, &p_length);
}
uint32_t zdatachannel_last_rx_data_haspassed_s();
#ifdef __cplusplus
}
#endif
#endif // ZDATACHANNEL_H__
/** @} */