From 889346a1fe12e95e59a1a49dbcb74f9615c7ec7a Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Fri, 25 Jun 2021 14:51:13 +0200 Subject: [PATCH] refactoring: extract system_info feature handling Change-Id: I9ee561f1ba9498e8660555ec93dbae691b0b7287 --- CMakeLists.txt | 3 ++- src/common.c | 20 ++++++++++++++++++++ src/common.h | 21 +++++++++++++++++---- src/peripheral_adc.c | 29 ++++------------------------- src/peripheral_gpio.c | 38 +++++++++----------------------------- src/peripheral_i2c.c | 40 +++++++++------------------------------- src/peripheral_pwm.c | 36 +++++++----------------------------- src/peripheral_spi.c | 42 ++++++++++-------------------------------- src/peripheral_uart.c | 42 ++++++++++-------------------------------- 9 files changed, 88 insertions(+), 183 deletions(-) create mode 100644 src/common.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3993e16..2fa3e3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,8 @@ SET(SOURCES src/peripheral_gpio.c src/peripheral_pwm.c src/peripheral_adc.c src/peripheral_uart.c - src/peripheral_spi.c) + src/peripheral_spi.c + src/common.c) ADD_LIBRARY(${fw_name} SHARED ${SOURCES}) diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..05a15ee --- /dev/null +++ b/src/common.c @@ -0,0 +1,20 @@ +#include "common.h" +#include +#include + +bool peripheral_is_feature_supported(const char *feature_name, int *feature_state) +{ + assert(feature_name); + assert(feature_state); + + if (*feature_state == PERIPHERAL_FEATURE_UNKNOWN) { + bool feature = false; + int ret = system_info_get_platform_bool(feature_name, &feature); + RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info"); + + *feature_state = (feature ? PERIPHERAL_FEATURE_TRUE : PERIPHERAL_FEATURE_FALSE); + } + + return *feature_state == PERIPHERAL_FEATURE_TRUE; +} + diff --git a/src/common.h b/src/common.h index 77ff0f7..39f810e 100644 --- a/src/common.h +++ b/src/common.h @@ -14,9 +14,8 @@ * limitations under the License. */ -#ifndef __PERIPHERAL_INTERFACE_COMMON_H__ -#define __PERIPHERAL_INTERFACE_COMMON_H__ - +#pragma once +#include #include #include #include @@ -44,4 +43,18 @@ typedef struct predefined_type { int len; } predefined_type_s; -#endif /*__PERIPHERAL_INTERFACE_COMMON_H__*/ + +#define PERIPHERAL_FEATURE_UNKNOWN -1 +#define PERIPHERAL_FEATURE_FALSE 0 +#define PERIPHERAL_FEATURE_TRUE 1 + +bool peripheral_is_feature_supported(const char *feature_name, int *feature_state); + +#define FEATURE(feature_name) \ + static bool __is_feature_supported(void) \ + { \ + static int feature = PERIPHERAL_FEATURE_UNKNOWN; \ + if (feature == PERIPHERAL_FEATURE_UNKNOWN) \ + return peripheral_is_feature_supported(feature_name, &feature); \ + return feature == PERIPHERAL_FEATURE_TRUE; \ + } diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c index 79a9fc4..51fb04d 100644 --- a/src/peripheral_adc.c +++ b/src/peripheral_adc.c @@ -21,17 +21,12 @@ #include #include #include -#include #include "peripheral_io.h" #include "common.h" #include "log.h" -#define PERIPHERAL_IO_ADC_FEATURE "http://tizen.org/feature/peripheral_io.adc" - -#define ADC_FEATURE_UNKNOWN -1 -#define ADC_FEATURE_FALSE 0 -#define ADC_FEATURE_TRUE 1 +FEATURE("http://tizen.org/feature/peripheral_io.adc") #define ADC_BUFFER_MAX 64 @@ -42,22 +37,6 @@ struct _peripheral_adc_s { int fd; }; -static int adc_feature = ADC_FEATURE_UNKNOWN; - -static bool __is_feature_supported(void) -{ - int ret = SYSTEM_INFO_ERROR_NONE; - bool feature = false; - - if (adc_feature == ADC_FEATURE_UNKNOWN) { - ret = system_info_get_platform_bool(PERIPHERAL_IO_ADC_FEATURE, &feature); - RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info"); - - adc_feature = (feature ? ADC_FEATURE_TRUE : ADC_FEATURE_FALSE); - } - return (adc_feature == ADC_FEATURE_TRUE ? true : false); -} - static inline void cleanup_handlep(peripheral_adc_h *handle) { if (*handle != NULL) { @@ -72,7 +51,7 @@ static inline void cleanup_handlep(peripheral_adc_h *handle) */ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc handle"); RETVM_IF(device < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc device number"); RETVM_IF(channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc channel number"); @@ -113,7 +92,7 @@ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc) */ int peripheral_adc_close(peripheral_adc_h adc) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "adc handle is NULL"); cleanup_handlep(&adc); @@ -126,7 +105,7 @@ int peripheral_adc_close(peripheral_adc_h adc) */ int peripheral_adc_read(peripheral_adc_h adc, uint32_t *value) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported"); RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "adc handle is NULL"); RETVM_IF(value == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "adc read value is invalid"); diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index d700aaf..f6b1b34 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -20,17 +20,13 @@ #include #include #include -#include #include "peripheral_io.h" #include "common.h" #include "log.h" -#define PERIPHERAL_IO_GPIO_FEATURE "http://tizen.org/feature/peripheral_io.gpio" +FEATURE("http://tizen.org/feature/peripheral_io.gpio") -#define GPIO_FEATURE_UNKNOWN -1 -#define GPIO_FEATURE_FALSE 0 -#define GPIO_FEATURE_TRUE 1 #define GPIO_STRUCTURE_VERMAGIC 13712 #define GPIO_CALLBACK_STRUCTURE_VERMAGIC 14469 @@ -69,22 +65,6 @@ struct _peripheral_gpio_s { peripheral_gpio_edge_e edge; }; -static int gpio_feature = GPIO_FEATURE_UNKNOWN; - -static bool __is_feature_supported(void) -{ - int ret = SYSTEM_INFO_ERROR_NONE; - bool feature = false; - - if (gpio_feature == GPIO_FEATURE_UNKNOWN) { - ret = system_info_get_platform_bool(PERIPHERAL_IO_GPIO_FEATURE, &feature); - RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info"); - - gpio_feature = (feature ? GPIO_FEATURE_TRUE : GPIO_FEATURE_FALSE); - } - return (gpio_feature == GPIO_FEATURE_TRUE ? true : false); -} - static int __gpio_create_udev_monitor(struct udev **udev, struct udev_monitor **monitor) { struct udev *_udev = NULL; @@ -358,7 +338,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) { int ret = PERIPHERAL_ERROR_NONE; - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio handle"); RETVM_IF(gpio_pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin number"); @@ -442,7 +422,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) */ int peripheral_gpio_close(peripheral_gpio_h gpio) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); peripheral_gpio_unset_interrupted_cb(gpio); @@ -457,7 +437,7 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) */ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); RETVM_IF((direction < PERIPHERAL_GPIO_DIRECTION_IN) || (direction > PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid direction input"); @@ -492,7 +472,7 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct */ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); RETVM_IF((edge < PERIPHERAL_GPIO_EDGE_NONE) || (edge > PERIPHERAL_GPIO_EDGE_BOTH), PERIPHERAL_ERROR_INVALID_PARAMETER, "edge input is invalid"); @@ -598,7 +578,7 @@ static gpointer __peripheral_gpio_poll(void *data) */ int peripheral_gpio_set_interrupted_cb(peripheral_gpio_h gpio, peripheral_gpio_interrupted_cb callback, void *user_data) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); RETVM_IF(callback == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio interrupted callback is NULL"); RETV_IF(gpio->direction != PERIPHERAL_GPIO_DIRECTION_IN, PERIPHERAL_ERROR_IO_ERROR); @@ -618,7 +598,7 @@ int peripheral_gpio_set_interrupted_cb(peripheral_gpio_h gpio, peripheral_gpio_i */ int peripheral_gpio_unset_interrupted_cb(peripheral_gpio_h gpio) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); g_atomic_int_set(&gpio->cb_info.status, GPIO_INTERRUPTED_CALLBACK_UNSET); @@ -636,7 +616,7 @@ int peripheral_gpio_unset_interrupted_cb(peripheral_gpio_h gpio) */ int peripheral_gpio_read(peripheral_gpio_h gpio, uint32_t *value) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); RETVM_IF(value == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio read value is invalid"); /* @@ -669,7 +649,7 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, uint32_t *value) */ int peripheral_gpio_write(peripheral_gpio_h gpio, uint32_t value) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "GPIO feature is not supported"); RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); RETVM_IF((value != 0) && (value != 1), PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio value is invalid"); /* diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 7e975fc..9c1b21e 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -18,17 +18,12 @@ #include #include #include -#include #include "peripheral_io.h" #include "common.h" #include "log.h" -#define PERIPHERAL_IO_I2C_FEATURE "http://tizen.org/feature/peripheral_io.i2c" - -#define I2C_FEATURE_UNKNOWN -1 -#define I2C_FEATURE_FALSE 0 -#define I2C_FEATURE_TRUE 1 +FEATURE("http://tizen.org/feature/peripheral_io.i2c") #define I2C_SLAVE 0x0703 /* Use this slave address */ #define I2C_SMBUS 0x0720 /* SMBus transfer */ @@ -70,23 +65,6 @@ struct i2c_smbus_ioctl_data { union i2c_smbus_data *data; }; -static int i2c_feature = I2C_FEATURE_UNKNOWN; - -static bool __is_feature_supported(void) -{ - int ret = SYSTEM_INFO_ERROR_NONE; - bool feature = false; - - if (i2c_feature == I2C_FEATURE_UNKNOWN) { - ret = system_info_get_platform_bool(PERIPHERAL_IO_I2C_FEATURE, &feature); - RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info"); - - i2c_feature = (feature ? I2C_FEATURE_TRUE : I2C_FEATURE_FALSE); - } - - return (i2c_feature == I2C_FEATURE_TRUE ? true : false); -} - static inline void cleanup_handlep(peripheral_i2c_h *handle) { if (*handle != NULL) { if ((*handle)->fd != -1) @@ -103,7 +81,7 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag #define DEV_PATH_BASE "/dev/i2c-" char path[sizeof(DEV_PATH_BASE "0000000000")] = {0, }; /* space for /dev/i2c-%d */ - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + 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"); RETVM_IF(bus < 0 || address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); RETVM_IF(flags != PERIPHERAL_OPEN_FLAGS_PRIVATE && flags != PERIPHERAL_OPEN_FLAGS_SHARED, @@ -150,7 +128,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) int peripheral_i2c_close(peripheral_i2c_h i2c) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); cleanup_handlep(&i2c); @@ -175,7 +153,7 @@ static int i2c_readwrite(peripheral_i2c_h i2c, uint8_t read_write, uint32_t size int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -195,7 +173,7 @@ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); RETVM_IF(data_in == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -210,7 +188,7 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -226,7 +204,7 @@ int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); union i2c_smbus_data data = {.block = {0,}}; @@ -237,7 +215,7 @@ int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_ int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -253,7 +231,7 @@ int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_ int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); union i2c_smbus_data data = {.block = {0,}}; diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 0eaf94b..880e714 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -19,17 +19,12 @@ #include #include #include -#include #include "peripheral_io.h" #include "common.h" #include "log.h" -#define PERIPHERAL_IO_PWM_FEATURE "http://tizen.org/feature/peripheral_io.pwm" - -#define PWM_FEATURE_UNKNOWN -1 -#define PWM_FEATURE_FALSE 0 -#define PWM_FEATURE_TRUE 1 +FEATURE("http://tizen.org/feature/peripheral_io.pwm") #define PWM_BUF_MAX 16 @@ -45,23 +40,6 @@ struct _peripheral_pwm_s { int fd_enable; }; -static int pwm_feature = PWM_FEATURE_UNKNOWN; - -static bool __is_feature_supported(void) -{ - int ret = SYSTEM_INFO_ERROR_NONE; - bool feature = false; - - if (pwm_feature == PWM_FEATURE_UNKNOWN) { - ret = system_info_get_platform_bool(PERIPHERAL_IO_PWM_FEATURE, &feature); - RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info"); - - pwm_feature = (feature ? PWM_FEATURE_TRUE : PWM_FEATURE_FALSE); - } - - return (pwm_feature == PWM_FEATURE_TRUE ? true : false); -} - static int __pwm_create_udev_monitor(struct udev **udev, struct udev_monitor **monitor) { struct udev *_udev = NULL; @@ -289,7 +267,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) { int ret = PERIPHERAL_ERROR_NONE; - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm handle"); RETVM_IF(chip < 0 || pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -367,7 +345,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) int peripheral_pwm_close(peripheral_pwm_h pwm) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); return cleanup_handle(pwm); @@ -375,7 +353,7 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) int peripheral_pwm_set_period(peripheral_pwm_h pwm, uint32_t period_ns) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); int ret; @@ -391,7 +369,7 @@ int peripheral_pwm_set_period(peripheral_pwm_h pwm, uint32_t period_ns) int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_cycle_ns) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); int ret; @@ -407,7 +385,7 @@ int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_cycle_ns) int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); RETVM_IF((polarity < PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH) || (polarity > PERIPHERAL_PWM_POLARITY_ACTIVE_LOW), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid polarity parameter"); @@ -424,7 +402,7 @@ int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, bool enable) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported"); RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); static predefined_type_s types[2] = { diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c index 6e50968..12645f9 100644 --- a/src/peripheral_spi.c +++ b/src/peripheral_spi.c @@ -22,17 +22,12 @@ #include #include #include -#include #include "peripheral_io.h" #include "common.h" #include "log.h" -#define PERIPHERAL_IO_SPI_FEATURE "http://tizen.org/feature/peripheral_io.spi" - -#define SPI_FEATURE_UNKNOWN -1 -#define SPI_FEATURE_FALSE 0 -#define SPI_FEATURE_TRUE 1 +FEATURE("http://tizen.org/feature/peripheral_io.spi") /** * @brief Internal struct for spi context @@ -41,23 +36,6 @@ struct _peripheral_spi_s { int fd; }; -static int spi_feature = SPI_FEATURE_UNKNOWN; - -static bool __is_feature_supported(void) -{ - int ret = SYSTEM_INFO_ERROR_NONE; - bool feature = false; - - if (spi_feature == SPI_FEATURE_UNKNOWN) { - ret = system_info_get_platform_bool(PERIPHERAL_IO_SPI_FEATURE, &feature); - RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info"); - - spi_feature = (feature ? SPI_FEATURE_TRUE : SPI_FEATURE_FALSE); - } - - return (spi_feature == SPI_FEATURE_TRUE ? true : false); -} - static inline void cleanup_handlep(peripheral_spi_h *handle) { if (*handle != NULL) { @@ -72,7 +50,7 @@ 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 */ - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + 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"); @@ -107,7 +85,7 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) int peripheral_spi_close(peripheral_spi_h spi) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); cleanup_handlep(&spi); @@ -117,7 +95,7 @@ int peripheral_spi_close(peripheral_spi_h spi) int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF((mode < PERIPHERAL_SPI_MODE_0) || (mode > PERIPHERAL_SPI_MODE_3), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi mode parameter"); @@ -129,7 +107,7 @@ int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode) int peripheral_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF((bit_order < PERIPHERAL_SPI_BIT_ORDER_MSB) || (bit_order > PERIPHERAL_SPI_BIT_ORDER_LSB), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid bit order parameter"); @@ -141,7 +119,7 @@ int peripheral_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_ int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); int ret = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits); @@ -152,7 +130,7 @@ int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits) int peripheral_spi_set_frequency(peripheral_spi_h spi, uint32_t freq_hz) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); int ret = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq_hz); @@ -163,7 +141,7 @@ int peripheral_spi_set_frequency(peripheral_spi_h spi, uint32_t freq_hz) int peripheral_spi_read(peripheral_spi_h spi, uint8_t *data, uint32_t length) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -181,7 +159,7 @@ int peripheral_spi_read(peripheral_spi_h spi, uint8_t *data, uint32_t length) int peripheral_spi_write(peripheral_spi_h spi, uint8_t *data, uint32_t length) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -199,7 +177,7 @@ int peripheral_spi_write(peripheral_spi_h spi, uint8_t *data, uint32_t length) int peripheral_spi_transfer(peripheral_spi_h spi, uint8_t *txdata, uint8_t *rxdata, uint32_t length) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF(txdata == NULL || rxdata == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index a2e10ee..eb02c80 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -21,18 +21,13 @@ #include #include #include -#include #include #include "peripheral_io.h" #include "common.h" #include "log.h" -#define PERIPHERAL_IO_UART_FEATURE "http://tizen.org/feature/peripheral_io.uart" - -#define UART_FEATURE_UNKNOWN -1 -#define UART_FEATURE_FALSE 0 -#define UART_FEATURE_TRUE 1 +FEATURE("http://tizen.org/feature/peripheral_io.uart") #ifndef ARRAY_SIZE #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) @@ -54,23 +49,6 @@ static const int peripheral_uart_br[] = { static const int byteinfo[4] = {CS5, CS6, CS7, CS8}; -static int uart_feature = UART_FEATURE_UNKNOWN; - -static bool __is_feature_supported(void) -{ - int ret = SYSTEM_INFO_ERROR_NONE; - bool feature = false; - - if (uart_feature == UART_FEATURE_UNKNOWN) { - ret = system_info_get_platform_bool(PERIPHERAL_IO_UART_FEATURE, &feature); - RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info"); - - uart_feature = (feature ? UART_FEATURE_TRUE : UART_FEATURE_FALSE); - } - - return (uart_feature == UART_FEATURE_TRUE ? true : false); -} - static inline void cleanup_handlep(peripheral_uart_h *handle) { if (*handle != NULL) { @@ -85,7 +63,7 @@ static inline void cleanup_handlep(peripheral_uart_h *handle) */ int peripheral_uart_open(int port, peripheral_uart_h *uart) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid uart handle"); RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid port number"); @@ -143,7 +121,7 @@ static void peripheral_uart_flush(peripheral_uart_h uart) */ int peripheral_uart_close(peripheral_uart_h uart) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); peripheral_uart_flush(uart); @@ -158,7 +136,7 @@ int peripheral_uart_close(peripheral_uart_h uart) */ int peripheral_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((baud < PERIPHERAL_UART_BAUD_RATE_0) || (baud > PERIPHERAL_UART_BAUD_RATE_230400), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid baud input"); @@ -185,7 +163,7 @@ int peripheral_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_r int peripheral_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((byte_size < PERIPHERAL_UART_BYTE_SIZE_5BIT) || (byte_size > PERIPHERAL_UART_BYTE_SIZE_8BIT), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -210,7 +188,7 @@ int peripheral_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_s int peripheral_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((parity < PERIPHERAL_UART_PARITY_NONE) || (parity > PERIPHERAL_UART_PARITY_ODD), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -247,7 +225,7 @@ int peripheral_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e int peripheral_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((stop_bits < PERIPHERAL_UART_STOP_BITS_1BIT) || (stop_bits > PERIPHERAL_UART_STOP_BITS_2BIT), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -283,7 +261,7 @@ int peripheral_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_b */ int peripheral_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_software_flow_control_e sw_flow_control, peripheral_uart_hardware_flow_control_e hw_flow_control) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF((sw_flow_control < PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE) || (sw_flow_control > PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid sw_flow_control parameter"); RETVM_IF((hw_flow_control < PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE) || (hw_flow_control > PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid hw_flow_control parameter"); @@ -321,7 +299,7 @@ int peripheral_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_sof */ int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, uint32_t length) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); @@ -337,7 +315,7 @@ int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, uint32_t length) */ int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, uint32_t length) { - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); + RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "UART feature is not supported"); RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); -- 2.7.4