platform_timer.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-10 22:16:41
  5. * @LastEditTime: 2020-06-05 17:18:48
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "platform_timer.h"
  9. void platform_timer_init(platform_timer_t* timer)
  10. {
  11. timer->time = (struct timeval){0, 0};
  12. }
  13. void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout)
  14. {
  15. struct timeval now;
  16. gettimeofday(&now, NULL);
  17. struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
  18. timeradd(&now, &interval, &timer->time);
  19. }
  20. char platform_timer_is_expired(platform_timer_t* timer)
  21. {
  22. struct timeval now, res;
  23. gettimeofday(&now, NULL);
  24. timersub(&timer->time, &now, &res);
  25. return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0));
  26. }
  27. int platform_timer_remain(platform_timer_t* timer)
  28. {
  29. struct timeval now, res;
  30. gettimeofday(&now, NULL);
  31. timersub(&timer->time, &now, &res);
  32. return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
  33. }
  34. unsigned long platform_timer_now(void)
  35. {
  36. return (unsigned long) time(NULL);
  37. }
  38. void platform_timer_usleep(unsigned long usec)
  39. {
  40. usleep(usec);
  41. }