xref: /aosp_15_r20/external/rmi4utils/rmidevice/util.cpp (revision a248dafd7653b99fc45f9d29e5f139b04f2f28bc)
1*a248dafdSChristopher Ferris #include <stdio.h>
2*a248dafdSChristopher Ferris #include <sys/types.h>
3*a248dafdSChristopher Ferris #include <errno.h>
4*a248dafdSChristopher Ferris #include <time.h>
5*a248dafdSChristopher Ferris 
diff_time(struct timespec * start,struct timespec * end)6*a248dafdSChristopher Ferris long long diff_time(struct timespec *start, struct timespec *end)
7*a248dafdSChristopher Ferris {
8*a248dafdSChristopher Ferris 	long long diff;
9*a248dafdSChristopher Ferris 	diff = (end->tv_sec - start->tv_sec) * 1000 * 1000;
10*a248dafdSChristopher Ferris 	diff += (end->tv_nsec - start->tv_nsec) / 1000;
11*a248dafdSChristopher Ferris 	return diff;
12*a248dafdSChristopher Ferris }
13*a248dafdSChristopher Ferris 
Sleep(int ms)14*a248dafdSChristopher Ferris int Sleep(int ms)
15*a248dafdSChristopher Ferris {
16*a248dafdSChristopher Ferris 	struct timespec ts;
17*a248dafdSChristopher Ferris 	struct timespec rem;
18*a248dafdSChristopher Ferris 
19*a248dafdSChristopher Ferris 	ts.tv_sec = ms / 1000;
20*a248dafdSChristopher Ferris 	ts.tv_nsec = (ms % 1000) * 1000 * 1000;
21*a248dafdSChristopher Ferris 	for (;;) {
22*a248dafdSChristopher Ferris 		if (nanosleep(&ts, &rem) == 0) {
23*a248dafdSChristopher Ferris 			break;
24*a248dafdSChristopher Ferris 		} else {
25*a248dafdSChristopher Ferris 			if (errno == EINTR) {
26*a248dafdSChristopher Ferris 				ts = rem;
27*a248dafdSChristopher Ferris 				continue;
28*a248dafdSChristopher Ferris 			}
29*a248dafdSChristopher Ferris 			return -1;
30*a248dafdSChristopher Ferris 		}
31*a248dafdSChristopher Ferris 	}
32*a248dafdSChristopher Ferris 	return 0;
33*a248dafdSChristopher Ferris }
34*a248dafdSChristopher Ferris 
print_buffer(const unsigned char * buf,unsigned int len)35*a248dafdSChristopher Ferris void print_buffer(const unsigned char *buf, unsigned int len)
36*a248dafdSChristopher Ferris {
37*a248dafdSChristopher Ferris 	for (unsigned int i = 0; i < len; ++i) {
38*a248dafdSChristopher Ferris 		fprintf(stdout, "0x%02X ", buf[i]);
39*a248dafdSChristopher Ferris 		if (i % 8 == 7)
40*a248dafdSChristopher Ferris 			fprintf(stdout, "\n");
41*a248dafdSChristopher Ferris 	}
42*a248dafdSChristopher Ferris 	fprintf(stdout, "\n");
43*a248dafdSChristopher Ferris }
44*a248dafdSChristopher Ferris 
45*a248dafdSChristopher Ferris 
StripPath(const char * path,ssize_t size)46*a248dafdSChristopher Ferris const char * StripPath(const char * path, ssize_t size)
47*a248dafdSChristopher Ferris {
48*a248dafdSChristopher Ferris 	int i;
49*a248dafdSChristopher Ferris 	const char * str;
50*a248dafdSChristopher Ferris 
51*a248dafdSChristopher Ferris 	for (i = size - 1, str = &path[size - 1]; i > 0; --i, --str)
52*a248dafdSChristopher Ferris 		if (path[i - 1] == '/')
53*a248dafdSChristopher Ferris 			break;
54*a248dafdSChristopher Ferris 
55*a248dafdSChristopher Ferris 	return str;
56*a248dafdSChristopher Ferris }
57*a248dafdSChristopher Ferris 
extract_long(const unsigned char * data)58*a248dafdSChristopher Ferris unsigned long extract_long(const unsigned char *data)
59*a248dafdSChristopher Ferris {
60*a248dafdSChristopher Ferris 	return (unsigned long)data [0]
61*a248dafdSChristopher Ferris 		+ (unsigned long)data [1] * 0x100
62*a248dafdSChristopher Ferris 		+ (unsigned long)data [2] * 0x10000
63*a248dafdSChristopher Ferris 		+ (unsigned long)data [3] * 0x1000000;
64*a248dafdSChristopher Ferris }
65*a248dafdSChristopher Ferris 
extract_short(const unsigned char * data)66*a248dafdSChristopher Ferris unsigned short extract_short(const unsigned char *data)
67*a248dafdSChristopher Ferris {
68*a248dafdSChristopher Ferris 	return (unsigned long)data [0]
69*a248dafdSChristopher Ferris 		+ (unsigned long)data [1] * 0x100;
70*a248dafdSChristopher Ferris }