Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_DDS/AP_DDS_Serial.cpp
Views: 1798
#include "AP_DDS_Client.h"12#include <AP_SerialManager/AP_SerialManager.h>34#include <errno.h>56/*7open connection on a serial port8*/9bool AP_DDS_Client::serial_transport_open(uxrCustomTransport *t)10{11AP_DDS_Client *dds = (AP_DDS_Client *)t->args;12AP_SerialManager *serial_manager = AP_SerialManager::get_singleton();13auto *dds_port = serial_manager->find_serial(AP_SerialManager::SerialProtocol_DDS_XRCE, 0);14if (dds_port == nullptr) {15return false;16}17// ensure we own the UART18dds_port->begin(0);19dds->serial.port = dds_port;20return true;21}2223/*24close serial transport25*/26bool AP_DDS_Client::serial_transport_close(uxrCustomTransport *t)27{28// we don't actually close the UART29return true;30}3132/*33write on serial transport34*/35size_t AP_DDS_Client::serial_transport_write(uxrCustomTransport *t, const uint8_t* buf, size_t len, uint8_t* error)36{37AP_DDS_Client *dds = (AP_DDS_Client *)t->args;38if (dds->serial.port == nullptr) {39*error = EINVAL;40return 0;41}42ssize_t bytes_written = dds->serial.port->write(buf, len);43if (bytes_written <= 0) {44*error = 1;45return 0;46}47//! @todo populate the error code correctly48*error = 0;49return bytes_written;50}5152/*53read from a serial transport54*/55size_t AP_DDS_Client::serial_transport_read(uxrCustomTransport *t, uint8_t* buf, size_t len, int timeout_ms, uint8_t* error)56{57AP_DDS_Client *dds = (AP_DDS_Client *)t->args;58if (dds->serial.port == nullptr) {59*error = EINVAL;60return 0;61}62const uint32_t tstart = AP_HAL::millis();63while (AP_HAL::millis() - tstart < uint32_t(timeout_ms) &&64dds->serial.port->available() < len) {65hal.scheduler->delay_microseconds(100); // TODO select or poll this is limiting speed (100us)66}67ssize_t bytes_read = dds->serial.port->read(buf, len);68if (bytes_read <= 0) {69*error = 1;70return 0;71}72//! @todo Add error reporting73*error = 0;74return bytes_read;75}7677/*78initialise serial connection79*/80bool AP_DDS_Client::ddsSerialInit()81{82// setup a framed transport for serial83uxr_set_custom_transport_callbacks(&serial.transport, true,84serial_transport_open,85serial_transport_close,86serial_transport_write,87serial_transport_read);8889if (!uxr_init_custom_transport(&serial.transport, (void*)this)) {90return false;91}92comm = &serial.transport.comm;93return true;94}959697