26#define DEBUG_PRINT(x, ...)
34LinuxUartDriver ::LinuxUartDriver(
const char*
const compName)
35 : LinuxUartDriverComponentBase(compName), m_fd(-1), m_allocationSize(-1), m_device(
"NOT_EXIST"), m_quitReadThread(false) {
39 LinuxUartDriverComponentBase::init(instance);
50 this->m_allocationSize = allocationSize;
52 this->m_device = device;
62 fd =
::open(device, O_RDWR | O_NOCTTY);
65 DEBUG_PRINT(
"open UART device %s failed.\n", device);
68 this->log_WARNING_HI_OpenError(_arg, this->m_fd, _err);
71 DEBUG_PRINT(
"Successfully opened UART device %s fd %d\n", device, fd);
79 stat = tcgetattr(fd, &cfg);
81 DEBUG_PRINT(
"tcgetattr failed: (%d): %s\n", stat, strerror(errno));
85 this->log_WARNING_HI_OpenError(_arg, fd, _err);
107 cfg.c_cc[VTIME] = 10;
109 stat = tcsetattr(fd, TCSANOW, &cfg);
111 DEBUG_PRINT(
"tcsetattr failed: (%d): %s\n", stat, strerror(errno));
115 this->log_WARNING_HI_OpenError(_arg, fd, _err);
125 int stat = tcgetattr(fd, &t);
131 this->log_WARNING_HI_OpenError(_arg, fd, _err);
136 t.c_cflag |= CRTSCTS;
138 stat = tcsetattr(fd, TCSANOW, &t);
144 this->log_WARNING_HI_OpenError(_arg, fd, _err);
169#if defined TGT_OS_TYPE_LINUX
182 struct termios newtio;
184 stat = tcgetattr(fd, &newtio);
190 this->log_WARNING_HI_OpenError(_arg, fd, _err);
209 newtio.c_cflag |= CS8 | CLOCAL | CREAD;
213 newtio.c_cflag |= (PARENB | PARODD);
216 newtio.c_cflag |= PARENB;
219 newtio.c_cflag &= ~PARENB;
227 stat = cfsetispeed(&newtio, relayRate);
233 this->log_WARNING_HI_OpenError(_arg, fd, _err);
236 stat = cfsetospeed(&newtio, relayRate);
242 this->log_WARNING_HI_OpenError(_arg, fd, _err);
252 newtio.c_iflag = INPCK;
255 (void)tcflush(fd, TCIFLUSH);
258 stat = tcsetattr(fd, TCSANOW, &newtio);
264 this->log_WARNING_HI_OpenError(_arg, fd, _err);
270 this->log_ACTIVITY_HI_PortOpened(_arg);
271 if (this->isConnected_ready_OutputPort(0)) {
277LinuxUartDriver ::~LinuxUartDriver() {
278 if (this->m_fd != -1) {
279 DEBUG_PRINT(
"Closing UART device %d\n", this->m_fd);
281 (void)close(this->m_fd);
290 Drv::SendStatus status = Drv::SendStatus::SEND_OK;
291 if (this->m_fd == -1 || serBuffer.
getData() ==
nullptr || serBuffer.
getSize() == 0) {
292 status = Drv::SendStatus::SEND_ERROR;
294 unsigned char *data = serBuffer.
getData();
299 if (-1 == stat || stat != xferSize) {
301 this->log_WARNING_HI_WriteError(_arg, stat);
302 status = Drv::SendStatus::SEND_ERROR;
306 if (isConnected_deallocate_OutputPort(0)) {
307 deallocate_out(0, serBuffer);
312void LinuxUartDriver ::serialReadTaskEntry(
void* ptr) {
314 Drv::RecvStatus status = RecvStatus::RECV_ERROR;
315 LinuxUartDriver* comp =
reinterpret_cast<LinuxUartDriver*
>(ptr);
316 while (!comp->m_quitReadThread) {
317 Fw::Buffer buff = comp->allocate_out(0, comp->m_allocationSize);
320 if (buff.
getData() ==
nullptr) {
322 comp->log_WARNING_HI_NoBuffers(_arg);
323 status = RecvStatus::RECV_ERROR;
324 comp->recv_out(0, buff, status);
338 while ((stat == 0) && !comp->m_quitReadThread) {
348 comp->log_WARNING_HI_ReadError(_arg, stat);
349 status = RecvStatus::RECV_ERROR;
350 }
else if (stat > 0) {
352 status = RecvStatus::RECV_OK;
354 status = RecvStatus::RECV_ERROR;
356 comp->recv_out(0, buff, status);
365 this->m_readTask.start(task, serialReadTaskEntry,
this, priority, stackSize, cpuAffinity);
369void LinuxUartDriver ::quitReadThread() {
370 this->m_quitReadThread =
true;
374 return m_readTask.join(value_ptr);
PlatformIntType NATIVE_INT_TYPE
PlatformUIntType NATIVE_UINT_TYPE
C++ header for working with basic fprime types.
#define DEBUG_PRINT(x,...)
bool open(const char *const device, UartBaudRate baud, UartFlowControl fc, UartParity parity, NATIVE_INT_TYPE allocationSize)
UartBaudRate
Configure UART parameters.
static TaskStatus delay(NATIVE_UINT_TYPE msecs)
delay the task
@ TASK_OK
message sent/received okay