MQTTTime.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************
  2. * Copyright (c) 2020, 2021 IBM Corp. and Ian Craggs
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial implementation
  15. *******************************************************************************/
  16. #include "MQTTTime.h"
  17. #include "StackTrace.h"
  18. #if defined(_WIN32) || defined(_WIN64)
  19. #include <windows.h>
  20. #else
  21. #include <unistd.h>
  22. #include <sys/time.h>
  23. #endif
  24. void MQTTTime_sleep(ELAPSED_TIME_TYPE milliseconds)
  25. {
  26. FUNC_ENTRY;
  27. #if defined(_WIN32) || defined(_WIN64)
  28. Sleep((DWORD)milliseconds);
  29. #else
  30. usleep((useconds_t)(milliseconds*1000));
  31. #endif
  32. FUNC_EXIT;
  33. }
  34. #if defined(_WIN32) || defined(_WIN64)
  35. START_TIME_TYPE MQTTTime_start_clock(void)
  36. {
  37. #if WINVER >= _WIN32_WINNT_VISTA
  38. return GetTickCount64();
  39. #else
  40. return GetTickCount();
  41. #endif
  42. }
  43. #elif defined(AIX)
  44. START_TIME_TYPE MQTTTime_start_clock(void)
  45. {
  46. struct timespec start;
  47. clock_gettime(CLOCK_MONOTONIC, &start);
  48. return start;
  49. }
  50. #else
  51. START_TIME_TYPE MQTTTime_start_clock(void)
  52. {
  53. struct timeval start;
  54. struct timespec start_ts;
  55. clock_gettime(CLOCK_MONOTONIC, &start_ts);
  56. start.tv_sec = start_ts.tv_sec;
  57. start.tv_usec = start_ts.tv_nsec / 1000;
  58. return start;
  59. }
  60. #endif
  61. START_TIME_TYPE MQTTTime_now(void)
  62. {
  63. return MQTTTime_start_clock();
  64. }
  65. #if defined(_WIN32) || defined(_WIN64)
  66. /*
  67. * @param t_new most recent time in milliseconds from GetTickCount()
  68. * @param t_old older time in milliseconds from GetTickCount()
  69. * @return difference in milliseconds
  70. */
  71. DIFF_TIME_TYPE MQTTTime_difftime(START_TIME_TYPE t_new, START_TIME_TYPE t_old)
  72. {
  73. #if WINVER >= _WIN32_WINNT_VISTA
  74. return (DIFF_TIME_TYPE)(t_new - t_old);
  75. #else
  76. if (t_old < t_new) /* check for wrap around condition in GetTickCount */
  77. return (DIFF_TIME_TYPE)(t_new - t_old);
  78. else
  79. return (DIFF_TIME_TYPE)((0xFFFFFFFFL - t_old) + 1 + t_new);
  80. #endif
  81. }
  82. #elif defined(AIX)
  83. #define assert(a)
  84. DIFF_TIME_TYPE MQTTTime_difftime(START_TIME_TYPE t_new, START_TIME_TYPE t_old)
  85. {
  86. struct timespec result;
  87. ntimersub(t_new, t_old, result);
  88. return (DIFF_TIME_TYPE)((result.tv_sec)*1000L + (result.tv_nsec)/1000000L); /* convert to milliseconds */
  89. }
  90. #else
  91. DIFF_TIME_TYPE MQTTTime_difftime(START_TIME_TYPE t_new, START_TIME_TYPE t_old)
  92. {
  93. struct timeval result;
  94. timersub(&t_new, &t_old, &result);
  95. return (DIFF_TIME_TYPE)(((DIFF_TIME_TYPE)result.tv_sec)*1000 + ((DIFF_TIME_TYPE)result.tv_usec)/1000); /* convert to milliseconds */
  96. }
  97. #endif
  98. ELAPSED_TIME_TYPE MQTTTime_elapsed(START_TIME_TYPE milliseconds)
  99. {
  100. return (ELAPSED_TIME_TYPE)MQTTTime_difftime(MQTTTime_now(), milliseconds);
  101. }