From 1637e1997425d6586219ae43920156018dc8fbe5 Mon Sep 17 00:00:00 2001 From: Segwon Date: Thu, 16 Nov 2017 15:09:39 +0900 Subject: [PATCH] interface: do not use the 'strlen' and 'strncmp' Signed-off-by: Segwon Change-Id: I5113aff8d544f2e5411dbb3d158313c5e4dece2f --- include/interface/peripheral_interface_common.h | 5 ++ src/interface/peripheral_interface_gpio.c | 65 +++++++++---------------- src/interface/peripheral_interface_pwm.c | 24 ++++----- 3 files changed, 38 insertions(+), 56 deletions(-) diff --git a/include/interface/peripheral_interface_common.h b/include/interface/peripheral_interface_common.h index 2646cc6..8c10519 100644 --- a/include/interface/peripheral_interface_common.h +++ b/include/interface/peripheral_interface_common.h @@ -37,4 +37,9 @@ } \ } while (0) +typedef struct predefined_type { + char *type; + int len; +} predefined_type_s; + #endif /*__PERIPHERAL_INTERFACE_COMMON_H__*/ \ No newline at end of file diff --git a/src/interface/peripheral_interface_gpio.c b/src/interface/peripheral_interface_gpio.c index b8361fb..683b853 100644 --- a/src/interface/peripheral_interface_gpio.c +++ b/src/interface/peripheral_interface_gpio.c @@ -20,19 +20,13 @@ int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) { - int status; - - if (direction == PERIPHERAL_GPIO_DIRECTION_IN) - status = write(gpio->fd_direction, "in", strlen("in")); - else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_HIGH) - status = write(gpio->fd_direction, "high", strlen("high")); - else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW) - status = write(gpio->fd_direction, "low", strlen("low")); - else { - _E("Error: gpio direction is wrong\n"); - return -EIO; - } + static predefined_type_s types[3] = { + {"in", 2}, + {"high", 4}, + {"low", 3} + }; + int status = write(gpio->fd_direction, types[direction].type, types[direction].len); CHECK_ERROR(status); return 0; @@ -40,21 +34,14 @@ int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_g int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge) { - int status; - - if (edge == PERIPHERAL_GPIO_EDGE_NONE) - status = write(gpio->fd_edge, "none", strlen("none")); - else if (edge == PERIPHERAL_GPIO_EDGE_RISING) - status = write(gpio->fd_edge, "rising", strlen("rising")); - else if (edge == PERIPHERAL_GPIO_EDGE_FALLING) - status = write(gpio->fd_edge, "falling", strlen("falling")); - else if (edge == PERIPHERAL_GPIO_EDGE_BOTH) - status = write(gpio->fd_edge, "both", strlen("both")); - else { - _E("Error: gpio edge is wrong\n"); - return -EIO; - } - + static predefined_type_s types[4] = { + {"none", 4}, + {"rising", 6}, + {"falling", 7}, + {"both", 4} + }; + + int status = write(gpio->fd_edge, types[edge].type, types[edge].len); CHECK_ERROR(status); return 0; @@ -62,17 +49,12 @@ int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_g int peripheral_interface_gpio_write(peripheral_gpio_h gpio, uint32_t value) { - int status; - - if (value == 1) - status = write(gpio->fd_value, "1", strlen("1")); - else if (value == 0) - status = write(gpio->fd_value, "0", strlen("0")); - else { - _E("Error: gpio write value error \n"); - return -EIO; - } + static predefined_type_s types[2] = { + {"0", 1}, + {"1", 1} + }; + int status = write(gpio->fd_value, types[value].type, types[value].len); CHECK_ERROR(status); return 0; @@ -86,14 +68,13 @@ int peripheral_interface_gpio_read(peripheral_gpio_h gpio, uint32_t *value) len = read(gpio->fd_value, &gpio_buf, 1); CHECK_ERROR(len); - if (0 == strncmp(gpio_buf, "1", strlen("1"))) - *value = 1; - else if (0 == strncmp(gpio_buf, "0", strlen("0"))) + if (gpio_buf[0] == '0') *value = 0; - else { + else if (gpio_buf[0] == '1') + *value = 1; + else _E("Error: gpio value is error \n"); return -EIO; - } return 0; } diff --git a/src/interface/peripheral_interface_pwm.c b/src/interface/peripheral_interface_pwm.c index d01776d..1da7752 100644 --- a/src/interface/peripheral_interface_pwm.c +++ b/src/interface/peripheral_interface_pwm.c @@ -67,17 +67,12 @@ int peripheral_interface_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_ int peripheral_interface_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) { - int status; - - if (polarity == PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH) - status = write(pwm->fd_polarity, "normal", strlen("normal")); - else if (polarity == PERIPHERAL_PWM_POLARITY_ACTIVE_LOW) - status = write(pwm->fd_polarity, "inversed", strlen("inversed")); - else { - _E("Invalid pwm polarity : %d", polarity); - return -EINVAL; - } + static predefined_type_s types[2] = { + {"normal", 6}, + {"inversed", 8} + }; + int status = write(pwm->fd_polarity, types[polarity].type, types[polarity].len); CHECK_ERROR(status); return 0; @@ -85,11 +80,12 @@ int peripheral_interface_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_p int peripheral_interface_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { - int len, status; - char pwm_buf[PWM_BUF_MAX] = {0}; + static predefined_type_s types[2] = { + {"0", 1}, + {"1", 1} + }; - len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", enable); - status = write(pwm->fd_enable, pwm_buf, len); + int status = write(pwm->fd_enable, types[enable].type, types[enable].len); CHECK_ERROR(status); return 0; -- 2.7.4