+ if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
+ return NULL;
+
+ service_ts_config = connman_service_get_timeservers_config(service);
+
+ /* First add Service Timeservers.Configuration to the list */
+ for (i = 0; service_ts_config && service_ts_config[i];
+ i++)
+ list = __connman_timeserver_add_list(list,
+ service_ts_config[i]);
+
+ service_ts = connman_service_get_timeservers(service);
+
+ /* Then add Service Timeservers via DHCP to the list */
+ for (i = 0; service_ts && service_ts[i]; i++)
+ list = __connman_timeserver_add_list(list, service_ts[i]);
+
+ /*
+ * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
+ * configuration option is set to true.
+ */
+ if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
+ network = __connman_service_get_network(service);
+ if (network) {
+ index = connman_network_get_index(network);
+ service_gw = __connman_ipconfig_get_gateway_from_index(index,
+ CONNMAN_IPCONFIG_TYPE_ALL);
+
+ if (service_gw)
+ list = __connman_timeserver_add_list(list, service_gw);
+ }
+ }
+
+ /* Then add Global Timeservers to the list */
+ timeservers = load_timeservers();
+
+ for (i = 0; timeservers && timeservers[i]; i++)
+ list = __connman_timeserver_add_list(list, timeservers[i]);
+
+ g_strfreev(timeservers);
+
+ fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
+
+ /* Lastly add the fallback servers */
+ for (i = 0; fallback_ts && fallback_ts[i]; i++)
+ list = __connman_timeserver_add_list(list, fallback_ts[i]);
+
+ return g_slist_reverse(list);
+}
+
+static gboolean ts_recheck(gpointer user_data)
+{
+ GSList *ts;
+
+ ts = __connman_timeserver_get_all(connman_service_get_default());
+
+ if (!ts) {
+ DBG("timeservers disabled");
+
+ return TRUE;
+ }
+
+ if (g_strcmp0(ts_current, ts->data) != 0) {
+ DBG("current %s preferred %s", ts_current, (char *)ts->data);
+
+ g_slist_free_full(ts, g_free);
+
+ __connman_timeserver_sync(NULL);
+
+ return FALSE;
+ }
+
+ DBG("");
+
+ g_slist_free_full(ts, g_free);
+
+ return TRUE;
+}
+
+static void ts_recheck_disable(void)
+{
+ if (ts_recheck_id == 0)
+ return;
+
+ g_source_remove(ts_recheck_id);
+ ts_recheck_id = 0;
+
+ if (ts_backoff_id) {
+ g_source_remove(ts_backoff_id);
+ ts_backoff_id = 0;
+ }
+
+ if (ts_current) {
+ g_free(ts_current);
+ ts_current = NULL;
+ }
+}
+
+static void ts_recheck_enable(void)
+{
+ if (ts_recheck_id > 0)
+ return;
+
+ ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
+ NULL);
+}
+
+/*
+ * This function must be called everytime the default service changes, the
+ * service timeserver(s) or gateway changes or the global timeserver(s) changes.
+ */
+int __connman_timeserver_sync(struct connman_service *default_service)
+{
+ struct connman_service *service;
+ char **nameservers;
+ int i;
+
+ if (default_service)