40
40
extern "C" {
41
41
#endif
42
42
43
+ /* Maximum and minimum value TIME_T can hold */
44
+ #define SYS_TIME_T_MAX ((((time_t)1 << (8 * sizeof(time_t) - 2)) - 1) * 2 + 1)
45
+ #define SYS_TIME_T_MIN (-SYS_TIME_T_MAX - 1)
46
+
47
+ /* Converts ticks to seconds, discarding any fractional seconds */
48
+ #define SYS_TICKS_TO_SECS (ticks ) \
49
+ (((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) ? SYS_TIME_T_MAX \
50
+ : k_ticks_to_sec_floor64(ticks))
51
+
52
+ /* Converts ticks to nanoseconds, modulo NSEC_PER_SEC */
53
+ #define SYS_TICKS_TO_NSECS (ticks ) \
54
+ (((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) \
55
+ ? (NSEC_PER_SEC - 1) \
56
+ : k_ticks_to_ns_floor32((uint64_t)(ticks) % CONFIG_SYS_CLOCK_TICKS_PER_SEC))
57
+
58
+ /* Define a timespec */
59
+ #define SYS_TIMESPEC (sec , nsec ) \
60
+ ((struct timespec){ \
61
+ .tv_sec = (time_t)CLAMP((int64_t)(sec), SYS_TIME_T_MIN, SYS_TIME_T_MAX), \
62
+ .tv_nsec = (long)(nsec), \
63
+ })
64
+
65
+ /* Initialize a struct timespec object from a tick count */
66
+ #define SYS_TICKS_TO_TIMESPEC (ticks ) SYS_TIMESPEC(SYS_TICKS_TO_SECS(ticks), \
67
+ SYS_TICKS_TO_NSECS(ticks))
68
+
69
+ /* The semantic equivalent of K_NO_WAIT but expressed as a timespec object*/
70
+ #define SYS_TIMESPEC_NO_WAIT SYS_TICKS_TO_TIMESPEC(0)
71
+
72
+ /* The semantic equivalent of K_TICK_MIN but expressed as a timespec object */
73
+ #define SYS_TIMESPEC_MIN SYS_TICKS_TO_TIMESPEC(K_TICK_MIN)
74
+
75
+ /* The semantic equivalent of K_TICK_MAX but expressed as a timespec object */
76
+ #define SYS_TIMESPEC_MAX SYS_TICKS_TO_TIMESPEC(K_TICK_MAX)
77
+
78
+ /* The semantic equivalent of K_FOREVER but expressed as a timespec object*/
79
+ #define SYS_TIMESPEC_FOREVER SYS_TIMESPEC(SYS_TIME_T_MAX, NSEC_PER_SEC - 1)
80
+
43
81
/**
44
82
* @defgroup timeutil_apis Time Utility APIs
45
83
* @ingroup utilities
@@ -634,13 +672,13 @@ static inline void timespec_from_timeout(k_timeout_t timeout, struct timespec *t
634
672
/* equivalent of K_FOREVER without including kernel.h */
635
673
if (K_TIMEOUT_EQ (timeout , (k_timeout_t ){K_TICKS_FOREVER })) {
636
674
/* duration == K_TICKS_FOREVER ticks */
637
- * ts = K_TIMESPEC_FOREVER ;
675
+ * ts = SYS_TIMESPEC_FOREVER ;
638
676
/* equivalent of K_NO_WAIT without including kernel.h */
639
677
} else if (K_TIMEOUT_EQ (timeout , (k_timeout_t ){0 })) {
640
678
/* duration <= 0 ticks */
641
- * ts = K_TIMESPEC_NO_WAIT ;
679
+ * ts = SYS_TIMESPEC_NO_WAIT ;
642
680
} else {
643
- * ts = K_TICKS_TO_TIMESPEC (timeout .ticks );
681
+ * ts = SYS_TICKS_TO_TIMESPEC (timeout .ticks );
644
682
}
645
683
646
684
__ASSERT_NO_MSG (timespec_is_valid (ts ));
@@ -681,7 +719,7 @@ static inline k_timeout_t timespec_to_timeout(const struct timespec *req, struct
681
719
682
720
__ASSERT_NO_MSG ((req != NULL ) && timespec_is_valid (req ));
683
721
684
- if (timespec_compare (req , & K_TIMESPEC_NO_WAIT ) <= 0 ) {
722
+ if (timespec_compare (req , & SYS_TIMESPEC_NO_WAIT ) <= 0 ) {
685
723
if (rem != NULL ) {
686
724
* rem = * req ;
687
725
}
@@ -690,16 +728,16 @@ static inline k_timeout_t timespec_to_timeout(const struct timespec *req, struct
690
728
return timeout ;
691
729
}
692
730
693
- if (timespec_compare (req , & K_TIMESPEC_FOREVER ) == 0 ) {
731
+ if (timespec_compare (req , & SYS_TIMESPEC_FOREVER ) == 0 ) {
694
732
if (rem != NULL ) {
695
- * rem = K_TIMESPEC_NO_WAIT ;
733
+ * rem = SYS_TIMESPEC_NO_WAIT ;
696
734
}
697
735
/* equivalent of K_FOREVER without including kernel.h */
698
736
timeout .ticks = K_TICKS_FOREVER ;
699
737
return timeout ;
700
738
}
701
739
702
- if (timespec_compare (req , & K_TIMESPEC_MAX ) >= 0 ) {
740
+ if (timespec_compare (req , & SYS_TIMESPEC_MAX ) >= 0 ) {
703
741
/* round down to align to max ticks */
704
742
timeout .ticks = K_TICK_MAX ;
705
743
} else {
0 commit comments