mbgtools-lx  4.2.8
cmp_time_util.c
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: cmp_time_util.c 1.3 2018/12/11 12:01:03 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Common device time compare utility functions.
10  *
11  * -----------------------------------------------------------------------
12  * $Log: cmp_time_util.c $
13  * Revision 1.3 2018/12/11 12:01:03 martin
14  * Fixed compiler warnings on Windows.
15  * Revision 1.2 2018/08/08 08:22:23Z martin
16  * New function mbg_snprint_hr_time_loc().
17  * Account for renamed library symbols.
18  * Removed obsolete code.
19  * Revision 1.1 2013/07/25 10:42:59 martin
20  * Initial revision.
21  *
22  **************************************************************************/
23 
24 #define _CMP_TIME_UTIL
25  #include <cmp_time_util.h>
26 #undef _CMP_TIME_UTIL
27 
28 #include <mbgdevio.h>
29 #include <mbgutil.h>
30 #include <pcpsutil.h>
31 #include <str_util.h>
32 
33 #include <toolutil.h> // for mbg_snprint_hr_tstamp(). FIXME: use function from mbgutil library
34 
35 #include <stdio.h>
36 
37 
39 
40 
41 
42 /*HDR*/
54 int chk_fast_tstamp_supp( MBG_DEV_HANDLE dh1, const PCPS_DEV *p_dev_1,
55  MBG_DEV_HANDLE dh2, const PCPS_DEV *p_dev_2,
56  MBG_ERR_MSG_FNC err_msg_fnc )
57 {
58  int rc;
59  const char n_supp_msg[] = "fast (memory mapped) time stamps";
60 
62  err_msg_fnc, n_supp_msg );
63 
64  if ( mbg_rc_is_success( rc ) )
66  err_msg_fnc, n_supp_msg );
67  return rc;
68 
69 } // chk_fast_tstamp_supp
70 
71 
72 
73 /*HDR*/
87  int read_fast )
88 {
89  int rc;
90 
91  if ( read_fast )
92  {
95 
96  rc = mbg_get_fast_hr_timestamp_cycles( dh1, &ts_c1 );
97 
98  if ( mbg_cond_err_msg( rc, "mbg_get_fast_hr_timestamp_cycles 1" ) )
99  goto done;
100 
101  rc = mbg_get_fast_hr_timestamp_cycles( dh2, &ts_c2 );
102 
103  if ( mbg_cond_err_msg( rc, "mbg_get_fast_hr_timestamp_cycles 2" ) )
104  goto done;
105 
108  }
109  else
110  {
111  rc = mbg_get_hr_time_cycles( dh1, p_htc1 );
112 
113  if ( mbg_cond_err_msg( rc, "mbg_get_hr_time_cycles 1" ) )
114  goto done;
115 
116  rc = mbg_get_hr_time_cycles( dh2, p_htc2 );
117 
118  if ( mbg_cond_err_msg( rc, "mbg_get_hr_time_cycles 2" ) )
119  goto done;
120  }
121 
122 done:
123  return rc;
124 
125 } // get_htc_timestamps
126 
127 
128 
129 /*HDR*/
140 double get_htc_delta( const PCPS_HR_TIME_CYCLES *p_htc,
141  const PCPS_HR_TIME_CYCLES *p_htc_ref,
142  double *p_delta_ts, double *p_delta_cyc )
143 {
144  double delta_ts;
145  double delta_cyc;
146 
147  delta_ts = (double) p_htc->t.tstamp.sec + dfrac_sec_from_bin( p_htc->t.tstamp.frac )
148  - (double) p_htc_ref->t.tstamp.sec - dfrac_sec_from_bin( p_htc_ref->t.tstamp.frac );
149 
150  #if defined( MBG_TGT_WIN32 )
151  {
152  __int64 dt_t = p_htc->cycles - p_htc_ref->cycles;
153  delta_cyc = (double) dt_t / (__int64) cyc_freq;
154  }
155  #else
156  delta_cyc = ( (double) ( p_htc->cycles - p_htc_ref->cycles ) ) / cyc_freq;
157  #endif
158 
159  if ( p_delta_ts )
160  *p_delta_ts = delta_ts;
161 
162  if ( p_delta_cyc )
163  *p_delta_cyc = delta_cyc;
164 
165  return delta_ts - delta_cyc;
166 
167 } // get_htc_delta
168 
169 
170 
171 /*HDR*/
188 int mbg_snprint_hr_tstamp_ext( char *s, int max_len,
189  const PCPS_TIME_STAMP *p, int print_raw )
190 {
191  int n = mbg_snprint_hr_tstamp( s, max_len, p, 0, 0 );
192 
193  if ( print_raw )
194  n += snprintf( &s[n], max_len - n, " (0x%08lX.%08lX)",
195  (ulong) p->sec,
196  (ulong) p->frac );
197 
198  return n;
199 
200 } // mbg_snprint_hr_tstamp_ext
201 
202 
203 
204 /*HDR*/
220 int mbg_snprint_hr_time_loc( char *s, size_t max_len, const PCPS_HR_TIME *p )
221 {
222  size_t n = mbg_snprint_hr_tstamp( s, max_len, &p->tstamp, p->utc_offs, 0 );
223 
224  n += snprintf_safe( &s[n], max_len - n, " UTC" );
225 
226  if ( !(p->status & PCPS_UTC ) )
227  n += mbg_str_pcps_hr_time_offs( &s[n], (int) ( max_len - n ), p, "" );
228 
229 #if 0
230  if(p->utc_offs != 0)
231  n += mbg_snprintf( n, len_s - n, "(%+02luh",
232  ( pt->utc_offs < 0 ) ? '-' : '+',
233  ldt.quot, ldt.rem );
234 #endif
235 
236  return _int_from_size_t( n );
237 
238 } // mbg_snprint_hr_time_loc
239 
240 
int mbg_snprint_hr_tstamp(char *s, size_t max_len, const PCPS_TIME_STAMP *p, long utc_offs, int show_raw)
Print date and time from a PCPS_TIME_STAMP structure to a string.
Definition: toolutil.c:799
PCPS_HR_TIME t
Definition: pcpsdev.h:1395
int mbg_snprintf(char *s, size_t max_len, const char *fmt,...)
A portable, safer implementation of snprintf().
Definition: mbgutil.c:157
bool mbg_cond_err_msg(int rc, const char *what)
Check if a value is an error code and print an associated error message.
Definition: mbgerror.c:714
MBG_PC_CYCLES cycles
Definition: pcpsdev.h:1394
PCPS_SECONDS sec
seconds since 1970, usually UTC scale
Definition: pcpsdefs.h:974
#define _int_from_size_t(_n)
Definition: words.h:662
_MBG_API_ATTR int _MBG_API mbg_get_hr_time_cycles(MBG_DEV_HANDLE dh, PCPS_HR_TIME_CYCLES *p)
Read the current high resolution time plus the associated PC cycles from a device.
Definition: mbgdevio.c:6682
static int print_raw
Definition: mbgcmptime.c:63
#define mbg_rc_is_success(_rc)
Definition: mbgerror.h:618
High resolution time stamp plus associated system cycles count.
Definition: pcpsdev.h:1369
_MBG_API_ATTR int _MBG_API mbg_get_fast_hr_timestamp_cycles(MBG_DEV_HANDLE dh, PCPS_TIME_STAMP_CYCLES *p)
Read a high resolution PCPS_TIME_STAMP_CYCLES structure via memory mapped access. ...
Definition: mbgdevio.c:6981
int get_htc_timestamps(MBG_DEV_HANDLE dh1, PCPS_HR_TIME_CYCLES *p_htc1, MBG_DEV_HANDLE dh2, PCPS_HR_TIME_CYCLES *p_htc2, int read_fast)
Read timestamps and cycles from both devices.
Definition: cmp_time_util.c:85
int32_t utc_offs
UTC offs [sec] (loc_time = tstamp + utc_offs)
Definition: pcpsdefs.h:1088
PCPS_TIME_STATUS_X status
status bits, see PCPS_TIME_STATUS_FLAGS
Definition: pcpsdefs.h:1089
PCPS_FRAC_32 frac
binary fractions of second, see PCPS_FRAC_32
Definition: pcpsdefs.h:975
MBG_PC_CYCLES_FREQUENCY cyc_freq
Must be set up tby the application.
Definition: mbgcmptime.c:72
PCPS_TIME_STAMP tstamp
High resolution time stamp (UTC)
Definition: pcpsdefs.h:1087
returned time is always UTC instead of some local time
Definition: pcpsdefs.h:1258
int mbg_snprint_hr_tstamp_ext(char *s, int max_len, const PCPS_TIME_STAMP *p, int print_raw)
Print UTC date and time from a PCPS_TIME_STAMP structure to a string.
static int read_fast
Definition: mbgcmptime.c:59
MBG_CHK_SUPP_FNC mbg_chk_dev_has_fast_hr_timestamp
Check if a device supports the mbg_get_fast_hr_timestamp... calls.
Definition: mbgdevio.c:1387
uint64_t MBG_PC_CYCLES_FREQUENCY
Definition: mbgpccyc.h:98
double get_htc_delta(const PCPS_HR_TIME_CYCLES *p_htc, const PCPS_HR_TIME_CYCLES *p_htc_ref, double *p_delta_ts, double *p_delta_cyc)
Compute the delta cycles and time difference.
High resolution time including status and local time offset.
Definition: pcpsdefs.h:1085
High resolution time plus associated system cycles count.
Definition: pcpsdev.h:1392
A high resolution time stamp.
Definition: pcpsdefs.h:972
int snprintf_safe(char *s, size_t max_len, const char *fmt,...)
A portable, safe implementation of snprintf()
Definition: str_util.c:156
int chk_feat_supp(MBG_DEV_HANDLE dh, const PCPS_DEV *p_dev, MBG_CHK_SUPP_FNC *chk_supp_fnc, MBG_ERR_MSG_FNC *err_msg_fnc, const char *not_supp_msg)
Definition: deviohlp.c:84
int MBG_ERR_MSG_FNC(const PCPS_DEV *p_dev, const char *s)
Definition: deviohlp.h:75
int mbg_snprint_hr_time_loc(char *s, size_t max_len, const PCPS_HR_TIME *p)
Print local date and time from a PCPS_TIME_STAMP structure to a string.
unsigned long ulong
Definition: words.h:292
static __mbg_inline void setup_hr_time_cycles_from_timestamp_cycles(PCPS_HR_TIME_CYCLES *p_ht_c, const PCPS_TIME_STAMP_CYCLES *p_ts_c)
Definition: pcpsdev.h:1500
static __mbg_inline double dfrac_sec_from_bin(uint32_t b)
Convert a binary fraction to "double" fractions.
Definition: mbgtime.h:534
int chk_fast_tstamp_supp(MBG_DEV_HANDLE dh1, const PCPS_DEV *p_dev_1, MBG_DEV_HANDLE dh2, const PCPS_DEV *p_dev_2, MBG_ERR_MSG_FNC err_msg_fnc)
Check if both devices support fast HR timestamps.
Definition: cmp_time_util.c:54
int mbg_str_pcps_hr_time_offs(char *s, int max_len, const PCPS_HR_TIME *pt, const char *info)
Print the UTC offset from a PCPS_HR_TIME structure to a string buffer.
Definition: mbgutil.c:703
Device info structure.
Definition: pcpsdev.h:1043