diff --git a/include/timesync.h b/include/timesync.h index c102c1c4..82da84f7 100644 --- a/include/timesync.h +++ b/include/timesync.h @@ -7,7 +7,7 @@ #include "timekeeper.h" //#define TIME_SYNC_TRIGGER 100 // threshold for time sync [milliseconds] -#define TIME_SYNC_FRAME_LENGTH 0x05 // timeserver answer frame length [bytes] +#define TIME_SYNC_FRAME_LENGTH 0x07 // timeserver answer frame length [bytes] #define TIME_SYNC_FIXUP 4 // calibration to fixup processing time [milliseconds] void timesync_init(void); @@ -16,4 +16,4 @@ int recv_timesync_ans(const uint8_t seq_no, const uint8_t buf[], const uint8_t b void process_timesync_req(void *taskparameter); void store_time_sync_req(uint32_t t_millisec); -#endif \ No newline at end of file +#endif diff --git a/src/timesync.cpp b/src/timesync.cpp index 8ce3238f..6c60a225 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -177,9 +177,14 @@ int recv_timesync_ans(const uint8_t seq_no, const uint8_t buf[], const uint8_t b // the 5th byte contains the fractional seconds in 2^-8 second steps // (= 1/250th sec), we convert this to ms - uint16_t timestamp_msec = 4 * buf[4]; - // pointers to 4 bytes containing UTC seconds since unix epoch, msb + uint16_t timestamp_msec = 4 * buf[6]; + // pointers to 4 bytes 4 bytes containing UTC seconds since unix epoch, msb uint32_t timestamp_sec, *timestamp_ptr; + uint32_t timezone_sec; + + // extract timezone from buffer (in 15min steps, one step being 15min * 60s = 900s) + timezone_sec = buf[0]*900; + buf++; // convert buffer to uint32_t, octet order is big endian timestamp_ptr = (uint32_t *)buf; @@ -187,7 +192,7 @@ int recv_timesync_ans(const uint8_t seq_no, const uint8_t buf[], const uint8_t b timestamp_sec = __builtin_bswap32(*timestamp_ptr); // construct the timepoint when message was seen on gateway - time_sync_rx[k] += seconds(timestamp_sec) + milliseconds(timestamp_msec); + time_sync_rx[k] += seconds(timestamp_sec+timezone_sec) + milliseconds(timestamp_msec); // we guess timepoint is recent if it newer than code compile date if (timeIsValid(myClock::to_time_t(time_sync_rx[k]))) {