-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest_xoro.c
51 lines (42 loc) · 1.24 KB
/
test_xoro.c
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
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_BYTES_PER_READ 8
static unsigned char rx[MAX_BYTES_PER_READ]; /* Receive buffer from the LKM */
void zero_rx(void)
{
for (int b_idx = 0; b_idx < MAX_BYTES_PER_READ; b_idx++) {
rx[b_idx] = 0;
}
}
int main(int argc, char *argv[])
{
int fd = open("/dev/xoro", O_RDONLY);
if (0 > fd) {
perror("Failed to open the device.");
return errno;
}
/* Test reading different numbers of bytes. */
for (int n_bytes = 0; n_bytes < 10; n_bytes++) {
/* Clear/zero the buffer before copying in read data. */
zero_rx();
/* Read the response from the LKM. */
ssize_t n_bytes_read = read(fd, rx, n_bytes);
if (0 > n_bytes_read) {
perror("Failed to read all bytes.");
return errno;
}
uint64_t value_ = 0;
for (int b_idx = 0; b_idx < n_bytes_read; b_idx++) {
unsigned char b = rx[b_idx];
value_ |= ((uint64_t) b << (8 * b_idx));
}
printf("n_bytes=%d n_bytes_read=%ld value=%016lx\n", n_bytes,
n_bytes_read, value_);
}
fflush(stdout);
return 0;
}