Skip to content

Commit

Permalink
Add RTC synchronization
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed Jun 8, 2024
1 parent fb0b06e commit b4f8be4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,5 @@ find_program(SETCAP setcap)
install(TARGETS pihole-FTL
RUNTIME DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
install(CODE "execute_process(COMMAND ${SETCAP} CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN,CAP_SYS_NICE,CAP_CHOWN,CAP_SYS_TIME+eip \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/bin/pihole-FTL)")
install(CODE "execute_process(COMMAND ${SETCAP} CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN,CAP_SYS_NICE,CAP_CHOWN,CAP_SYS_TIME,CAP_SYS_RESOURCE+eip \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/bin/pihole-FTL)")

13 changes: 13 additions & 0 deletions src/api/docs/content/specs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ components:
type: integer
count:
type: integer
rtc:
type: object
properties:
set:
type: boolean
device:
type: string
utc:
type: boolean
resolver:
type: object
properties:
Expand Down Expand Up @@ -700,6 +709,10 @@ components:
server: "pool.ntp.org"
interval: 3600
count: 8
rtc:
set: true
device: ""
utc: true
resolver:
resolveIPv4: true
resolveIPv6: true
Expand Down
19 changes: 19 additions & 0 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,25 @@ void initConfig(struct config *conf)
conf->ntp.sync.count.d.ui = 8;
conf->ntp.sync.count.c = validate_stub; // Only type-based checking

conf->ntp.rtc.set.k = "ntp.rtc.set";
conf->ntp.rtc.set.h = "Should FTL update a real-time clock (RTC) if available?";
conf->ntp.rtc.set.t = CONF_BOOL;
conf->ntp.rtc.set.d.b = true;
conf->ntp.rtc.set.c = validate_stub; // Only type-based checking

conf->ntp.rtc.device.k = "ntp.rtc.device";
conf->ntp.rtc.device.h = "Path to the RTC device to update. Leave emtpy for auto-discovery";

Check failure on line 861 in src/config/config.c

View workflow job for this annotation

GitHub Actions / spell-check

emtpy ==> empty
conf->ntp.rtc.device.a = cJSON_CreateStringReference("Path to the RTC device, e.g., \"/dev/rtc0\"");
conf->ntp.rtc.device.t = CONF_STRING;
conf->ntp.rtc.device.d.s = (char*)"";
conf->ntp.rtc.device.c = validate_stub; // Only type-based checking

conf->ntp.rtc.utc.k = "ntp.rtc.utc";
conf->ntp.rtc.utc.h = "Should the RTC be set to UTC?";
conf->ntp.rtc.utc.t = CONF_BOOL;
conf->ntp.rtc.utc.d.b = true;
conf->ntp.rtc.utc.c = validate_stub; // Only type-based checking


// struct resolver
conf->resolver.resolveIPv6.k = "resolver.resolveIPv6";
Expand Down
5 changes: 5 additions & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ struct config {
struct conf_item interval;
struct conf_item count;
} sync;
struct {
struct conf_item set;
struct conf_item device;
struct conf_item utc;
} rtc;
} ntp;

struct {
Expand Down
1 change: 1 addition & 0 deletions src/ntp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
set(ntp_sources
server.c
client.c
rtc.c
ntp.h
)

Expand Down
4 changes: 4 additions & 0 deletions src/ntp/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ static bool settime_step(const double offset)
return false;
}

// Adjust RTC if available
if(config.ntp.rtc.set.v.b)
ntp_sync_rtc();

return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/ntp/ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ bool ntp_client(const char *server, const bool settime, const bool print);
// Start NTP sync thread
bool ntp_start_sync_thread(pthread_attr_t *attr);

// Sync RTC time
bool ntp_sync_rtc(void);

// Number of NTP queries to average. The more queries, the more accurate the
// time, but the longer it takes to synchronize. The minimum is 1.
#define NTP_AVERGAGE_COUNT 8
Expand Down
15 changes: 14 additions & 1 deletion test/pihole.toml
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,19 @@
# Number of NTP syncs to perform and average before updating the system time
count = 8

[ntp.rtc]
# Should FTL update a real-time clock (RTC) if available?
set = true

# Path to the RTC device to update. Leave emtpy for auto-discovery

Check failure on line 502 in test/pihole.toml

View workflow job for this annotation

GitHub Actions / spell-check

emtpy ==> empty
#
# Possible values are:
# Path to the RTC device, e.g., "/dev/rtc0"
device = ""

# Should the RTC be set to UTC?
utc = true

[resolver]
# Should FTL try to resolve IPv4 addresses to hostnames?
resolveIPv4 = false ### CHANGED, default = true
Expand Down Expand Up @@ -1077,7 +1090,7 @@
all = true ### CHANGED, default = false

# Configuration statistics:
# 144 total entries out of which 89 entries are default
# 147 total entries out of which 92 entries are default
# --> 55 entries are modified
# 2 entries are forced through environment:
# - misc.nice
Expand Down

0 comments on commit b4f8be4

Please sign in to comment.