interface: create file the "peripheral_inteface_common.h" 56/160956/2
authorSegwon <segwon.han@samsung.com>
Tue, 21 Nov 2017 01:37:37 +0000 (10:37 +0900)
committerSegwon <segwon.han@samsung.com>
Tue, 21 Nov 2017 01:43:18 +0000 (10:43 +0900)
 - change the errno check to macro
 - move common included header files to peripheral_interface_common.h
 - move common constants to peripheral_interface_common.h

Change-Id: I1a4b6fea351a045b829fb122bcec9d80f1e3ac7d
Signed-off-by: Segwon <segwon.han@samsung.com>
src/interface/include/peripheral_interface_common.h [new file with mode: 0644]
src/interface/include/peripheral_interface_gpio.h
src/interface/include/peripheral_interface_i2c.h
src/interface/include/peripheral_interface_uart.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

diff --git a/src/interface/include/peripheral_interface_common.h b/src/interface/include/peripheral_interface_common.h
new file mode 100644 (file)
index 0000000..e51f611
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <peripheral_io.h>
+
+#include "peripheral_common.h"
+
+#define MAX_ERR_LEN 255
+#define MAX_BUF_LEN 64
+
+#define CHECK_ERROR(expr) \
+       do { \
+               if (expr) { \
+                       if (errno == EAGAIN) \
+                               return PERIPHERAL_ERROR_TRY_AGAIN; \
+                       char errmsg[MAX_ERR_LEN]; \
+                       strerror_r(errno, errmsg, sizeof(errmsg)); \
+                       _E("Failed the %s(%d) function. errmsg: %s", __FUNCTION__, __LINE__, errmsg); \
+                       return PERIPHERAL_ERROR_IO_ERROR; \
+               } \
+       } while (0)
+
+#endif /*__PERIPHERAL_INTERFACE_COMMON_H__*/
\ No newline at end of file
index 483968d..4dfab84 100644 (file)
@@ -17,9 +17,6 @@
 #ifndef __PERIPHERAL_INTERFACE_GPIO_H__
 #define __PERIPHERAL_INTERFACE_GPIO_H__
 
-#define SYSFS_GPIO_DIR "/sys/class/gpio"
-#define GPIO_BUFFER_MAX 64
-
 int gpio_open(int gpiopin);
 int gpio_close(int gpiopin);
 
index fb1df3d..9aae2ff 100644 (file)
 #ifndef __PERIPHERAL_INTERFACE_I2C_H__
 #define __PERIPHERAL_INTERFACE_I2C_H__
 
-#include <stdint.h>
-
-#define SYSFS_I2C_DIR "/dev/i2c"
-#define I2C_BUFFER_MAX 64
-
-#define I2C_SLAVE      0x0703  /* Use this slave address */
-#define I2C_SMBUS      0x0720  /* SMBus transfer */
-
 int i2c_open(int bus, int *fd);
 int i2c_close(int fd);
 int i2c_set_address(int fd, int address);
index 9c61401..0cecfb2 100644 (file)
@@ -17,8 +17,6 @@
 #ifndef __PERIPHERAL_INTERFACE_UART_H__
 #define __PERIPHERAL_INTERFACE_UART_H__
 
-#include <stdint.h>
-
 /**
 * @brief uart_open() initializes uart port.
 *
@@ -36,13 +34,5 @@ int uart_open(int port, int *file_hndl);
 */
 int uart_close(int file_hndl);
 
-/**
-* @brief uart_flush() flushes uart buffer.
-*
-* @param[in] file_hndl handle of uart_context
-* @return On success, 0 is returned. On failure, a negative value is returned.
-*/
-int uart_flush(int file_hndl);
-
 #endif /* __PERIPHERAL_INTERFACE_UART_H__ */
 
index 168f67e..245b0ff 100644 (file)
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <poll.h>
-
 #include "peripheral_interface_gpio.h"
-#include "peripheral_common.h"
+#include "peripheral_interface_common.h"
 
