Moved timesync offset get/set from handler to ports to fix projects not using handler (#699)
This commit is contained in:
+29
-23
@@ -15,14 +15,18 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "bacnet/basic/service/h_ts.h"
|
|
||||||
#include "bacport.h"
|
#include "bacport.h"
|
||||||
#include "bacnet/datetime.h"
|
#include "bacnet/datetime.h"
|
||||||
|
|
||||||
|
static int32_t Time_Offset; /* Time offset in ms */
|
||||||
|
|
||||||
static int32_t timedifference(struct timeval t0, struct timeval t1)
|
/**
|
||||||
|
* @brief Calculate the time offset from the system clock.
|
||||||
|
* @return Time offset in ms
|
||||||
|
*/
|
||||||
|
static int32_t time_difference(struct timeval t0, struct timeval t1)
|
||||||
{
|
{
|
||||||
return (t0.tv_sec - t1.tv_sec)*1000 + (t0.tv_usec - t1.tv_usec) / 1000;
|
return (t0.tv_sec - t1.tv_sec) * 1000 + (t0.tv_usec - t1.tv_usec) / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,32 +36,32 @@ static int32_t timedifference(struct timeval t0, struct timeval t1)
|
|||||||
* @param utc - True for UTC sync, False for Local time
|
* @param utc - True for UTC sync, False for Local time
|
||||||
* @return True if time is set
|
* @return True if time is set
|
||||||
*/
|
*/
|
||||||
void datetime_timesync(
|
void datetime_timesync(BACNET_DATE *bdate, BACNET_TIME *btime, bool utc)
|
||||||
BACNET_DATE *bdate, BACNET_TIME *btime, bool utc)
|
|
||||||
{
|
{
|
||||||
struct timeval tv_inp, tv_sys;
|
struct timeval tv_inp, tv_sys;
|
||||||
struct tm *timeinfo;
|
struct tm *timeinfo;
|
||||||
time_t rawtime;
|
time_t rawtime;
|
||||||
time( &rawtime);
|
time(&rawtime);
|
||||||
timeinfo = localtime(&rawtime);
|
timeinfo = localtime(&rawtime);
|
||||||
/* fixme: only set the time if off by some amount */
|
/* fixme: only set the time if off by some amount */
|
||||||
timeinfo->tm_year = bdate->year-1900;
|
timeinfo->tm_year = bdate->year - 1900;
|
||||||
timeinfo->tm_mon = bdate->month-1;
|
timeinfo->tm_mon = bdate->month - 1;
|
||||||
timeinfo->tm_mday = bdate->day;
|
timeinfo->tm_mday = bdate->day;
|
||||||
timeinfo->tm_hour = btime->hour;
|
timeinfo->tm_hour = btime->hour;
|
||||||
timeinfo->tm_min = btime->min;
|
timeinfo->tm_min = btime->min;
|
||||||
timeinfo->tm_sec = btime->sec;
|
timeinfo->tm_sec = btime->sec;
|
||||||
tv_inp.tv_sec = mktime(timeinfo);
|
tv_inp.tv_sec = mktime(timeinfo);
|
||||||
tv_inp.tv_usec = btime->hundredths*10000;
|
tv_inp.tv_usec = btime->hundredths * 10000;
|
||||||
if (gettimeofday(&tv_sys, NULL) == 0) {
|
if (gettimeofday(&tv_sys, NULL) == 0) {
|
||||||
if (utc) {
|
if (utc) {
|
||||||
handler_timesync_offset_set(timedifference(tv_inp, tv_sys) - (timezone - timeinfo->tm_isdst*3600)*1000);
|
Time_Offset = time_difference(tv_inp, tv_sys) -
|
||||||
|
(timezone - timeinfo->tm_isdst * 3600) * 1000;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
handler_timesync_offset_set(timedifference(tv_inp, tv_sys));
|
Time_Offset = time_difference(tv_inp, tv_sys);
|
||||||
}
|
}
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
printf("Time offset = %d\n",handler_timesync_offset());
|
printf("Time offset = %d\n", Time_Offset);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -72,7 +76,8 @@ void datetime_timesync(
|
|||||||
* @param true if DST is enabled and active
|
* @param true if DST is enabled and active
|
||||||
* @return true if local time was retrieved
|
* @return true if local time was retrieved
|
||||||
*/
|
*/
|
||||||
bool datetime_local(BACNET_DATE *bdate,
|
bool datetime_local(
|
||||||
|
BACNET_DATE *bdate,
|
||||||
BACNET_TIME *btime,
|
BACNET_TIME *btime,
|
||||||
int16_t *utc_offset_minutes,
|
int16_t *utc_offset_minutes,
|
||||||
bool *dst_active)
|
bool *dst_active)
|
||||||
@@ -83,9 +88,9 @@ bool datetime_local(BACNET_DATE *bdate,
|
|||||||
int32_t to;
|
int32_t to;
|
||||||
|
|
||||||
if (gettimeofday(&tv, NULL) == 0) {
|
if (gettimeofday(&tv, NULL) == 0) {
|
||||||
to = handler_timesync_offset();
|
to = Time_Offset;
|
||||||
tv.tv_sec += (int) to/1000;
|
tv.tv_sec += (int)to / 1000;
|
||||||
tv.tv_usec += (to%1000)*1000;
|
tv.tv_usec += (to % 1000) * 1000;
|
||||||
tblock = (struct tm *)localtime((const time_t *)&tv.tv_sec);
|
tblock = (struct tm *)localtime((const time_t *)&tv.tv_sec);
|
||||||
}
|
}
|
||||||
if (tblock) {
|
if (tblock) {
|
||||||
@@ -101,11 +106,12 @@ bool datetime_local(BACNET_DATE *bdate,
|
|||||||
* int tm_yday Day of year [0,365].
|
* int tm_yday Day of year [0,365].
|
||||||
* int tm_isdst Daylight Savings flag.
|
* int tm_isdst Daylight Savings flag.
|
||||||
*/
|
*/
|
||||||
datetime_set_date(bdate, (uint16_t)tblock->tm_year + 1900,
|
datetime_set_date(
|
||||||
|
bdate, (uint16_t)tblock->tm_year + 1900,
|
||||||
(uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday);
|
(uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday);
|
||||||
datetime_set_time(btime, (uint8_t)tblock->tm_hour,
|
datetime_set_time(
|
||||||
(uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec,
|
btime, (uint8_t)tblock->tm_hour, (uint8_t)tblock->tm_min,
|
||||||
(uint8_t)(tv.tv_usec / 10000));
|
(uint8_t)tblock->tm_sec, (uint8_t)(tv.tv_usec / 10000));
|
||||||
if (dst_active) {
|
if (dst_active) {
|
||||||
/* The value of tm_isdst is:
|
/* The value of tm_isdst is:
|
||||||
- positive if Daylight Saving Time is in effect,
|
- positive if Daylight Saving Time is in effect,
|
||||||
|
|||||||
@@ -69,18 +69,6 @@ static void show_bacnet_date_time(BACNET_DATE *bdate, BACNET_TIME *btime)
|
|||||||
/* Callback for timesync set */
|
/* Callback for timesync set */
|
||||||
static handler_timesync_set_callback_t handler_timesync_set_callback;
|
static handler_timesync_set_callback_t handler_timesync_set_callback;
|
||||||
|
|
||||||
static int32_t Time_Offset; /* Time offset in ms */
|
|
||||||
|
|
||||||
int32_t handler_timesync_offset(void)
|
|
||||||
{
|
|
||||||
return Time_Offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
void handler_timesync_offset_set(int32_t offset)
|
|
||||||
{
|
|
||||||
Time_Offset = offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
void handler_timesync(
|
void handler_timesync(
|
||||||
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src)
|
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -48,10 +48,6 @@ typedef void (*handler_timesync_set_callback_t)(
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
|
||||||
BACNET_STACK_EXPORT
|
|
||||||
int32_t handler_timesync_offset(void);
|
|
||||||
BACNET_STACK_EXPORT
|
|
||||||
void handler_timesync_offset_set(int32_t offset);
|
|
||||||
/* time synchronization handlers */
|
/* time synchronization handlers */
|
||||||
BACNET_STACK_EXPORT
|
BACNET_STACK_EXPORT
|
||||||
void handler_timesync(
|
void handler_timesync(
|
||||||
|
|||||||
Reference in New Issue
Block a user