2022-11-12 00:31:10 +08:00
|
|
|
/*
|
|
|
|
* @Author: jiejie
|
|
|
|
* @Github: https://github.com/jiejieTop
|
|
|
|
* @Date: 2019-12-10 22:16:41
|
|
|
|
* @LastEditTime: 2020-06-05 17:18:48
|
2022-11-12 14:02:43 +08:00
|
|
|
* @Description: the code belongs to jiejie, please keep the author information
|
|
|
|
* and source code according to the license.
|
2022-11-12 00:31:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "platform_timer.h"
|
|
|
|
|
2022-11-12 14:02:43 +08:00
|
|
|
void platform_timer_init(platform_timer_t* timer) {
|
2022-11-12 00:31:10 +08:00
|
|
|
timer->time = (struct timeval){0, 0};
|
|
|
|
}
|
|
|
|
|
2022-11-12 14:02:43 +08:00
|
|
|
void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) {
|
2022-11-12 00:31:10 +08:00
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
|
|
|
|
timeradd(&now, &interval, &timer->time);
|
|
|
|
}
|
|
|
|
|
2022-11-12 14:02:43 +08:00
|
|
|
char platform_timer_is_expired(platform_timer_t* timer) {
|
2022-11-12 00:31:10 +08:00
|
|
|
struct timeval now, res;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
timersub(&timer->time, &now, &res);
|
|
|
|
return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0));
|
|
|
|
}
|
|
|
|
|
2022-11-12 14:02:43 +08:00
|
|
|
int platform_timer_remain(platform_timer_t* timer) {
|
2022-11-12 00:31:10 +08:00
|
|
|
struct timeval now, res;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
timersub(&timer->time, &now, &res);
|
|
|
|
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
|
2022-11-12 14:02:43 +08:00
|
|
|
unsigned long platform_timer_now(void) {
|
|
|
|
return (unsigned long)time(NULL);
|
2022-11-12 00:31:10 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 14:02:43 +08:00
|
|
|
void platform_timer_usleep(unsigned long usec) {
|
2022-11-12 00:31:10 +08:00
|
|
|
usleep(usec);
|
|
|
|
}
|