[Solved] Turn SNTP on and off on the fly

  1. My goal is: I want to start running with the SNTP client turned off and, later, “on-the-fly” (no intervening reboot) turn it on. This is for testing purposes (need to test logic that accumulates events before SNTP gets the time, and then corrects and transmits them after we have received a time update (using the MGOS_EVENT_TIME_CHANGED event to detect that).
  2. My actions are:
    My mos.yml has sntp.enable set to false, and debug.level set to 3. I have this RPC handler:
  bool was_enabled = mgos_sys_config_get_sntp_enable();

  mgos_sys_config_set_sntp_enable(true);

  if (!was_enabled) {
    LOG (LL_INFO, ("turning on the SNTP client"));
    mgos_sntp_init();
  }

  mg_rpc_send_responsef(ri, "{enabled: %d}", mgos_sys_config_get_sntp_enable());
  1. The result I see is: when this RPC is invoked, I see the “turning on the SNTP client”, but the SNTP client does not start (no “mgos_sntp.c:127 SNTP next query in 1040 ms”, no logging of UDP packets to time.google.com by mg_net.c)

I have verified that I would see these in the log if I reboot with sntp.enable=true:

[Jul 23 11:37:50.507] mg_net.c:911            0x3ffc8c08 udp://time.google.com:123 -,-,-
...
[Jul 23 11:37:50.521] mgos_sntp.c:95          SNTP query to time.google.com
[Jul 23 11:37:50.527] mgos_sntp.c:127         SNTP next query in 2196 ms
...
[Jul 23 11:37:50.551] mg_net.c:794            0x3ffc8c08 udp://216.239.35.12:123 -> 0
[Jul 23 11:37:50.556] mgos_sntp.c:48          SNTP sent query to 216.239.35.12
[Jul 23 11:37:50.592] mgos_sntp.c:59          SNTP reply from 216.239.35.12: time 1595518671.429117, local 3.856112, delta 1595518667.573005
[Jul 23 11:37:50.601] mgos_event.c:135        ev MOS3 triggered 2 handlers
[Jul 23 11:37:50.607] mgos_sntp.c:78          SNTP close
[Jul 23 11:37:50.610] mgos_sntp.c:127         SNTP next query in 7275545 ms
  1. My expectation & question is: I would expect for the SNTP client to begin querying the SNTP server if I call mgos_sys_config_set_sntp_enable(true), then call mgos_sntp_init(). Why doesn’t it?

I reread the source code in mgos_sntp. The process of firing off SNTP requests only starts after receipt of a MGOS_NET_EV_IP_ACQUIRED event after mgos_sntp_init() is called.

Adding mgos_event_trigger(MGOS_NET_EV_IP_ACQUIRED, NULL); after the mgos_sntp_init(); seems to work, without any adverse effects.

DId you try changing the update time to a huge number instead of disabling ? What is the reason for disabling it ? Perhaps something related to that can be exploited

Changing the update time won’t help; if I enable at system start, I will get SNTP updates as soon as the IP address gets assigned.