blob: c45c8ea4b00dbf85767d3645e47439de19fcf05d [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 Silverman50a9d032014-02-16 17:20:57 -080077 LOG(FATAL, "open(%s, O_RDWR | O_NOCTTY | O_NOBLOCK) failed with %d: %s\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080078 device, errno, strerror(errno));
Daniel Petti059be422013-12-14 19:47:42 -080079 }
Brian Silvermaned030062013-12-20 21:03:47 -080080
Brian Silverman0b6d9f42013-12-23 22:03:57 -080081 if (aos_uart_reader_set_tty_options(fd_, baud_rate) != 0) {
82 LOG(FATAL, "aos_uart_reader_set_tty_options(%d) failed with %d: %s\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080083 fd_, errno, strerror(errno));
84 }
Brian Silvermanff1218b2014-01-03 14:47:39 -080085
86 FD_ZERO(&fd_set_);
87 FD_SET(fd_, &fd_set_);
Daniel Petti059be422013-12-14 19:47:42 -080088}
89
Brian Silvermanf6b68842013-12-20 12:34:58 -080090UartReader::~UartReader() {
Brian Silverman1662a0e2013-12-19 17:50:01 -080091 if (fd_ > 0) close(fd_);
92}
Daniel Petti059be422013-12-14 19:47:42 -080093
Brian Silverman04fac622014-01-26 18:32:15 -080094ssize_t UartReader::ReadBytes(uint8_t *dest, size_t max_bytes,
Brian Silverman7d16c572014-01-03 20:27:57 -080095 const ::aos::time::Time &timeout_time) {
Brian Silverman803b7a52014-01-01 13:21:18 -080096 do {
Brian Silverman7d16c572014-01-03 20:27:57 -080097 ::aos::time::Time timeout = timeout_time - ::aos::time::Time::Now();
98 if (timeout < ::aos::time::Time(0, 0)) return -2;
Brian Silvermanff1218b2014-01-03 14:47:39 -080099 struct timeval timeout_timeval = timeout.ToTimeval();
100 switch (select(fd_ + 1, &fd_set_, NULL, NULL, &timeout_timeval)) {
101 case 0:
102 return -2;
103 case -1:
104 continue;
105 case 1:
106 break;
107 default:
108 LOG(WARNING, "unknown select return value\n");
109 return -1;
110 }
Brian Silverman803b7a52014-01-01 13:21:18 -0800111 ssize_t r = read(fd_, dest, max_bytes);
112 if (r != -1) return r;
113 } while (errno == EINTR);
114 return -1;
Brian Silverman1662a0e2013-12-19 17:50:01 -0800115}
Daniel Petti059be422013-12-14 19:47:42 -0800116
Brian Silverman04fac622014-01-26 18:32:15 -0800117bool UartReader::WriteBytes(uint8_t *bytes, size_t number_bytes) {
118 size_t written = 0;
119 while (written < number_bytes) {
120 ssize_t r = write(fd_, &bytes[written], number_bytes - written);
121 if (r == -1) return false;
122 written += r;
123 }
124 return true;
125}
126
Brian Silvermanffeef3f2013-12-22 14:06:23 -0800127} // namespace bbb