Extract generic flocks.
Extract ARRAY_SIZE.
Extract close_fd.
Extract MAX_d_FMT.
Change-Id: I1f165414e95c0c3d4ac5c7e21b70896b8ae4a288
#include "common.h"
#include <assert.h>
+#include <sys/file.h>
#include <system_info.h>
+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);
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
#pragma once
+
+#include <errno.h>
#include <stdbool.h>
#include <string.h>
-#include <errno.h>
+#include <sys/file.h>
#include <unistd.h>
#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) { \
} \
} 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
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <unistd.h>
#include "peripheral_io.h"
#include "common.h"
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);
}
}
#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;
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
+#include <unistd.h>
#include "peripheral_io.h"
#include "common.h"
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;
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;
}
out:
- if (fd != -1)
- close(fd);
+ close_fd(fd);
udev_monitor_unref(monitor);
udev_unref(udev);
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);
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);
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;
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;
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));
/* 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);
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) {
#include <stdlib.h>
#include <sys/file.h>
#include <sys/ioctl.h>
+#include <unistd.h>
#include "peripheral_io.h"
#include "common.h"
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");
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;
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
+#include <unistd.h>
#include "peripheral_io.h"
#include "common.h"
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;
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;
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)
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);
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);
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
/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));
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);
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;
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <unistd.h>
#include "peripheral_io.h"
#include "common.h"
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");
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;
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
+#include <unistd.h>
#include "peripheral_io.h"
#include "common.h"
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
*/
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);
}
}
};
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);
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;