-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.c
executable file
·207 lines (170 loc) · 3.85 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/** \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"
/** 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");
}
#ifndef HAVE_STRLCAT
/*
* '_cups_strlcat()' - Safely concatenate two strings.
*/
size_t /* O - Length of string */
strlcat(char *dst, /* O - Destination string */
const char *src, /* I - Source string */
size_t size) /* I - Size of destination string buffer */
{
size_t srclen; /* Length of source string */
size_t dstlen; /* Length of destination string */
/*
* Figure out how much room is left...
*/
dstlen = strlen(dst);
size -= dstlen + 1;
if (!size)
return (dstlen); /* No room, return immediately... */
/*
* Figure out how much room is needed...
*/
srclen = strlen(src);
/*
* Copy the appropriate amount...
*/
if (srclen > size)
srclen = size;
memcpy(dst + dstlen, src, srclen);
dst[dstlen + srclen] = '\0';
return (dstlen + srclen);
}
#endif /* !HAVE_STRLCAT */
#ifndef HAVE_STRLCPY
/*
* '_cups_strlcpy()' - Safely copy two strings.
*/
size_t /* O - Length of string */
strlcpy(
char *dst, /* O - Destination string */
const char *src, /* I - Source string */
size_t size /* I - Size of destination string buffer */
) {
size_t srclen; /* Length of source string */
/*
* Figure out how much room is needed...
*/
size --;
srclen = strlen(src);
/*
* Copy the appropriate amount...
*/
if (srclen > size)
srclen = size;
memcpy(dst, src, srclen);
dst[srclen] = '\0';
return (srclen);
}
#endif /* !HAVE_STRLCPY */
const char* color_channel_order_to_string(color_channel_order_t color_channel_order) {
switch (color_channel_order) {
case COLOR_ORDER_RGB: return "RGB";
case COLOR_ORDER_RBG: return "RBG";
case COLOR_ORDER_GRB: return "GRB";
case COLOR_ORDER_GBR: return "GBR";
case COLOR_ORDER_BGR: return "BGR";
case COLOR_ORDER_BRG: return "BRG";
default: return "<invalid color_channel_order>";
}
}
color_channel_order_t color_channel_order_from_string(const char* str) {
if (strcasecmp(str, "RGB") == 0) {
return COLOR_ORDER_RGB;
}
else if (strcasecmp(str, "RBG") == 0) {
return COLOR_ORDER_RBG;
}
else if (strcasecmp(str, "GRB") == 0) {
return COLOR_ORDER_GRB;
}
else if (strcasecmp(str, "GBR") == 0) {
return COLOR_ORDER_GBR;
}
else if (strcasecmp(str, "BGR") == 0) {
return COLOR_ORDER_BGR;
}
else if (strcasecmp(str, "BRG") == 0) {
return COLOR_ORDER_BRG;
}
else {
return COLOR_ORDER_RGB;
}
}