* openUBMC is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <bcal/uart.h>
#include <algorithm>
#include <atomic>
#include <cerrno>
#include <chrono>
#include <climits>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <map>
#include <memory>
#include <mutex>
#include <poll.h>
#include <stdexcept>
#include <string>
#include <sys/eventfd.h>
#include <system_error>
#include <termios.h>
#include <unistd.h>
namespace BCAL_UART_ASPEED {
using namespace bcal;
using namespace BCAL_DRIVER_UART;
namespace {
constexpr const char* DRIVER_VERSION = "bcal_uart_aspeed v1.0.0 (ttyS termios)";
string_t error_text(int error)
{
return std::generic_category().message(error);
}
constexpr int32_t MAX_UART_INDEX = 12;
constexpr uint32_t MAX_IO_LEN_EXCLUSIVE = 1024 * 1024;
constexpr uint64_t UART_CLK_DIVISOR = 16;
constexpr uint64_t BAUD_SNAP_PERCENT = 2;
constexpr size_t SYSFS_VALUE_MAX = 64;
#define UART_BAUD_CONST(rate) B##rate
constexpr speed_t UART_BAUD_19200 = UART_BAUD_CONST(19200);
constexpr speed_t UART_BAUD_38400 = UART_BAUD_CONST(38400);
constexpr speed_t UART_BAUD_57600 = UART_BAUD_CONST(57600);
#undef UART_BAUD_CONST
const std::map<uint32_t, speed_t> g_baud_to_speed = {
{1200, B1200}, {1800, B1800}, {2400, B2400}, {4800, B4800},
{9600, B9600}, {19200, UART_BAUD_19200}, {38400, UART_BAUD_38400}, {57600, UART_BAUD_57600},
{115200, B115200}, {230400, B230400}, {460800, B460800}, {921600, B921600},
};
const std::map<uint8_t, tcflag_t> g_data_bits = {{5, CS5}, {6, CS6}, {7, CS7}, {8, CS8}};
const std::map<char, tcflag_t> g_parity_flags = {
{'N', 0}, {'E', PARENB}, {'O', PARENB | PARODD}, {'M', PARENB | PARODD | CMSPAR}, {'S', PARENB | CMSPAR},
};
speed_t baud_to_speed(uint32_t baud)
{
auto it = g_baud_to_speed.find(baud);
return it == g_baud_to_speed.end() ? 0 : it->second;
}
uint32_t read_uint_file(const string_t& path)
{
const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (fd < 0) {
const int error = errno;
throw std::runtime_error("open " + path + " failed: " + error_text(error));
}
char buffer[SYSFS_VALUE_MAX];
ssize_t n;
do {
errno = 0;
n = ::read(fd, buffer, sizeof(buffer) - 1);
} while (n < 0 && errno == EINTR);
const int error = n < 0 ? errno : 0;
::close(fd);
if (n <= 0) {
if (error != 0) {
throw std::runtime_error("read " + path + " failed: " + error_text(error));
}
throw std::runtime_error("read " + path + " failed: empty");
}
buffer[n] = '\0';
char* end = nullptr;
errno = 0;
const unsigned long value = std::strtoul(buffer, &end, 0);
if (errno != 0 || end == buffer) {
throw std::runtime_error("invalid numeric value in " + path);
}
return static_cast<uint32_t>(value);
}
uint32_t baud_from_termios(const termios& term)
{
const speed_t speed = ::cfgetispeed(&term);
for (const auto& item : g_baud_to_speed) {
if (item.second == speed) {
return item.first;
}
}
throw std::runtime_error("termios reports an unsupported UART baud rate");
}
uint8_t data_bits_from_termios(const termios& term)
{
for (const auto& item : g_data_bits) {
if ((term.c_cflag & CSIZE) == item.second) {
return item.first;
}
}
throw std::runtime_error("invalid UART data bits in termios");
}
char parity_from_termios(const termios& term)
{
if ((term.c_cflag & PARENB) == 0) {
return 'N';
}
if ((term.c_cflag & CMSPAR) != 0) {
return (term.c_cflag & PARODD) != 0 ? 'M' : 'S';
}
return (term.c_cflag & PARODD) != 0 ? 'O' : 'E';
}
}
class UartImpl : public Uart {
public:
explicit UartImpl(int32_t index);
~UartImpl() override;
void init(void* args, uint32_t size) override;
void lock(void) override;
void unlock(void) override;
string_t get_version(void) override;
string_t get_driver_name(void) const override;
int32_t control(uint32_t cmd, void* args_in, void* args_out) override;
void open(void) override;
void close(void) override;
string_t read(uint32_t len) override;
string_t select_read(uint32_t len, uint32_t timeout) override;
int32_t write(const string_view_t& val) override;
void send_break(int32_t duration) override;
void set_baud_rate(uint32_t baud) override;
void set_parity(char parity) override;
void set_data_bits(uint8_t bits) override;
void set_stop_bits(uint8_t bits) override;
uint32_t get_host_baud_rate(void) override;
private:
class InFlightGuard;
struct Session {
Session(int data_fd, int wake) : fd(data_fd), wake_fd(wake) {}
Session(const Session&) = delete;
Session& operator=(const Session&) = delete;
~Session();
int fd;
int wake_fd;
};
std::shared_ptr<Session> require_session() const;
std::shared_ptr<Session> take_session();
std::shared_ptr<Session> create_session() const;
std::shared_ptr<Session> open_once(bool* created);
std::unique_lock<std::mutex> lock_if_current(const std::shared_ptr<Session>& session) const;
std::unique_lock<std::mutex> lock_or_throw(const std::shared_ptr<Session>& session, const char* what) const;
void wake_session(const std::shared_ptr<Session>& session) const;
void close_if_current(const std::shared_ptr<Session>& session);
void check_chan(uint8_t chan, const char* cmd_name) const;
bool apply_format(const std::shared_ptr<Session>& session, speed_t speed);
void apply_baud_rate(uint32_t baud);
bool set_raw_mode(int fd) const;
termios get_termios(int fd) const;
void apply_termios(int fd, const termios& term) const;
void verify_baud_applied(int fd, speed_t speed) const;
uint32_t get_baud_from_reg(int fd) const;
string_t read_from(int fd, uint32_t len) const;
int32_t m_index;
std::string m_path;
std::mutex m_hardware_mutex;
mutable std::mutex m_session_mutex;
std::shared_ptr<Session> m_session;
std::mutex m_life_mutex;
std::condition_variable m_drained;
uint32_t m_inflight{0};
std::atomic<bool> m_closing{false};
};
class UartImpl::InFlightGuard {
public:
explicit InFlightGuard(UartImpl& self) : m_self(self)
{
std::lock_guard<std::mutex> guard(m_self.m_life_mutex);
if (!m_self.m_closing.load()) {
++m_self.m_inflight;
m_registered = true;
}
}
~InFlightGuard()
{
if (!m_registered) {
return;
}
std::lock_guard<std::mutex> guard(m_self.m_life_mutex);
if (--m_self.m_inflight == 0) {
m_self.m_drained.notify_all();
}
}
bool registered() const
{
return m_registered;
}
InFlightGuard(const InFlightGuard&) = delete;
InFlightGuard& operator=(const InFlightGuard&) = delete;
private:
UartImpl& m_self;
bool m_registered{false};
};
UartImpl::Session::~Session()
{
if (fd >= 0) {
::close(fd);
}
if (wake_fd >= 0) {
::close(wake_fd);
}
}
UartImpl::UartImpl(int32_t index) : m_index(index)
{
if (index < 0 || index > MAX_UART_INDEX) {
throw std::runtime_error("UART index " + std::to_string(index) + " is out of range(0-" +
std::to_string(MAX_UART_INDEX) + ")");
}
m_path = "/dev/ttyS" + std::to_string(index);
}
UartImpl::~UartImpl()
{
* 否则已登记的 open 可能在摘链后装入新会话,它的 select_read 无人唤醒,drain 会挂住。 */
{
std::lock_guard<std::mutex> guard(m_life_mutex);
m_closing.store(true);
}
auto session = take_session();
wake_session(session);
{
std::unique_lock<std::mutex> guard(m_life_mutex);
m_drained.wait(guard, [this] { return m_inflight == 0; });
}
}
void UartImpl::init(void* args, uint32_t size)
{
(void)args;
(void)size;
}
void UartImpl::lock(void)
{
m_hardware_mutex.lock();
}
void UartImpl::unlock(void)
{
m_hardware_mutex.unlock();
}
string_t UartImpl::get_version(void)
{
return DRIVER_VERSION;
}
string_t UartImpl::get_driver_name(void) const
{
return "uart";
}
void UartImpl::check_chan(uint8_t chan, const char* cmd_name) const
{
if (chan != static_cast<uint8_t>(m_index)) {
throw std::runtime_error(std::string("UART ") + cmd_name + " chan " + std::to_string(chan) +
" does not match driver index " + std::to_string(m_index));
}
}
bool UartImpl::set_raw_mode(int fd) const
{
termios term{};
if (::tcgetattr(fd, &term) != 0) {
return false;
}
::cfmakeraw(&term);
term.c_cflag |= CLOCAL | CREAD;
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
return ::tcsetattr(fd, TCSANOW, &term) == 0;
}
std::shared_ptr<UartImpl::Session> UartImpl::require_session() const
{
std::lock_guard<std::mutex> guard(m_session_mutex);
if (m_closing.load() || !m_session) {
throw std::runtime_error("UART " + std::to_string(m_index) + " is not open");
}
return m_session;
}
std::shared_ptr<UartImpl::Session> UartImpl::take_session()
{
std::lock_guard<std::mutex> guard(m_session_mutex);
return std::move(m_session);
}
std::unique_lock<std::mutex> UartImpl::lock_if_current(const std::shared_ptr<Session>& session) const
{
std::unique_lock<std::mutex> lock(m_session_mutex);
if (m_session != session) {
lock.unlock();
}
return lock;
}
std::unique_lock<std::mutex> UartImpl::lock_or_throw(const std::shared_ptr<Session>& session, const char* what) const
{
auto lock = lock_if_current(session);
if (!lock) {
throw std::runtime_error("UART " + std::to_string(m_index) + " session was closed during " + what);
}
return lock;
}
void UartImpl::wake_session(const std::shared_ptr<Session>& session) const
{
if (!session || session->wake_fd < 0) {
return;
}
const uint64_t value = 1;
while (::write(session->wake_fd, &value, sizeof(value)) < 0) {
if (errno == EINTR) {
continue;
}
const int error = errno;
std::fprintf(stderr, "[WARN] uart: write session wake eventfd failed: %s\n", error_text(error).c_str());
return;
}
}
std::shared_ptr<UartImpl::Session> UartImpl::create_session() const
{
auto session = std::make_shared<Session>(-1, -1);
const int fd = ::open(m_path.c_str(), O_RDWR | O_NOCTTY | O_NDELAY | O_CLOEXEC);
if (fd < 0) {
const int error = errno;
throw std::runtime_error("open " + m_path + " failed: " + error_text(error));
}
session->fd = fd;
if (::fcntl(fd, F_SETFL, 0) != 0) {
const int error = errno;
throw std::runtime_error("set blocking mode on " + m_path + " failed: " + error_text(error));
}
if (!set_raw_mode(fd)) {
const int error = errno;
throw std::runtime_error("configure raw mode on " + m_path + " failed: " + error_text(error));
}
const int wake_fd = ::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (wake_fd < 0) {
const int error = errno;
throw std::runtime_error("eventfd for UART session wakeup failed: " + error_text(error));
}
session->wake_fd = wake_fd;
return session;
}
void UartImpl::open(void)
{
InFlightGuard life{*this};
if (!life.registered()) {
return;
}
(void)open_once(nullptr);
}
std::shared_ptr<UartImpl::Session> UartImpl::open_once(bool* created)
{
if (created != nullptr) {
*created = false;
}
{
std::lock_guard<std::mutex> guard(m_session_mutex);
if (m_closing.load()) {
return nullptr;
}
if (m_session) {
return m_session;
}
}
auto session = create_session();
{
std::lock_guard<std::mutex> guard(m_session_mutex);
if (m_closing.load()) {
return nullptr;
}
if (m_session) {
return m_session;
}
m_session = session;
if (created != nullptr) {
*created = true;
}
return session;
}
}
void UartImpl::close_if_current(const std::shared_ptr<Session>& session)
{
std::shared_ptr<Session> detached;
{
std::lock_guard<std::mutex> guard(m_session_mutex);
if (m_session != session) {
return;
}
detached = std::move(m_session);
}
wake_session(detached);
}
void UartImpl::close(void)
{
InFlightGuard life{*this};
if (!life.registered()) {
return;
}
auto session = take_session();
wake_session(session);
}
termios UartImpl::get_termios(int fd) const
{
termios term{};
if (::tcgetattr(fd, &term) != 0) {
const int error = errno;
throw std::runtime_error("tcgetattr " + m_path + " failed: " + error_text(error));
}
return term;
}
void UartImpl::apply_termios(int fd, const termios& term) const
{
if (::tcsetattr(fd, TCSANOW, &term) != 0) {
const int error = errno;
throw std::runtime_error("tcsetattr " + m_path + " failed: " + error_text(error));
}
}
void UartImpl::verify_baud_applied(int fd, speed_t speed) const
{
termios applied{};
if (::tcgetattr(fd, &applied) != 0) {
const int error = errno;
throw std::runtime_error("tcgetattr " + m_path + " failed: " + error_text(error));
}
if (::cfgetispeed(&applied) == speed && ::cfgetospeed(&applied) == speed) {
return;
}
throw std::runtime_error("UART " + m_path +
" baud rate was not applied: kernel kept the previous rate, the UART clock may be "
"too low for this rate (e.g. 24MHz/13 => max 115200)");
}
bool UartImpl::apply_format(const std::shared_ptr<Session>& session, speed_t speed)
{
auto lock = lock_if_current(session);
if (!lock) {
return false;
}
const int fd = session->fd;
termios term{};
term.c_cflag |= CLOCAL | CREAD | CS8;
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
(void)::cfsetispeed(&term, speed);
(void)::cfsetospeed(&term, speed);
(void)::tcflush(fd, TCIFLUSH);
apply_termios(fd, term);
verify_baud_applied(fd, speed);
return true;
}
string_t UartImpl::read_from(int fd, uint32_t len) const
{
char chunk[4096];
const size_t want = std::min(sizeof(chunk), static_cast<size_t>(len));
ssize_t count;
do {
count = ::read(fd, chunk, want);
} while (count < 0 && errno == EINTR);
if (count < 0) {
const int error = errno;
throw std::runtime_error("read " + m_path + " failed: " + error_text(error));
}
return string_t(chunk, static_cast<size_t>(count));
}
string_t UartImpl::read(uint32_t len)
{
InFlightGuard life{*this};
if (!life.registered()) {
return {};
}
if (len == 0) {
return {};
}
if (len >= MAX_IO_LEN_EXCLUSIVE) {
throw std::runtime_error("UART read length is too large: " + std::to_string(len));
}
auto session = require_session();
auto lock = lock_if_current(session);
if (!lock) {
return {};
}
return read_from(session->fd, len);
}
string_t UartImpl::select_read(uint32_t len, uint32_t timeout)
{
InFlightGuard life{*this};
if (!life.registered()) {
return {};
}
if (len == 0) {
return {};
}
if (len >= MAX_IO_LEN_EXCLUSIVE) {
throw std::runtime_error("UART select_read length is too large: " + std::to_string(len));
}
auto session = require_session();
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout);
for (;;) {
int wait_ms = 0;
if (timeout != 0) {
const auto now = std::chrono::steady_clock::now();
if (now >= deadline) {
return {};
}
const auto left_us = std::chrono::duration_cast<std::chrono::microseconds>(deadline - now).count();
wait_ms = static_cast<int>(std::min<int64_t>((left_us + 999) / 1000, INT_MAX));
}
pollfd fds[2] = {{session->fd, POLLIN, 0}, {session->wake_fd, POLLIN, 0}};
const int ret = ::poll(fds, 2, wait_ms);
if (ret < 0) {
if (errno == EINTR) {
continue;
}
const int error = errno;
throw std::runtime_error("poll " + m_path + " failed: " + error_text(error));
}
if (fds[1].revents != 0) {
return {};
}
if (ret == 0) {
if (timeout == 0 || std::chrono::steady_clock::now() >= deadline) {
return {};
}
continue;
}
if (fds[0].revents == 0 || (fds[0].revents & POLLNVAL) != 0) {
return {};
}
auto lock = lock_if_current(session);
if (!lock) {
return {};
}
string_t data = read_from(session->fd, len);
if (!data.empty() || (fds[0].revents & (POLLHUP | POLLERR)) != 0 ||
timeout == 0 || std::chrono::steady_clock::now() >= deadline) {
return data;
}
}
}
int32_t UartImpl::write(const string_view_t& val)
{
InFlightGuard life{*this};
if (!life.registered()) {
return -1;
}
auto session = require_session();
const int fd = session->fd;
if (val.empty()) {
throw std::runtime_error("UART write data is empty");
}
if (val.size() >= MAX_IO_LEN_EXCLUSIVE) {
throw std::runtime_error("UART write length is too large: " + std::to_string(val.size()));
}
size_t written = 0;
while (written < val.size()) {
const ssize_t count = ::write(fd, val.data() + written, val.size() - written);
if (count < 0) {
if (errno == EINTR) {
continue;
}
const int error = errno;
throw std::runtime_error("write " + m_path + " failed: " + error_text(error));
}
if (count == 0) {
throw std::runtime_error("write " + m_path + " returned 0");
}
written += static_cast<size_t>(count);
}
return 0;
}
void UartImpl::send_break(int32_t duration)
{
InFlightGuard life{*this};
if (!life.registered()) {
return;
}
if (duration <= 0) {
return;
}
auto session = require_session();
* 内核再按该值 × 100ms 发送,因此最小 100ms,必须传毫秒原值。 */
if (::tcsendbreak(session->fd, duration) != 0) {
const int error = errno;
throw std::runtime_error("send break on " + m_path + " failed: " + error_text(error));
}
}
void UartImpl::set_baud_rate(uint32_t baud)
{
InFlightGuard life{*this};
if (!life.registered()) {
return;
}
apply_baud_rate(baud);
}
void UartImpl::apply_baud_rate(uint32_t baud)
{
const speed_t speed = baud_to_speed(baud);
if (speed == 0) {
throw std::runtime_error("unsupported UART baud rate: " + std::to_string(baud));
}
auto session = require_session();
auto lock = lock_or_throw(session, "set_baud_rate");
const int fd = session->fd;
termios term = get_termios(fd);
if (::cfgetispeed(&term) == speed && ::cfgetospeed(&term) == speed) {
return;
}
(void)::cfsetispeed(&term, speed);
(void)::cfsetospeed(&term, speed);
apply_termios(fd, term);
verify_baud_applied(fd, speed);
}
void UartImpl::set_parity(char parity)
{
InFlightGuard life{*this};
if (!life.registered()) {
return;
}
const auto flags = g_parity_flags.find(parity);
if (flags == g_parity_flags.end()) {
throw std::runtime_error("invalid UART parity: " + std::string(1, parity));
}
auto session = require_session();
auto lock = lock_or_throw(session, "set_parity");
const int fd = session->fd;
termios term = get_termios(fd);
term.c_cflag &= ~(PARENB | PARODD | CMSPAR);
term.c_cflag |= flags->second;
term.c_iflag &= ~ISTRIP;
if (parity == 'N') {
term.c_iflag &= ~INPCK;
} else {
term.c_iflag |= INPCK;
}
apply_termios(fd, term);
}
void UartImpl::set_data_bits(uint8_t bits)
{
InFlightGuard life{*this};
if (!life.registered()) {
return;
}
const auto size = g_data_bits.find(bits);
if (size == g_data_bits.end()) {
throw std::runtime_error("invalid UART data bits: " + std::to_string(bits));
}
auto session = require_session();
auto lock = lock_or_throw(session, "set_data_bits");
const int fd = session->fd;
termios term = get_termios(fd);
term.c_cflag &= ~CSIZE;
term.c_cflag |= size->second;
apply_termios(fd, term);
}
void UartImpl::set_stop_bits(uint8_t bits)
{
InFlightGuard life{*this};
if (!life.registered()) {
return;
}
if (bits != 1 && bits != 2) {
throw std::runtime_error("invalid UART stop bits: " + std::to_string(bits));
}
auto session = require_session();
auto lock = lock_or_throw(session, "set_stop_bits");
const int fd = session->fd;
termios term = get_termios(fd);
if (bits == 1) {
term.c_cflag &= ~CSTOPB;
} else {
term.c_cflag |= CSTOPB;
}
apply_termios(fd, term);
}
uint32_t UartImpl::get_host_baud_rate(void)
{
InFlightGuard life{*this};
(void)life;
return 0;
}
uint32_t UartImpl::get_baud_from_reg(int fd) const
{
const uint32_t uartclk = read_uint_file("/sys/class/tty/ttyS" + std::to_string(m_index) + "/uartclk");
if (uartclk == 0) {
throw std::runtime_error("UART /dev/ttyS" + std::to_string(m_index) + " reports uartclk=0");
}
const uint32_t baud = baud_from_termios(get_termios(fd));
const uint64_t divisor = std::max<uint64_t>(
1, (static_cast<uint64_t>(uartclk) + (UART_CLK_DIVISOR / 2) * baud) / (UART_CLK_DIVISOR * baud));
const uint32_t actual = static_cast<uint32_t>(uartclk / (UART_CLK_DIVISOR * divisor));
for (const auto& item : g_baud_to_speed) {
const uint32_t standard = item.first;
const uint64_t diff = actual > standard ? actual - standard : standard - actual;
if (diff * 100 <= static_cast<uint64_t>(standard) * BAUD_SNAP_PERCENT) {
return standard;
}
}
return actual;
}
int32_t UartImpl::control(uint32_t cmd, void* args_in, void* args_out)
{
InFlightGuard life{*this};
if (!life.registered()) {
return -1;
}
switch (cmd) {
case CMD_OPEN_PORT: {
if (args_in == nullptr) {
throw std::runtime_error("UART CMD_OPEN_PORT args_in is null");
}
auto* info = static_cast<UART_OPEN_PORT_INFO_S*>(args_in);
check_chan(info->chan, "CMD_OPEN_PORT");
const speed_t speed = baud_to_speed(info->baud);
if (speed == 0) {
throw std::runtime_error("unsupported UART baud rate: " + std::to_string(info->baud));
}
bool created = false;
auto session = open_once(&created);
if (!session) {
return -1;
}
try {
if (!apply_format(session, speed)) {
throw std::runtime_error("UART CMD_OPEN_PORT session was closed during format");
}
} catch (...) {
if (created) {
close_if_current(session);
}
throw;
}
return 0;
}
case CMD_UPDATE_BAUD:
throw std::runtime_error("UART CMD_UPDATE_BAUD is not supported on current platform: "
"use CMD_OPEN_PORT or CMD_UPDATE_BAUD_V2 with an explicit baud rate");
case CMD_UPDATE_BAUD_V2: {
if (args_in == nullptr) {
throw std::runtime_error("UART CMD_UPDATE_BAUD_V2 args_in is null");
}
const auto* info = static_cast<UART_UPDATE_BAUD_V2_INFO_S*>(args_in);
check_chan(info->chan, "CMD_UPDATE_BAUD_V2");
apply_baud_rate(info->val);
return 0;
}
case CMD_GET_BAUD_FROM_TCGETATTR: {
if (args_in == nullptr || args_out == nullptr) {
throw std::runtime_error("UART get baud args is null");
}
check_chan(static_cast<CMD_GET_CHAN_IN_S*>(args_in)->chan, "get baud");
auto session = require_session();
auto lock = lock_if_current(session);
if (!lock) {
return -1;
}
static_cast<CMD_GET_BAUD_OUT_S*>(args_out)->baud = baud_from_termios(get_termios(session->fd));
return 0;
}
case CMD_GET_BAUD_FROM_REG: {
if (args_in == nullptr || args_out == nullptr) {
throw std::runtime_error("UART get baud args is null");
}
check_chan(static_cast<CMD_GET_CHAN_IN_S*>(args_in)->chan, "get baud");
auto session = require_session();
auto lock = lock_if_current(session);
if (!lock) {
return -1;
}
static_cast<CMD_GET_BAUD_OUT_S*>(args_out)->baud = get_baud_from_reg(session->fd);
return 0;
}
case CMD_GET_DATA_BITS_FROM_TCGETATTR:
case CMD_GET_DATA_BITS_FROM_REG: {
if (args_in == nullptr || args_out == nullptr) {
throw std::runtime_error("UART get data bits args is null");
}
check_chan(static_cast<CMD_GET_CHAN_IN_S*>(args_in)->chan, "get data bits");
auto session = require_session();
auto lock = lock_if_current(session);
if (!lock) {
return -1;
}
const termios term = get_termios(session->fd);
static_cast<CMD_GET_DATA_BITS_OUT_S*>(args_out)->data_bits = data_bits_from_termios(term);
return 0;
}
case CMD_GET_PARITY_FROM_TCGETATTR:
case CMD_GET_PARITY_FROM_REG: {
if (args_in == nullptr || args_out == nullptr) {
throw std::runtime_error("UART get parity args is null");
}
check_chan(static_cast<CMD_GET_CHAN_IN_S*>(args_in)->chan, "get parity");
auto session = require_session();
auto lock = lock_if_current(session);
if (!lock) {
return -1;
}
const termios term = get_termios(session->fd);
static_cast<CMD_GET_PARITY_OUT_S*>(args_out)->parity = parity_from_termios(term);
return 0;
}
case CMD_GET_STOP_BITS_FROM_TCGETATTR:
case CMD_GET_STOP_BITS_FROM_REG: {
if (args_in == nullptr || args_out == nullptr) {
throw std::runtime_error("UART get stop bits args is null");
}
check_chan(static_cast<CMD_GET_CHAN_IN_S*>(args_in)->chan, "get stop bits");
auto session = require_session();
auto lock = lock_if_current(session);
if (!lock) {
return -1;
}
const termios term = get_termios(session->fd);
static_cast<CMD_GET_STOP_BITS_OUT_S*>(args_out)->stop_bits = (term.c_cflag & CSTOPB) != 0 ? 2 : 1;
return 0;
}
default:
throw std::runtime_error("unsupported UART control command: " + std::to_string(cmd));
}
}
}
extern "C" {
__attribute__((visibility("default"))) bcal::IDriver* create_driver(int32_t index)
{
try {
return new BCAL_UART_ASPEED::UartImpl(index);
} catch (const std::exception& e) {
std::fprintf(stderr, "[ERROR] uart: create driver(index=%d) failed: %s\n", static_cast<int>(index), e.what());
return nullptr;
} catch (...) {
std::fprintf(stderr, "[ERROR] uart: create driver(index=%d) failed: unknown exception\n",
static_cast<int>(index));
return nullptr;
}
}
__attribute__((visibility("default"))) void destroy_driver(bcal::IDriver* driver)
{
delete driver;
}
}