MicroOcpp icon indicating copy to clipboard operation
MicroOcpp copied to clipboard

Clock instance does not appear to handle rollover on ESP-IDF

Open devunwired opened this issue 3 months ago • 4 comments

The time retrieval function for Clock::now() relies on the platform-defined function mocpp_tick_ms(). For ESP-IDF, this function is defined as follows:

unsigned long mocpp_tick_ms_espidf() {
    auto ticks_now = xTaskGetTickCount();
    MicroOcpp::mocpp_millis_count += ((ticks_now - MicroOcpp::mocpp_ticks_count) * 1000UL) / configTICK_RATE_HZ;
    MicroOcpp::mocpp_ticks_count = ticks_now;
    return MicroOcpp::mocpp_millis_count;
}

While the frequency of xTaskGetTickCount() is configurable (defaults to 1000), on the Xtensa architecture unsigned long and TickType_t are both defined as 32-bits. This means that, regardless of the tick counter configuration, a value like mocpp_millis_count will overflow after ~49.7 days of continuous operation.

Clock::now() does not account for this rollover and simply calculates the difference, allowing a potentially negative time difference to be applied to the internal clock.

devunwired avatar Oct 13 '25 15:10 devunwired