interface: use the CHECK_ERROR macro for error set by system call 22/160022/3
authorSegwon <segwon.han@samsung.com>
Tue, 14 Nov 2017 06:55:58 +0000 (15:55 +0900)
committerSegwon Han <segwon.han@samsung.com>
Tue, 14 Nov 2017 08:30:32 +0000 (08:30 +0000)
 - created a peripheral_interface_common.h file and implemented error macro
 - macro name : CHECK_ERROR

Signed-off-by: Segwon <segwon.han@samsung.com>
Change-Id: I5d1167ab2d7aa3e5e3b22ef2192efbd68b1f17bc

include/interface/peripheral_interface_common.h [new file with mode: 0644]
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

diff --git a/include/interface/peripheral_interface_common.h b/include/interface/peripheral_interface_common.h
new file mode 100644 (file)
index 0000000..33a8d39
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __PERIPHERAL_INTERFACE_COMMON_H__
+#define __PERIPHERAL_INTERFACE_COMMON_H__
+
+#define CHECK_ERROR(val) \
+       do { \
+               if (val < 0) { \
+                       if (errno == EAGAIN) \
+                               return -EAGAIN; \
+                       char errmsg[255]; \
+                       strerror_r(errno, errmsg, sizeof(errmsg)); \
+                       _E("Failed the %s(%d) function. errmsg: %s", __FUNCTION__, __LINE__, errmsg); \
+                       return -EIO; \
+               } \
+       } while (0)
+
+#endif /*__PERIPHERAL_INTERFACE_COMMON_H__*/
\ No newline at end of file
index e2ce235..7d4ef2f 100644 (file)
@@ -22,6 +22,7 @@
 #include <fcntl.h>
 #include <poll.h>
 
+#include "peripheral_interface_common.h"
 #include "peripheral_interface_gpio.h"
 #include "peripheral_common.h"
 #include "peripheral_internal.h"
@@ -43,10 +44,7 @@ int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_g
                return -EIO;
        }
 
