--- /dev/null
+/*
+ * 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
#include <fcntl.h>
#include <poll.h>
+#include "peripheral_interface_common.h"
#include "peripheral_interface_gpio.h"
#include "peripheral_common.h"
#include "peripheral_internal.h"
return -EIO;
}
- if (status <= 0) {
- _E("Error: gpio direction set error\n");
- return -EIO;
- }
+ CHECK_ERROR(status);
return 0;
}
return -EIO;
}
- if (status <= 0) {
- _E("Error: gpio edge set error\n");
- return -EIO;
- }
+ CHECK_ERROR(status);
return 0;
}
return -EIO;
}
- if (status <= 0) {
- _E("Error: gpio write error\n");
- return -EIO;
- }
+ CHECK_ERROR(status);
return 0;
}
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;
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;
}
#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"
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;
}
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;
}
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;
}
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;
}
#include <unistd.h>
#include <stdbool.h>
+#include "peripheral_interface_common.h"
#include "peripheral_interface_pwm.h"
#include "peripheral_common.h"
#include "peripheral_internal.h"
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;
}
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;
}
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;
}
return -EINVAL;
}
- if (status <= 0) {
- _E("Failed to set polarity");
- return -EIO;
- }
+ CHECK_ERROR(status);
return 0;
}
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;
}
#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"
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
#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"
}
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;
}
}
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;
}
}
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;
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;
}
}
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;
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;
}
}
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) {
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;
}
}
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) {
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;
}
}
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;
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;
}
}
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;
}
}
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;
}