From: TaeminYeom Date: Mon, 27 Feb 2023 06:22:52 +0000 (+0900) Subject: Add privilege check in all public APIs X-Git-Tag: accepted/tizen/unified/20230313.022859~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e1eb7906a9c4496e7b4fa89544ae500cbf7d615a;p=platform%2Fcore%2Fapi%2Fperipheral-io.git Add privilege check in all public APIs Previous checking privilege was done by peripheral-bus daemon. After changing to use direct API, it was needed to add code checking privilege but it was not added. Change-Id: Ib893f5fe71995f3cb5e54a0dcf196057af1f09b9 Signed-off-by: TaeminYeom --- diff --git a/CMakeLists.txt b/CMakeLists.txt index b321988..3183c3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ SET(fw_name "${project_prefix}-${service}-${submodule}") PROJECT(${fw_name}) -SET(dependents "dlog glib-2.0 capi-base-common capi-system-info libudev") +SET(dependents "dlog glib-2.0 capi-base-common capi-system-info libudev cynara-client cynara-session") SET(pc_dependents "capi-base-common") SET(CMAKE_INSTALL_PREFIX ${prefix}) diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec index fd4cd35..5f10efa 100644 --- a/packaging/capi-system-peripheral-io.spec +++ b/packaging/capi-system-peripheral-io.spec @@ -16,6 +16,8 @@ BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(cynara-client) +BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(libudev) %if 0%{?gcov:1} BuildRequires: lcov diff --git a/src/common.c b/src/common.c index ed56e67..c38befa 100644 --- a/src/common.c +++ b/src/common.c @@ -1,13 +1,21 @@ -#include "common.h" #include #include #include #include +#include #include + #include +#include +#include + +#include "common.h" #define UDEV_MONITOR_POLL_TIMEOUT_MILISECONDS 100 #define UDEV_MONITOR_POLL_ITERATIONS 10 //! How many times to poll? Affects the total timeout. +#define BUFF_MAX 255 +#define UID_MAX_LENGTH 16 +#define PRIVILEGE_PERIPHERALIO "http://tizen.org/privilege/peripheralio" int peripheral_lock(const char *file_name) { @@ -45,6 +53,49 @@ bool peripheral_is_feature_supported(const char *feature_name, int *feature_stat return *feature_state == PERIPHERAL_FEATURE_TRUE; } +bool peripheral_is_privilege_supported(void) +{ + cynara *cynara = NULL; + FILE *fp = NULL; + char uid[UID_MAX_LENGTH]; + char *session = NULL; + char smack_label[BUFF_MAX] = {0, }; + int ret; + + if (cynara_initialize(&cynara, NULL) != CYNARA_API_SUCCESS) { + _E("failed to initialize cynara"); + return false; + } + + fp = fopen("/proc/self/attr/current", "r"); + if (fp != NULL) { + int ch = 0; + int idx = 0; + while (EOF != (ch = fgetc(fp))) { + smack_label[idx] = ch; + idx++; + } + fclose(fp); + } + + pid_t pid = getpid(); + session = cynara_session_from_pid(pid); + snprintf(uid, UID_MAX_LENGTH, "%d", getuid()); + uid[UID_MAX_LENGTH - 1] = '\0'; + + ret = cynara_check(cynara, smack_label, session, uid, PRIVILEGE_PERIPHERALIO); + if (session) + free(session); + if (cynara) + cynara_finish(cynara); + if (ret != CYNARA_API_ACCESS_ALLOWED) { + _E("'%s' privilege is not supported", PRIVILEGE_PERIPHERALIO); + return false; + } + + return true; +} + int peripheral_create_udev_monitor(struct udev **udev, struct udev_monitor **monitor, const char *filter_name) { struct udev_monitor *_monitor = NULL; diff --git a/src/common.h b/src/common.h index 45a1a54..42cb9a5 100644 --- a/src/common.h +++ b/src/common.h @@ -100,6 +100,7 @@ bool peripheral_is_feature_supported(const char *feature_name, int *feature_stat return feature == PERIPHERAL_FEATURE_TRUE; \ } +bool peripheral_is_privilege_supported(void); int peripheral_create_udev_monitor(struct udev **udev, struct udev_monitor **monitor, const char *filter_name); typedef bool (*UdevCompareFunc)(struct udev_device *, void *); diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c index 788d813..e4b7623 100644 --- a/src/peripheral_adc.c +++ b/src/peripheral_adc.c @@ -56,6 +56,7 @@ static inline void cleanup_handlep(peripheral_adc_h *handle) */ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -89,6 +90,7 @@ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc) */ int peripheral_adc_close(peripheral_adc_h adc) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -102,6 +104,7 @@ int peripheral_adc_close(peripheral_adc_h adc) */ int peripheral_adc_read(peripheral_adc_h adc, uint32_t *value) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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 0e8fd54..3460df7 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -84,6 +84,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) { int ret = PERIPHERAL_ERROR_NONE; + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -126,6 +127,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) */ int peripheral_gpio_close(peripheral_gpio_h gpio) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -143,6 +145,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -175,6 +178,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -272,6 +276,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -292,6 +297,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -310,6 +316,7 @@ int peripheral_gpio_unset_interrupted_cb(peripheral_gpio_h gpio) */ int peripheral_gpio_read(peripheral_gpio_h gpio, uint32_t *value) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -328,6 +335,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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 cd64453..3711613 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -83,6 +83,7 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag int ret; char path[DEV_PATH_FMT_MAX_SIZE] = {0, }; /* space for /dev/i2c-%d */ + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -120,6 +121,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) int peripheral_i2c_close(peripheral_i2c_h i2c) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -145,6 +147,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -165,6 +168,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -180,6 +184,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -196,6 +201,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -207,6 +213,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -223,6 +230,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index fe0bafd..63f5945 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -175,6 +175,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) { int ret = PERIPHERAL_ERROR_NONE; + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -241,6 +242,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) int peripheral_pwm_close(peripheral_pwm_h pwm) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -249,6 +251,7 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) int peripheral_pwm_set_period(peripheral_pwm_h pwm, uint32_t period_ns) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -265,6 +268,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -281,6 +285,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -298,6 +303,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c index 8e84a29..5049ef4 100644 --- a/src/peripheral_spi.c +++ b/src/peripheral_spi.c @@ -52,7 +52,7 @@ static inline void cleanup_handlep(peripheral_spi_h *handle) int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) { - + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -82,6 +82,7 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) int peripheral_spi_close(peripheral_spi_h spi) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -92,6 +93,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -104,6 +106,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -116,6 +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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -127,6 +131,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -138,6 +143,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -156,6 +162,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -174,6 +181,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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 3cd2e13..dcc6f73 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -101,6 +101,7 @@ static int peripheral_uart_find_devpath(int port, char *buffer, int len) */ int peripheral_uart_open_flags(int port, peripheral_open_flags_e flags, peripheral_uart_h *uart) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -180,6 +181,7 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) int peripheral_uart_flush(peripheral_uart_h uart) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -191,6 +193,7 @@ int peripheral_uart_flush(peripheral_uart_h uart) int peripheral_uart_drain(peripheral_uart_h uart) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -205,6 +208,7 @@ int peripheral_uart_drain(peripheral_uart_h uart) */ int peripheral_uart_close(peripheral_uart_h uart) { + RETVM_IF(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -220,6 +224,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -247,6 +252,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -272,6 +278,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -309,6 +316,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -337,6 +345,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -371,6 +380,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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"); @@ -387,6 +397,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(!peripheral_is_privilege_supported(), PERIPHERAL_ERROR_PERMISSION_DENIED, "Peripheral-io privilege 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");