-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
153 lines (140 loc) · 3.68 KB
/
main.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
#include "uart/posix.h"
#include "payload/client.h"
#include "payload/server.h"
#include "payload/power.h"
#include "payload/vars.h"
#include "payload/conf.h"
#define BAUD B230400
static void init_serial(int fd, int baud)
{
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0) {
logsysfail("tcgetattr");
return;
}
/* Set raw (8-bit, no translation, no parity) */
cfmakeraw(&tty);
if (cfsetospeed(&tty, baud) == -1) {
logsysfail("cfsetospeed");
}
if (cfsetispeed(&tty, baud) == -1) {
logsysfail("cfsetispeed");
}
/* Read must return at least 1-byte (unless timeout/error/eof occurs) */
tty.c_cc[VMIN] = 1;
/* No interbyte read timeout */
tty.c_cc[VTIME] = 0;
/* No xon/xoff */
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
/* No modem controls, enable reading */
tty.c_cflag |= (CLOCAL | CREAD);
/* No stop bits / RTS / CTS */
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
logsysfail("tcsetattr");
return;
}
}
static bool run_server(struct uart_context *uart)
{
const char *driver;
if (getenv("USE_V4L")) {
driver = "v4l";
capture_cmd_fmt = CAPTURE_V4L;
} else {
driver = "picam";
capture_cmd_fmt = CAPTURE_PICAM;
}
loginfo("Using %s driver, exec = %s", driver, capture_cmd_fmt);
const struct uart_packet *packet;
/* Ignore timeouts */
while (true) {
while ((packet = uart_rx_packet(uart))) {
loginfo("Packet received");
svr_dispatch(uart, packet);
}
}
loginfo("Server stopping");
return true;
}
static bool run_client(struct uart_context *uart, const char *name, const char *path, int width, int height)
{
char dest[100];
snprintf(dest, sizeof(dest), "%s/%s", path, name);
if (!cmd_capture_and_save_image(uart, name, dest, width, height)) {
logfail("Failed");
return false;
}
loginfo("Image %s captured and saved to file %s", name, dest);
return true;
}
int main(int argc, char *argv[])
{
static struct uart_context uart;
if (argc < 3) {
goto syntax;
}
bool client = strcmp(argv[1], "client") == 0;
bool server = strcmp(argv[1], "server") == 0;
bool ping = strcmp(argv[1], "ping") == 0;
if (client + server + ping != 1) {
goto syntax;
}
if (server && argc != 3) {
goto syntax;
}
if (client && argc != 5 && argc != 7) {
goto syntax;
}
if (ping && argc != 3) {
goto syntax;
}
int fd = open(argv[2], O_RDWR | O_SYNC | O_NOCTTY);
if (fd == -1) {
logsysfail("open");
return 1;
}
init_serial(fd, BAUD);
map_uart_file(&uart, fd);
bool res;
if (ping) {
struct timespec t0;
struct timespec t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
res = cmd_ping(&uart);
clock_gettime(CLOCK_MONOTONIC, &t1);
uint64_t ns = (t1.tv_sec - t0.tv_sec) * 1000000000 + ((int64_t) t1.tv_nsec - t0.tv_nsec);
if (res) {
loginfo("Ping! %.3fms", ns * 1e-6);
} else {
logfail("No reply to ping");
}
} else if (client) {
const char *name = argv[3];
const char *path = argv[4];
const int width = argc == 7 ? atoi(argv[5]) : 640;
const int height = argc == 7 ? atoi(argv[6]) : 480;
const bool mock_power = getenv("MOCK_POWER");
if (!mock_power && !payload_power_on(&uart)) {
logfail("Payload failed to start up");
}
res = run_client(&uart, name, path, width, height);
if (!mock_power && !payload_power_off(&uart)) {
logfail("Payload failed to power down");
}
} else {
res = run_server(&uart);
}
close(fd);
if (!res) {
logfail("Program failed");
}
return res ? 0 : 3;
syntax:
logfail("Syntax: %s ping <uart>", argv[0]);
logfail("Syntax: %s server <uart>", argv[0]);
logfail("Syntax: %s client <uart> <image-name> <save-path> [<width> <height>]", argv[0]);
return 2;
}