refactoring: beautify snprintf formats 00/260500/3
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 25 Jun 2021 14:07:07 +0000 (16:07 +0200)
committerAdrian Szyndela <adrian.s@samsung.com>
Mon, 28 Jun 2021 09:34:48 +0000 (11:34 +0200)
Change-Id: I40fe25dd18dc87e38dd522b6b1614ae7fd6a6465

src/peripheral_adc.c
src/peripheral_gpio.c
src/peripheral_i2c.c
src/peripheral_pwm.c
src/peripheral_spi.c
src/peripheral_uart.c

index eb69971..85908ae 100644 (file)
 
 FEATURE("http://tizen.org/feature/peripheral_io.adc")
 
+/* Path format for device file */
+#define DEV_PATH_BASE(device, channel) ("/sys/bus/iio/devices/iio:device" device "/in_voltage" channel "_raw")
+#define DEV_PATH_FMT_MAX_SIZE sizeof(DEV_PATH_BASE(MAX_d_FMT, MAX_d_FMT))
+#define DEV_PATH_FMT DEV_PATH_BASE("%d", "%d")
+
 #define ADC_BUFFER_MAX 64
 
 /**
@@ -63,11 +68,10 @@ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc)
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
 
-#define DEV_PATH_BASE(device, channel) ("/sys/bus/iio/devices/iio:device" device "/in_voltage" channel "_raw")
        /* space for /sys/bus/iio/devices/iio:device%d/in_voltage%d_raw */
-       char path[sizeof(DEV_PATH_BASE(MAX_d_FMT, MAX_d_FMT))] = {0, };
+       char path[DEV_PATH_FMT_MAX_SIZE] = {0, };
 
-       snprintf(path, sizeof path, DEV_PATH_BASE("%d", "%d"), device, channel);
+       snprintf(path, sizeof path, DEV_PATH_FMT, device, channel);
        handle->fd = open(path, O_RDONLY | O_CLOEXEC);
        CHECK_ERROR(handle->fd < 0);
 
index af3b5ce..cb99fba 100644 (file)
 
 FEATURE("http://tizen.org/feature/peripheral_io.gpio")
 
+/* Path format for device file */
+#define DEV_PATH_BASE(pin, file) ("/sys/class/gpio/gpio" pin "/" file)
+/* direction is the longest file name of all accesses in the device directory.
+   The other are: edge and value.
+ */
+#define DEV_PATH_FMT_MAX_SIZE sizeof(DEV_PATH_BASE(MAX_d_FMT, "direction"))
+#define DEV_PATH_FMT(file) DEV_PATH_BASE("%d", file)
+
+/* Format for gpio name */
+#define GPIO_NAME_BASE "gpio"
+#define GPIO_NAME_FMT_MAX_SIZE sizeof(GPIO_NAME_BASE MAX_d_FMT)
+#define GPIO_NAME_FMT (GPIO_NAME_BASE "%d")
+
 #define GPIO_STRUCTURE_VERMAGIC 13712
 #define GPIO_CALLBACK_STRUCTURE_VERMAGIC 14469
 
@@ -69,7 +82,7 @@ struct _peripheral_gpio_s {
 #define GPIO_BASE "gpio"
 
 typedef struct FilterData {
-       char gpio_name[sizeof(GPIO_BASE MAX_d_FMT)];
+       char gpio_name[GPIO_NAME_FMT_MAX_SIZE];
 } FilterData;
 
 static bool __filter_device(struct udev_device *device, void *data)
@@ -111,7 +124,7 @@ static int peripheral_gpio_export(int pin)
        }
 
        FilterData filter;
-       snprintf(filter.gpio_name, sizeof filter.gpio_name, GPIO_BASE "%d", pin);
+       snprintf(filter.gpio_name, sizeof filter.gpio_name, GPIO_NAME_FMT, pin);
 
        ret = peripheral_wait_for_udev(monitor, __filter_device, &filter);
        if (ret < 0) {
@@ -255,23 +268,22 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio)
 
        handle->pin = gpio_pin;
 
-#define DEV_PATH(pin) ("/sys/class/gpio/gpio" pin "/direction")
        /* space for /sys/class/gpio/gpio%d/direction,
           which is larger than /sys/class/gpio/gpio%d/edge and /sys/class/gpio/gpio%d/value
         */
