Hi,
in my current project I need to have a wall clock time, thus I am using the date_time library. While trying to understand it I came across a few issues/questions.
All in here: ncs 2.1.2
The reason is to compensate for crystal clock drift (esp the clock_gettime based on this) of long running system each time I connect to the network to send my data maybe once a day. The modem is kept in psm the other time.
Ideal would be: update the time from the cellular network time if available and if not use an ntp server. All of this automatically as soon as I connect (get out of psm).
By reading the docs my first try was:
CONFIG_DATE_TIME=y
CONFIG_DATE_TIME_MODEM=y
CONFIG_DATE_TIME_NTP=y
CONFIG_DATE_TIME_AUTO_UPDATE=y
CONFIG_DATE_TIME_TOO_OLD_SECONDS= some value > 0
CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS = 0 (to disable automatic time polling, only when I come out of psm)
First issue here is the ASSERT in date_time_core.c line 19 does not allow me to set CONFIG_DATE_TIME_TOO_OLD_SECONDS to
a value bigger than CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS. But according to the documentation this should be possible
(Kconfig help for CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS: "Setting this option to 0 disables sequential date time updates."
And by looking at the source code I do no see a reason why this assert should not pass for CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS=0
(I might have overseen something).
Second issue: On a regular basis the lib only updates automatically on %XTIME notifications from the network. I guess I do have no influence on
when this happens. I see it when I register to the network but not when coming out of psm (at least not every time at short intervals). The lib also
forces an update (besides XTIME) when the modem registers to the network (and no XTIME was received before) but this should ideally only
happen once at startup.
I had somehow expected it to query NTP when no network time information (XTIME) is received when coming out of psm in automatic mode.
The documentation states "Automatic update when connecting to LTE network if the option CONFIG_DATE_TIME_AUTO_UPDATE
is set". Maybe
this should be clarified to "...when registering to the network..." (so others don't have the misunderstanding as I)
Third issue (not really affecting me but maybe others): as soon as the first %XTIME notification was received once the lib will always trusts the modem time
if the modem provides a time by +CCLK (noticed by reading the source code). Isn't that an issue when changing the network (asset tracking applications) to
a network that does not provide a time?
Or does the modem invalidates it's time when it hasn't received a time from the network for a longer period? There must be a clock drift, too...
Somehow I would expect the lib to fallback to NTP then (when configured) but it doesn't (when the modem still provides a time).
In the end I am a little bit puzzled on how to achieve a robust and deterministic behavior with the library. After writing these questions and reading over
the documentation again the documentation seems to be mostly correct but still not very intuitive, see my misunderstandings above.
For me I will most likely do my updates manually by date_time update_async().
Whishlist for future versions:
- Query NTP if last XTIME event is older than CONFIG_XYZ
- Also have this functionality callable (like update_async now). So you can be sure to have a "fresh" time after the call and not a silent fallback to the modem's time.
Two question for the end: How accurate is the modem clock? A comment in the lib states "... as modem clock is more accurate than
application side clock". I somehow assumed it is also only based on the LFXO which is also the base for the zephyr timekeeping when the modem is enabled.
How often are time updates send typically from the network if they are provided?
Thanks for following me up to here.
Regads
Clemens