-       if (status <= 0) {
-               _E("Error: gpio direction set error\n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -68,10 +66,7 @@ int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_g
                return -EIO;
        }
 
-       if (status <= 0) {
-               _E("Error: gpio edge set error\n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -89,10 +84,7 @@ int peripheral_interface_gpio_write(peripheral_gpio_h gpio, uint32_t value)
                return -EIO;
        }
 
-       if (status <= 0) {
-               _E("Error: gpio write error\n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -103,10 +95,7 @@ int peripheral_interface_gpio_read(peripheral_gpio_h gpio, uint32_t *value)
        char gpio_buf[GPIO_BUFFER_MAX] = {0, };
 
        len = read(gpio->fd_value, &gpio_buf, 1);
-       if (len <= 0) {
-               _E("Error: gpio read error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(len);
 
        if (0 == strncmp(gpio_buf, "1", strlen("1")))
                *value = 1;
@@ -125,22 +114,13 @@ int peripheral_interface_gpio_close(peripheral_gpio_h gpio)
        int status;
 
        status = close(gpio->fd_direction);
-       if (status < 0) {
-               _E("Error: gpio direction fd close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        status = close(gpio->fd_edge);
-       if (status < 0) {
-               _E("Error: gpio edge fd close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        status = close(gpio->fd_value);
-       if (status < 0) {
-               _E("Error: gpio value fd close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
index 6b235ff..fadbf63 100644 (file)
@@ -22,6 +22,7 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 
+#include "peripheral_interface_common.h"
 #include "peripheral_interface_i2c.h"
 #include "peripheral_common.h"
 #include "peripheral_internal.h"
@@ -36,12 +37,7 @@ int peripheral_interface_i2c_close(peripheral_i2c_h i2c)
        RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd");
 
        status = close(i2c->fd);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d", i2c->fd);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -53,12 +49,7 @@ int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t
        RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd : %d", i2c->fd);
 
        status = read(i2c->fd, data, length);
-       if (status != length) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("i2c read failed, fd : %d, errmsg : %s", i2c->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -70,12 +61,7 @@ int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t
        RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd : %d", i2c->fd);
 
        status = write(i2c->fd, data, length);
-       if (status != length) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("i2c write failed fd : %d, errmsg : %s", i2c->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -87,12 +73,7 @@ int peripheral_interface_i2c_smbus_ioctl(peripheral_i2c_h i2c, struct i2c_smbus_
        RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd : %d", i2c->fd);
 
        status = ioctl(i2c->fd, I2C_SMBUS, data);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("i2c transaction failed fd : %d, errmsg : %s", i2c->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
index 822332b..faefc1a 100644 (file)
@@ -22,6 +22,7 @@
 #include <unistd.h>
 #include <stdbool.h>
 
+#include "peripheral_interface_common.h"
 #include "peripheral_interface_pwm.h"
 #include "peripheral_common.h"
 #include "peripheral_internal.h"
@@ -37,28 +38,16 @@ int peripheral_interface_pwm_close(peripheral_pwm_h pwm)
        int status;
 
        status = close(pwm->fd_period);
-       if (status < 0) {
-               _E("Error: pwm period fd close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        status = close(pwm->fd_duty_cycle);
-       if (status < 0) {
-               _E("Error: pwm duty cycle fd close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        status = close(pwm->fd_polarity);
-       if (status < 0) {
-               _E("Error: pwm polarity fd close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        status = close(pwm->fd_enable);
-       if (status < 0) {
-               _E("Error: pwm enable fd close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -70,10 +59,7 @@ int peripheral_interface_pwm_set_period(peripheral_pwm_h pwm, uint32_t period)
 
        len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", period);
        status = write(pwm->fd_period, pwm_buf, len);
-       if (status < 0) {
-               _E("Failed to set period");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -85,10 +71,7 @@ int peripheral_interface_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_
 
        len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", duty_cycle);
        status = write(pwm->fd_duty_cycle, pwm_buf, len);
-       if (status < 0) {
-               _E("Failed to set duty cycle");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -106,10 +89,7 @@ int peripheral_interface_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_p
                return -EINVAL;
        }
 
-       if (status <= 0) {
-               _E("Failed to set polarity");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -121,10 +101,7 @@ int peripheral_interface_pwm_set_enable(peripheral_pwm_h pwm, bool enable)
 
        len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", enable);
        status = write(pwm->fd_enable, pwm_buf, len);
-       if (status < 0) {
-               _E("Failed to set enable");
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
index ddc643c..1c83726 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/ioctl.h>
 #include <linux/spi/spidev.h>
 
+#include "peripheral_interface_common.h"
 #include "peripheral_interface_spi.h"
 #include "peripheral_common.h"
 #include "peripheral_internal.h"
@@ -40,12 +41,7 @@ int peripheral_interface_spi_close(peripheral_spi_h spi)
        RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
 
        status = close(spi->fd);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d", spi->fd);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -58,12 +54,7 @@ int peripheral_interface_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_
        RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
 
        status = ioctl(spi->fd, SPI_IOC_WR_MODE, &mode);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to set mode(%d) : %s", mode, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -76,12 +67,7 @@ int peripheral_interface_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_
        RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
 
        status = ioctl(spi->fd, SPI_IOC_WR_LSB_FIRST, &bit_order);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to set lsb first(%d), fd : %d, errmsg : %s", bit_order, spi->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -94,12 +80,7 @@ int peripheral_interface_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bit
        RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
 
        status = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to set bits(%d), fd : %d, errmsg : %s", bits, spi->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -112,12 +93,7 @@ int peripheral_interface_spi_set_frequency(peripheral_spi_h spi, uint32_t freq)
        RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
 
        status = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to set frequency(%d), fd : %d, errmsg : %s", freq, spi->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -134,12 +110,7 @@ int peripheral_interface_spi_read(peripheral_spi_h spi, uint8_t *rxbuf, uint32_t
        xfer.len = length;
 
        status = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to read data, fd : %d, length : %d, errmsg : %s", spi->fd, length, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -156,12 +127,7 @@ int peripheral_interface_spi_write(peripheral_spi_h spi, uint8_t *txbuf, uint32_
        xfer.len = length;
 
        status = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to write data(%d) : %s", length, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -181,12 +147,7 @@ int peripheral_interface_spi_transfer(peripheral_spi_h spi, uint8_t *txbuf, uint
        xfer.len = length;
 
        status = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to exchange data(%d) : %s", length, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
index 4933ab4..a0f9637 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdbool.h>
 #include <sys/ioctl.h>
 
+#include "peripheral_interface_common.h"
 #include "peripheral_interface_uart.h"
 #include "peripheral_common.h"
 #include "peripheral_internal.h"
@@ -65,20 +66,10 @@ int peripheral_interface_uart_close(peripheral_uart_h uart)
        }
 
        status = peripheral_interface_uart_flush(uart);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d, errmsg : %s", uart->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        status = close(uart->fd);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d, errmsg : %s", uart->fd, errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(status);
 
        return 0;
 }
@@ -93,12 +84,7 @@ int peripheral_interface_uart_flush(peripheral_uart_h uart)
        }
 
        ret = tcflush(uart->fd, TCIOFLUSH);
-       if (ret < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcflush failed, errmsg : %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        return 0;
 }
@@ -122,12 +108,8 @@ int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_u
        }
 
        ret = tcgetattr(uart->fd, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcgetattr failed, errmsg : %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
+
        tio.c_cflag = peripheral_uart_br[baud];
        tio.c_iflag = IGNPAR;
        tio.c_oflag = 0;
@@ -137,12 +119,7 @@ int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_u
 
        peripheral_interface_uart_flush(uart);
        ret = tcsetattr(uart->fd, TCSANOW, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcsetattr failed, errmsg: %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        return 0;
 }
@@ -165,12 +142,8 @@ int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_u
        }
 
        ret = tcgetattr(uart->fd, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcgetattr failed, errmsg: %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
+
 
        /* set byte size */
        tio.c_cflag &= ~CSIZE;
@@ -179,12 +152,7 @@ int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_u
 
        peripheral_interface_uart_flush(uart);
        ret = tcsetattr(uart->fd, TCSANOW, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcsetattr failed, errmsg : %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        return 0;
 }
@@ -202,12 +170,7 @@ int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart
        }
 
        ret = tcgetattr(uart->fd, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcgetattr failed, errmsg: %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        /* set parity info */
        switch (parity) {
@@ -228,12 +191,7 @@ int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart
 
        peripheral_interface_uart_flush(uart);
        ret = tcsetattr(uart->fd, TCSANOW, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcsetattr failed, errmsg : %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        return 0;
 }
@@ -251,12 +209,7 @@ int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_u
        }
 
        ret = tcgetattr(uart->fd, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcgetattr failed, errmsg: %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        /* set stop bit */
        switch (stop_bits) {
@@ -273,12 +226,7 @@ int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_u
 
        peripheral_interface_uart_flush(uart);
        ret = tcsetattr(uart->fd, TCSANOW, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcsetattr failed, errmsg : %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        return 0;
 }
@@ -296,12 +244,7 @@ int peripheral_interface_uart_set_flow_control(peripheral_uart_h uart, periphera
        }
 
        ret = tcgetattr(uart->fd, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcgetattr failed, errmsg : %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS)
                tio.c_cflag |= CRTSCTS;
@@ -318,12 +261,7 @@ int peripheral_interface_uart_set_flow_control(peripheral_uart_h uart, periphera
                return -EINVAL;
 
        ret = tcsetattr(uart->fd, TCSANOW, &tio);
-       if (ret) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("tcsetattr failed, errmsg : %s", errmsg);
-               return -1;
-       }
+       CHECK_ERROR(ret);
 
        return 0;
 }
@@ -338,14 +276,7 @@ int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_
        }
 
        ret = read(uart->fd, (void *)buf, length);
-       if (ret <= 0) {
-               if (errno == EAGAIN)
-                       return -EAGAIN;
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("read failed, errmsg : %s", errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(ret);
 
        return ret;
 }
@@ -360,14 +291,7 @@ int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32
        }
 
        ret = write(uart->fd, buf, length);
-       if (ret <= 0) {
-               if (errno == EAGAIN)
-                       return -EAGAIN;
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("write failed, errmsg : %s", errmsg);
-               return -EIO;
-       }
+       CHECK_ERROR(ret);
 
        return ret;
 }