From: Adrian Szyndela Date: Fri, 25 Jun 2021 13:08:20 +0000 (+0200) Subject: refactoring: extract common code X-Git-Tag: submit/tizen/20210629.011533~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F98%2F260498%2F2;p=platform%2Fcore%2Fapi%2Fperipheral-io.git refactoring: extract common code Extract generic flocks. Extract ARRAY_SIZE. Extract close_fd. Extract MAX_d_FMT. Change-Id: I1f165414e95c0c3d4ac5c7e21b70896b8ae4a288 --- diff --git a/src/common.c b/src/common.c index 05a15ee..6c891d9 100644 --- a/src/common.c +++ b/src/common.c @@ -1,7 +1,28 @@ #include "common.h" #include +#include #include +int peripheral_lock(const char *file_name) +{ + int fd = open(file_name, O_WRONLY | O_CLOEXEC); + if (fd < 0) + return -1; + + if (flock(fd, LOCK_EX)) { + close(fd); + fd = -1; + } + + return fd; +} + +int peripheral_unlock(int lock) +{ + close_fd(lock); + return -1; +} + bool peripheral_is_feature_supported(const char *feature_name, int *feature_state) { assert(feature_name); diff --git a/src/common.h b/src/common.h index 39f810e..560242b 100644 --- a/src/common.h +++ b/src/common.h @@ -13,17 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once + +#include #include #include -#include +#include #include #include "log.h" #define MAX_ERR_LEN 255 +/* A string that is of maximum size of the result of printf("%d", integer), + assuming that the passed integer is zero or positive */ +#define MAX_d_FMT "1234567890" + #define CHECK_ERROR(expr) \ do { \ if (expr) { \ @@ -38,11 +43,40 @@ } \ } while (0) +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) +#endif + +#define TRY_FLOCK(ret, fd, lock_type, fmt, arg...) \ + do { \ + if (flock(fd, lock_type)) { \ + if (errno == EWOULDBLOCK) { \ + _E("Resource is in use: " fmt, ##arg); \ + ret = PERIPHERAL_ERROR_RESOURCE_BUSY; \ + } else { \ + ret = PERIPHERAL_ERROR_IO_ERROR; \ + } \ + } \ + } while (0) + typedef struct predefined_type { char *type; int len; } predefined_type_s; +int peripheral_lock(const char *file_name); +int peripheral_unlock(int lock); + +static inline void peripheral_unlockp(int *lock) +{ + *lock = peripheral_unlock(*lock); +} + +static inline int close_fd(int fd) { + if (fd >= 0) + return close(fd); + return -1; +} #define PERIPHERAL_FEATURE_UNKNOWN -1 #define PERIPHERAL_FEATURE_FALSE 0 diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c index 51fb04d..eb69971 100644 --- a/src/peripheral_adc.c +++ b/src/peripheral_adc.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "peripheral_io.h" #include "common.h" @@ -40,8 +41,7 @@ struct _peripheral_adc_s { static inline void cleanup_handlep(peripheral_adc_h *handle) { if (*handle != NULL) { - if ((*handle)->fd != -1) - close((*handle)->fd); + close_fd((*handle)->fd); free(*handle); } } @@ -65,21 +65,15 @@ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc) #define DEV_PATH_BASE(device, channel) ("/sys/bus/iio/devices/iio:device" device "/in_voltage" channel "_raw") /* space for /sys/bus/iio/devices/iio:device%d/in_voltage%d_raw */ - char path[sizeof(DEV_PATH_BASE("1234567890", "1234567890"))] = {0, }; + char path[sizeof(DEV_PATH_BASE(MAX_d_FMT, MAX_d_FMT))] = {0, }; snprintf(path, sizeof path, DEV_PATH_BASE("%d", "%d"), device, channel); handle->fd = open(path, O_RDONLY | O_CLOEXEC); CHECK_ERROR(handle->fd < 0); - if (flock(handle->fd, LOCK_EX | LOCK_NB)) { - if (errno == EWOULDBLOCK) { - _E("device : %d, channel : 0x%x is not available", device, channel); - return PERIPHERAL_ERROR_RESOURCE_BUSY; - } else { - _E("device : %d, channel : 0x%x flock() error: %d", device, channel, errno); - return PERIPHERAL_ERROR_IO_ERROR; - } - } + int ret = PERIPHERAL_ERROR_NONE; + TRY_FLOCK(ret, handle->fd, LOCK_EX | LOCK_NB, "device : %d, channel : 0x%x", device, channel); + CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE); *adc = handle; handle = NULL; diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index f6b1b34..b6666bb 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "peripheral_io.h" #include "common.h" @@ -111,7 +112,7 @@ static int __gpio_wait_for_udev(struct udev_monitor *monitor, int pin) struct udev_device *dev; struct pollfd pfd; #define GPIO_BASE "gpio" - char gpio_name[sizeof(GPIO_BASE "1234567890")]; + char gpio_name[sizeof(GPIO_BASE MAX_d_FMT)]; pfd.fd = udev_monitor_get_fd(monitor); pfd.events = POLLIN; @@ -147,7 +148,7 @@ static int peripheral_gpio_export(int pin) int ret; int fd = -1; int length; - char buf[sizeof "1234567890"] = {0, }; /* space for pin %d */ + char buf[sizeof MAX_d_FMT] = {0, }; /* space for pin %d */ struct udev *udev = NULL; struct udev_monitor *monitor = NULL; @@ -195,8 +196,7 @@ static int peripheral_gpio_export(int pin) } out: - if (fd != -1) - close(fd); + close_fd(fd); udev_monitor_unref(monitor); udev_unref(udev); @@ -204,41 +204,16 @@ out: return ret; } -static inline void close_fd(int fd) { - if (fd != -1) - close(fd); -} - static int peripheral_gpio_lock_export(void) { - int fd = open("/sys/class/gpio/export", O_WRONLY | O_CLOEXEC); - if (fd < 0) - return -1; - - if (flock(fd, LOCK_EX)) { - close(fd); - fd = -1; - } - - return fd; -} - -static int peripheral_gpio_unlock_export(int lock) -{ - close_fd(lock); - return -1; -} - -static void peripheral_gpio_unlock_exportp(int *lock) -{ - peripheral_gpio_unlock_export(*lock); + return peripheral_lock("/sys/class/gpio/export"); } static inline int cleanup_handle(peripheral_gpio_h handle) { if (handle == NULL) return PERIPHERAL_ERROR_NONE; - __attribute__ ((cleanup(peripheral_gpio_unlock_exportp))) int lock = peripheral_gpio_lock_export(); + __attribute__ ((cleanup(peripheral_unlockp))) int lock = peripheral_gpio_lock_export(); int pin = handle->pin; close_fd(handle->fd_edge); @@ -249,7 +224,7 @@ static inline int cleanup_handle(peripheral_gpio_h handle) { if (pin < 0) return PERIPHERAL_ERROR_NONE; - char buf[sizeof "1234567890"] = {0, }; /* space for %d */ + char buf[sizeof MAX_d_FMT] = {0, }; /* space for %d */ int fd = open("/sys/class/gpio/unexport", O_WRONLY | O_CLOEXEC); if (fd < 0) { _E("gpio: unexport pin %d: open() failed: %m", pin); @@ -286,7 +261,7 @@ static int peripheral_gpio_set_initial_direction_into_handle(peripheral_gpio_h g int ret = read(gpio->fd_direction, &gpio_buf, GPIO_BUFFER_MAX); CHECK_ERROR(ret <= 0); - for (index = 0; index < 2; index++) { + for (index = 0; index < ARRAY_SIZE(types); index++) { if (!strncmp(gpio_buf, types[index].type, types[index].len)) { // PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_HIGH and PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW : out type gpio->direction = (peripheral_gpio_direction_e)index; @@ -321,7 +296,7 @@ static int peripheral_gpio_set_initial_edge_into_handle(peripheral_gpio_h gpio) int ret = read(gpio->fd_edge, &gpio_buf, GPIO_BUFFER_MAX); CHECK_ERROR(ret <= 0); - for (index = 0; index < 4; index++) { + for (index = 0; index < ARRAY_SIZE(types); index++) { if (!strncmp(gpio_buf, types[index].type, types[index].len)) { gpio->edge = (peripheral_gpio_edge_e)index; return PERIPHERAL_ERROR_NONE; @@ -343,7 +318,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) RETVM_IF(gpio_pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin number"); __attribute__ ((cleanup(cleanup_handlep))) peripheral_gpio_h handle = NULL; - __attribute__ ((cleanup(peripheral_gpio_unlock_exportp))) int lock = -1; + __attribute__ ((cleanup(peripheral_unlockp))) int lock = -1; /* Initialize */ handle = (peripheral_gpio_h)calloc(1, sizeof(struct _peripheral_gpio_s)); @@ -372,20 +347,13 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) /* space for /sys/class/gpio/gpio%d/direction, which is larger than /sys/class/gpio/gpio%d/edge and /sys/class/gpio/gpio%d/value */ - char path[sizeof(DEV_PATH("1234567890"))] = {0, }; + char path[sizeof(DEV_PATH(MAX_d_FMT))] = {0, }; snprintf(path, sizeof path, "/sys/class/gpio/gpio%d/direction", handle->pin); handle->fd_direction = open(path, O_RDWR | O_CLOEXEC); CHECK_ERROR(handle->fd_direction < 0); - if (flock(handle->fd_direction, LOCK_EX | LOCK_NB)) { - if (errno == EWOULDBLOCK) { - _E("gpio: pin %d is not available", gpio_pin); - return PERIPHERAL_ERROR_RESOURCE_BUSY; - } else { - _E("gpio: pin %d flock() error: %d", gpio_pin, errno); - return PERIPHERAL_ERROR_IO_ERROR; - } - } + TRY_FLOCK(ret, handle->fd_direction, LOCK_EX | LOCK_NB, "gpio pin %d", gpio_pin); + CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE); snprintf(path, sizeof path, "/sys/class/gpio/gpio%d/edge", handle->pin); handle->fd_edge = open(path, O_RDWR | O_CLOEXEC); @@ -395,7 +363,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) handle->fd_value = open(path, O_RDWR | O_CLOEXEC); CHECK_ERROR(handle->fd_value < 0); - lock = peripheral_gpio_unlock_export(lock); + lock = peripheral_unlock(lock); ret = peripheral_gpio_set_initial_direction_into_handle(handle); if (ret != PERIPHERAL_ERROR_NONE) { diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 9c1b21e..c3b69f3 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "peripheral_io.h" #include "common.h" @@ -67,19 +68,18 @@ struct i2c_smbus_ioctl_data { static inline void cleanup_handlep(peripheral_i2c_h *handle) { if (*handle != NULL) { - if ((*handle)->fd != -1) - close((*handle)->fd); + close_fd((*handle)->fd); free(*handle); } } int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flags, peripheral_i2c_h *i2c) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; int lock_type = LOCK_EX; #define DEV_PATH_BASE "/dev/i2c-" - char path[sizeof(DEV_PATH_BASE "0000000000")] = {0, }; /* space for /dev/i2c-%d */ + char path[sizeof(DEV_PATH_BASE MAX_d_FMT)] = {0, }; /* space for /dev/i2c-%d */ RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid i2c handle"); @@ -105,15 +105,9 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag if (flags == PERIPHERAL_OPEN_FLAGS_SHARED) lock_type = LOCK_SH; - if (flock(handle->fd, lock_type | LOCK_NB)) { - if (errno == EWOULDBLOCK) { - _E("bus : %d, address : 0x%x is not available", bus, address); - return PERIPHERAL_ERROR_RESOURCE_BUSY; - } else { - _E("bus : %d, address : 0x%x flock() error: %d", bus, address, errno); - return PERIPHERAL_ERROR_IO_ERROR; - } - } + ret = PERIPHERAL_ERROR_NONE; + TRY_FLOCK(ret, handle->fd, lock_type | LOCK_NB, "bus : %d, address : 0x%x", bus, address); + CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE); *i2c = handle; handle = NULL; diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 880e714..d9b2c37 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "peripheral_io.h" #include "common.h" @@ -86,9 +87,9 @@ static int __pwm_wait_for_udev(struct udev_monitor *monitor, int chip, int pin) struct udev_device *dev = NULL; struct pollfd pfd; #define PWMCHIP_BASE "pwmchip" - char pwmchip_name[sizeof(PWMCHIP_BASE "1234567890")]; /* space for pwmchip%d */ + char pwmchip_name[sizeof(PWMCHIP_BASE MAX_d_FMT)]; /* space for pwmchip%d */ #define PWM_BASE "pwm" - char pwm_name[sizeof(PWM_BASE "1234567890")]; /* space for pwm%d */ + char pwm_name[sizeof(PWM_BASE MAX_d_FMT)]; /* space for pwm%d */ pfd.fd = udev_monitor_get_fd(monitor); pfd.events = POLLIN; @@ -130,8 +131,8 @@ static int peripheral_pwm_export(int chip, int pin) int fd = -1; int length; #define EXPORT_PATH(chip) ("/sys/class/pwm/pwmchip" chip "/export") - char path[sizeof EXPORT_PATH("1234567890")] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */ - char buf[sizeof "1234567890"] = {0, }; /* space for pin %d */ + char path[sizeof EXPORT_PATH(MAX_d_FMT)] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */ + char buf[sizeof MAX_d_FMT] = {0, }; /* space for pin %d */ struct udev *udev = NULL; struct udev_monitor *monitor = NULL; @@ -183,37 +184,12 @@ out: return ret; } -static inline void close_fd(int fd) { - if (fd != -1) - close(fd); -} - static int peripheral_pwm_lock_export(int chip) { - char path[sizeof EXPORT_PATH("1234567890")] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */ + char path[sizeof EXPORT_PATH(MAX_d_FMT)] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */ snprintf(path, sizeof path, EXPORT_PATH("%d"), chip); - int fd = open(path, O_WRONLY | O_CLOEXEC); - if (fd < 0) - return -1; - - if (flock(fd, LOCK_EX)) { - close(fd); - fd = -1; - } - - return fd; -} - -static int peripheral_pwm_unlock_export(int lock) -{ - close_fd(lock); - return -1; -} - -static void peripheral_pwm_unlock_exportp(int *lock) -{ - peripheral_pwm_unlock_export(*lock); + return peripheral_lock(path); } static inline int cleanup_handle(peripheral_pwm_h handle) @@ -223,7 +199,7 @@ static inline int cleanup_handle(peripheral_pwm_h handle) int pin = handle->pin; int chip = handle->chip; - __attribute__ ((cleanup(peripheral_pwm_unlock_exportp))) int lock = peripheral_pwm_lock_export(chip); + __attribute__ ((cleanup(peripheral_unlockp))) int lock = peripheral_pwm_lock_export(chip); close_fd(handle->fd_duty_cycle); close_fd(handle->fd_polarity); @@ -234,9 +210,9 @@ static inline int cleanup_handle(peripheral_pwm_h handle) if (pin <0) return PERIPHERAL_ERROR_NONE; - char buf[sizeof "1234567890"] = {0, }; /* space for pin %d */ + char buf[sizeof MAX_d_FMT] = {0, }; /* space for pin %d */ #define UNEXPORT_PATH(chip) ("/sys/class/pwm/pwmchip" chip "/unexport") - char path[sizeof UNEXPORT_PATH("1234567890")] = {0, }; /* space for /sys/class/pwm/pwmchip%d/unexport */ + char path[sizeof UNEXPORT_PATH(MAX_d_FMT)] = {0, }; /* space for /sys/class/pwm/pwmchip%d/unexport */ snprintf(path, sizeof path, UNEXPORT_PATH("%d"), chip); int fd = open(path, O_WRONLY | O_CLOEXEC); @@ -271,7 +247,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm handle"); RETVM_IF(chip < 0 || pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - __attribute__ ((cleanup(peripheral_pwm_unlock_exportp))) int lock = -1; + __attribute__ ((cleanup(peripheral_unlockp))) int lock = -1; __attribute__ ((cleanup(cleanup_handlep))) peripheral_pwm_h handle = NULL; #define PWM_PATH_BASE(chip, pin, file) ("/sys/class/pwm/pwmchip" chip "/pwm" pin "/" file) /* space for /sys/class/pwm/pwmchip%d/pwm%d/duty_cycle @@ -280,7 +256,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) /sys/class/pwm/pwmchip%d/pwm%d/polarity /sys/class/pwm/pwmchip%d/pwm%d/enable */ - char path[sizeof (PWM_PATH_BASE("1234567890", "1234567890", "duty_cycle"))] = {0, }; + char path[sizeof (PWM_PATH_BASE(MAX_d_FMT, MAX_d_FMT, "duty_cycle"))] = {0, }; /* Initialize */ handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s)); @@ -313,15 +289,8 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) handle->fd_period = open(path, O_RDWR | O_CLOEXEC); CHECK_ERROR(handle->fd_period < 0); - if (flock(handle->fd_period, LOCK_EX | LOCK_NB)) { - if (errno == EWOULDBLOCK) { - _E("pwm: chip %d, pin %d is not available", chip, pin); - return PERIPHERAL_ERROR_RESOURCE_BUSY; - } else { - _E("pwm: chip %d, pin %d flock() error: %m", chip, pin); - return PERIPHERAL_ERROR_IO_ERROR; - } - } + TRY_FLOCK(ret, handle->fd_period, LOCK_EX | LOCK_NB, "pwm: chip %d, pin %d", chip, pin); + CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE); snprintf(path, sizeof path, PWM_PATH_BASE("%d", "%d", "duty_cycle"), chip, pin); handle->fd_duty_cycle = open(path, O_RDWR | O_CLOEXEC); @@ -335,7 +304,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) handle->fd_enable = open(path, O_RDWR | O_CLOEXEC); CHECK_ERROR(handle->fd_enable < 0); - lock = peripheral_pwm_unlock_export(lock); + lock = peripheral_unlock(lock); *pwm = handle; handle = NULL; diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c index 12645f9..29b6122 100644 --- a/src/peripheral_spi.c +++ b/src/peripheral_spi.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "peripheral_io.h" #include "common.h" @@ -39,16 +40,15 @@ struct _peripheral_spi_s { static inline void cleanup_handlep(peripheral_spi_h *handle) { if (*handle != NULL) { - if ((*handle)->fd != -1) - close((*handle)->fd); + close_fd((*handle)->fd); free(*handle); } } int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) { -#define DEV_PATH_BASE "/dev/spidev" - char path[sizeof(DEV_PATH_BASE "1234567890.1234567890")] = {0, }; /* space for /dev/spidev%d.%d */ +#define DEV_PATH_BASE(bus, cs) "/dev/spidev" bus "." cs + char path[sizeof(DEV_PATH_BASE(MAX_d_FMT, MAX_d_FMT))] = {0, }; /* space for /dev/spidev%d.%d */ RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi handle"); @@ -63,19 +63,13 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - snprintf(path, sizeof path, DEV_PATH_BASE "%d.%d", bus, cs); + snprintf(path, sizeof path, DEV_PATH_BASE("%d","%d"), bus, cs); handle->fd = open(path, O_RDWR | O_CLOEXEC); CHECK_ERROR(handle->fd < 0); - if (flock(handle->fd, LOCK_EX | LOCK_NB)) { - if (errno == EWOULDBLOCK) { - _E("bus : %d, cs : 0x%x is not available", bus, cs); - return PERIPHERAL_ERROR_RESOURCE_BUSY; - } else { - _E("bus : %d, cs : 0x%x flock() error: %d", bus, cs, errno); - return PERIPHERAL_ERROR_IO_ERROR; - } - } + int ret = PERIPHERAL_ERROR_NONE; + TRY_FLOCK(ret, handle->fd, LOCK_EX | LOCK_NB, "bus : %d, cs : 0x%x", bus, cs); + CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE); *spi = handle; handle = NULL; diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index eb02c80..c5cce8e 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "peripheral_io.h" #include "common.h" @@ -29,10 +30,6 @@ FEATURE("http://tizen.org/feature/peripheral_io.uart") -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) -#endif - /** * @brief Internal struct for uart context */ @@ -52,8 +49,7 @@ static const int byteinfo[4] = {CS5, CS6, CS7, CS8}; static inline void cleanup_handlep(peripheral_uart_h *handle) { if (*handle != NULL) { - if ((*handle)->fd != -1) - close((*handle)->fd); + close_fd((*handle)->fd); free(*handle); } } @@ -81,7 +77,7 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) }; size_t index; - char path[sizeof("/dev/ttyABC1234567890")] = {0, }; /* space for /dev/ttyXXX%d */ + char path[sizeof("/dev/ttyABC" MAX_d_FMT)] = {0, }; /* space for /dev/ttyXXX%d */ for (index = 0; index < ARRAY_SIZE(sysfs_uart_path); index++) { snprintf(path, sizeof path, "%s%d", sysfs_uart_path[index], port); @@ -95,15 +91,9 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) handle->fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC); CHECK_ERROR(handle->fd < 0); - if (flock(handle->fd, LOCK_EX | LOCK_NB)) { - if (errno == EWOULDBLOCK) { - _E("path : %s is not available", path); - return PERIPHERAL_ERROR_RESOURCE_BUSY; - } else { - _E("path: %s flock() error: %d", path, errno); - return PERIPHERAL_ERROR_IO_ERROR; - } - } + int ret = PERIPHERAL_ERROR_NONE; + TRY_FLOCK(ret, handle->fd, LOCK_EX | LOCK_NB, "uart port: %d, path: %s", port, path); + CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE); *uart = handle; handle = NULL;