blob: 9abb28421957d99d279c9d60b4a7c9366d0e229f [file] [log] [blame]
Brian Silvermanf6b68842013-12-20 12:34:58 -08001#include "bbb/uart_reader.h"
Brian Silverman1662a0e2013-12-19 17:50:01 -08002
Daniel Pettia4fe7bc2013-12-22 12:57:50 -08003#include <errno.h>
Daniel Pettib36f5b82013-12-15 08:55:04 -08004#include <fcntl.h>
5#include <linux/serial.h>
Daniel Pettib36f5b82013-12-15 08:55:04 -08006#include <unistd.h>
Daniel Petti059be422013-12-14 19:47:42 -08007
Brian Silverman1662a0e2013-12-19 17:50:01 -08008#include "aos/common/logging/logging.h"
Brian Silverman53f29182013-12-21 15:16:27 -08009
Daniel Petti059be422013-12-14 19:47:42 -080010// This is the code for receiving data from the cape via UART.
Brian Silverman04fac622014-01-26 18:32:15 -080011// fragment active.
12// `su -c "echo BB-UART1 > /sys/devices/bone_capemgr.*/slots"` works, but
13// you have to do it every time. It is also possible to set it up to do that
14// every time it boots.
Daniel Pettib36f5b82013-12-15 08:55:04 -080015
Daniel Petti059be422013-12-14 19:47:42 -080016namespace bbb {
Brian Silverman1662a0e2013-12-19 17:50:01 -080017namespace {
Brian Silverman0b882522014-02-16 15:47:34 -080018
Brian Silverman1662a0e2013-12-19 17:50:01 -080019// TODO(brians): Determine this in some way that allows easy switching for
20// testing with /dev/ttyUSB0 for example.
Brian Silverman0b6d9f42013-12-23 22:03:57 -080021const char *device = "/dev/ttyO1";
Brian Silverman0b882522014-02-16 15:47:34 -080022
23bool easy_access(const char *path) {
24 if (access(path, R_OK | W_OK) == 0) return true;
25 if (errno == EACCES || errno == ENOENT) return false;
26 LOG(FATAL, "access(%s, F_OK) failed with %d: %s\n", path, errno,
27 strerror(errno));
28}
29
30int open_device() {
31 if (easy_access(device)) {
32 LOG(INFO, "unexporting BB-UART1\n");
33 if (system("bash -c 'echo -$(cat /sys/devices/bone_capemgr.*/slots"
34 " | fgrep BB-UART1"
Brian Silverman2508c082014-02-17 15:45:02 -080035 " | cut -d : -f 1 | tr -d \" \")"
36 " > /sys/devices/bone_capemgr.*/slots'") == -1) {
Brian Silverman0b882522014-02-16 15:47:34 -080037 LOG(FATAL, "system([disable OMAP UART]) failed with %d: %s\n", errno,
38 strerror(errno));
39 }
40 while (easy_access(device)) {
Brian Silverman2508c082014-02-17 15:45:02 -080041 LOG(DEBUG, "waiting for BB-UART1 to be unexported\n");
Brian Silverman0b882522014-02-16 15:47:34 -080042 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
43 }
44 }
45
46 LOG(INFO, "exporting BB-UART1\n");
47 // 2 strings to work around a VIM bug where the indenter locks up when they're
48 // combined as 1...
49 if (system("bash -c 'echo BB-UART1 > /sys/devices/bone_capemgr.*"
50 "/slots'") ==
51 -1) {
52 LOG(FATAL, "system([enable OMAP UART]) failed with %d: %s\n", errno,
53 strerror(errno));
54 }
55 while (!easy_access(device)) {
Brian Silverman2508c082014-02-17 15:45:02 -080056 LOG(DEBUG, "waiting for BB-UART1 to be exported\n");
Brian Silverman0b882522014-02-16 15:47:34 -080057 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
58 }
59
60 return open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
61}
62
Brian Silverman1662a0e2013-12-19 17:50:01 -080063} // namespace
Daniel Petti059be422013-12-14 19:47:42 -080064
Brian Silverman0b6d9f42013-12-23 22:03:57 -080065extern "C" {
66
67// Sets all of the weird, annoying TTY flags on fd. In a separate file because
68// the structure it uses is so weird and annoying and carries so much baggage it
69// messes up all the #includes in whatever file it's used in.
Brian Silvermanc82c5122013-12-25 15:53:44 -080070// Defined in uart_reader_termios2.c.
Brian Silverman0b6d9f42013-12-23 22:03:57 -080071extern int aos_uart_reader_set_tty_options(int fd, int baud_rate);
72
73} // extern "C"
74
Brian Silvermanf6b68842013-12-20 12:34:58 -080075UartReader::UartReader(int32_t baud_rate)
Brian Silverman0b882522014-02-16 15:47:34 -080076 : fd_(open_device()) {
Daniel Pettia4fe7bc2013-12-22 12:57:50 -080077
Brian Silverman1662a0e2013-12-19 17:50:01 -080078 if (fd_ < 0) {
Brian Silverman50a9d032014-02-16 17:20:57 -080079 LOG(FATAL, "open(%s, O_RDWR | O_NOCTTY | O_NOBLOCK) failed with %d: %s\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080080 device, errno, strerror(errno));
Daniel Petti059be422013-12-14 19:47:42 -080081 }
Brian Silvermaned030062013-12-20 21:03:47 -080082
Brian Silverman0b6d9f42013-12-23 22:03:57 -080083 if (aos_uart_reader_set_tty_options(fd_, baud_rate) != 0) {
84 LOG(FATAL, "aos_uart_reader_set_tty_options(%d) failed with %d: %s\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080085 fd_, errno, strerror(errno));
86 }
Daniel Petti059be422013-12-14 19:47:42 -080087}
88
Brian Silvermanf6b68842013-12-20 12:34:58 -080089UartReader::~UartReader() {
Brian Silverman1662a0e2013-12-19 17:50:01 -080090 if (fd_ > 0) close(fd_);
91}
Daniel Petti059be422013-12-14 19:47:42 -080092
Brian Silverman04fac622014-01-26 18:32:15 -080093ssize_t UartReader::ReadBytes(uint8_t *dest, size_t max_bytes,
Brian Silverman7d16c572014-01-03 20:27:57 -080094 const ::aos::time::Time &timeout_time) {
Brian Silverman803b7a52014-01-01 13:21:18 -080095 do {
Brian Silverman7d16c572014-01-03 20:27:57 -080096 ::aos::time::Time timeout = timeout_time - ::aos::time::Time::Now();
97 if (timeout < ::aos::time::Time(0, 0)) return -2;
Brian Silvermanff1218b2014-01-03 14:47:39 -080098 struct timeval timeout_timeval = timeout.ToTimeval();
Brian Silverman0fc4ca72014-03-02 17:26:06 -080099 fd_set fds;
100 FD_ZERO(&fds);
101 FD_SET(fd_, &fds);
102 switch (select(fd_ + 1, &fds, NULL, NULL, &timeout_timeval)) {
Brian Silvermanff1218b2014-01-03 14:47:39 -0800103 case 0:
104 return -2;
105 case -1:
106 continue;
107 case 1:
108 break;
109 default:
110 LOG(WARNING, "unknown select return value\n");
111 return -1;
112 }
Brian Silverman803b7a52014-01-01 13:21:18 -0800113 ssize_t r = read(fd_, dest, max_bytes);
114 if (r != -1) return r;
115 } while (errno == EINTR);
116 return -1;
Brian Silverman1662a0e2013-12-19 17:50:01 -0800117}
Daniel Petti059be422013-12-14 19:47:42 -0800118
Brian Silverman04fac622014-01-26 18:32:15 -0800119bool UartReader::WriteBytes(uint8_t *bytes, size_t number_bytes) {
120 size_t written = 0;
121 while (written < number_bytes) {
122 ssize_t r = write(fd_, &bytes[written], number_bytes - written);
123 if (r == -1) return false;
124 written += r;
125 }
126 return true;
127}
128
Brian Silvermanffeef3f2013-12-22 14:06:23 -0800129} // namespace bbb