move timesync seq_no into message body from port

This commit is contained in:
Florian Ludwig 2019-08-29 10:32:55 +03:00
parent 4a573bc638
commit 4d42c961b2
4 changed files with 16 additions and 17 deletions

View File

@ -12,7 +12,7 @@
void timesync_init(void); void timesync_init(void);
void send_timesync_req(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 process_timesync_req(void *taskparameter);
void store_time_sync_req(uint32_t t_millisec); void store_time_sync_req(uint32_t t_millisec);
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t timesource); void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t timesource);

View File

@ -265,10 +265,8 @@ void onEvent(ev_t ev) {
#if (TIME_SYNC_LORASERVER) #if (TIME_SYNC_LORASERVER)
// timesync answer -> call timesync processor // timesync answer -> call timesync processor
if ((LMIC.frame[LMIC.dataBeg] >= TIMEANSWERPORT_MIN) && if ((LMIC.frame[LMIC.dataBeg] == TIMEPORT) {
(LMIC.frame[LMIC.dataBeg] <= TIMEANSWERPORT_MAX)) { recv_timesync_ans(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
recv_timesync_ans(LMIC.frame[LMIC.dataBeg],
LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
break; break;
} }
#endif #endif

View File

@ -93,10 +93,9 @@
#define BEACONPORT 6 // beacon alarms #define BEACONPORT 6 // beacon alarms
#define BMEPORT 7 // BME680 sensor #define BMEPORT 7 // BME680 sensor
#define BATTPORT 8 // battery voltage #define BATTPORT 8 // battery voltage
#define TIMEPORT 9 // time query #define TIMEPORT 9 // time query and response
#define TIMEDIFFPORT 13 // time adjust diff #define TIMEDIFFPORT 13 // time adjust diff
#define TIMEANSWERPORT_MIN 0xA0 // time answer, start of port range #define TIMEREQUEST_MAX_SEQNO 250 // time answer, start of port range
#define TIMEANSWERPORT_MAX 0xDF // time answer, end of port range
#define SENSOR1PORT 10 // user sensor #1 #define SENSOR1PORT 10 // user sensor #1
#define SENSOR2PORT 11 // user sensor #2 #define SENSOR2PORT 11 // user sensor #2
#define SENSOR3PORT 12 // user sensor #3 #define SENSOR3PORT 12 // user sensor #3

View File

@ -26,7 +26,7 @@ typedef std::chrono::duration<long long int, std::ratio<1, 1000>>
TaskHandle_t timeSyncReqTask = NULL; 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 bool timeSyncPending = false;
static myClock_timepoint time_sync_tx[TIME_SYNC_SAMPLES]; static myClock_timepoint time_sync_tx[TIME_SYNC_SAMPLES];
static myClock_timepoint time_sync_rx[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<milliseconds>(time_sync_tx[k]); time_point_cast<milliseconds>(time_sync_tx[k]);
// wrap around seqNo, keeping it in time port range // wrap around seqNo, keeping it in time port range
time_sync_seqNo = (time_sync_seqNo < TIMEANSWERPORT_MAX) time_sync_seqNo++;
? time_sync_seqNo + 1 if(time_sync_seqNo > TIMEREQUEST_MAX_SEQNO) {
: TIMEANSWERPORT_MIN; time_sync_seqNo = 0;
}
if (i < TIME_SYNC_SAMPLES - 1) { if (i < TIME_SYNC_SAMPLES - 1) {
// wait until next cycle // wait until next cycle
@ -155,7 +156,9 @@ void store_time_sync_req(uint32_t timestamp) {
} }
// process timeserver timestamp answer, called from lorawan.cpp // 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 no timesync handshake is pending then exit
if (!timeSyncPending) 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 else { // we received a probably valid time frame
uint8_t k = seq_no % TIME_SYNC_SAMPLES; 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 // the 5th byte contains the fractional seconds in 2^-8 second steps
// (= 1/250th sec), we convert this to ms // (= 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 // pointers to 4 bytes 4 bytes containing UTC seconds since unix epoch, msb
uint32_t timestamp_sec, *timestamp_ptr; uint32_t timestamp_sec, *timestamp_ptr;
// convert buffer to uint32_t, octet order is big endian // 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 // swap byte order from msb to lsb, note: this is platform dependent
timestamp_sec = __builtin_bswap32(*timestamp_ptr); timestamp_sec = __builtin_bswap32(*timestamp_ptr);