xref: /aosp_15_r20/external/pigweed/pw_stream_uart_linux/stream.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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,Config config)71 Status UartStreamLinux::Open(const char* path, Config config) {
72   std::optional<speed_t> speed;
73   if (config.baud_rate.has_value()) {
74     const auto speed_result = BaudRateToSpeed(*config.baud_rate);
75     if (!speed_result.ok()) {
76       PW_LOG_ERROR("Unsupported baud rate: %" PRIu32, *config.baud_rate);
77       return speed_result.status();
78     }
79     speed = speed_result.value();
80   }
81 
82   if (fd_ != kInvalidFd) {
83     PW_LOG_ERROR("UART device already open");
84     return Status::FailedPrecondition();
85   }
86 
87   fd_ = open(path, O_RDWR);
88   if (fd_ < 0) {
89     PW_LOG_ERROR(
90         "Failed to open UART device '%s', %s", path, std::strerror(errno));
91     return Status::Unknown();
92   }
93 
94   struct termios tty;
95   int result = tcgetattr(fd_, &tty);
96   if (result < 0) {
97     PW_LOG_ERROR("Failed to get TTY attributes for '%s', %s",
98                  path,
99                  std::strerror(errno));
100     return Status::Unknown();
101   }
102 
103   cfmakeraw(&tty);
104 
105   if (speed.has_value()) {
106     result = cfsetspeed(&tty, *speed);
107     if (result < 0) {
108       PW_LOG_ERROR(
109           "Failed to set TTY speed for '%s', %s", path, std::strerror(errno));
110       return Status::Unknown();
111     }
112   }
113 
114   if (config.flow_control.has_value()) {
115     if (*config.flow_control) {
116       // Enable hardware flow control.
117       tty.c_cflag |= (CRTSCTS);
118     } else {
119       // Disable hardware flow control.
120       tty.c_cflag &= ~(CRTSCTS);
121     }
122   }
123 
124   result = tcsetattr(fd_, TCSANOW, &tty);
125   if (result < 0) {
126     PW_LOG_ERROR("Failed to set TTY attributes for '%s', %s",
127                  path,
128                  std::strerror(errno));
129     return Status::Unknown();
130   }
131 
132   return OkStatus();
133 }
134 
Close()135 void UartStreamLinux::Close() {
136   if (fd_ != kInvalidFd) {
137     close(fd_);
138     fd_ = kInvalidFd;
139   }
140 }
141 
DoWrite(ConstByteSpan data)142 Status UartStreamLinux::DoWrite(ConstByteSpan data) {
143   const size_t size = data.size_bytes();
144   size_t written = 0;
145   while (written < size) {
146     int bytes = write(fd_, &data[written], size - written);
147     if (bytes < 0) {
148       PW_LOG_ERROR("Failed to write to UART, %s", std::strerror(errno));
149       return Status::Unknown();
150     }
151     written += bytes;
152   }
153   return OkStatus();
154 }
155 
DoRead(ByteSpan dest)156 StatusWithSize UartStreamLinux::DoRead(ByteSpan dest) {
157   int bytes = read(fd_, &dest[0], dest.size_bytes());
158   if (bytes < 0) {
159     PW_LOG_ERROR("Failed to read from UART, %s", std::strerror(errno));
160     return StatusWithSize::Unknown();
161   }
162   return StatusWithSize(bytes);
163 }
164 
165 }  // namespace pw::stream
166