oi_time.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 The Android Open Source Project
  4. * Copyright 2002 - 2004 Open Interface North America, Inc. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. ******************************************************************************/
  19. #ifndef _OI_TIME_H
  20. #define _OI_TIME_H
  21. /** @file
  22. *
  23. * This file provides time type definitions and interfaces to time-related functions.
  24. *
  25. * The stack maintains a 64-bit real-time millisecond clock. The choice of
  26. * milliseconds is for convenience, not accuracy.
  27. *
  28. * Timeouts are specified as tenths of seconds in a 32-bit value. Timeout values
  29. * specified by the Bluetooth specification are usually muliple seconds, so
  30. * accuracy to a tenth of a second is more than adequate.
  31. *
  32. * This file also contains macros to convert between seconds and the Link
  33. * Manager's 1.28-second units.
  34. *
  35. */
  36. /**********************************************************************************
  37. $Revision: #1 $
  38. ***********************************************************************************/
  39. #include "oi_stddefs.h"
  40. /** \addtogroup Misc Miscellaneous APIs */
  41. /**@{*/
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * Within the core stack timeouts are specified in intervals of tenths of seconds
  47. */
  48. typedef OI_UINT16 OI_INTERVAL;
  49. #define OI_INTERVALS_PER_SECOND 10
  50. #define MSECS_PER_OI_INTERVAL (1000 / OI_INTERVALS_PER_SECOND)
  51. /** maximum interval (54 min 36.7 sec) */
  52. #define OI_MAX_INTERVAL 0x7fff
  53. /**
  54. * Macro to convert seconds to OI_INTERVAL time units
  55. */
  56. #define OI_SECONDS(n) ((OI_INTERVAL) ((n) * OI_INTERVALS_PER_SECOND))
  57. /**
  58. * Macro to convert milliseconds to OI_INTERVAL time units (Rounded Up)
  59. */
  60. #define OI_MSECONDS(n) ((OI_INTERVAL) ((n + MSECS_PER_OI_INTERVAL - 1) / MSECS_PER_OI_INTERVAL))
  61. /**
  62. * Macro to convert minutes to OI_INTERVAL time units
  63. */
  64. #define OI_MINUTES(n) ((OI_INTERVAL) ((n) * OI_SECONDS(60)))
  65. /** Convert an OI_INTERVAL to milliseconds. */
  66. #define OI_INTERVAL_TO_MILLISECONDS(i) ((i) * MSECS_PER_OI_INTERVAL)
  67. /**
  68. * The stack depends on relative not absolute time. Any mapping between the
  69. * stack's real-time clock and absolute time and date is implementation-dependent.
  70. */
  71. typedef struct {
  72. OI_INT32 seconds;
  73. OI_INT16 mseconds;
  74. } OI_TIME;
  75. /**
  76. * Convert an OI_TIME to milliseconds.
  77. *
  78. * @param t the time to convert
  79. *
  80. * @return the time in milliseconds
  81. */
  82. OI_UINT32 OI_Time_ToMS(OI_TIME *t);
  83. /**
  84. * This function compares two time values.
  85. *
  86. * @param T1 first time to compare.
  87. *
  88. * @param T2 second time to compare.
  89. *
  90. * @return
  91. @verbatim
  92. -1 if t1 < t2
  93. 0 if t1 = t2
  94. +1 if t1 > t2
  95. @endverbatim
  96. */
  97. OI_INT16 OI_Time_Compare(OI_TIME *T1,
  98. OI_TIME *T2);
  99. /**
  100. * This function returns the interval between two times to a granularity of 0.1 seconds.
  101. *
  102. * @param Sooner a time value more recent that Later
  103. *
  104. * @param Later a time value later than Sooner
  105. *
  106. * @note The result is an OI_INTERVAL value so this function only works for time intervals
  107. * that are less than about 71 minutes.
  108. *
  109. * @return the time interval between the two times = (Later - Sooner)
  110. */
  111. OI_INTERVAL OI_Time_Interval(OI_TIME *Sooner,
  112. OI_TIME *Later);
  113. /**
  114. * This function returns the interval between two times to a granularity of milliseconds.
  115. *
  116. * @param Sooner a time value more recent that Later
  117. *
  118. * @param Later a time value later than Sooner
  119. *
  120. * @note The result is an OI_UINT32 value so this function only works for time intervals
  121. * that are less than about 50 days.
  122. *
  123. * @return the time interval between the two times = (Later - Sooner)
  124. */
  125. OI_UINT32 OI_Time_IntervalMsecs(OI_TIME *Sooner,
  126. OI_TIME *Later);
  127. /**
  128. * This function answers the question, Have we reached or gone past the target time?
  129. *
  130. * @param pTargetTime target time
  131. *
  132. * @return TRUE means time now is at or past target time
  133. * FALSE means target time is still some time in the future
  134. */
  135. OI_BOOL OI_Time_NowReachedTime(OI_TIME *pTargetTime);
  136. /**
  137. * Convert seconds to the Link Manager 1.28-second units
  138. * Approximate by using 1.25 conversion factor.
  139. */
  140. #define OI_SECONDS_TO_LM_TIME_UNITS(lmUnits) ((lmUnits)<4?(lmUnits):(lmUnits)-((lmUnits)>>2))
  141. /**
  142. * Convert Link Manager 1.28-second units to seconds.
  143. * Approximate by using 1.25 conversion factor.
  144. */
  145. #define OI_LM_TIME_UNITS_TO_SECONDS(lmUnits) ((lmUnits) + ((lmUnits)>>2))
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. /**@}*/
  150. /* Include for OI_Time_Now() prototype
  151. * Must be included at end to obtain OI_TIME typedef
  152. */
  153. #include "oi_osinterface.h"
  154. /*****************************************************************************/
  155. #endif /* _OI_TIME_H */