This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Value returned by model_timer_elapsed_ticks_get() is wrong

The function model_timer_elapsed_ticks_get() returns the total number of ticks since the timer was started. Hence it's expected that this function to return a value > 0 when called after the timer has expired for the first time. However, this isn't the case.

This function simply returns the value p_timer->total_rtc_ticks which is updated in the function timeout_update_and_schedule(). The problem is that this function is called after the user callback for the timer is called. This can be seen in the code below.

static void model_timer_cb(void * p_context)
{
    model_timer_t * p_timer = (model_timer_t *) p_context;

    NRF_MESH_ASSERT(p_timer->cb != NULL);

    if (p_timer->remaining_ticks == 0)
    {
        /* Trigger callback and repeat if required */
        p_timer->cb_active = true;
        p_timer->cb(p_timer->p_context);
        p_timer->cb_active = false;

        if (p_timer->mode == MODEL_TIMER_MODE_REPEATED)
        {
            p_timer->remaining_ticks = p_timer->timeout_rtc_ticks;
        }
    }

    /* Continue if time is left */
    if (p_timer->remaining_ticks > 0)
    {
        (void) timeout_update_and_schedule(p_timer);
    }
}

The attached patch moves the update code in timeout_update_and_schedule() into model_timer_cb() where it belongs.

0001-Update-model-timer-total_rtc_ticks-before-callback.patch

The unit test for model_common should probably be updated to verify this bug.

Related