/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
330 by Suren A. Chilingaryan
Support for 64-bit registes
1
#define _POSIX_C_SOURCE 200112L
2
#define _GNU_SOURCE
3
4
#include <stdio.h>
5
#include <string.h>
6
#include <unistd.h>
7
#include <stdint.h>
8
#include <assert.h>
9
#include <ctype.h>
10
#include <time.h>
11
#include <sched.h>
12
#include <arpa/inet.h>
13
#include <sys/time.h>
14
15
#include "pci.h"
16
#include "tools.h"
17
#include "error.h"
18
19
int pcilib_add_timeout(struct timeval *tv, pcilib_timeout_t timeout) {
20
    tv->tv_usec += timeout%1000000;
21
    if (tv->tv_usec > 999999) {
22
	tv->tv_usec -= 1000000;
23
	tv->tv_sec += 1 + timeout/1000000;
24
    } else {
25
	tv->tv_sec += timeout/1000000;
26
    }
27
28
    return 0;
29
}
30
31
int pcilib_calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) {
32
    gettimeofday(tv, NULL);
33
    pcilib_add_timeout(tv, timeout);
34
35
    return 0;
36
}
37
38
int pcilib_check_deadline(struct timeval *tve, pcilib_timeout_t timeout) {
39
    int64_t res;
40
    struct timeval tvs;
41
42
    if (!tve->tv_sec) return 0;
43
44
    gettimeofday(&tvs, NULL);
45
    res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
46
	// Hm... Some problems comparing signed and unsigned. So, sign check first
47
    if ((res < 0)||(res < timeout)) {
48
	return 1;
49
    }
50
51
    return 0;
52
}
53
54
pcilib_timeout_t pcilib_calc_time_to_deadline(struct timeval *tve) {
55
    int64_t res;
56
    struct timeval tvs;
57
    
58
    gettimeofday(&tvs, NULL);
59
    res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
60
    
61
    if (res < 0) return 0;
62
    return res;
63
}
64
65
int pcilib_sleep_until_deadline(struct timeval *tv) {
66
    struct timespec wait;
67
    pcilib_timeout_t duration;
68
69
    duration = pcilib_calc_time_to_deadline(tv);
70
    if (duration > 0) {
71
	wait.tv_sec = duration / 1000000;
72
	wait.tv_nsec = 1000 * (duration % 1000000);
73
	nanosleep(&wait, NULL);
74
    }
75
76
    return 0;
77
}
78
79
pcilib_timeout_t pcilib_timediff(struct timeval *tvs, struct timeval *tve) {
80
    return ((tve->tv_sec - tvs->tv_sec)*1000000 + (tve->tv_usec - tvs->tv_usec));
81
}
82
83
int pcilib_timecmp(struct timeval *tv1, struct timeval *tv2) {
84
    if (tv1->tv_sec > tv2->tv_sec) return 1;
85
    else if (tv1->tv_sec < tv2->tv_sec) return -1;
86
    else if (tv1->tv_usec > tv2->tv_usec) return 1;
87
    else if (tv1->tv_usec < tv2->tv_usec) return -1;
88
    return 0;
89
}