From 0c4da2bb2f4d7d3cd2cdea1369374d5b4f7e3b29 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 14 Jun 2010 22:57:45 +0200 Subject: Enumerate entities using MEDIA_ENTITY_ID_FLAG_NEXT Entity IDs don't have to be contiguous. Use the new flag-based enumeration system instead of looping over consecutive IDs. Signed-off-by: Laurent Pinchart --- main.c | 7 +++---- media.c | 42 ++++++++++++++++++++++++++++++++---------- media.h | 2 ++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index c946f2d..c59b633 100644 --- a/main.c +++ b/main.c @@ -63,11 +63,10 @@ static struct media_entity_pad *parse_pad(struct media_device *media, const char for (++end; isspace(*end); ++end); } else { - entity_id = strtoul(p, &end, 10) - 1; - if (entity_id >= media->entities_count) + entity_id = strtoul(p, &end, 10); + entity = media_get_entity_by_id(media, entity_id); + if (entity == NULL) return NULL; - - entity = &media->entities[entity_id]; } if (*end != ':') diff --git a/media.c b/media.c index 59a3a58..6677843 100644 --- a/media.c +++ b/media.c @@ -130,7 +130,7 @@ struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad) } /* - * media_entity_by_name - + * media_get_entity_by_name - */ struct media_entity *media_get_entity_by_name(struct media_device *media, const char *name, size_t length) @@ -147,6 +147,24 @@ struct media_entity *media_get_entity_by_name(struct media_device *media, return NULL; } +/* + * media_get_entity_by_id - + */ +struct media_entity *media_get_entity_by_id(struct media_device *media, + __u32 id) +{ + unsigned int i; + + for (i = 0; i < media->entities_count; ++i) { + struct media_entity *entity = &media->entities[i]; + + if (entity->info.id == id) + return entity; + } + + return NULL; +} + /* * media_setup_link - */ @@ -393,16 +411,16 @@ static int media_enum_links(struct media_device *media) struct media_entity *source; struct media_entity *sink; - if (link->source.entity > media->entities_count || - link->sink.entity > media->entities_count) { + source = media_get_entity_by_id(media, link->source.entity); + sink = media_get_entity_by_id(media, link->sink.entity); + + if (source == NULL || sink == NULL) { printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n", id, i, link->source.entity, link->source.index, link->sink.entity, link->sink.index); ret = -EINVAL; } - source = &media->entities[link->source.entity - 1]; - sink = &media->entities[link->sink.entity - 1]; entity->links[i].source = &source->pads[link->source.index]; entity->links[i].sink = &sink->pads[link->sink.index]; entity->links[i].flags = links.links[i].flags; @@ -420,17 +438,19 @@ static int media_enum_entities(struct media_device *media) struct media_entity *entity; struct stat devstat; char devname[32]; + unsigned int size; unsigned int i; __u32 id; int ret; - for (id = 1; ; id++) { - media->entities = - realloc(media->entities, id * sizeof(*media->entities)); - entity = &media->entities[id - 1]; + for (id = 0; ; id = entity->info.id) { + size = (media->entities_count + 1) * sizeof(*media->entities); + media->entities = realloc(media->entities, size); + + entity = &media->entities[media->entities_count]; memset(entity, 0, sizeof(*entity)); entity->fd = -1; - entity->info.id = id; + entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT; ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info); if (ret < 0) { @@ -468,6 +488,8 @@ static int media_enum_entities(struct media_device *media) break; } } + + id = entity->info.id; } return 0; diff --git a/media.h b/media.h index 7dbcc1c..bd8162d 100644 --- a/media.h +++ b/media.h @@ -56,6 +56,8 @@ struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad); struct media_entity *media_get_entity_by_name(struct media_device *media, const char *name, size_t length); +struct media_entity *media_get_entity_by_id(struct media_device *media, + __u32 id); void media_print_topology(struct media_device *media, int dot); int media_setup_link(struct media_device *media, struct media_entity_pad *source, struct media_entity_pad *sink, -- cgit v1.2.3