RaspberrPi project source code
guowenxue
11 hours ago 24345339421493cdacdbaae0248c3928ea9404c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/********************************************************************************
 *      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