-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
64 lines (50 loc) · 1.67 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
#include "stdio.h"
#include "termios.h"
#include "fcntl.h"
#include "unistd.h"
#include "string.h"
#include "protocol.h"
int
main(int argc, char **argv) {
if (argc < 3) {
printf("Error: not enough arguments passed.\n");
printf("1st argument must be serial port address.\n");
printf("2nd argument must be firmware binary location.\n");
return 1;
}
int ret = 0;
int serial_fd, firmware_fd;
serial_fd = open(argv[1], O_RDWR | O_NOCTTY);
if (serial_fd < 0) {
printf("Failed to open %s serial port.\n", argv[1]);
return 1;
} else {
printf("Serial port %s was opened succesfully!\n", argv[1]);
}
firmware_fd = open(argv[2], O_RDONLY | O_NOCTTY);
if (serial_fd < 0) {
printf("Failed to open %s firmware binary.\n", argv[2]);
return 1;
} else {
printf("Firmware binary %s was opened succesfully!\n", argv[2]);
}
struct termios serial_config;
bzero(&serial_config, sizeof(serial_config));
serial_config.c_cflag |= B115200;
serial_config.c_cflag &= ~PARENB;
serial_config.c_cflag &= ~CSTOPB;
serial_config.c_cflag &= ~CSIZE;
serial_config.c_cflag |= CS8;
serial_config.c_cflag &= ~CRTSCTS;
serial_config.c_cflag |= CREAD | CLOCAL;
serial_config.c_iflag &= ~(IXON | IXOFF | IXANY);
serial_config.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
serial_config.c_oflag &= ~OPOST;
if (tcsetattr(serial_fd, TCSANOW, &serial_config) != 0) {
printf("Error configuring %s\n", argv[1]);
close(serial_fd);
}
tcflush(serial_fd, TCIFLUSH);
ret = firmware_upgrade_serial(serial_fd, firmware_fd);
return ret;
}