chore(ble): 增强连接与发送日志,补充 README TODO
- 连接/断开事件打印详细信息 - 发送统计计数(成功/失败) - README 补充 L1 标定、BR 通道排查、马达频率确认待办
This commit is contained in:
+14
-4
@@ -37,11 +37,15 @@ static void adv_work_handler(struct k_work *work) {
|
||||
|
||||
/** @brief 连接建立回调,接管连接引用并收紧连接参数。 */
|
||||
static void connected(struct bt_conn *conn, uint8_t err) {
|
||||
if (err) return;
|
||||
if (err) {
|
||||
LOG_WRN("Connection failed (err %u)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
current_conn = bt_conn_ref(conn);
|
||||
k_work_cancel_delayable(&adv_work);
|
||||
nus_notification_enabled = false;
|
||||
LOG_INF("BLE connected");
|
||||
|
||||
struct bt_le_conn_param param = { .interval_min = 6, .interval_max = 12, .latency = 0, .timeout = 400 };
|
||||
bt_conn_le_param_update(conn, ¶m);
|
||||
@@ -50,7 +54,7 @@ static void connected(struct bt_conn *conn, uint8_t err) {
|
||||
/** @brief 断开回调,释放连接引用并安排延时重广播。 */
|
||||
static void disconnected(struct bt_conn *conn, uint8_t reason) {
|
||||
ARG_UNUSED(conn);
|
||||
ARG_UNUSED(reason);
|
||||
LOG_WRN("BLE disconnected (reason 0x%02x)", reason);
|
||||
if (current_conn) {
|
||||
bt_conn_unref(current_conn);
|
||||
current_conn = NULL;
|
||||
@@ -108,23 +112,29 @@ void ble_transport_adv_start(void) {
|
||||
|
||||
void ble_transport_send(const uint8_t *data, uint16_t len) {
|
||||
static bool first_send_logged = false;
|
||||
static uint32_t send_ok_count;
|
||||
static uint32_t send_fail_count;
|
||||
|
||||
if (!nus_notification_enabled || !current_conn) return;
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int err = bt_nus_send(current_conn, data, len);
|
||||
if (err == 0) {
|
||||
send_ok_count++;
|
||||
if (!first_send_logged) {
|
||||
LOG_INF("First NUS send OK (len=%u)", len);
|
||||
first_send_logged = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (i == 0) {
|
||||
LOG_DBG("NUS send retry (err=%d)", err);
|
||||
}
|
||||
k_usleep(500);
|
||||
}
|
||||
LOG_WRN("NUS send failed after retries (len=%u)", len);
|
||||
send_fail_count++;
|
||||
LOG_WRN("NUS send failed x3 (len=%u, ok=%u, fail=%u)", len, send_ok_count, send_fail_count);
|
||||
}
|
||||
|
||||
bool ble_transport_is_ready(void) {
|
||||
return nus_notification_enabled && (current_conn != NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user