uart: add uart device read and write functionality
Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Thomas Ingleby
parent
6d269eeabd
commit
cf14939cca
@@ -93,6 +93,27 @@ mraa_result_t mraa_uart_open_dev(mraa_uart_context dev, speed_t baud);
|
|||||||
*/
|
*/
|
||||||
mraa_result_t mraa_uart_close_dev(mraa_uart_context dev);
|
mraa_result_t mraa_uart_close_dev(mraa_uart_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read bytes from the device into a buffer
|
||||||
|
*
|
||||||
|
* @param dev uart context
|
||||||
|
* @param buf buffer pointer
|
||||||
|
* @param len maximum size of buffer
|
||||||
|
* @return the number of bytes read, or -1 if an error occurred
|
||||||
|
*/
|
||||||
|
int mraa_uart_read(mraa_uart_context dev, char *buf, size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write bytes in buffer to a device
|
||||||
|
*
|
||||||
|
* @param dev uart context
|
||||||
|
* @param buf buffer pointer
|
||||||
|
* @param len maximum size of buffer
|
||||||
|
* @return the number of bytes written, or -1 if an error occurred
|
||||||
|
*/
|
||||||
|
int mraa_uart_write(mraa_uart_context dev, char *buf, size_t len);
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -102,6 +102,32 @@ class Uart
|
|||||||
return mraa_uart_close_dev(m_uart);
|
return mraa_uart_close_dev(m_uart);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read bytes from the device into a buffer
|
||||||
|
*
|
||||||
|
* @param buf buffer pointer
|
||||||
|
* @param len maximum size of buffer
|
||||||
|
* @return the number of bytes read, or -1 if an error occurred
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
read(char *buf, size_t len)
|
||||||
|
{
|
||||||
|
return mraa_uart_read(m_uart, buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write bytes in buffer to a device
|
||||||
|
*
|
||||||
|
* @param buf buffer pointer
|
||||||
|
* @param len maximum size of buffer
|
||||||
|
* @return the number of bytes written, or -1 if an error occurred
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
write(char *buf, size_t len)
|
||||||
|
{
|
||||||
|
return mraa_uart_write(m_uart, buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mraa_uart_context m_uart;
|
mraa_uart_context m_uart;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -172,3 +172,35 @@ mraa_uart_close_dev(mraa_uart_context dev)
|
|||||||
dev->fd = -1;
|
dev->fd = -1;
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_uart_read(mraa_uart_context dev, char *buf, size_t len)
|
||||||
|
{
|
||||||
|
if (!dev) {
|
||||||
|
syslog(LOG_ERR, "uart: read: context is NULL");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dev->fd < 0) {
|
||||||
|
syslog(LOG_ERR, "uart: port is not open");
|
||||||
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return read(dev->fd, buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_uart_write(mraa_uart_context dev, char *buf, size_t len)
|
||||||
|
{
|
||||||
|
if (!dev) {
|
||||||
|
syslog(LOG_ERR, "uart: write: context is NULL");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dev->fd < 0) {
|
||||||
|
syslog(LOG_ERR, "uart: port is not open");
|
||||||
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return write(dev->fd, buf, len);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user