zhaohe 4 months ago
parent
commit
fef5155bcb
  1. 37
      src/components/channel/uart_channel.cpp
  2. 31
      src/components/uart/uart.cpp
  3. 2
      src/components/uart/uart.hpp
  4. 2
      src/configs/version.hpp
  5. 7
      src/service/extapi_service.cpp

37
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::registerOnDataCallback(OnData_t ondata) { m_ondata = ondata; }
void UartChannel::callcmd(string cmd, unordered_map<string, string> param, json& receipt) { void UartChannel::callcmd(string cmd, unordered_map<string, string> 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<string> UartChannel::getCmdList() { list<string> UartChannel::getCmdList() {

31
src/components/uart/uart.cpp

@ -36,7 +36,6 @@ wait.
int uartStart(struct UartDevice* dev) { int uartStart(struct UartDevice* dev) {
struct termios* tty; struct termios* tty;
int fd; int fd;
int rc;
fd = open(dev->name, O_RDWR | O_NOCTTY); fd = open(dev->name, O_RDWR | O_NOCTTY);
if (fd < 0) { if (fd < 0) {
@ -49,6 +48,14 @@ int uartStart(struct UartDevice* dev) {
printf("%s: failed to allocate tty instance\r\n", __func__); printf("%s: failed to allocate tty instance\r\n", __func__);
return UART_FAILURE; 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)); // memset(tty, 0, sizeof(struct termios));
/* /*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
@ -59,36 +66,34 @@ int uartStart(struct UartDevice* dev) {
CREAD : enable receiving characters CREAD : enable receiving characters
*/ */
// tty->c_cflag = dev->rate | CRTSCTS | CS8 | CLOCAL | CREAD; // 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 // not canonic
/* /*
IGNPAR : ignore bytes with parity errorsc_cc[VTIME] IGNPAR : ignore bytes with parity errorsc_cc[VTIME]
*/ */
tty->c_iflag = IGNPAR;
dev->tty->c_iflag = IGNPAR;
/* set input mode (non-canonical, no echo,...) */ /* set input mode (non-canonical, no echo,...) */
tty->c_lflag = 0;
dev->tty->c_lflag = 0;
/* Do not wait for data */ /* 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. Raw output.
*/ */
tty->c_oflag = 0;
dev->tty->c_oflag = 0;
dev->rate = rate;
/* Flush port */ /* Flush port */
tcflush(fd, TCIFLUSH);
tcflush(dev->fd, TCIFLUSH);
/* Apply attributes */ /* Apply attributes */
rc = tcsetattr(fd, TCSANOW, tty);
int rc = tcsetattr(dev->fd, TCSANOW, dev->tty);
if (rc) { if (rc) {
printf("%s: failed to set TCSANOW attr\r\n", __func__); printf("%s: failed to set TCSANOW attr\r\n", __func__);
return UART_FAILURE; return UART_FAILURE;
} }
dev->fd = fd;
dev->tty = tty;
return UART_SUCCESS; return UART_SUCCESS;
} }

2
src/components/uart/uart.hpp

@ -15,7 +15,6 @@
struct UartDevice { struct UartDevice {
char* name; char* name;
int rate; int rate;
int fd; int fd;
struct termios* tty; struct termios* tty;
}; };
@ -24,5 +23,6 @@ int uartStart(struct UartDevice* dev);
int uartSend(struct UartDevice* dev, const char* data, int size); int uartSend(struct UartDevice* dev, const char* data, int size);
int uartReceive(struct UartDevice* dev, char* data, int size_max); int uartReceive(struct UartDevice* dev, char* data, int size_max);
int uartStop(struct UartDevice* dev); int uartStop(struct UartDevice* dev);
int uartSetRate(struct UartDevice* dev,int rate);
#endif /* SRC_UART_H_ */ #endif /* SRC_UART_H_ */

2
src/configs/version.hpp

@ -1,2 +1,2 @@
#pragma once #pragma once
#define VERSION "1"
#define VERSION "2"

7
src/service/extapi_service.cpp

@ -77,8 +77,8 @@ void ExtAPIService::initialize() {
if (channelName == "server") { if (channelName == "server") {
if (cmd == "restart") { if (cmd == "restart") {
exit(0); 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()) { for (auto it : DataChannelMgr::getChannels()) {
if (it->getAlias().empty()) { if (it->getAlias().empty()) {
receipt["data"][it->getChannelName()] = it->getChannelInfo(); receipt["data"][it->getChannelName()] = it->getChannelInfo();
@ -86,6 +86,9 @@ void ExtAPIService::initialize() {
receipt["data"][it->getAlias()] = it->getChannelInfo(); receipt["data"][it->getAlias()] = it->getChannelInfo();
} }
} }
} else if (cmd == "getVersion") {
receipt["status"] = 0;
receipt["data"]["version"] = VERSION;
} }
} }

Loading…
Cancel
Save