Private
Public Access
2
0

uart.c: rework mraa_uart_init_raw to use common cleanup block

This will align it with spi.c and make it easier to introduce
a replace function.

Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Alex Tereschenko
2016-10-16 16:47:44 +02:00
committed by Brendan Le Foll
parent ccc4185eff
commit 1b3a9371a1

View File

@@ -201,23 +201,27 @@ mraa_uart_init(int index)
mraa_uart_context mraa_uart_context
mraa_uart_init_raw(const char* path) mraa_uart_init_raw(const char* path)
{ {
mraa_result_t status = MRAA_SUCCESS;
if (!path) { if (!path) {
syslog(LOG_ERR, "uart: device path undefined"); syslog(LOG_ERR, "uart: device path undefined");
return NULL; status = MRAA_ERROR_INVALID_PARAMETER;
goto init_raw_cleanup;
} }
mraa_uart_context dev = mraa_uart_init_internal(plat == NULL ? NULL : plat->adv_func); mraa_uart_context dev = mraa_uart_init_internal(plat == NULL ? NULL : plat->adv_func);
if (dev == NULL) { if (dev == NULL) {
syslog(LOG_ERR, "uart: Failed to allocate memory for context"); syslog(LOG_ERR, "uart: Failed to allocate memory for context");
return NULL; status = MRAA_ERROR_NO_RESOURCES;
goto init_raw_cleanup;
} }
dev->path = path; dev->path = path;
// now open the device // now open the device
if ((dev->fd = open(dev->path, O_RDWR)) == -1) { if ((dev->fd = open(dev->path, O_RDWR)) == -1) {
syslog(LOG_ERR, "uart: open(%s) failed: %s", path, strerror(errno)); syslog(LOG_ERR, "uart: open(%s) failed: %s", path, strerror(errno));
free(dev); status = MRAA_ERROR_INVALID_RESOURCE;
return NULL; goto init_raw_cleanup;
} }
// now setup the tty and the selected baud rate // now setup the tty and the selected baud rate
@@ -226,9 +230,8 @@ mraa_uart_init_raw(const char* path)
// get current modes // get current modes
if (tcgetattr(dev->fd, &termio)) { if (tcgetattr(dev->fd, &termio)) {
syslog(LOG_ERR, "uart: tcgetattr(%s) failed: %s", path, strerror(errno)); syslog(LOG_ERR, "uart: tcgetattr(%s) failed: %s", path, strerror(errno));
close(dev->fd); status = MRAA_ERROR_INVALID_RESOURCE;
free(dev); goto init_raw_cleanup;
return NULL;
} }
// setup for a 'raw' mode. 8N1, no echo or special character // setup for a 'raw' mode. 8N1, no echo or special character
@@ -237,14 +240,23 @@ mraa_uart_init_raw(const char* path)
cfmakeraw(&termio); cfmakeraw(&termio);
if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) { if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
syslog(LOG_ERR, "uart: tcsetattr(%s) failed after cfmakeraw(): %s", path, strerror(errno)); syslog(LOG_ERR, "uart: tcsetattr(%s) failed after cfmakeraw(): %s", path, strerror(errno));
close(dev->fd); status = MRAA_ERROR_INVALID_RESOURCE;
free(dev); goto init_raw_cleanup;
return NULL;
} }
if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) { if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) {
close(dev->fd); status = MRAA_ERROR_INVALID_RESOURCE;
free(dev); goto init_raw_cleanup;
}
init_raw_cleanup:
if (status != MRAA_SUCCESS) {
if (dev != NULL) {
if (dev->fd != -1) {
close(dev->fd);
}
free(dev);
}
return NULL; return NULL;
} }