0403eead |
1 | #include <stdio.h> |
713c9224 |
2 | #include <dirent.h> |
0403eead |
3 | #include <sys/time.h> |
4 | #include <time.h> |
5 | #include <unistd.h> |
6 | |
713c9224 |
7 | #include "../common/plat.h" |
8 | |
9 | |
10 | int plat_is_dir(const char *path) |
11 | { |
12 | DIR *dir; |
13 | if ((dir = opendir(path))) { |
14 | closedir(dir); |
15 | return 1; |
16 | } |
17 | return 0; |
18 | } |
19 | |
0403eead |
20 | unsigned int plat_get_ticks_ms(void) |
21 | { |
22 | struct timeval tv; |
23 | unsigned int ret; |
24 | |
25 | gettimeofday(&tv, NULL); |
26 | |
27 | ret = (unsigned)tv.tv_sec * 1000; |
28 | /* approximate division */ |
29 | ret += ((unsigned)tv.tv_usec * 4195) >> 22; |
30 | |
31 | return ret; |
32 | } |
33 | |
34 | void plat_sleep_ms(int ms) |
35 | { |
36 | usleep(ms * 1000); |
37 | } |
38 | |
39 | int plat_wait_event(int *fds_hnds, int count, int timeout_ms) |
40 | { |
41 | struct timeval tv, *timeout = NULL; |
42 | int i, ret, fdmax = -1; |
43 | fd_set fdset; |
44 | |
45 | if (timeout_ms >= 0) { |
46 | tv.tv_sec = timeout_ms / 1000; |
47 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
48 | timeout = &tv; |
49 | } |
50 | |
51 | FD_ZERO(&fdset); |
52 | for (i = 0; i < count; i++) { |
53 | if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i]; |
54 | FD_SET(fds_hnds[i], &fdset); |
55 | } |
56 | |
57 | ret = select(fdmax + 1, &fdset, NULL, NULL, timeout); |
58 | if (ret == -1) |
59 | { |
60 | perror("plat_wait_event: select failed"); |
61 | sleep(1); |
62 | return -1; |
63 | } |
64 | |
65 | if (ret == 0) |
66 | return -1; /* timeout */ |
67 | |
68 | ret = -1; |
69 | for (i = 0; i < count; i++) |
70 | if (FD_ISSET(fds_hnds[i], &fdset)) |
71 | ret = fds_hnds[i]; |
72 | |
73 | return ret; |
74 | } |
75 | |