mraa.c: Moved iio detection code into a function
Signed-off-by: Henry Bruce <henry.bruce@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
0049ba1378
commit
131021a629
76
src/mraa.c
76
src/mraa.c
@@ -152,38 +152,10 @@ mraa_init()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
plat_iio = (mraa_iio_info_t*) calloc(1, sizeof(mraa_iio_info_t));
|
mraa_result_t iio_result = mraa_iio_detect();
|
||||||
// Now detect IIO devices, linux only
|
if (iio_result != MRAA_SUCCESS)
|
||||||
// find how many iio devices we have if we haven't already
|
return iio_result;
|
||||||
if (num_iio_devices == 0) {
|
|
||||||
if (nftw("/sys/bus/iio/devices", &mraa_count_iio_devices, 20, FTW_PHYS) == -1) {
|
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
char name[64], filepath[64];
|
|
||||||
int fd, len, i;
|
|
||||||
plat_iio->iio_device_count = num_iio_devices;
|
|
||||||
plat_iio->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
|
|
||||||
struct _iio* device;
|
|
||||||
for (i=0; i < num_iio_devices; i++) {
|
|
||||||
device = &plat_iio->iio_devices[i];
|
|
||||||
device->num = i;
|
|
||||||
snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
|
|
||||||
fd = open(filepath, O_RDONLY);
|
|
||||||
if (fd > 0) {
|
|
||||||
len = read(fd, &name, 64);
|
|
||||||
if (len > 1) {
|
|
||||||
// remove any trailing CR/LF symbols
|
|
||||||
name[strcspn(name, "\r\n")] = '\0';
|
|
||||||
len = strlen(name);
|
|
||||||
// use strndup
|
|
||||||
device->name = malloc((sizeof(char) * len) + sizeof(char));
|
|
||||||
strncpy(device->name, name, len+1);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
|
syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -226,6 +198,46 @@ mraa_set_priority(const unsigned int priority)
|
|||||||
return sched_setscheduler(0, SCHED_RR, &sched_s);
|
return sched_setscheduler(0, SCHED_RR, &sched_s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_iio_detect()
|
||||||
|
{
|
||||||
|
plat_iio = (mraa_iio_info_t*) calloc(1, sizeof(mraa_iio_info_t));
|
||||||
|
plat_iio->iio_device_count = num_iio_devices;
|
||||||
|
// Now detect IIO devices, linux only
|
||||||
|
// find how many iio devices we have if we haven't already
|
||||||
|
if (num_iio_devices == 0) {
|
||||||
|
if (nftw("/sys/bus/iio/devices", &mraa_count_iio_devices, 20, FTW_PHYS) == -1) {
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char name[64], filepath[64];
|
||||||
|
int fd, len, i;
|
||||||
|
plat_iio->iio_device_count = num_iio_devices;
|
||||||
|
plat_iio->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
|
||||||
|
struct _iio* device;
|
||||||
|
for (i=0; i < num_iio_devices; i++) {
|
||||||
|
device = &plat_iio->iio_devices[i];
|
||||||
|
device->num = i;
|
||||||
|
snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
|
||||||
|
fd = open(filepath, O_RDONLY);
|
||||||
|
if (fd > 0) {
|
||||||
|
len = read(fd, &name, 64);
|
||||||
|
if (len > 1) {
|
||||||
|
// remove any trailing CR/LF symbols
|
||||||
|
name[strcspn(name, "\r\n")] = '\0';
|
||||||
|
len = strlen(name);
|
||||||
|
// use strndup
|
||||||
|
device->name = malloc((sizeof(char) * len) + sizeof(char));
|
||||||
|
strncpy(device->name, name, len+1);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_setup_mux_mapped(mraa_pin_t meta)
|
mraa_setup_mux_mapped(mraa_pin_t meta)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user