-       char path[sizeof(DEV_PATH(MAX_d_FMT))] = {0, };
-       snprintf(path, sizeof path, "/sys/class/gpio/gpio%d/direction", handle->pin);
+       char path[DEV_PATH_FMT_MAX_SIZE] = {0, };
+       snprintf(path, sizeof path, DEV_PATH_FMT("direction"), handle->pin);
        handle->fd_direction = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_direction < 0);
 
        TRY_FLOCK(ret, handle->fd_direction, LOCK_EX | LOCK_NB, "gpio pin %d", gpio_pin);
        CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE);
 
-       snprintf(path, sizeof path, "/sys/class/gpio/gpio%d/edge", handle->pin);
+       snprintf(path, sizeof path, DEV_PATH_FMT("edge"), handle->pin);
        handle->fd_edge = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_edge < 0);
 
-       snprintf(path, sizeof path, "/sys/class/gpio/gpio%d/value", handle->pin);
+       snprintf(path, sizeof path, DEV_PATH_FMT("value"), handle->pin);
        handle->fd_value = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_value < 0);
 
index c3b69f3..5ce29a6 100644 (file)
 
 FEATURE("http://tizen.org/feature/peripheral_io.i2c")
 
+/* Path format for device file */
+#define DEV_PATH_BASE "/dev/i2c-"
+#define DEV_PATH_FMT_MAX_SIZE sizeof(DEV_PATH_BASE MAX_d_FMT)
+#define DEV_PATH_FMT (DEV_PATH_BASE "%d")
+
 #define I2C_SLAVE      0x0703  /* Use this slave address */
 #define I2C_SMBUS      0x0720  /* SMBus transfer */
 
@@ -78,8 +83,7 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag
        int ret;
        int lock_type = LOCK_EX;
 
-#define DEV_PATH_BASE "/dev/i2c-"
-       char path[sizeof(DEV_PATH_BASE MAX_d_FMT)] = {0, };     /* space for /dev/i2c-%d */
+       char path[DEV_PATH_FMT_MAX_SIZE] = {0, };       /* space for /dev/i2c-%d */
 
        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");
@@ -95,7 +99,7 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
 
-       snprintf(path, sizeof path, DEV_PATH_BASE "%d", bus);
+       snprintf(path, sizeof path, DEV_PATH_FMT, bus);
        handle->fd = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd < 0);
 
index 82eff5f..10e31ea 100644 (file)
@@ -28,6 +28,33 @@ FEATURE("http://tizen.org/feature/peripheral_io.pwm")
 
 #define PWM_BUF_MAX 16
 
+/* Path format for exporting pins on a chip */
+#define EXPORT_PATH(chip) ("/sys/class/pwm/pwmchip" chip "/export")
+#define EXPORT_PATH_FMT EXPORT_PATH("%d")
+#define EXPORT_PATH_FMT_MAX_SIZE sizeof(EXPORT_PATH(MAX_d_FMT))
+
+/* Path format for unexporting pins on a chip */
+#define UNEXPORT_PATH(chip) ("/sys/class/pwm/pwmchip" chip "/unexport")
+#define UNEXPORT_PATH_FMT EXPORT_PATH("%d")
+#define UNEXPORT_PATH_FMT_MAX_SIZE sizeof(EXPORT_PATH(MAX_d_FMT))
+
+/* Format for pwmchip name */
+#define PWMCHIP_BASE "pwmchip"
+#define PWMCHIP_FMT_MAX_SIZE sizeof(PWMCHIP_BASE MAX_d_FMT)
+#define PWMCHIP_FMT (PWMCHIP_BASE "%d")
+
+/* Format for pwm name */
+#define PWM_BASE "pwm"
+#define PWM_FMT_MAX_SIZE sizeof(PWM_BASE MAX_d_FMT)
+#define PWM_FMT (PWM_BASE "%d")
+
+/* Format for path to files inside pin directory */
+#define PWM_PATH_BASE(chip, pin, file) ("/sys/class/pwm/pwmchip" chip "/pwm" pin "/" file)
+/* duty_cycle is the longest file name of the accesses files. The other files are:
+   period, polarity, enable */
+#define PWM_PATH_FMT_MAX_SIZE sizeof(PWM_PATH_BASE(MAX_d_FMT, MAX_d_FMT, "duty_cycle"))
+#define PWM_PATH_FMT(file) PWM_PATH_BASE("%d", "%d", file)
+
 /**
  * @brief Internal struct for pwm context
  */
@@ -40,13 +67,10 @@ struct _peripheral_pwm_s {
        int fd_enable;
 };
 
