对以下代码进行注释并给出可复制代码static void ble_tp_notify_task(void *pvParameters) { int err = -1; char data[244] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; while(1) { err = bt_gatt_notify(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_NOT_ATTR_VAL_INDEX), data, (tx_mtu_size - 3)); BT_WARN("ble tp send notify : %d", err); } } static void ble_tp_not_ccc_changed(const struct bt_gatt_attr attr, u16_t value) { int err; BT_WARN("ccc:value=[%d]",value); if(tp_start) { if(value == BT_GATT_CCC_NOTIFY) { if(xTaskCreate(ble_tp_notify_task, (char)"bletp", 256, NULL, TP_PRIO, &ble_tp_task_h) == pdPASS) { created_tp_task = 1; BT_WARN("Create throughput tx task success."); } else { created_tp_task = 0; BT_WARN("Create throughput tx task fail."); } } else { if(created_tp_task) { BT_WARN("Delete throughput tx task."); vTaskDelete(ble_tp_task_h); created_tp_task = 0; } } } else { if(created_tp_task) { BT_WARN("Delete throughput tx task."); vTaskDelete(ble_tp_task_h); created_tp_task = 0; } if(value == BT_GATT_CCC_NOTIFY) { err = bt_gatt_notify(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_NOT_ATTR_VAL_INDEX), "notify", strlen("notify")); BT_WARN("ble tp send indatcate: %d", err); } } } static struct bt_gatt_attr attrs[]= { BT_GATT_PRIMARY_SERVICE(BT_UUID_SVC_BLE_TP), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_RD, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, ble_tp_recv_rd, NULL, NULL), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_WR, BT_GATT_CHRC_WRITE |BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE|BT_GATT_PERM_PREPARE_WRITE, NULL, ble_tp_recv_wr, NULL), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_IND, BT_GATT_CHRC_INDICATE, 0, NULL, NULL, NULL), BT_GATT_CCC(ble_tp_ind_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_NOT, BT_GATT_CHRC_NOTIFY, 0, NULL, NULL, NULL), BT_GATT_CCC(ble_tp_not_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE) };
时间: 2023-06-26 12:08:00 浏览: 187
ble-sniffer_win_1.0.1_1111_Sniffer (1).zip_BLE win_ble-sniffer_b
5星 · 资源好评率100%
// 以下代码为BLE传输协议任务和相关函数的实现
// BLE传输协议任务函数,负责向BLE设备发送数据通知
static void ble_tp_notify_task(void *pvParameters) {
int err = -1; // 错误码
char data[244] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; // 待发送的数据
while(1) {
err = bt_gatt_notify(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_NOT_ATTR_VAL_INDEX), data, (tx_mtu_size - 3)); // 发送数据通知
BT_WARN("ble tp send notify : %d", err); // 打印调试信息
}
}
// BLE传输协议通知CCC改变回调函数
static void ble_tp_not_ccc_changed(const struct bt_gatt_attr attr, u16_t value) {
int err; // 错误码
BT_WARN("ccc:value=[%d]",value); // 打印调试信息
if(tp_start) { // 如果传输已经开始
if(value == BT_GATT_CCC_NOTIFY) { // 如果CCC值为通知
if(xTaskCreate(ble_tp_notify_task, (char)"bletp", 256, NULL, TP_PRIO, &ble_tp_task_h) == pdPASS) { // 创建数据通知任务
created_tp_task = 1; // 标记任务已经创建
BT_WARN("Create throughput tx task success."); // 打印调试信息
} else {
created_tp_task = 0; // 标记任务未创建
BT_WARN("Create throughput tx task fail."); // 打印调试信息
}
} else { // 如果CCC值为非通知
if(created_tp_task) { // 如果任务已经创建
BT_WARN("Delete throughput tx task."); // 打印调试信息
vTaskDelete(ble_tp_task_h); // 删除数据通知任务
created_tp_task = 0; // 标记任务未创建
}
}
} else { // 如果传输未开始
if(created_tp_task) { // 如果任务已经创建
BT_WARN("Delete throughput tx task."); // 打印调试信息
vTaskDelete(ble_tp_task_h); // 删除数据通知任务
created_tp_task = 0; // 标记任务未创建
}
if(value == BT_GATT_CCC_NOTIFY) { // 如果CCC值为通知
err = bt_gatt_notify(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_NOT_ATTR_VAL_INDEX), "notify", strlen("notify")); // 发送数据指示
BT_WARN("ble tp send indatcate: %d", err); // 打印调试信息
}
}
}
// 定义BLE传输协议的Gatt属性
static struct bt_gatt_attr attrs[]= {
BT_GATT_PRIMARY_SERVICE(BT_UUID_SVC_BLE_TP), // BLE传输协议服务
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_RD, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, ble_tp_recv_rd, NULL, NULL), // BLE传输协议读特征
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_WR, BT_GATT_CHRC_WRITE |BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE|BT_GATT_PERM_PREPARE_WRITE, NULL, ble_tp_recv_wr, NULL), // BLE传输协议写特征
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_IND, BT_GATT_CHRC_INDICATE, 0, NULL, NULL, NULL), // BLE传输协议通知特征
BT_GATT_CCC(ble_tp_ind_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), // BLE传输协议通知CCC
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_NOT, BT_GATT_CHRC_NOTIFY, 0, NULL, NULL, NULL), // BLE传输协议指示特征
BT_GATT_CCC(ble_tp_not_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE) // BLE传输协议指示CCC
};
阅读全文