int peripheral_create_udev_monitor(struct udev **udev, struct udev_monitor **monitor, const char *filter_name)
{
- struct udev *_udev = NULL;
struct udev_monitor *_monitor = NULL;
int ret = -EIO;
- _udev = udev_new();
+ struct udev *_udev = udev_new();
if (!_udev) {
_E("Cannot create udev");
goto error;
assert(monitor);
assert(func);
- struct udev_device *dev = NULL;
struct pollfd pfd;
pfd.fd = udev_monitor_get_fd(monitor);
return -EIO;
}
- dev = udev_monitor_receive_device(monitor);
+ struct udev_device *dev = udev_monitor_receive_device(monitor);
if (dev) {
if (func(dev, func_data)) {
udev_device_unref(dev);
{
assert(pin >= 0);
- char buf[sizeof MAX_d_FMT] = {0, }; /* space for pin %d */
-
int fd = open(file_name, O_WRONLY | O_CLOEXEC);
if (fd < 0) {
_E("open(%s) failed: %m", file_name);
return -1;
}
+ char buf[sizeof MAX_d_FMT] = {0, }; /* space for pin %d */
+
int ret = 0;
int length = snprintf(buf, sizeof buf, "%d", pin);
if (write(fd, buf, length) != length)
RETVM_IF(device < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc device number");
RETVM_IF(channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc channel number");
- __attribute__ ((cleanup(cleanup_handlep))) peripheral_adc_h handle = NULL;
- handle = (peripheral_adc_h)calloc(1, sizeof *handle);
+ __attribute__ ((cleanup(cleanup_handlep))) peripheral_adc_h handle = (peripheral_adc_h)calloc(1, sizeof *handle);
if (handle == NULL) {
_E("Failed to allocate peripheral_adc_h");
return PERIPHERAL_ERROR_OUT_OF_MEMORY;
RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio handle");
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_unlockp))) int lock = -1;
-
/* Initialize */
- handle = (peripheral_gpio_h)calloc(1, sizeof *handle);
+ __attribute__ ((cleanup(cleanup_handlep))) peripheral_gpio_h handle = (peripheral_gpio_h)calloc(1, sizeof *handle);
if (handle == NULL) {
_E("Failed to allocate peripheral_gpio_h");
return PERIPHERAL_ERROR_OUT_OF_MEMORY;
handle->fd_value = -1;
handle->pin = -1;
- lock = peripheral_gpio_lock_export();
+ __attribute__ ((cleanup(peripheral_unlockp))) int lock = peripheral_gpio_lock_export();
RETVM_IF(lock == -1, PERIPHERAL_ERROR_IO_ERROR, "Failed to lock 'export'");
ret = peripheral_gpio_export(gpio_pin);
{
peripheral_gpio_h gpio = (peripheral_gpio_h)data;
- int ret;
- int poll_state = 0;
struct pollfd poll_fd;
poll_fd.fd = gpio->fd_value;
while (g_atomic_int_get(&gpio->cb_info.status) == GPIO_INTERRUPTED_CALLBACK_SET) {
- poll_state = poll(&poll_fd, 1, GPIO_INTERRUPTED_CALLBACK_CHECK_SET_INTERVAL_MILISECONDS);
+ int poll_state = poll(&poll_fd, 1, GPIO_INTERRUPTED_CALLBACK_CHECK_SET_INTERVAL_MILISECONDS);
if (poll_state == 0)
continue;
}
if (poll_fd.revents & POLLPRI) {
- ret = peripheral_gpio_read(gpio, &value);
+ int ret = peripheral_gpio_read(gpio, &value);
if (ret != PERIPHERAL_ERROR_NONE)
continue;
} else {
RETVM_IF(flags != PERIPHERAL_OPEN_FLAGS_PRIVATE && flags != PERIPHERAL_OPEN_FLAGS_SHARED,
PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid flags");
- __attribute__ ((cleanup(cleanup_handlep))) peripheral_i2c_h handle = NULL;
-
- handle = (peripheral_i2c_h)malloc(sizeof *handle);
+ __attribute__ ((cleanup(cleanup_handlep))) peripheral_i2c_h handle = (peripheral_i2c_h)malloc(sizeof *handle);
if (handle == NULL) {
_E("Failed to allocate peripheral_i2c_h");
return PERIPHERAL_ERROR_OUT_OF_MEMORY;
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_unlockp))) int lock = -1;
- __attribute__ ((cleanup(cleanup_handlep))) peripheral_pwm_h handle = NULL;
- /* space for /sys/class/pwm/pwmchip%d/pwm%d/duty_cycle
- which is longer than:
- /sys/class/pwm/pwmchip%d/pwm%d/period
- /sys/class/pwm/pwmchip%d/pwm%d/polarity
- /sys/class/pwm/pwmchip%d/pwm%d/enable
- */
- char path[PWM_PATH_FMT_MAX_SIZE] = {0, };
-
/* Initialize */
- handle = (peripheral_pwm_h)calloc(1, sizeof *handle);
+ __attribute__ ((cleanup(cleanup_handlep))) peripheral_pwm_h handle = (peripheral_pwm_h)calloc(1, sizeof *handle);
if (handle == NULL) {
_E("Failed to allocate peripheral_pwm_h");
return PERIPHERAL_ERROR_OUT_OF_MEMORY;
}
-
handle->fd_period = -1;
handle->fd_duty_cycle = -1;
handle->fd_polarity = -1;
handle->chip = -1;
handle->pin = -1;
- lock = peripheral_pwm_lock_export(chip);
+ __attribute__ ((cleanup(peripheral_unlockp))) int lock = peripheral_pwm_lock_export(chip);
RETVM_IF(lock == -1, PERIPHERAL_ERROR_IO_ERROR, "Failed to lock 'export'");
ret = peripheral_pwm_export(chip, pin);
handle->chip = chip;
handle->pin = pin;
+ /* space for /sys/class/pwm/pwmchip%d/pwm%d/duty_cycle
+ which is longer than:
+ /sys/class/pwm/pwmchip%d/pwm%d/period
+ /sys/class/pwm/pwmchip%d/pwm%d/polarity
+ /sys/class/pwm/pwmchip%d/pwm%d/enable
+ */
+ char path[PWM_PATH_FMT_MAX_SIZE] = {0, };
+
snprintf(path, sizeof path, PWM_PATH_FMT("period"), chip, pin);
handle->fd_period = open(path, O_RDWR | O_CLOEXEC);
CHECK_ERROR(handle->fd_period < 0);
int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
{
- char path[DEV_PATH_FMT_MAX_SIZE] = {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");
RETVM_IF(bus < 0 || cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
- __attribute__ ((cleanup(cleanup_handlep))) peripheral_spi_h handle = NULL;
-
/* Initialize */
- handle = (peripheral_spi_h)calloc(1, sizeof *handle);
+ __attribute__ ((cleanup(cleanup_handlep))) peripheral_spi_h handle = (peripheral_spi_h)calloc(1, sizeof *handle);
if (handle == NULL) {
_E("Failed to allocate peripheral_spi_h");
return PERIPHERAL_ERROR_OUT_OF_MEMORY;
}
+ char path[DEV_PATH_FMT_MAX_SIZE] = {0, }; /* space for /dev/spidev%d.%d */
+
snprintf(path, sizeof path, DEV_PATH_FMT, bus, cs);
handle->fd = open(path, O_RDWR | O_CLOEXEC);
CHECK_ERROR(handle->fd < 0);
RETVM_IF(flags != PERIPHERAL_OPEN_FLAGS_PRIVATE && flags != PERIPHERAL_OPEN_FLAGS_SHARED,
PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid flags");
- __attribute__ ((cleanup(cleanup_handlep))) peripheral_uart_h handle = NULL;
- handle = (peripheral_uart_h)calloc(1, sizeof *handle);
+ __attribute__ ((cleanup(cleanup_handlep))) peripheral_uart_h handle = (peripheral_uart_h)calloc(1, sizeof *handle);
if (handle == NULL) {
_E("Failed to allocate peripheral_uart_h");
return PERIPHERAL_ERROR_OUT_OF_MEMORY;