uart: Fix read and write functions 29/239429/3
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Fri, 24 Jul 2020 12:34:03 +0000 (14:34 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Fri, 24 Jul 2020 12:58:24 +0000 (14:58 +0200)
Return number of bytes read/written to fulfill declaration
given in docs.

Change-Id: I81e229a1b719c9370ed97d38316fbef8e712bba5

src/interface/peripheral_interface_uart.c

index 45e0690..09c4978 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include <termios.h>
+#include <limits.h>
 
 #include "peripheral_interface_uart.h"
 
@@ -178,16 +179,18 @@ int peripheral_interface_uart_set_flow_control(peripheral_uart_h uart, periphera
 
 int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
 {
-       int ret = read(uart->fd, (void *)buf, length);
-       CHECK_ERROR(ret != length);
+       /* the return type has to allow negative values (for error codes), so we can't use the upper half of unsigned values */
+       int ret = read(uart->fd, (void *)buf, length > INT_MAX ? INT_MAX : length);
+       CHECK_ERROR(ret < 0);
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
 int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
 {
-       int ret = write(uart->fd, buf, length);
-       CHECK_ERROR(ret != length);
+       /* the return type has to allow negative values (for error codes), so we can't use the upper half of unsigned values */
+       int ret = write(uart->fd, buf, length > INT_MAX ? INT_MAX : length);
+       CHECK_ERROR(ret < 0);
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }