diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h index 7d753a21a..6f47d8184 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h @@ -244,7 +244,7 @@ int k_delayed_work_cancel(struct k_delayed_work *work); int k_delayed_work_free(struct k_delayed_work *work); -void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler); +int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler); /** * @brief Get system uptime. diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c b/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c index 76b7ac2a9..50fe7813e 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c @@ -59,13 +59,13 @@ void bt_mesh_timer_deinit(void) } } -void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) +int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) { osi_alarm_t *alarm = NULL; if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); - return; + return -EINVAL; } k_work_init(&work->work, handler); @@ -74,28 +74,28 @@ void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) if (!hash_map_has_key(bm_alarm_hash_map, (void *)work)) { alarm = osi_alarm_new("bt_mesh", (osi_alarm_callback_t)handler, (void *)&work->work, 0); if (alarm == NULL) { - BT_ERR("%s, Unable to create alarm", __func__); + BT_ERR("%s, Alarm not created", __func__); bt_mesh_alarm_unlock(); - return; + return -EIO; } if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { - BT_ERR("%s Unable to add the timer to hash map.", __func__); + BT_ERR("%s, Alarm not set", __func__); bt_mesh_alarm_unlock(); - return; + return -EIO; } } alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { - BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + BT_ERR("%s, Alarm not found", __func__); bt_mesh_alarm_unlock(); - return; + return -ENODEV; } // Just init the work timer only, don't start it. osi_alarm_cancel(alarm); bt_mesh_alarm_unlock(); - return; + return 0; } int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay) @@ -108,7 +108,7 @@ int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay) bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { - BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + BT_WARN("%s, Alarm not found", __func__); bt_mesh_alarm_unlock(); return -EINVAL; } @@ -130,7 +130,7 @@ int k_delayed_work_submit_periodic(struct k_delayed_work *work, s32_t period) bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { - BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + BT_WARN("%s, Alarm not found", __func__); bt_mesh_alarm_unlock(); return -EINVAL; } @@ -152,7 +152,7 @@ int k_delayed_work_cancel(struct k_delayed_work *work) bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { - BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + BT_WARN("%s, Alarm not found", __func__); bt_mesh_alarm_unlock(); return -EINVAL; } @@ -173,7 +173,7 @@ int k_delayed_work_free(struct k_delayed_work *work) bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { - BT_WARN("%s Unable to find expected alarm in hash map", __func__); + BT_WARN("%s, Alarm not found", __func__); bt_mesh_alarm_unlock(); return -EINVAL; } @@ -196,7 +196,7 @@ s32_t k_delayed_work_remaining_get(struct k_delayed_work *work) bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { - BT_WARN("%s Unable to find expected alarm in hash map", __func__); + BT_WARN("%s, Alarm not found", __func__); bt_mesh_alarm_unlock(); return 0; } diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index 29cc945bb..4e1d94849 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -1163,7 +1163,16 @@ int bt_mesh_start_ble_advertising(const struct bt_mesh_ble_adv_param *param, front = (tx->param.priority == BLE_MESH_BLE_ADV_PRIO_HIGH) ? true : false; bt_mesh_ble_adv_send(buf, &ble_adv_send_cb, tx, front); if (param->count) { - k_delayed_work_init(&tx->resend, ble_adv_resend); + if (k_delayed_work_init(&tx->resend, ble_adv_resend)) { + /* If failed to create a timer, the BLE adv packet will be + * sent only once. Just give a warning here, and since the + * BLE adv packet can be sent, return 0 here. + */ + BT_WARN("Send BLE adv packet only once"); + tx->param.count = 0; + net_buf_unref(buf); + return 0; + } bt_mesh_atomic_set_bit(tx->flags, TIMER_INIT); } else { /* Send the BLE advertising packet only once */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c index fe5653b7b..c976c2737 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c @@ -324,13 +324,17 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, node->opcode = opcode; node->op_pending = bt_mesh_client_get_status_op(client->op_pair, client->op_pair_size, opcode); if (node->op_pending == 0U) { - BT_ERR("%s, Not found the status opcode in the op_pair list", __func__); + BT_ERR("Not found the status opcode in op_pair list"); bt_mesh_free(node); return -EINVAL; } node->timeout = bt_mesh_client_calc_timeout(ctx, msg, opcode, timeout ? timeout : CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT); - k_delayed_work_init(&node->timer, timer_handler); + if (k_delayed_work_init(&node->timer, timer_handler)) { + BT_ERR("%s, Failed to create a timer", __func__); + bt_mesh_free(node); + return -EIO; + } bt_mesh_list_lock(); sys_slist_append(&internal->queue, &node->client_node); @@ -492,6 +496,7 @@ int bt_mesh_client_clear_list(void *data) bt_mesh_list_lock(); while (!sys_slist_is_empty(&internal->queue)) { node = (void *)sys_slist_get_not_empty(&internal->queue); + k_delayed_work_free(&node->timer); bt_mesh_free(node); } bt_mesh_list_unlock();