From: Segwon Date: Wed, 22 Nov 2017 02:42:58 +0000 (+0900) Subject: interface: change ERROR_CHECK macro to IF_ERROR_RETURN X-Git-Tag: submit/tizen_4.0/20171220.125806^2~13 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cfcca8d88f0e8a63bfaa9bd79268341741ec8f2c;p=platform%2Fcore%2Fsystem%2Fperipheral-bus.git interface: change ERROR_CHECK macro to IF_ERROR_RETURN - input function parameter and run function before return Change-Id: I119be474d5b96b5b40aa40956b76092c80667fd0 Signed-off-by: Segwon --- diff --git a/include/interface/peripheral_interface_common.h b/include/interface/peripheral_interface_common.h index d411ae7..1b032ac 100644 --- a/include/interface/peripheral_interface_common.h +++ b/include/interface/peripheral_interface_common.h @@ -28,13 +28,15 @@ #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; \ } \ diff --git a/src/interface/peripheral_interface_gpio.c b/src/interface/peripheral_interface_gpio.c index 3d7da22..f7cd764 100644 --- a/src/interface/peripheral_interface_gpio.c +++ b/src/interface/peripheral_interface_gpio.c @@ -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; } diff --git a/src/interface/peripheral_interface_i2c.c b/src/interface/peripheral_interface_i2c.c index 79fa339..a66b6a8 100644 --- a/src/interface/peripheral_interface_i2c.c +++ b/src/interface/peripheral_interface_i2c.c @@ -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 diff --git a/src/interface/peripheral_interface_pwm.c b/src/interface/peripheral_interface_pwm.c index b66c4d8..4a155d8 100644 --- a/src/interface/peripheral_interface_pwm.c +++ b/src/interface/peripheral_interface_pwm.c @@ -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; } diff --git a/src/interface/peripheral_interface_spi.c b/src/interface/peripheral_interface_spi.c index 4fb0707..0f86cf6 100644 --- a/src/interface/peripheral_interface_spi.c +++ b/src/interface/peripheral_interface_spi.c @@ -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; } diff --git a/src/interface/peripheral_interface_uart.c b/src/interface/peripheral_interface_uart.c index c28cc10..a04559b 100644 --- a/src/interface/peripheral_interface_uart.c +++ b/src/interface/peripheral_interface_uart.c @@ -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