@@ -95,6 +95,41 @@ namespace SQM
9595 }
9696 return false ;
9797 }
98+
99+ bool isTimeSourceEnabled (const Config &cfg, TimeSource source)
100+ {
101+ return source == TimeSource::NTP ? cfg.ntp .enabled : cfg.gps .enabled ;
102+ }
103+
104+ void normalizeTimeSources (Config &cfg)
105+ {
106+ if (!cfg.ntp .enabled && !cfg.gps .enabled )
107+ {
108+ return ;
109+ }
110+
111+ if (!isTimeSourceEnabled (cfg, cfg.primaryTimeSource ))
112+ {
113+ cfg.primaryTimeSource = cfg.ntp .enabled ? TimeSource::NTP : TimeSource::GPS ;
114+ }
115+
116+ if (!isTimeSourceEnabled (cfg, cfg.secondaryTimeSource ))
117+ {
118+ cfg.secondaryTimeSource = cfg.gps .enabled && cfg.primaryTimeSource != TimeSource::GPS
119+ ? TimeSource::GPS
120+ : TimeSource::NTP ;
121+ }
122+
123+ if (cfg.ntp .enabled && cfg.gps .enabled && cfg.primaryTimeSource == cfg.secondaryTimeSource )
124+ {
125+ cfg.secondaryTimeSource = cfg.primaryTimeSource == TimeSource::NTP ? TimeSource::GPS : TimeSource::NTP ;
126+ }
127+
128+ if (!cfg.ntp .enabled || !cfg.gps .enabled )
129+ {
130+ cfg.secondaryTimeSource = cfg.primaryTimeSource ;
131+ }
132+ }
98133 } // namespace
99134
100135 std::optional<Config> Config::load ()
@@ -352,6 +387,26 @@ namespace SQM
352387 return setError (error, " Secondary time source is invalid" );
353388 }
354389
390+ if (!ntp.enabled && !gps.enabled )
391+ {
392+ return setError (error, " At least one time source must be enabled" );
393+ }
394+
395+ if (!isTimeSourceEnabled (*this , primaryTimeSource))
396+ {
397+ return setError (error, " Primary time source is disabled" );
398+ }
399+
400+ if (ntp.enabled && gps.enabled && !isTimeSourceEnabled (*this , secondaryTimeSource))
401+ {
402+ return setError (error, " Secondary time source is disabled" );
403+ }
404+
405+ if (ntp.enabled && gps.enabled && primaryTimeSource == secondaryTimeSource)
406+ {
407+ return setError (error, " Time sources must be different when both NTP and GPS are enabled" );
408+ }
409+
355410 if (wifi.reconnectDelayMs == 0 || wifi.maxReconnectDelayMs == 0 ||
356411 wifi.reconnectDelayMs > 86400000 || wifi.maxReconnectDelayMs > 86400000 ||
357412 wifi.reconnectDelayMs > wifi.maxReconnectDelayMs )
@@ -615,6 +670,8 @@ namespace SQM
615670 cfg.sensor .i2cFrequency = sensor[" i2cFrequency" ] | 100000 ;
616671 }
617672
673+ normalizeTimeSources (cfg);
674+
618675 std::string validationError;
619676 if (!cfg.validate (&validationError))
620677 {
0 commit comments