-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart_port.c
195 lines (163 loc) · 3.7 KB
/
uart_port.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
//
#include <stdio.h>
#include <fcntl.h>
#include "uart_win32.h"
#define command_write_to_uart 0
#define command_read_from_uart 1
#define command_dbg_msg 2
#define command_shutdown 3
#define MAX_COMM_PACK_SIZE 65536
#define dbg_printf port_dbg_print
typedef unsigned char byte;
int read_exact(byte *buf, int len)
{
int i, got=0;
do
{
if ((i = read(0, buf + got, len - got)) <= 0)
return i;
got += i;
} while (got < len);
return len;
}
int write_exact(byte *buf, int len)
{
int i, wrote = 0;
do
{
if ((i = write(1, buf + wrote, len - wrote)) <= 0)
return i;
wrote += i;
} while (wrote < len);
return len;
}
int read_cmd(byte *buf)
{
int len;
if (read_exact(buf, 2) != 2)
return -1;
len = (buf[0] << 8) | buf[1];
return read_exact(buf, len);
}
int write_cmd(byte *buf, int len)
{
byte li;
li = (len >> 8) & 0xff;
write_exact(&li, 1);
li = len & 0xff;
write_exact(&li, 1);
return write_exact(buf, len);
}
struct uart_port_comm
{
char t;
int len;
byte *b;
};
bool read_comm_cmd(uart_port_comm &r)
{
static byte cmd_buf[MAX_COMM_PACK_SIZE];
r.len = -1;
int len = read_cmd(cmd_buf);
if (len < 0)
return false;
r.t = cmd_buf[0];
r.len = len - 1;
r.b = cmd_buf + 1;
r.b[r.len] = 0;
return true;
}
bool send_comm_response(const int t, const byte *s, const int len)
{
static byte out_buf[MAX_COMM_PACK_SIZE];
if (1 + len > MAX_COMM_PACK_SIZE)
return false;
out_buf[0] = t;
memcpy(out_buf + 1, s, len);
return write_cmd(out_buf, len + 1) > 0;
}
static int port_dbg_print(const char *s, ...)
{
char t[20 * 1024];
t[0] = '\0';
va_list va;
va_start(va, s);
vsprintf(t, s, va);
va_end(va);
send_comm_response(command_dbg_msg, (byte *)t, strlen(t));
return 0;
}
static void on_comm_read(uart_obj *uart, byte *buf, const int l)
{
send_comm_response(command_read_from_uart, buf, l);
}
static void on_comm_close(uart_obj *uart, const enum_comm_close reason)
{
dbg_printf("COM closed: %s\n", reason == cc_shutdown ? "shutdown" : "error");
exit(0);
}
int main(const int argc, const char *args[])
{
uart_obj uart;
int port = -1;
int baud = -1;
char parity[20] = {'\0'};
int databits = -1;
int stopbits = -1;
setmode(0, O_BINARY);
setmode(1, O_BINARY);
#define load_i_param(param) \
if (strcmp(args[i], "-"#param) == 0) \
{ param = atoi(args[i + 1]); i += 2; }
int i = 1;
while (i < argc - 1)
{
load_i_param(port)
else load_i_param(baud)
else load_i_param(databits)
else load_i_param(stopbits)
else if (strcmp(args[i], "-parity") == 0)
{
strncpy(parity, args[i + 1], 19);
i += 2;
}
else
i++;
}
if (port < 0)
{
dbg_printf("port unspecified\n");
return -1;
}
if (uart_open(&uart,
port,
baud,
parity,
databits,
stopbits,
f_on_comm_read(on_comm_read),
&uart,
f_on_comm_close(on_comm_close),
&uart) == NULL)
{
dbg_printf("failed to open the specified COM port\n");
return -1;
}
while (true)
{
uart_port_comm c;
if (!read_comm_cmd(c))
break;
switch (c.t)
{
case command_write_to_uart:
uart_send(&uart, (char *)c.b, c.len);
break;
case command_shutdown:
uart_shutdown(&uart);
break;
default:
break;
}
}
}