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
/**
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);
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
#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)
}
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) {
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);
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 */
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");
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);
#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
*/
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)
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;
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);
}
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) {
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);
}
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) {
__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));
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);
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
*/
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");
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);
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
*/
};
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;
}