-#define PWMCHIP_BASE "pwmchip"
-#define PWM_BASE "pwm"
-
 typedef struct FilterData
 {
-       char pwmchip_name[sizeof(PWMCHIP_BASE MAX_d_FMT)]; /* space for pwmchip%d */
-       char pwm_name[sizeof(PWM_BASE MAX_d_FMT)]; /* space for pwm%d */
+       char pwmchip_name[PWMCHIP_FMT_MAX_SIZE]; /* space for pwmchip%d */
+       char pwm_name[PWM_FMT_MAX_SIZE]; /* space for pwm%d */
 } FilterData;
 
 static bool __filter_device(struct udev_device *device, void *data)
@@ -68,8 +92,7 @@ static int peripheral_pwm_export(int chip, int pin)
        RETVM_IF(chip < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm chip");
        RETVM_IF(pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm pin");
 
-#define EXPORT_PATH(chip) ("/sys/class/pwm/pwmchip" chip "/export")
-       char path[sizeof EXPORT_PATH(MAX_d_FMT)] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */
+       char path[EXPORT_PATH_FMT_MAX_SIZE] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */
        struct udev *udev = NULL;
        struct udev_monitor *monitor = NULL;
        int ret;
@@ -81,7 +104,7 @@ static int peripheral_pwm_export(int chip, int pin)
                goto out;
        };
 
-       snprintf(path, sizeof path, EXPORT_PATH("%d"), chip);
+       snprintf(path, sizeof path, EXPORT_PATH_FMT, chip);
        ret = peripheral_write_pin_to_file(path, pin);
        if (ret != 0) {
                _E("pwm: export pin %d failed with error %d", pin, ret);
@@ -89,8 +112,8 @@ static int peripheral_pwm_export(int chip, int pin)
        }
 
        FilterData filter;
-       snprintf(filter.pwmchip_name, sizeof filter.pwmchip_name, PWMCHIP_BASE "%d", chip);
-       snprintf(filter.pwm_name, sizeof filter.pwm_name, PWM_BASE "%d", pin);
+       snprintf(filter.pwmchip_name, sizeof filter.pwmchip_name, PWMCHIP_FMT, chip);
+       snprintf(filter.pwm_name, sizeof filter.pwm_name, PWM_FMT, pin);
 
        ret = peripheral_wait_for_udev(monitor, __filter_device, &filter);
        if (ret != 0) {
@@ -108,9 +131,8 @@ out:
 
 static int peripheral_pwm_lock_export(int chip)
 {
-       char path[sizeof EXPORT_PATH(MAX_d_FMT)] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */
-       snprintf(path, sizeof path, EXPORT_PATH("%d"), chip);
-
+       char path[EXPORT_PATH_FMT_MAX_SIZE] = {0, }; /* space for /sys/class/pwm/pwmchip%d/export */
+       snprintf(path, sizeof path, EXPORT_PATH_FMT, chip);
        return peripheral_lock(path);
 }
 
@@ -132,9 +154,8 @@ static inline int cleanup_handle(peripheral_pwm_h handle)
        if (pin < 0)
                return PERIPHERAL_ERROR_NONE;
 
-#define UNEXPORT_PATH(chip) ("/sys/class/pwm/pwmchip" chip "/unexport")
-       char path[sizeof UNEXPORT_PATH(MAX_d_FMT)] = {0, }; /* space for /sys/class/pwm/pwmchip%d/unexport */
-       snprintf(path, sizeof path, UNEXPORT_PATH("%d"), chip);
+       char path[UNEXPORT_PATH_FMT_MAX_SIZE] = {0}; /* space for /sys/class/pwm/pwmchip%d/unexport */
+       snprintf(path, sizeof path, UNEXPORT_PATH_FMT, chip);
 
        int ret = peripheral_write_pin_to_file(path, pin);
        if (ret != 0) {
@@ -160,14 +181,13 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
 
        __attribute__ ((cleanup(peripheral_unlockp))) int lock = -1;
        __attribute__ ((cleanup(cleanup_handlep))) peripheral_pwm_h handle = NULL;
-#define PWM_PATH_BASE(chip, pin, file) ("/sys/class/pwm/pwmchip" chip "/pwm" pin "/" file)
        /* space for /sys/class/pwm/pwmchip%d/pwm%d/duty_cycle
           which is longer than:
                     /sys/class/pwm/pwmchip%d/pwm%d/period
                     /sys/class/pwm/pwmchip%d/pwm%d/polarity
                     /sys/class/pwm/pwmchip%d/pwm%d/enable
        */
-       char path[sizeof (PWM_PATH_BASE(MAX_d_FMT, MAX_d_FMT, "duty_cycle"))] = {0, };
+       char path[PWM_PATH_FMT_MAX_SIZE] = {0, };
 
        /* Initialize */
        handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s));
@@ -196,22 +216,22 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
        handle->chip = chip;
        handle->pin = pin;
 
-       snprintf(path, sizeof path, PWM_PATH_BASE("%d", "%d", "period"), chip, pin);
+       snprintf(path, sizeof path, PWM_PATH_FMT("period"), chip, pin);
        handle->fd_period = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_period < 0);
 
        TRY_FLOCK(ret, handle->fd_period, LOCK_EX | LOCK_NB, "pwm: chip %d, pin %d", chip, pin);
        CHECK_ERROR(ret != PERIPHERAL_ERROR_NONE);
 
-       snprintf(path, sizeof path, PWM_PATH_BASE("%d", "%d", "duty_cycle"), chip, pin);
+       snprintf(path, sizeof path, PWM_PATH_FMT("duty_cycle"), chip, pin);
        handle->fd_duty_cycle = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_duty_cycle < 0);
 
-       snprintf(path, sizeof path, PWM_PATH_BASE("%d", "%d", "polarity"), chip, pin);
+       snprintf(path, sizeof path, PWM_PATH_FMT("polarity"), chip, pin);
        handle->fd_polarity = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_polarity < 0);
 
-       snprintf(path, sizeof path, PWM_PATH_BASE("%d", "%d", "enable"), chip, pin);
+       snprintf(path, sizeof path, PWM_PATH_FMT("enable"), chip, pin);
        handle->fd_enable = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_enable < 0);
 
index 29b6122..53270ba 100644 (file)
 
 FEATURE("http://tizen.org/feature/peripheral_io.spi")
 
+/* Path format for device file */
+#define DEV_PATH_BASE(bus, cs) "/dev/spidev" bus "." cs
+#define DEV_PATH_FMT_MAX_SIZE sizeof(DEV_PATH_BASE(MAX_d_FMT, MAX_d_FMT))
+#define DEV_PATH_FMT DEV_PATH_BASE("%d", "%d")
+
 /**
  * @brief Internal struct for spi context
  */
@@ -47,8 +52,7 @@ static inline void cleanup_handlep(peripheral_spi_h *handle)
 
 int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
 {
-#define DEV_PATH_BASE(bus, cs) "/dev/spidev" bus "." cs
-       char path[sizeof(DEV_PATH_BASE(MAX_d_FMT, MAX_d_FMT))] = {0, }; /* space for /dev/spidev%d.%d */
+       char path[DEV_PATH_FMT_MAX_SIZE] = {0, }; /* space for /dev/spidev%d.%d */
 
        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");
@@ -63,7 +67,7 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
 
-       snprintf(path, sizeof path, DEV_PATH_BASE("%d","%d"), bus, cs);
+       snprintf(path, sizeof path, DEV_PATH_FMT, bus, cs);
        handle->fd = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd < 0);
 
index c5cce8e..a354d2e 100644 (file)
 
 FEATURE("http://tizen.org/feature/peripheral_io.uart")
 
+/* Path format for device file.
+   For now, one of
+               /dev/ttyS,
+               /dev/ttyAMA,
+               /dev/ttySAC,
+   is taken as the device file name base, depending on availability.
+   This is subject to change.
+ */
+#define DEV_PATH_FMT_MAX_SIZE sizeof("/dev/ttyXXX" MAX_d_FMT)
+#define DEV_PATH_FMT "%s%d"
+
 /**
  * @brief Internal struct for uart context
  */
@@ -77,10 +88,10 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart)
        };
 
        size_t index;
-       char path[sizeof("/dev/ttyABC" MAX_d_FMT)] = {0, }; /* space for /dev/ttyXXX%d */
+       char path[DEV_PATH_FMT_MAX_SIZE] = {0, }; /* space for /dev/ttyXXX%d */
 
        for (index = 0; index < ARRAY_SIZE(sysfs_uart_path); index++) {
-               snprintf(path, sizeof path, "%s%d", sysfs_uart_path[index], port);
+               snprintf(path, sizeof path, DEV_PATH_FMT, sysfs_uart_path[index], port);
                if (access(path, F_OK) == 0)
                        break;
        }