blob: 9ed0c39c8570aac9b4bfbb59645b4aa50de3ea78 [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"
35 " | cut -c 2) > /sys/devices/bone_capemgr.*/slots'") ==
36 -1) {
37 LOG(FATAL, "system([disable OMAP UART]) failed with %d: %s\n", errno,
38 strerror(errno));
39 }
40 while (easy_access(device)) {
41 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
42 }
43 }
44
45 LOG(INFO, "exporting BB-UART1\n");
46 // 2 strings to work around a VIM bug where the indenter locks up when they're
47 // combined as 1...
48 if (system("bash -c 'echo BB-UART1 > /sys/devices/bone_capemgr.*"
49 "/slots'") ==
50 -1) {
51 LOG(FATAL, "system([enable OMAP UART]) failed with %d: %s\n", errno,
52 strerror(errno));
53 }
54 while (!easy_access(device)) {
55 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
56 }
57
58 return open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
59}
60
Brian Silverman1662a0e2013-12-19 17:50:01 -080061} // namespace
Daniel Petti059be422013-12-14 19:47:42 -080062
Brian Silverman0b6d9f42013-12-23 22:03:57 -080063extern "C" {
64
65// Sets all of the weird, annoying TTY flags on fd. In a separate file because
66// the structure it uses is so weird and annoying and carries so much baggage it
67// messes up all the #includes in whatever file it's used in.
Brian Silvermanc82c5122013-12-25 15:53:44 -080068// Defined in uart_reader_termios2.c.
Brian Silverman0b6d9f42013-12-23 22:03:57 -080069extern int aos_uart_reader_set_tty_options(int fd, int baud_rate);
70
71} // extern "C"
72
Brian Silvermanf6b68842013-12-20 12:34:58 -080073UartReader::UartReader(int32_t baud_rate)
Brian Silverman0b882522014-02-16 15:47:34 -080074 : fd_(open_device()) {
Daniel Pettia4fe7bc2013-12-22 12:57:50 -080075
Brian Silverman1662a0e2013-12-19 17:50:01 -080076 if (fd_ < 0) {
Brian Silvermanff1218b2014-01-03 14:47:39 -080077 LOG(FATAL, "open(%s, O_RDWR | O_NOCTTY | O_NOBLOCK) failed with %d: %s."
Brian Silvermanf6b68842013-12-20 12:34:58 -080078 " Did you read my note in bbb/uart_reader.cc?\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080079 device, errno, strerror(errno));
Daniel Petti059be422013-12-14 19:47:42 -080080 }
Brian Silvermaned030062013-12-20 21:03:47 -080081
Brian Silverman0b6d9f42013-12-23 22:03:57 -080082 if (aos_uart_reader_set_tty_options(fd_, baud_rate) != 0) {
83 LOG(FATAL, "aos_uart_reader_set_tty_options(%d) failed with %d: %s\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080084 fd_, errno, strerror(errno));
85 }
Brian Silvermanff1218b2014-01-03 14:47:39 -080086
87 FD_ZERO(&fd_set_);
88 FD_SET(fd_, &fd_set_);
Daniel Petti059be422013-12-14 19:47:42 -080089}
90
Brian Silvermanf6b68842013-12-20 12:34:58 -080091UartReader::~UartReader() {
Brian Silverman1662a0e2013-12-19 17:50:01 -080092 if (fd_ > 0) close(fd_);
93}
Daniel Petti059be422013-12-14 19:47:42 -080094
Brian Silverman04fac622014-01-26 18:32:15 -080095ssize_t UartReader::ReadBytes(uint8_t *dest, size_t max_bytes,
Brian Silverman7d16c572014-01-03 20:27:57 -080096 const ::aos::time::Time &timeout_time) {
Brian Silverman803b7a52014-01-01 13:21:18 -080097 do {
Brian Silverman7d16c572014-01-03 20:27:57 -080098 ::aos::time::Time timeout = timeout_time - ::aos::time::Time::Now();
99 if (timeout < ::aos::time::Time(0, 0)) return -2;
Brian Silvermanff1218b2014-01-03 14:47:39 -0800100 struct timeval timeout_timeval = timeout.ToTimeval();
101 switch (select(fd_ + 1, &fd_set_, NULL, NULL, &timeout_timeval)) {
102 case 0:
103 return -2;
104 case -1:
105 continue;
106 case 1:
107 break;
108 default:
109 LOG(WARNING, "unknown select return value\n");
110 return -1;
111 }
Brian Silverman803b7a52014-01-01 13:21:18 -0800112 ssize_t r = read(fd_, dest, max_bytes);
113 if (r != -1) return r;
114 } while (errno == EINTR);
115 return -1;
Brian Silverman1662a0e2013-12-19 17:50:01 -0800116}
Daniel Petti059be422013-12-14 19:47:42 -0800117
Brian Silverman04fac622014-01-26 18:32:15 -0800118bool UartReader::WriteBytes(uint8_t *bytes, size_t number_bytes) {
119 size_t written = 0;
120 while (written < number_bytes) {
121 ssize_t r = write(fd_, &bytes[written], number_bytes - written);
122 if (r == -1) return false;
123 written += r;
124 }
125 return true;
126}
127
Brian Silvermanffeef3f2013-12-22 14:06:23 -0800128} // namespace bbb