interface: change ERROR_CHECK macro to IF_ERROR_RETURN 59/161159/6
authorSegwon <segwon.han@samsung.com>
Wed, 22 Nov 2017 02:42:58 +0000 (11:42 +0900)
committerSegwon <segwon.han@samsung.com>
Wed, 22 Nov 2017 10:09:18 +0000 (19:09 +0900)
 - input function parameter and run function before return

Change-Id: I119be474d5b96b5b40aa40956b76092c80667fd0
Signed-off-by: Segwon <segwon.han@samsung.com>
include/interface/peripheral_interface_common.h
src/interface/peripheral_interface_gpio.c
src/interface/peripheral_interface_i2c.c
src/interface/peripheral_interface_pwm.c
src/interface/peripheral_interface_spi.c
src/interface/peripheral_interface_uart.c

index d411ae703f54e0b1ebe88a3bc5f13515bc8f6715..1b032ace940216caea1ae2a30786b14fd38de703 100644 (file)
 #define MAX_ERR_LEN 255
 #define MAX_BUF_LEN 64
 
-#define CHECK_ERROR(expr) \
+#define IF_ERROR_RETURN(expr, func...) \
        do { \
                if (expr) { \
-                       if (errno == EAGAIN) \
+                       int temp = errno; \
+                       func; \
+                       if (temp == EAGAIN) \
                                return PERIPHERAL_ERROR_TRY_AGAIN; \
                        char errmsg[MAX_ERR_LEN]; \
-                       strerror_r(errno, errmsg, sizeof(errmsg)); \
+                       strerror_r(temp, errmsg, sizeof(errmsg)); \
                        _E("Failed the %s(%d) function. errmsg: %s", __FUNCTION__, __LINE__, errmsg); \
                        return PERIPHERAL_ERROR_IO_ERROR; \
                } \
index 3d7da223153f13ae9b3ce73189f707472011f4e2..f7cd764dbe37b2b8f9258c77c9fbf7d7a3dd1526 100644 (file)
@@ -27,12 +27,12 @@ int gpio_open(int gpiopin)
        _D("gpiopin : %d", gpiopin);
 
        fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
-       CHECK_ERROR(fd < 0);
+       IF_ERROR_RETURN(fd < 0);
 
        len = snprintf(gpio_export, MAX_BUF_LEN, "%d", gpiopin);
        status = write(fd, gpio_export, len);
+       IF_ERROR_RETURN(status != len, close(fd));
        close(fd);
-       CHECK_ERROR(status != len);
 
        return 0;
 }
@@ -45,12 +45,12 @@ int gpio_close(int gpiopin)
        _D("gpiopin : %d", gpiopin);
 
        fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
-       CHECK_ERROR(fd < 0);
+       IF_ERROR_RETURN(fd < 0);
 
        len = snprintf(gpio_unexport, MAX_BUF_LEN, "%d", gpiopin);
        status = write(fd, gpio_unexport, len);
+       IF_ERROR_RETURN(status != len, close(fd));
        close(fd);
-       CHECK_ERROR(status != len);
 
        return 0;
 }
index 79fa339d195e01ba39c4ff47086b111b8815c362..a66b6a841f158b613718089be8e8ba1a60070ebc 100644 (file)
@@ -31,7 +31,7 @@ int i2c_open(int bus, int *fd)
 
        snprintf(i2c_dev, MAX_BUF_LEN, SYSFS_I2C_DIR"-%d", bus);
        new_fd = open(i2c_dev, O_RDWR);
-       CHECK_ERROR(new_fd < 0);
+       IF_ERROR_RETURN(new_fd < 0);
 
        _D("fd : %d", new_fd);
        *fd = new_fd;
@@ -47,7 +47,7 @@ int i2c_close(int fd)
        RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
 
        status = close(fd);
-       CHECK_ERROR(status != 0);
+       IF_ERROR_RETURN(status != 0);
 
        return 0;
 }
@@ -60,7 +60,8 @@ int i2c_set_address(int fd, int address)
        RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
 
        status = ioctl(fd, I2C_SLAVE, address);
-       CHECK_ERROR(status != 0);
+       IF_ERROR_RETURN(status != 0, close(fd));
+       close(fd);
 
        return 0;
 }
\ No newline at end of file
index b66c4d8655fbde26125695dcc79b99b77237429e..4a155d8b8242cb3bb914983a475a4d07c249af98 100644 (file)
@@ -29,12 +29,12 @@ int pwm_open(int chip, int pin)
 
        snprintf(pwm_dev, MAX_BUF_LEN, SYSFS_PWM_PATH "/pwmchip%d/export", chip);
        fd = open(pwm_dev, O_WRONLY);
-       CHECK_ERROR(fd < 0);
+       IF_ERROR_RETURN(fd < 0);
 
        len = snprintf(pwm_buf, MAX_BUF_LEN, "%d", pin);
        status = write(fd, pwm_buf, len);
+       IF_ERROR_RETURN(status != len, close(fd));
        close(fd);
-       CHECK_ERROR(status != len);
 
        return 0;
 }
@@ -49,12 +49,12 @@ int pwm_close(int chip, int pin)
 
        snprintf(pwm_dev, MAX_BUF_LEN, SYSFS_PWM_PATH "/pwmchip%d/unexport", chip);
        fd = open(pwm_dev, O_WRONLY);
-       CHECK_ERROR(fd < 0);
+       IF_ERROR_RETURN(fd < 0);
 
        len = snprintf(pwm_buf, MAX_BUF_LEN, "%d", pin);
        status = write(fd, pwm_buf, len);
+       IF_ERROR_RETURN(status != len, close(fd));
        close(fd);
-       CHECK_ERROR(status != len);
 
        return 0;
 }
index 4fb0707bc4db1ef5af249e3b807323045b160632..0f86cf619d7ad3ad42bb6f871d7a3bd6d4ea83a4 100644 (file)
@@ -29,7 +29,7 @@ int spi_open(int bus, int cs, int *fd)
        snprintf(spi_dev, MAX_BUF_LEN, SYSFS_SPI_DIR"%d.%d", bus, cs);
 
        new_fd = open(spi_dev, O_RDWR);
-       CHECK_ERROR(new_fd < 0);
+       IF_ERROR_RETURN(new_fd < 0);
 
        _D("fd : %d", new_fd);
        *fd = new_fd;
@@ -45,7 +45,7 @@ int spi_close(int fd)
        RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
 
        status = close(fd);
-       CHECK_ERROR(status != 0);
+       IF_ERROR_RETURN(status != 0);
 
        return 0;
 }
index c28cc1055eb9e2a7429dc6ad27e1c3ab814e7e90..a04559bfb8ca9ca8d8c9e12e0a462f4e6a82b85f 100644 (file)
@@ -44,7 +44,7 @@ int uart_open(int port, int *file_hndl)
 
        _D("uart_dev : %s", uart_dev);
        fd = open(uart_dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
-       CHECK_ERROR(fd < 0);
+       IF_ERROR_RETURN(fd < 0);
 
        *file_hndl = fd;
        return 0;
@@ -61,11 +61,8 @@ int uart_close(int file_hndl)
                return -EINVAL;
        }
 
-       status = tcflush(file_hndl, TCIOFLUSH);
-       CHECK_ERROR(status != 0);
-
        status = close(file_hndl);
-       CHECK_ERROR(status != 0);
+       IF_ERROR_RETURN(status != 0);
 
        return 0;
 }
\ No newline at end of file