对以下代码进行注释,并给出可复制代码static int ble_tp_recv_wr(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { BT_WARN("recv data len=%d, offset=%d, flag=%d", len, offset, flags); if (flags & BT_GATT_WRITE_FLAG_PREPARE) { BT_WARN("rcv prepare write request"); return 0; } if(flags & BT_GATT_WRITE_FLAG_CMD) { BT_WARN("rcv write command"); } else { BT_WARN("rcv write request / exce write"); } return len; } void indicate_rsp(struct bt_conn *conn, const struct bt_gatt_attr *attr, u8_t err) { BT_WARN("receive confirm, err:%d", err); struct bt_gatt_indicate_params *ind_params = (struct bt_gatt_indicate_params *)bt_att_get_att_req(conn); if(ind_params) { k_free(ind_params); } } static void ble_tp_ind_ccc_changed(const struct bt_gatt_attr *attr, u16_t value) { int err = -1; char data[9] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; if(value == BT_GATT_CCC_INDICATE) { err = bl_tp_send_indicate(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_IND_ATTR_VAL_INDEX), data, 9); BT_WARN("ble tp send indatcate: %d", err); } }
时间: 2023-06-26 08:07:53 浏览: 132
// 定义一个函数,用于处理 BLE 数据接收
static int ble_tp_recv_wr(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) {
// 打印接收到的数据长度、偏移量和标志位
BT_WARN("recv data len=%d, offset=%d, flag=%d", len, offset, flags);
// 如果是准备写操作,则返回 0
if (flags & BT_GATT_WRITE_FLAG_PREPARE) {
BT_WARN("rcv prepare write request");
return 0;
}
// 如果是写命令,则打印接收到的写命令信息
if(flags & BT_GATT_WRITE_FLAG_CMD) {
BT_WARN("rcv write command");
} else { // 否则打印接收到的写请求或执行写操作
BT_WARN("rcv write request / exce write");
}
// 返回数据长度
return len;
}
// 定义一个函数,用于处理 BLE 确认响应
void indicate_rsp(struct bt_conn *conn, const struct bt_gatt_attr *attr, u8_t err) {
// 打印接收到的确认响应和错误码
BT_WARN("receive confirm, err:%d", err);
// 获取指示参数,并释放内存
struct bt_gatt_indicate_params *ind_params = (struct bt_gatt_indicate_params *)bt_att_get_att_req(conn);
if(ind_params) {
k_free(ind_params);
}
}
// 定义一个函数,用于处理 BLE 通知/指示属性的 CCC(Client Characteristic Configuration)变化
static void ble_tp_ind_ccc_changed(const struct bt_gatt_attr *attr, u16_t value) {
// 定义一个数组,用于存储指示数据
int err = -1;
char data[9] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
// 如果 CCC 值为 BT_GATT_CCC_INDICATE,则发送指示数据
if(value == BT_GATT_CCC_INDICATE) {
err = bl_tp_send_indicate(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_IND_ATTR_VAL_INDEX), data, 9);
BT_WARN("ble tp send indatcate: %d", err);
}
}
阅读全文