-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
94 lines (86 loc) · 2.34 KB
/
io.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
93
94
/**
* @file io.c
* IO abstraction routines based on read/write system calls to reliably read
* and write some number of bytes over a Unix file descriptor.
*
* @copyright
* Copyright (C) 2016 Real Flight Systems
* @author James F Dougherty <[email protected]>
*/
/**
* @defgroup io io
* @addtogroup io
* @{
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <errno.h>
#include <unistd.h>
#include "digole.h"
static int byte_count = 0; /*!< For metering - every 64-bytes of data requires a 40ms delay from the host to avoid overwhelming the display controller */
/**
* @brief write n bytes of data to a file descriptor
*
* On Linux, since write is not guaranteed to write all of the bytes
* provided, we keep track of the number of bytes written and retry
* sending the data as needed.
*
* @param[in] fd the file descriptor to write to
* @param[in] ptr the address of the data buffer to write
* @param[in] nbytes the number of bytes to write
* @return number of bytes successfully written
*/
int io_write(int fd, unsigned char* ptr, int nbytes)
{
int nleft, nwritten;
nleft = nbytes;
while (nleft > 0){
nwritten = write(fd, ptr, nleft);
byte_count += nwritten;
if (nwritten <= 0)
return(nwritten);
nleft -= nwritten;
ptr += nwritten;
}
#if 0
/* rate limit for 9600 */
if (!(byte_count % 64))
delay(40);
#endif
return(nbytes - nleft);
}
/**
* @brief read n bytes of data from a file descriptor
*
* On Linux, since read is not guaranteed to read all of the bytes
* available in one call, we continously read the specified number of bytes
* from the descriptor into the data buffer provided.
*
* @param[in] fd the file descriptor to read from
* @param[in,out] ptr the address of the data buffer to read into
* @param[in] nbytes the number of bytes to read
* @return number of bytes successfully read
*/
int io_read(int fd, unsigned char* ptr, int nbytes)
{
int nleft, nread;
nleft = nbytes;
while (nleft > 0) {
top:
nread = read(fd, ptr, nleft);
if ((nread < 0) && (errno == EAGAIN)) {
errno = 0;
goto top;
}
else if (nread < 0)
return(nread); /* error, return < 0 */
else if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
}
return(nbytes - nleft); /* return >= 0 */
}
/** @}*/