From fef5155bcb51a8b20ef4f9702c5c9c80bab7a825 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Fri, 28 Mar 2025 17:26:50 +0800 Subject: [PATCH] v2 --- src/components/channel/uart_channel.cpp | 37 ++++++++++++++++++++++++++++++++- src/components/uart/uart.cpp | 31 +++++++++++++++------------ src/components/uart/uart.hpp | 2 +- src/configs/version.hpp | 2 +- src/service/extapi_service.cpp | 7 +++++-- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/components/channel/uart_channel.cpp b/src/components/channel/uart_channel.cpp index ead30eb..fb13287 100644 --- a/src/components/channel/uart_channel.cpp +++ b/src/components/channel/uart_channel.cpp @@ -141,7 +141,42 @@ void UartChannel::senddata(bool binary, const char* data, size_t len) { } void UartChannel::registerOnDataCallback(OnData_t ondata) { m_ondata = ondata; } void UartChannel::callcmd(string cmd, unordered_map param, json& receipt) { - // logger->info("{} callcmd {} {}", m_chname, cmd, paramToString(param)); + logger->info("{} callcmd {} {}", m_chname, cmd, paramToString(param)); + if (cmd == "setBaudrate") { + int rate = atoi(param["baudrate"].c_str()); + if (rate == 0) { + logger->error("rate is 0"); + receipt["status"] = -1; + receipt["msg"] = "rate is not support"; + return; + } + + int setrate = findBaudrate(rate); + if (setrate == -1) { + logger->error("unsupport baudrate:{}", m_baudrate); + receipt["status"] = -1; + receipt["msg"] = "unsupport baudrate"; + return; + } + + int suc = uartSetRate(&m_uartHandler, setrate); + if (suc != 0) { + logger->error("uartSetRate fail ,ecode = {}", strerror(errno)); + receipt["status"] = -1; + receipt["msg"] = fmt::format("uartSetRate fail ,ecode = {}", strerror(errno)); + return; + } + m_baudrate = rate; + receipt["status"] = 0; + receipt["msg"] = "ok"; + } else if (cmd == "getBaudrate") { + receipt["status"] = 0; + receipt["data"]["baudrate"] = m_baudrate; + } else { + logger->error("unsupport cmd:{}", cmd); + receipt["status"] = -1; + receipt["msg"] = "unsupport cmd"; + } } list UartChannel::getCmdList() { diff --git a/src/components/uart/uart.cpp b/src/components/uart/uart.cpp index e39756f..a646344 100644 --- a/src/components/uart/uart.cpp +++ b/src/components/uart/uart.cpp @@ -36,7 +36,6 @@ wait. int uartStart(struct UartDevice* dev) { struct termios* tty; int fd; - int rc; fd = open(dev->name, O_RDWR | O_NOCTTY); if (fd < 0) { @@ -49,6 +48,14 @@ int uartStart(struct UartDevice* dev) { printf("%s: failed to allocate tty instance\r\n", __func__); return UART_FAILURE; } + + dev->fd = fd; + dev->tty = tty; + + return uartSetRate(dev, dev->rate); +} + +int uartSetRate(struct UartDevice* dev, int rate) { // memset(tty, 0, sizeof(struct termios)); /* BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. @@ -59,36 +66,34 @@ int uartStart(struct UartDevice* dev) { CREAD : enable receiving characters */ // tty->c_cflag = dev->rate | CRTSCTS | CS8 | CLOCAL | CREAD; - tty->c_cflag = dev->rate | CS8 | CLOCAL | CREAD; + dev->rate = rate; + dev->tty->c_cflag = dev->rate | CS8 | CLOCAL | CREAD; // not canonic /* IGNPAR : ignore bytes with parity errorsc_cc[VTIME] */ - tty->c_iflag = IGNPAR; + dev->tty->c_iflag = IGNPAR; /* set input mode (non-canonical, no echo,...) */ - tty->c_lflag = 0; + dev->tty->c_lflag = 0; /* Do not wait for data */ - tty->c_cc[VTIME] = 0; /* inter-character timer unused */ - tty->c_cc[VMIN] = 0; /* blocking read until 5 chars received */ + dev->tty->c_cc[VTIME] = 0; /* inter-character timer unused */ + dev->tty->c_cc[VMIN] = 0; /* blocking read until 5 chars received */ /* Raw output. */ - tty->c_oflag = 0; + dev->tty->c_oflag = 0; + dev->rate = rate; /* Flush port */ - tcflush(fd, TCIFLUSH); + tcflush(dev->fd, TCIFLUSH); /* Apply attributes */ - rc = tcsetattr(fd, TCSANOW, tty); + int rc = tcsetattr(dev->fd, TCSANOW, dev->tty); if (rc) { printf("%s: failed to set TCSANOW attr\r\n", __func__); return UART_FAILURE; } - - dev->fd = fd; - dev->tty = tty; - return UART_SUCCESS; } diff --git a/src/components/uart/uart.hpp b/src/components/uart/uart.hpp index 46b8417..c817ede 100644 --- a/src/components/uart/uart.hpp +++ b/src/components/uart/uart.hpp @@ -15,7 +15,6 @@ struct UartDevice { char* name; int rate; - int fd; struct termios* tty; }; @@ -24,5 +23,6 @@ int uartStart(struct UartDevice* dev); int uartSend(struct UartDevice* dev, const char* data, int size); int uartReceive(struct UartDevice* dev, char* data, int size_max); int uartStop(struct UartDevice* dev); +int uartSetRate(struct UartDevice* dev,int rate); #endif /* SRC_UART_H_ */ \ No newline at end of file diff --git a/src/configs/version.hpp b/src/configs/version.hpp index ed61e6b..4fe30a0 100644 --- a/src/configs/version.hpp +++ b/src/configs/version.hpp @@ -1,2 +1,2 @@ #pragma once -#define VERSION "1" \ No newline at end of file +#define VERSION "2" \ No newline at end of file diff --git a/src/service/extapi_service.cpp b/src/service/extapi_service.cpp index 44e12ac..cc308e2 100644 --- a/src/service/extapi_service.cpp +++ b/src/service/extapi_service.cpp @@ -77,8 +77,8 @@ void ExtAPIService::initialize() { if (channelName == "server") { if (cmd == "restart") { exit(0); - } else if (cmd == "get-status") { - receipt["status"] = 0; + } else if (cmd == "getStatus" || cmd == "get-status") { + receipt["status"] = 0; for (auto it : DataChannelMgr::getChannels()) { if (it->getAlias().empty()) { receipt["data"][it->getChannelName()] = it->getChannelInfo(); @@ -86,6 +86,9 @@ void ExtAPIService::initialize() { receipt["data"][it->getAlias()] = it->getChannelInfo(); } } + } else if (cmd == "getVersion") { + receipt["status"] = 0; + receipt["data"]["version"] = VERSION; } }