From 4d42c961b22b697eac107e627a315a69a8df93ca Mon Sep 17 00:00:00 2001 From: Florian Ludwig Date: Thu, 29 Aug 2019 10:32:55 +0300 Subject: [PATCH] move timesync seq_no into message body from port --- include/timesync.h | 2 +- src/lorawan.cpp | 6 ++---- src/paxcounter.conf | 7 +++---- src/timesync.cpp | 18 ++++++++++-------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/include/timesync.h b/include/timesync.h index 286d82e7..1f431acb 100644 --- a/include/timesync.h +++ b/include/timesync.h @@ -12,7 +12,7 @@ void timesync_init(void); void send_timesync_req(void); -int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len); +int recv_timesync_ans(uint8_t buf[], uint8_t buf_len); void process_timesync_req(void *taskparameter); void store_time_sync_req(uint32_t t_millisec); void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t timesource); diff --git a/src/lorawan.cpp b/src/lorawan.cpp index b5e941cb..ca4425f1 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -265,10 +265,8 @@ void onEvent(ev_t ev) { #if (TIME_SYNC_LORASERVER) // timesync answer -> call timesync processor - 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); + if ((LMIC.frame[LMIC.dataBeg] == TIMEPORT) { + recv_timesync_ans(LMIC.frame + LMIC.dataBeg, LMIC.dataLen); break; } #endif diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 9dffe04e..6d5eb508 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -28,7 +28,7 @@ * |< Scan Window > |< Scan Window > | ... |< Scan Window > | * |< Scan Interval >|< Scan Interval >| ... |< Scan Interval >| * |< Scan duration >| -* +* * Scan duration sets how long scanning should be going on, before starting a new scan cycle. 0 means infinite (default). * Scan window sets how much of the interval should be occupied by scanning. Should be >= BLESCANINTERVAL. * Scan interval is how long scanning should be done on each channel. BLE uses 3 channels for advertising. @@ -93,10 +93,9 @@ #define BEACONPORT 6 // beacon alarms #define BMEPORT 7 // BME680 sensor #define BATTPORT 8 // battery voltage -#define TIMEPORT 9 // time query +#define TIMEPORT 9 // time query and response #define TIMEDIFFPORT 13 // time adjust diff -#define TIMEANSWERPORT_MIN 0xA0 // time answer, start of port range -#define TIMEANSWERPORT_MAX 0xDF // time answer, end of port range +#define TIMEREQUEST_MAX_SEQNO 250 // time answer, start of port range #define SENSOR1PORT 10 // user sensor #1 #define SENSOR2PORT 11 // user sensor #2 #define SENSOR3PORT 12 // user sensor #3 diff --git a/src/timesync.cpp b/src/timesync.cpp index 09e28b3b..11ca1787 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -26,7 +26,7 @@ typedef std::chrono::duration> TaskHandle_t timeSyncReqTask = NULL; -static uint8_t time_sync_seqNo = random(TIMEANSWERPORT_MIN, TIMEANSWERPORT_MAX); +static uint8_t time_sync_seqNo = 0; static bool timeSyncPending = false; static myClock_timepoint time_sync_tx[TIME_SYNC_SAMPLES]; static myClock_timepoint time_sync_rx[TIME_SYNC_SAMPLES]; @@ -94,9 +94,10 @@ void process_timesync_req(void *taskparameter) { time_point_cast(time_sync_tx[k]); // wrap around seqNo, keeping it in time port range - time_sync_seqNo = (time_sync_seqNo < TIMEANSWERPORT_MAX) - ? time_sync_seqNo + 1 - : TIMEANSWERPORT_MIN; + time_sync_seqNo++; + if(time_sync_seqNo > TIMEREQUEST_MAX_SEQNO) { + time_sync_seqNo = 0; + } if (i < TIME_SYNC_SAMPLES - 1) { // wait until next cycle @@ -155,7 +156,9 @@ void store_time_sync_req(uint32_t timestamp) { } // process timeserver timestamp answer, called from lorawan.cpp -int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len) { +int recv_timesync_ans(uint8_t buf[], uint8_t buf_len) { + uint8_t seq_no = buf[0]; + buf++; // if no timesync handshake is pending then exit if (!timeSyncPending) @@ -175,15 +178,14 @@ 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 * timestamp_buf[4]; + uint16_t timestamp_msec = 4 * 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 *)timestamp_buf; + timestamp_ptr = (uint32_t *)buf; // swap byte order from msb to lsb, note: this is platform dependent timestamp_sec = __builtin_bswap32(*timestamp_ptr);