• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_stream_uart_linux/stream.h"
16 
17 #include <fcntl.h>
18 #include <termios.h>
19 #include <unistd.h>
20 
21 #include <cerrno>
22 #include <cinttypes>
23 
24 #include "pw_log/log.h"
25 
26 namespace pw::stream {
27 
BaudRateToSpeed(uint32_t baud_rate)28 Result<speed_t> BaudRateToSpeed(uint32_t baud_rate) {
29   switch (baud_rate) {
30     case 9600:
31       return B9600;
32     case 19200:
33       return B19200;
34     case 38400:
35       return B38400;
36     case 57600:
37       return B57600;
38     case 115200:
39       return B115200;
40     case 230400:
41       return B230400;
42     case 460800:
43       return B460800;
44     case 500000:
45       return B500000;
46     case 576000:
47       return B576000;
48     case 921600:
49       return B921600;
50     case 1000000:
51       return B1000000;
52     case 1152000:
53       return B1152000;
54     case 1500000:
55       return B1500000;
56     case 2000000:
57       return B2000000;
58     case 2500000:
59       return B2500000;
60     case 3000000:
61       return B3000000;
62     case 3500000:
63       return B3500000;
64     case 4000000:
65       return B4000000;
66     default:
67       return Status::InvalidArgument();
68   }
69 }
70 
Open(const char * path,uint32_t baud_rate)71 Status UartStreamLinux::Open(const char* path, uint32_t baud_rate) {
72   const auto speed_result = BaudRateToSpeed(baud_rate);
73   if (!speed_result.ok()) {
74     PW_LOG_ERROR("Unsupported baud rate: %" PRIu32, baud_rate);
75     return speed_result.status();
76   }
77   speed_t speed = speed_result.value();
78 
79   if (fd_ != kInvalidFd) {
80     PW_LOG_ERROR("UART device already open");
81     return Status::FailedPrecondition();
82   }
83 
84   fd_ = open(path, O_RDWR);
85   if (fd_ < 0) {
86     PW_LOG_ERROR(
87         "Failed to open UART device '%s', %s", path, std::strerror(errno));
88     return Status::Unknown();
89   }
90 
91   struct termios tty;
92   int result = tcgetattr(fd_, &tty);
93   if (result < 0) {
94     PW_LOG_ERROR("Failed to get TTY attributes for '%s', %s",
95                  path,
96                  std::strerror(errno));
97     return Status::Unknown();
98   }
99 
100   cfmakeraw(&tty);
101   result = cfsetspeed(&tty, speed);
102   if (result < 0) {
103     PW_LOG_ERROR(
104         "Failed to set TTY speed for '%s', %s", path, std::strerror(errno));
105     return Status::Unknown();
106   }
107 
108   result = tcsetattr(fd_, TCSANOW, &tty);
109   if (result < 0) {
110     PW_LOG_ERROR("Failed to set TTY attributes for '%s', %s",
111                  path,
112                  std::strerror(errno));
113     return Status::Unknown();
114   }
115 
116   return OkStatus();
117 }
118 
Close()119 void UartStreamLinux::Close() {
120   if (fd_ != kInvalidFd) {
121     close(fd_);
122     fd_ = kInvalidFd;
123   }
124 }
125 
DoWrite(ConstByteSpan data)126 Status UartStreamLinux::DoWrite(ConstByteSpan data) {
127   const size_t size = data.size_bytes();
128   size_t written = 0;
129   while (written < size) {
130     int bytes = write(fd_, &data[written], size - written);
131     if (bytes < 0) {
132       PW_LOG_ERROR("Failed to write to UART, %s", std::strerror(errno));
133       return Status::Unknown();
134     }
135     written += bytes;
136   }
137   return OkStatus();
138 }
139 
DoRead(ByteSpan dest)140 StatusWithSize UartStreamLinux::DoRead(ByteSpan dest) {
141   int bytes = read(fd_, &dest[0], dest.size_bytes());
142   if (bytes < 0) {
143     PW_LOG_ERROR("Failed to read from UART, %s", std::strerror(errno));
144     return StatusWithSize::Unknown();
145   }
146   return StatusWithSize(bytes);
147 }
148 
149 }  // namespace pw::stream
150