-#define MAX_ERR_LEN 255
+#define SYSFS_GPIO_DIR "/sys/class/gpio"
 
 int gpio_open(int gpiopin)
 {
        int fd, len, status;
-       char gpio_export[GPIO_BUFFER_MAX] = {0, };
+       char gpio_export[MAX_BUF_LEN] = {0, };
 
        _D("gpiopin : %d", gpiopin);
 
        fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
-       if (fd < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Can't Open /sys/class/gpio/export :%s\n", errmsg);
-               return -ENXIO;
-       }
+       CHECK_ERROR(fd < 0);
 
-       len = snprintf(gpio_export, GPIO_BUFFER_MAX, "%d", gpiopin);
+       len = snprintf(gpio_export, MAX_BUF_LEN, "%d", gpiopin);
        status = write(fd, gpio_export, len);
-
-       if (status != len) {
-               close(fd);
-               _E("Error: gpio open error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status != len);
 
        close(fd);
 
@@ -59,26 +41,16 @@ int gpio_open(int gpiopin)
 int gpio_close(int gpiopin)
 {
        int fd, len, status;
-       char gpio_unexport[GPIO_BUFFER_MAX] = {0, };
+       char gpio_unexport[MAX_BUF_LEN] = {0, };
 
        _D("gpiopin : %d", gpiopin);
 
        fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
-       if (fd < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Can't Open /sys/class/gpio/unexport %s\n", errmsg);
-               return -ENXIO;
-       }
+       CHECK_ERROR(fd < 0);
 
-       len = snprintf(gpio_unexport, GPIO_BUFFER_MAX, "%d", gpiopin);
+       len = snprintf(gpio_unexport, MAX_BUF_LEN, "%d", gpiopin);
        status = write(fd, gpio_unexport, len);
-
-       if (status != len) {
-               close(fd);
-               _E("Error: gpio close error \n");
-               return -EIO;
-       }
+       CHECK_ERROR(status != len);
 
        close(fd);
 
index 933e009..79fa339 100644 (file)
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
 #include <sys/ioctl.h>
 
 #include "peripheral_interface_i2c.h"
-#include "peripheral_common.h"
+#include "peripheral_interface_common.h"
 
-#define MAX_ERR_LEN 255
+#define SYSFS_I2C_DIR "/dev/i2c"
+#define I2C_SLAVE      0x0703  /* Use this slave address */
 
 int i2c_open(int bus, int *fd)
 {
        int new_fd;
-       char i2c_dev[I2C_BUFFER_MAX] = {0,};
+       char i2c_dev[MAX_BUF_LEN] = {0,};
 
        _D("bus : %d", bus);
 
-       snprintf(i2c_dev, sizeof(i2c_dev)-1, SYSFS_I2C_DIR"-%d", bus);
+       snprintf(i2c_dev, MAX_BUF_LEN, SYSFS_I2C_DIR"-%d", bus);
        new_fd = open(i2c_dev, O_RDWR);
-       if (new_fd < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Can't Open %s : %s", i2c_dev, errmsg);
-               return -ENXIO;
-       }
+       CHECK_ERROR(new_fd < 0);
+
        _D("fd : %d", new_fd);
        *fd = new_fd;
 
@@ -56,12 +47,7 @@ int i2c_close(int fd)
        RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
 
        status = close(fd);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d", fd);
-               return -EIO;
-       }
+       CHECK_ERROR(status != 0);
 
        return 0;
 }
@@ -74,12 +60,7 @@ int i2c_set_address(int fd, int address)
        RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
 
        status = ioctl(fd, I2C_SLAVE, address);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to set slave address(%x), fd : %d, errmsg : %s", address, fd, errmsg);
-               return status;
-       }
+       CHECK_ERROR(status != 0);
 
        return 0;
 }
\ No newline at end of file
index 77f7fac..5eaab96 100644 (file)
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdbool.h>
-
 #include "peripheral_interface_pwm.h"
-#include "peripheral_common.h"
+#include "peripheral_interface_common.h"
 
 #define SYSFS_PWM_PATH "/sys/class/pwm"
 
-#define PATH_BUF_MAX   64
-#define PWM_BUF_MAX    16
-#define MAX_ERR_LEN    255
-
 int pwm_open(int chip, int pin)
 {
        int fd, len, status;
-       char pwm_dev[PATH_BUF_MAX] = {0};
-       char pwm_buf[PWM_BUF_MAX] = {0};
+       char pwm_dev[MAX_BUF_LEN] = {0};
+       char pwm_buf[MAX_BUF_LEN] = {0};
 
        _D("chip : %d, pin : %d", chip, pin);
 
-       snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/export", chip);
+       snprintf(pwm_dev, MAX_BUF_LEN, SYSFS_PWM_PATH "/pwmchip%d/export", chip);
        fd = open(pwm_dev, O_WRONLY);
-       if (fd < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
-               return -ENXIO;
-       }
+       CHECK_ERROR(fd < 0);
 
-       len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", pin);
+       len = snprintf(pwm_buf, MAX_BUF_LEN, "%d", pin);
        status = write(fd, pwm_buf, len);
-       if (status < 0) {
-               _E("Failed to export pwmchip%d, pwm%d", chip, pin);
-               close(fd);
-               return -EIO;
-       }
+       CHECK_ERROR(status != len);
+
        close(fd);
 
        return 0;
@@ -63,27 +43,19 @@ int pwm_open(int chip, int pin)
 int pwm_close(int chip, int pin)
 {
        int fd, len, status;
-       char pwm_dev[PATH_BUF_MAX] = {0};
-       char pwm_buf[PWM_BUF_MAX] = {0};
+       char pwm_dev[MAX_BUF_LEN] = {0};
+       char pwm_buf[MAX_BUF_LEN] = {0};
 
        _D("chip : %d, pin : %d", chip, pin);
 
-       snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/unexport", chip);
+       snprintf(pwm_dev, MAX_BUF_LEN, SYSFS_PWM_PATH "/pwmchip%d/unexport", chip);
        fd = open(pwm_dev, O_WRONLY);
-       if (fd < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
-               return -ENXIO;
-       }
+       CHECK_ERROR(fd < 0);
 
-       len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", pin);
+       len = snprintf(pwm_buf, MAX_BUF_LEN, "%d", pin);
        status = write(fd, pwm_buf, len);
-       if (status < 0) {
-               _E("Failed to unexport pwmchip%d, pwm%", chip, pin);
-               close(fd);
-               return -EIO;
-       }
+       CHECK_ERROR(status != len);
+
        close(fd);
 
        return 0;
index 8e9a255..4fb0707 100644 (file)
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <linux/spi/spidev.h>
-
 #include "peripheral_interface_spi.h"
-#include "peripheral_common.h"
+#include "peripheral_interface_common.h"
 
 #define SYSFS_SPI_DIR "/dev/spidev"
-#define SPI_BUFFER_MAX 64
-#define MAX_ERR_LEN 255
 
 int spi_open(int bus, int cs, int *fd)
 {
        int new_fd = 0;
-       char spi_dev[SPI_BUFFER_MAX] = {0,};
+       char spi_dev[MAX_BUF_LEN] = {0,};
 
        _D("bus : %d, cs : %d", bus, cs);
 
-       snprintf(spi_dev, sizeof(spi_dev)-1, SYSFS_SPI_DIR"%d.%d", bus, cs);
+       snprintf(spi_dev, MAX_BUF_LEN, SYSFS_SPI_DIR"%d.%d", bus, cs);
 
        new_fd = open(spi_dev, O_RDWR);
-       if (new_fd < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Can't Open %s, errmsg : %s", spi_dev, errmsg);
-               return -ENXIO;
-       }
+       CHECK_ERROR(new_fd < 0);
+
        _D("fd : %d", new_fd);
        *fd = new_fd;
 
@@ -60,12 +45,7 @@ int spi_close(int fd)
        RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
 
        status = close(fd);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d", fd);
-               return -EIO;
-       }
+       CHECK_ERROR(status != 0);
 
        return 0;
 }
index eb1fa8f..c28cc10 100644 (file)
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
 #include <termios.h>
-#include <stdbool.h>
-#include <sys/ioctl.h>
 
 #include "peripheral_interface_uart.h"
-#include "peripheral_common.h"
-
-#define PATH_BUF_MAX 64
+#include "peripheral_interface_common.h"
 
 #ifndef ARRAY_SIZE
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
 #endif
-#define MAX_ERR_LEN 128
 
 char *sysfs_uart_path[] = {
        "/dev/ttyS",
@@ -43,22 +32,20 @@ char *sysfs_uart_path[] = {
 int uart_open(int port, int *file_hndl)
 {
        int i, fd;
-       char uart_dev[PATH_BUF_MAX] = {0};
+       char uart_dev[MAX_BUF_LEN] = {0};
 
        _D("port : %d", port);
 
        for (i = 0; i < ARRAY_SIZE(sysfs_uart_path); i++) {
-               snprintf(uart_dev, PATH_BUF_MAX, "%s%d", sysfs_uart_path[i], port);
+               snprintf(uart_dev, MAX_BUF_LEN, "%s%d", sysfs_uart_path[i], port);
                if (access(uart_dev, F_OK) == 0)
                        break;
        }
+
        _D("uart_dev : %s", uart_dev);
-       if ((fd = open(uart_dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("can't open %s, errmsg : %s", uart_dev, errmsg);
-               return -ENXIO;
-       }
+       fd = open(uart_dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
+       CHECK_ERROR(fd < 0);
+
        *file_hndl = fd;
        return 0;
 }
@@ -74,41 +61,11 @@ int uart_close(int file_hndl)
                return -EINVAL;
        }
 
-       status = uart_flush(file_hndl);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d, errmsg : %s", file_hndl, errmsg);
-               return -EIO;
-       }
+       status = tcflush(file_hndl, TCIOFLUSH);
+       CHECK_ERROR(status != 0);
 
        status = close(file_hndl);
-       if (status < 0) {
-               char errmsg[MAX_ERR_LEN];
-               strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("Failed to close fd : %d, errmsg : %s", file_hndl, errmsg);
-               return -EIO;
-       }
-
-       return 0;
-}
-
-int uart_flush(int file_hndl)
-{
-       int ret;
-
-       if (!file_hndl) {
-               _E("Invalid NULL parameter");
-               return -EINVAL;
-       }
-
-       ret = tcflush(file_hndl, 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(status != 0);
 
        return 0;
-}
+}
\ No newline at end of file