diff --git a/include/timesync.h b/include/timesync.h index 498952c1..286d82e7 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 0x06 // timeserver answer frame length [bytes] #define TIME_SYNC_FIXUP 4 // calibration to fixup processing time [milliseconds] void timesync_init(void); diff --git a/src/lorawan.cpp b/src/lorawan.cpp index e82ac1f5..b5e941cb 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -265,9 +265,9 @@ void onEvent(ev_t ev) { #if (TIME_SYNC_LORASERVER) // timesync answer -> call timesync processor - if ((LMIC.frame[LMIC.dataBeg - 1] >= TIMEANSWERPORT_MIN) && - (LMIC.frame[LMIC.dataBeg - 1] <= TIMEANSWERPORT_MAX)) { - recv_timesync_ans(LMIC.frame[LMIC.dataBeg - 1], + if ((LMIC.frame[LMIC.dataBeg] >= TIMEANSWERPORT_MIN) && + (LMIC.frame[LMIC.dataBeg] <= TIMEANSWERPORT_MAX)) { + recv_timesync_ans(LMIC.frame[LMIC.dataBeg], LMIC.frame + LMIC.dataBeg, LMIC.dataLen); break; } diff --git a/src/timesync.cpp b/src/timesync.cpp index 940696c5..488561c2 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -174,15 +174,15 @@ int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len) { else { // we received a probably valid time frame uint8_t k = seq_no % TIME_SYNC_SAMPLES; - + uint8_t *timestamp_buf = buf+1; // 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]; + uint16_t timestamp_msec = 4 * timestamp_buf[4]; // pointers to 4 bytes 4 bytes containing UTC seconds since unix epoch, msb uint32_t timestamp_sec, *timestamp_ptr; // convert buffer to uint32_t, octet order is big endian - timestamp_ptr = (uint32_t *)buf; + timestamp_ptr = (uint32_t *)timestamp_buf; // swap byte order from msb to lsb, note: this is platform dependent timestamp_sec = __builtin_bswap32(*timestamp_ptr);