From 3b5ad1c0fb64a43d21909b00d84e31eeaf79782d Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 13 Jun 2017 18:57:05 +0900 Subject: [PATCH] Add log messages to API function - Add log message for failure cases. - Improve parameter validation with log messages. Change-Id: I21f99c8fbbe1f50032da8933dbb92b6b21faad11 Signed-off-by: Hyeongsik Min --- src/peripheral_adc.c | 4 +- src/peripheral_gpio.c | 79 ++++++++++++++++---------------- src/peripheral_i2c.c | 57 ++++++++++++++++-------- src/peripheral_pwm.c | 90 ++++++++++++++++++++++++++++--------- src/peripheral_spi.c | 121 +++++++++++++++++++++++++++++++++++++++----------- src/peripheral_uart.c | 81 +++++++++++++++++++++++++-------- 6 files changed, 306 insertions(+), 126 deletions(-) diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c index 8df1d75..5997ad1 100644 --- a/src/peripheral_adc.c +++ b/src/peripheral_adc.c @@ -20,12 +20,12 @@ int peripheral_adc_read(unsigned int device, unsigned int channel, int *data) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; adc_proxy_init(); ret = peripheral_gdbus_adc_read(device, channel, data); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Failed to read, ret : %d", ret); return ret; diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index e49477c..6c64b98 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -115,7 +115,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) int ret = PERIPHERAL_ERROR_NONE; peripheral_gpio_h handle; - assert(gpio_pin >= 0); + RETVM_IF(gpio_pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin number"); /* Initialize */ handle = (peripheral_gpio_h)calloc(1, sizeof(struct _peripheral_gpio_s)); @@ -131,6 +131,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) ret = peripheral_gdbus_gpio_open(handle); if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to open the gpio pin, ret : %d", ret); free(handle); handle = NULL; } @@ -148,14 +149,12 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); /* call gpio_close */ ret = peripheral_gdbus_gpio_close(gpio); - if (ret) - ret = TIZEN_ERROR_IO_ERROR; + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to close the gpio pin, ret : %d", ret); gpio_proxy_deinit(); free(gpio); @@ -171,11 +170,11 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_get_direction(gpio, direction); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get direction of the gpio pin, ret : %d", ret); return ret; } @@ -188,15 +187,14 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; - - if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); + RETVM_IF(direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid direction input"); /* call gpio_set_direction */ ret = peripheral_gdbus_gpio_set_direction(gpio, direction); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set gpio direction, ret : %d", ret); return ret; } @@ -208,12 +206,12 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, int *value) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); /* call gpio_read */ ret = peripheral_gdbus_gpio_read(gpio, value); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read value of the gpio pin, ret : %d", ret); return ret; } @@ -225,12 +223,12 @@ int peripheral_gpio_write(peripheral_gpio_h gpio, int value) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); /* call gpio_write */ ret = peripheral_gdbus_gpio_write(gpio, value); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write to the gpio pin, ret : %d", ret); return ret; } @@ -242,11 +240,11 @@ int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_get_edge_mode(gpio, edge); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get edge mode of the gpio pin, ret : %d", ret); return ret; } @@ -258,15 +256,14 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; - - if (edge > PERIPHERAL_GPIO_EDGE_BOTH) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); + RETVM_IF(edge > PERIPHERAL_GPIO_EDGE_BOTH, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid edge input"); /* call gpio_set_edge_mode */ ret = peripheral_gdbus_gpio_set_edge_mode(gpio, edge); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set edge mode of the gpio pin, ret : %d", ret); return ret; } @@ -278,16 +275,18 @@ int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, vo { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_register_cb(gpio, callback, user_data); - if (ret != PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to register cb, ret : %d", ret); return ret; + } /* set isr */ ret = peripheral_gpio_isr_set(gpio->pin, callback, user_data); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to register gpio isr, ret : %d", ret); return ret; } @@ -299,13 +298,13 @@ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_unregister_cb(gpio); - if (ret != PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to unregister gpio isr, ret : %d", ret); return ret; + } /* clean up isr */ ret = peripheral_gpio_isr_unset(gpio->pin); @@ -318,9 +317,7 @@ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio) */ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin) { - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); *gpio_pin = gpio->pin; diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 0af6dc8..4035eb6 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -39,8 +39,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; - if (bus < 0) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(bus < 0 || address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); @@ -54,7 +53,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) ret = peripheral_gdbus_i2c_open(handle, bus, address); if (ret != PERIPHERAL_ERROR_NONE) { - _E("[PERIPHERAL] I2C init error\n"); + _E("Failed to open i2c communication, ret : %d", ret); free(handle); handle = NULL; } @@ -65,11 +64,13 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) int peripheral_i2c_close(peripheral_i2c_h i2c) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); ret = peripheral_gdbus_i2c_close(i2c); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to close i2c communcation, ret : %d", ret); i2c_proxy_deinit(); free(i2c); @@ -80,31 +81,41 @@ int peripheral_i2c_close(peripheral_i2c_h i2c) int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); ret = peripheral_gdbus_i2c_read(i2c, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read data from device, ret : %d", ret); return ret; } int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) { - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_gdbus_i2c_write(i2c, data, length); + ret = peripheral_gdbus_i2c_write(i2c, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write data to device, ret : %d", ret); + + return ret; } int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; uint16_t w_data, dummy = 0; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, 0x0, I2C_SMBUS_BYTE, dummy, &w_data); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Failed to read, ret : %d", ret); *data = (uint8_t)w_data; @@ -117,11 +128,11 @@ int peripheral_i2c_write_byte(peripheral_i2c_h i2c, uint8_t data) int ret = PERIPHERAL_ERROR_NONE; uint16_t dummy = 0; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, dummy, I2C_SMBUS_BYTE, (uint16_t)data, &dummy); - if (ret < PERIPHERAL_ERROR_NONE) - _E("Failed to read, ret : %d", ret); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write, ret : %d", ret); return ret; } @@ -131,8 +142,10 @@ int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t address, uin int ret; uint16_t w_data, dummy = 0; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, address, I2C_SMBUS_BYTE_DATA, dummy, &w_data); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); *data = (uint8_t)w_data; @@ -145,8 +158,10 @@ int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t address, ui int ret; uint16_t dummy = 0; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, address, I2C_SMBUS_BYTE_DATA, (uint16_t)data, &dummy); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); return ret; @@ -157,8 +172,10 @@ int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t address, uin int ret; uint16_t dummy = 0, data_out; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, address, I2C_SMBUS_WORD_DATA, dummy, &data_out); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); *data = data_out; @@ -171,8 +188,10 @@ int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t address, ui int ret; uint16_t dummy = 0; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, address, I2C_SMBUS_WORD_DATA, data, &dummy); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); return ret; diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 7e98766..e3718ee 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -29,7 +29,7 @@ int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm) peripheral_pwm_h handle; int ret = PERIPHERAL_ERROR_NONE; - if (device < 0 || channel < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(device < 0 || channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); /* Initialize */ handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s)); @@ -44,7 +44,7 @@ int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm) ret = peripheral_gdbus_pwm_open(handle, device, channel); if (ret != PERIPHERAL_ERROR_NONE) { - _E("PWM open error (%d, %d)", device, channel); + _E("Failed to open PWM device : %d, channel : %d", device, channel); free(handle); handle = NULL; } @@ -57,10 +57,10 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) { int ret = PERIPHERAL_ERROR_NONE; - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0) - _E("Failed to close PWM device, continuing anyway"); + _E("Failed to close PWM device, continuing anyway, ret : %d", ret); pwm_proxy_deinit(); free(pwm); @@ -70,56 +70,106 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_period(pwm, period); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_set_period(pwm, period); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set period, ret : %d", ret); + + return ret; } int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_period(pwm, period); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_period(pwm, period); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get period, ret : %d", ret); + + return ret; } int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set duty cycle, ret : %d", ret); + + return ret; } int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get duty cycle, ret : %d", ret); + + return ret; } int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_polarity(pwm, polarity); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + RETVM_IF(polarity > PERIPHERAL_PWM_POLARITY_INVERSED, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid polarity parameter"); + + ret = peripheral_gdbus_pwm_set_polarity(pwm, polarity); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set polarity, ret : %d", ret); + + return ret; } int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_polarity(pwm, polarity); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_polarity(pwm, polarity); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get polarity, ret : %d", ret); + + return ret; } int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_enable(pwm, enable); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_set_enable(pwm, enable); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set enable, ret : %d", ret); + + return ret; } int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_enable(pwm, enable); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_enable(pwm, enable); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get enable, ret : %d", ret); + + return ret; } diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c index 661b68a..eff8f23 100644 --- a/src/peripheral_spi.c +++ b/src/peripheral_spi.c @@ -30,7 +30,7 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) peripheral_spi_h handle; int ret = PERIPHERAL_ERROR_NONE; - if (bus < 0 || cs < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(bus < 0 || cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); /* Initialize */ handle = (peripheral_spi_h)calloc(1, sizeof(struct _peripheral_spi_s)); @@ -58,10 +58,11 @@ int peripheral_spi_close(peripheral_spi_h spi) { int ret = PERIPHERAL_ERROR_NONE; - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - if ((ret = peripheral_gdbus_spi_close(spi)) < 0) - _E("Failed to close SPI device, continuing anyway"); + ret = peripheral_gdbus_spi_close(spi); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to close SPI device, continuing anyway, ret : %d", ret); spi_proxy_deinit(); free(spi); @@ -71,77 +72,145 @@ int peripheral_spi_close(peripheral_spi_h spi) int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_spi_set_mode(spi, mode); + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + RETVM_IF(mode > PERIPHERAL_SPI_MODE_3, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid spi mode parameter"); + + ret = peripheral_gdbus_spi_set_mode(spi, mode); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set mode, ret : %d", ret); + + return ret; } int peripheral_spi_get_mode(peripheral_spi_h spi, peripheral_spi_mode_e *mode) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_mode(spi, mode); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get spi mode, ret : %d", ret); - return peripheral_gdbus_spi_get_mode(spi, mode); + return ret; } int peripheral_spi_set_lsb_first(peripheral_spi_h spi, bool lsb) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_set_lsb_first(spi, lsb); + ret = peripheral_gdbus_spi_set_lsb_first(spi, lsb); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set lsb first, ret : %d", ret); + + return ret; } int peripheral_spi_get_lsb_first(peripheral_spi_h spi, bool *lsb) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_spi_get_lsb_first(spi, lsb); + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_lsb_first(spi, lsb); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get lsb first, ret : %d", ret); + + return ret; } int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, unsigned char bits) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_set_bits(spi, bits); + ret = peripheral_gdbus_spi_set_bits(spi, bits); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set bits per word, ret : %d", ret); + + return ret; } int peripheral_spi_get_bits_per_word(peripheral_spi_h spi, unsigned char *bits) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_bits(spi, bits); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get bits per word, ret : %d", ret); - return peripheral_gdbus_spi_get_bits(spi, bits); + return ret; } int peripheral_spi_set_frequency(peripheral_spi_h spi, unsigned int freq) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_set_frequency(spi, freq); + ret = peripheral_gdbus_spi_set_frequency(spi, freq); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set frequency, ret : %d", ret); + + return ret; } int peripheral_spi_get_frequency(peripheral_spi_h spi, unsigned int *freq) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_spi_get_frequency(spi, freq); + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_frequency(spi, freq); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get spi frequency, ret : %d", ret); + + return ret; } int peripheral_spi_read(peripheral_spi_h spi, unsigned char *data, int length) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_read(spi, data, length); + ret = peripheral_gdbus_spi_read(spi, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read from spi device, ret : %d", ret); + + return ret; } int peripheral_spi_write(peripheral_spi_h spi, unsigned char *data, int length) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_write(spi, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write to spi device, ret : %d", ret); - return peripheral_gdbus_spi_write(spi, data, length); + return ret; } int peripheral_spi_read_write(peripheral_spi_h spi, unsigned char *txdata, unsigned char *rxdata, int length) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_read_write(spi, txdata, rxdata, length); + ret = peripheral_gdbus_spi_read_write(spi, txdata, rxdata, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read and write, ret : %d", ret); + + return ret; } diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index 66d96f0..1b18c17 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -30,10 +30,9 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) { peripheral_uart_h handle; - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (port < 0) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid port number"); handle = (peripheral_uart_h)calloc(1, sizeof(struct _peripheral_uart_s)); @@ -47,7 +46,7 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) ret = peripheral_gdbus_uart_open(handle, port); if (ret != PERIPHERAL_ERROR_NONE) { - _E("[PERIPHERAL] UART open error\n"); + _E("Failed to open uart port, ret : %d", ret); free(handle); handle = NULL; } @@ -61,11 +60,13 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) */ int peripheral_uart_close(peripheral_uart_h uart) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); ret = peripheral_gdbus_uart_close(uart); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to close uart communication, continuing anyway, ret : %d", ret); uart_proxy_deinit(); free(uart); @@ -80,9 +81,15 @@ int peripheral_uart_close(peripheral_uart_h uart) */ int peripheral_uart_flush(peripheral_uart_h uart) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_uart_flush(uart); + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + + ret = peripheral_gdbus_uart_flush(uart); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to flush, ret : %d", ret); + + return ret; } /** @@ -90,9 +97,17 @@ int peripheral_uart_flush(peripheral_uart_h uart) */ int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(baud > PERIPHERAL_UART_BAUDRATE_230400, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid baud input"); + + ret = peripheral_gdbus_uart_set_baudrate(uart, baud); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set baudrate, ret : %d", ret); - return peripheral_gdbus_uart_set_baudrate(uart, baud); + return ret; } /** @@ -100,9 +115,19 @@ int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrat */ int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(bytesize > PERIPHERAL_UART_BYTESIZE_8BIT + || parity > PERIPHERAL_UART_PARITY_ODD + || stopbits > PERIPHERAL_UART_STOPBITS_2BIT, + PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_gdbus_uart_set_mode(uart, bytesize, parity, stopbits); + ret = peripheral_gdbus_uart_set_mode(uart, bytesize, parity, stopbits); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set uart mode, ret : %d", ret); + + return ret; } /** @@ -110,9 +135,15 @@ int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e */ int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_uart_set_flowcontrol(uart, xonxoff, rtscts); + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + + ret = peripheral_gdbus_uart_set_flowcontrol(uart, xonxoff, rtscts); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set flocontrol, ret : %d", ret); + + return ret; } /** @@ -120,9 +151,16 @@ int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool r */ int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + ret = peripheral_gdbus_uart_read(uart, data, length); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to read from uart device, ret : %d", ret); - return peripheral_gdbus_uart_read(uart, data, length); + return ret; } /** @@ -130,7 +168,14 @@ int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length) */ int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, int length) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_gdbus_uart_write(uart, data, length); + ret = peripheral_gdbus_uart_write(uart, data, length); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to write to uart device, ret : %d", ret); + + return ret; } -- 2.7.4