interface: do not use the 'strlen' and 'strncmp' 27/160427/2
authorSegwon <segwon.han@samsung.com>
Thu, 16 Nov 2017 06:09:39 +0000 (15:09 +0900)
committerSegwon <segwon.han@samsung.com>
Thu, 16 Nov 2017 06:47:51 +0000 (15:47 +0900)
Signed-off-by: Segwon <segwon.han@samsung.com>
Change-Id: I5113aff8d544f2e5411dbb3d158313c5e4dece2f

include/interface/peripheral_interface_common.h
src/interface/peripheral_interface_gpio.c
src/interface/peripheral_interface_pwm.c

index 2646cc6..8c10519 100644 (file)
@@ -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
index b8361fb..683b853 100644 (file)
 
 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;
 }
index d01776d..1da7752 100644 (file)
@@ -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;