forked from osresearch/LEDscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
93 lines (79 loc) · 1.49 KB
/
util.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/** \file
* Various utility functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <termios.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include "util.h"
#include "bitslice.h"
/** Write all the bytes to a fd, even if there is a brief interruption.
* \return number of bytes written or -1 on any fatal error.
*/
ssize_t
write_all(
const int fd,
const void * const buf_ptr,
const size_t len
)
{
const uint8_t * const buf = buf_ptr;
size_t offset = 0;
while (offset < len)
{
const ssize_t rc = write(fd, buf + offset, len - offset);
if (rc < 0)
{
if (errno == EAGAIN)
continue;
return -1;
}
if (rc == 0)
return -1;
offset += rc;
}
return len;
}
int
serial_open(
const char * const dev
)
{
const int fd = open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY, 0666);
if (fd < 0)
return -1;
// Disable modem control signals
struct termios attr;
tcgetattr(fd, &attr);
attr.c_cflag |= CLOCAL | CREAD;
attr.c_oflag &= ~OPOST;
tcsetattr(fd, TCSANOW, &attr);
return fd;
}
void
hexdump(
FILE * const outfile,
const void * const buf,
const size_t len
)
{
const uint8_t * const p = buf;
for(size_t i = 0 ; i < len ; i++)
{
if (i % 8 == 0)
fprintf(outfile, "%s%04zu:", i == 0 ? "": "\n", i);
fprintf(outfile, " %02x", p[i]);
}
fprintf(outfile, "\n");
}