Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 1 | #include "bbb/uart_reader.h" |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 2 | |
Daniel Petti | a4fe7bc | 2013-12-22 12:57:50 -0800 | [diff] [blame] | 3 | #include <errno.h> |
Daniel Petti | b36f5b8 | 2013-12-15 08:55:04 -0800 | [diff] [blame] | 4 | #include <fcntl.h> |
| 5 | #include <linux/serial.h> |
Daniel Petti | b36f5b8 | 2013-12-15 08:55:04 -0800 | [diff] [blame] | 6 | #include <unistd.h> |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 7 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 8 | #include "aos/common/logging/logging.h" |
Brian Silverman | 53f2918 | 2013-12-21 15:16:27 -0800 | [diff] [blame] | 9 | |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 10 | // This is the code for receiving data from the cape via UART. |
Brian Silverman | 04fac62 | 2014-01-26 18:32:15 -0800 | [diff] [blame] | 11 | // 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 Petti | b36f5b8 | 2013-12-15 08:55:04 -0800 | [diff] [blame] | 15 | |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 16 | namespace bbb { |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 17 | namespace { |
Brian Silverman | 0b88252 | 2014-02-16 15:47:34 -0800 | [diff] [blame^] | 18 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 19 | // TODO(brians): Determine this in some way that allows easy switching for |
| 20 | // testing with /dev/ttyUSB0 for example. |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 21 | const char *device = "/dev/ttyO1"; |
Brian Silverman | 0b88252 | 2014-02-16 15:47:34 -0800 | [diff] [blame^] | 22 | |
| 23 | bool 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 | |
| 30 | int 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 Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 61 | } // namespace |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 62 | |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 63 | extern "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 Silverman | c82c512 | 2013-12-25 15:53:44 -0800 | [diff] [blame] | 68 | // Defined in uart_reader_termios2.c. |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 69 | extern int aos_uart_reader_set_tty_options(int fd, int baud_rate); |
| 70 | |
| 71 | } // extern "C" |
| 72 | |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 73 | UartReader::UartReader(int32_t baud_rate) |
Brian Silverman | 0b88252 | 2014-02-16 15:47:34 -0800 | [diff] [blame^] | 74 | : fd_(open_device()) { |
Daniel Petti | a4fe7bc | 2013-12-22 12:57:50 -0800 | [diff] [blame] | 75 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 76 | if (fd_ < 0) { |
Brian Silverman | ff1218b | 2014-01-03 14:47:39 -0800 | [diff] [blame] | 77 | LOG(FATAL, "open(%s, O_RDWR | O_NOCTTY | O_NOBLOCK) failed with %d: %s." |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 78 | " Did you read my note in bbb/uart_reader.cc?\n", |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 79 | device, errno, strerror(errno)); |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 80 | } |
Brian Silverman | ed03006 | 2013-12-20 21:03:47 -0800 | [diff] [blame] | 81 | |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 82 | 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 Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 84 | fd_, errno, strerror(errno)); |
| 85 | } |
Brian Silverman | ff1218b | 2014-01-03 14:47:39 -0800 | [diff] [blame] | 86 | |
| 87 | FD_ZERO(&fd_set_); |
| 88 | FD_SET(fd_, &fd_set_); |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 91 | UartReader::~UartReader() { |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 92 | if (fd_ > 0) close(fd_); |
| 93 | } |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 94 | |
Brian Silverman | 04fac62 | 2014-01-26 18:32:15 -0800 | [diff] [blame] | 95 | ssize_t UartReader::ReadBytes(uint8_t *dest, size_t max_bytes, |
Brian Silverman | 7d16c57 | 2014-01-03 20:27:57 -0800 | [diff] [blame] | 96 | const ::aos::time::Time &timeout_time) { |
Brian Silverman | 803b7a5 | 2014-01-01 13:21:18 -0800 | [diff] [blame] | 97 | do { |
Brian Silverman | 7d16c57 | 2014-01-03 20:27:57 -0800 | [diff] [blame] | 98 | ::aos::time::Time timeout = timeout_time - ::aos::time::Time::Now(); |
| 99 | if (timeout < ::aos::time::Time(0, 0)) return -2; |
Brian Silverman | ff1218b | 2014-01-03 14:47:39 -0800 | [diff] [blame] | 100 | 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 Silverman | 803b7a5 | 2014-01-01 13:21:18 -0800 | [diff] [blame] | 112 | ssize_t r = read(fd_, dest, max_bytes); |
| 113 | if (r != -1) return r; |
| 114 | } while (errno == EINTR); |
| 115 | return -1; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 116 | } |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 117 | |
Brian Silverman | 04fac62 | 2014-01-26 18:32:15 -0800 | [diff] [blame] | 118 | bool 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 Silverman | ffeef3f | 2013-12-22 14:06:23 -0800 | [diff] [blame] | 128 | } // namespace bbb |