iio.c: run clang format on iio.c
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
150
src/iio/iio.c
150
src/iio/iio.c
@@ -32,8 +32,8 @@
|
|||||||
#define MAX_SIZE 128
|
#define MAX_SIZE 128
|
||||||
#define IIO_DEVICE "iio:device"
|
#define IIO_DEVICE "iio:device"
|
||||||
#define IIO_SCAN_ELEM "scan_elements"
|
#define IIO_SCAN_ELEM "scan_elements"
|
||||||
#define IIO_SLASH_DEV "/dev/"IIO_DEVICE
|
#define IIO_SLASH_DEV "/dev/" IIO_DEVICE
|
||||||
#define IIO_SYSFS_DEVICE "/sys/bus/iio/devices/"IIO_DEVICE
|
#define IIO_SYSFS_DEVICE "/sys/bus/iio/devices/" IIO_DEVICE
|
||||||
#define IIO_EVENTS "events"
|
#define IIO_EVENTS "events"
|
||||||
|
|
||||||
mraa_iio_context
|
mraa_iio_context
|
||||||
@@ -70,7 +70,7 @@ mraa_iio_get_channel_count(mraa_iio_context dev)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_iio_get_channel_data(mraa_iio_context dev)
|
mraa_iio_get_channel_data(mraa_iio_context dev)
|
||||||
{
|
{
|
||||||
const struct dirent *ent;
|
const struct dirent* ent;
|
||||||
DIR* dir;
|
DIR* dir;
|
||||||
int chan_num = 0;
|
int chan_num = 0;
|
||||||
char buf[MAX_SIZE];
|
char buf[MAX_SIZE];
|
||||||
@@ -89,14 +89,14 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
|
|||||||
if (dir != NULL) {
|
if (dir != NULL) {
|
||||||
while ((ent = readdir(dir)) != NULL) {
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
|
if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
|
||||||
chan_num++;
|
chan_num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dev->chan_num = chan_num;
|
dev->chan_num = chan_num;
|
||||||
//no need proceed if no channel found
|
// no need proceed if no channel found
|
||||||
if (chan_num == 0)
|
if (chan_num == 0)
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
mraa_iio_channel* chan;
|
mraa_iio_channel* chan;
|
||||||
dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
|
dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
|
||||||
seekdir(dir, 0);
|
seekdir(dir, 0);
|
||||||
@@ -113,21 +113,20 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
|
|||||||
chan->index = chan_num;
|
chan->index = chan_num;
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
buf[(strlen(buf)-5)] = '\0';
|
buf[(strlen(buf) - 5)] = '\0';
|
||||||
char* str = strdup(buf);
|
char* str = strdup(buf);
|
||||||
// grab the type of the buffer
|
// grab the type of the buffer
|
||||||
snprintf(buf, MAX_SIZE, "%stype", str);
|
snprintf(buf, MAX_SIZE, "%stype", str);
|
||||||
fd = open(buf, O_RDONLY);
|
fd = open(buf, O_RDONLY);
|
||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
read(fd, readbuf, 31 * sizeof(char));
|
read(fd, readbuf, 31 * sizeof(char));
|
||||||
ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf,
|
ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf, &signchar, &chan->bits_used,
|
||||||
&signchar, &chan->bits_used,
|
&padint, &chan->shift);
|
||||||
&padint, &chan->shift);
|
|
||||||
chan->bytes = padint / 8;
|
chan->bytes = padint / 8;
|
||||||
if (curr_bytes % chan->bytes == 0) {
|
if (curr_bytes % chan->bytes == 0) {
|
||||||
chan->location = curr_bytes;
|
chan->location = curr_bytes;
|
||||||
} else {
|
} else {
|
||||||
chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
|
chan->location = curr_bytes - curr_bytes % chan->bytes + chan->bytes;
|
||||||
}
|
}
|
||||||
curr_bytes = chan->location + chan->bytes;
|
curr_bytes = chan->location + chan->bytes;
|
||||||
// probably should be 5?
|
// probably should be 5?
|
||||||
@@ -155,7 +154,7 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
chan->enabled = (int) strtol(readbuf, NULL, 10);
|
chan->enabled = (int) strtol(readbuf, NULL, 10);
|
||||||
//only calculate enable buffer size for trigger buffer extract data
|
// only calculate enable buffer size for trigger buffer extract data
|
||||||
if (chan->enabled) {
|
if (chan->enabled) {
|
||||||
dev->datasize += chan->bytes;
|
dev->datasize += chan->bytes;
|
||||||
}
|
}
|
||||||
@@ -195,7 +194,7 @@ mraa_iio_get_device_num_by_name(const char* name)
|
|||||||
struct _iio* device;
|
struct _iio* device;
|
||||||
device = &plat_iio->iio_devices[i];
|
device = &plat_iio->iio_devices[i];
|
||||||
// we want to check for exact match
|
// we want to check for exact match
|
||||||
if (strncmp(device->name, name, strlen(device->name)+1) == 0) {
|
if (strncmp(device->name, name, strlen(device->name) + 1) == 0) {
|
||||||
return device->num;
|
return device->num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -224,14 +223,14 @@ mraa_iio_write(mraa_iio_context dev, const char* attr_chan, const char* data)
|
|||||||
snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
|
snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
|
||||||
int fd = open(buf, O_WRONLY);
|
int fd = open(buf, O_WRONLY);
|
||||||
if (fd != -1) {
|
if (fd != -1) {
|
||||||
write(fd, data, (strlen(data)+1));
|
write(fd, data, (strlen(data) + 1));
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
static mraa_result_t
|
static mraa_result_t
|
||||||
mraa_iio_wait_event(int fd, char* data, int *read_size)
|
mraa_iio_wait_event(int fd, char* data, int* read_size)
|
||||||
{
|
{
|
||||||
struct pollfd pfd;
|
struct pollfd pfd;
|
||||||
|
|
||||||
@@ -257,14 +256,14 @@ mraa_iio_trigger_handler(void* arg)
|
|||||||
{
|
{
|
||||||
mraa_iio_context dev = (mraa_iio_context) arg;
|
mraa_iio_context dev = (mraa_iio_context) arg;
|
||||||
int i;
|
int i;
|
||||||
char data[MAX_SIZE*100];
|
char data[MAX_SIZE * 100];
|
||||||
int read_size;
|
int read_size;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (mraa_iio_wait_event(dev->fp, &data[0], &read_size) == MRAA_SUCCESS) {
|
if (mraa_iio_wait_event(dev->fp, &data[0], &read_size) == MRAA_SUCCESS) {
|
||||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||||
//only can process if readsize >= enabled channel's datasize
|
// only can process if readsize >= enabled channel's datasize
|
||||||
for (i=0; i<(read_size/dev->datasize); i++) {
|
for (i = 0; i < (read_size / dev->datasize); i++) {
|
||||||
dev->isr(&data);
|
dev->isr(&data);
|
||||||
}
|
}
|
||||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||||
@@ -299,7 +298,7 @@ mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* ar
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_iio_get_event_data(mraa_iio_context dev)
|
mraa_iio_get_event_data(mraa_iio_context dev)
|
||||||
{
|
{
|
||||||
const struct dirent *ent;
|
const struct dirent* ent;
|
||||||
DIR* dir;
|
DIR* dir;
|
||||||
int event_num = 0;
|
int event_num = 0;
|
||||||
char buf[MAX_SIZE];
|
char buf[MAX_SIZE];
|
||||||
@@ -319,37 +318,35 @@ mraa_iio_get_event_data(mraa_iio_context dev)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
dev->event_num = event_num;
|
dev->event_num = event_num;
|
||||||
//no need proceed if no event found
|
// no need proceed if no event found
|
||||||
if (event_num == 0)
|
if (event_num == 0)
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
mraa_iio_event* event;
|
mraa_iio_event* event;
|
||||||
dev->events = calloc(event_num, sizeof(mraa_iio_event));
|
dev->events = calloc(event_num, sizeof(mraa_iio_event));
|
||||||
if ( dev->events == NULL)
|
if (dev->events == NULL) {
|
||||||
{
|
closedir(dir);
|
||||||
closedir(dir);
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
}
|
||||||
}
|
rewinddir(dir);
|
||||||
rewinddir(dir);
|
event_num = 0;
|
||||||
event_num = 0;
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
while ((ent = readdir(dir)) != NULL)
|
|
||||||
{
|
|
||||||
if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
|
if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
|
||||||
event = &dev->events[event_num];
|
event = &dev->events[event_num];
|
||||||
event->name = strdup(ent->d_name);
|
event->name = strdup(ent->d_name);
|
||||||
snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
|
snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
|
||||||
fd = open(buf, O_RDONLY);
|
fd = open(buf, O_RDONLY);
|
||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
|
if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
event->enabled = ((int) strtol(readbuf, NULL, 10));
|
event->enabled = ((int) strtol(readbuf, NULL, 10));
|
||||||
//Todo, read other event info.
|
// Todo, read other event info.
|
||||||
event_num++;
|
event_num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
}
|
}
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -369,14 +366,14 @@ mraa_iio_event_read(mraa_iio_context dev, const char* attribute, float* data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_iio_event_write(mraa_iio_context dev, const char* attribute, const char* data)
|
mraa_iio_event_write(mraa_iio_context dev, const char* attribute, const char* data)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
char buf[MAX_SIZE];
|
char buf[MAX_SIZE];
|
||||||
snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
|
snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
|
||||||
int fd = open(buf, O_WRONLY);
|
int fd = open(buf, O_WRONLY);
|
||||||
if (fd != -1) {
|
if (fd != -1) {
|
||||||
int len = write(fd, data, ( strlen(data) +1 ));
|
int len = write(fd, data, (strlen(data) + 1));
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
@@ -408,27 +405,27 @@ mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
|
|||||||
{
|
{
|
||||||
char bu[MAX_SIZE];
|
char bu[MAX_SIZE];
|
||||||
int ret;
|
int ret;
|
||||||
int event_fd;
|
int event_fd;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
|
sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
|
||||||
fd = open(bu, 0);
|
fd = open(bu, 0);
|
||||||
ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
|
ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
if (ret == -1 || event_fd == -1)
|
if (ret == -1 || event_fd == -1)
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
|
||||||
ret = read(event_fd, data, sizeof(struct iio_event_data));
|
ret = read(event_fd, data, sizeof(struct iio_event_data));
|
||||||
|
|
||||||
close(event_fd);
|
close(event_fd);
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void*
|
static void*
|
||||||
mraa_iio_event_handler(void* arg)
|
mraa_iio_event_handler(void* arg)
|
||||||
{
|
{
|
||||||
struct iio_event_data data;
|
struct iio_event_data data;
|
||||||
mraa_iio_context dev = (mraa_iio_context) arg;
|
mraa_iio_context dev = (mraa_iio_context) arg;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -447,7 +444,7 @@ mraa_iio_event_handler(void* arg)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
|
mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
char bu[MAX_SIZE];
|
char bu[MAX_SIZE];
|
||||||
if (dev->thread_id != 0) {
|
if (dev->thread_id != 0) {
|
||||||
return MRAA_ERROR_NO_RESOURCES;
|
return MRAA_ERROR_NO_RESOURCES;
|
||||||
@@ -458,13 +455,12 @@ mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_even
|
|||||||
if (dev->fp == -1) {
|
if (dev->fp == -1) {
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
|
ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
|
||||||
close(dev->fp);
|
close(dev->fp);
|
||||||
|
|
||||||
if (ret == -1 || dev->fp_event == -1)
|
if (ret == -1 || dev->fp_event == -1) {
|
||||||
{
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
dev->isr_event = fptr;
|
dev->isr_event = fptr;
|
||||||
pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
|
pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
|
||||||
@@ -473,15 +469,22 @@ mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_even
|
|||||||
}
|
}
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_iio_event_extract_event(struct iio_event_data* event, int* chan_type, int* modifier, int* type, int* direction, int* channel, int* channel2, int* different)
|
mraa_iio_event_extract_event(struct iio_event_data* event,
|
||||||
|
int* chan_type,
|
||||||
|
int* modifier,
|
||||||
|
int* type,
|
||||||
|
int* direction,
|
||||||
|
int* channel,
|
||||||
|
int* channel2,
|
||||||
|
int* different)
|
||||||
{
|
{
|
||||||
*chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
|
*chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
|
||||||
*modifier= IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
|
*modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
|
||||||
*type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
|
*type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
|
||||||
*direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
|
*direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
|
||||||
*channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
|
*channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
|
||||||
*channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
|
*channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
|
||||||
*different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
|
*different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
#if 0
|
#if 0
|
||||||
@@ -492,4 +495,3 @@ mraa_iio_stop(mraa_iio_context dev)
|
|||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user