/********************************************************************************
|
* Copyright: (C) 2020 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: util.h
|
* Description: This file is common utility functions
|
*
|
* Version: 1.0.0(7/06/2012~)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "7/06/2012 09:21:33 PM"
|
*
|
********************************************************************************/
|
|
#ifndef __UTIL_H_
|
#define __UTIL_H_
|
|
#include <time.h>
|
#include <stddef.h>
|
|
#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
|
|
/* +------------------------+
|
* | time functions API |
|
* +------------------------+*/
|
static inline void msleep(unsigned long ms)
|
{
|
struct timespec cSleep;
|
unsigned long ulTmp;
|
|
cSleep.tv_sec = ms / 1000;
|
if (cSleep.tv_sec == 0)
|
{
|
ulTmp = ms * 10000;
|
cSleep.tv_nsec = ulTmp * 100;
|
}
|
else
|
{
|
cSleep.tv_nsec = 0;
|
}
|
|
nanosleep(&cSleep, 0);
|
return ;
|
}
|
|
static inline int check_timeout(time_t *last_time, int interval)
|
{
|
int timeout = 0;
|
time_t now;
|
|
time(&now);
|
|
if( difftime(now, *last_time)>interval )
|
{
|
timeout = 1;
|
*last_time = now;
|
}
|
|
return timeout;
|
}
|
|
#endif
|