From 4e78147df919b6cbc4f117d6a0229819647d503e Mon Sep 17 00:00:00 2001 From: Jacopo Mondi Date: Thu, 3 Jan 2019 16:33:19 +0100 Subject: libcamera: Add link handling functions Add a function to the MediaLink class to set the state of a link to enabled or disabled. The function makes use of an internal MediaDevice method, which is defined private and only accessible by the MediaLink setEnabled() function itself. Also add to MediaDevice a function to reset all links registered in the media graph. Signed-off-by: Jacopo Mondi --- src/libcamera/media_device.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'src/libcamera/media_device.cpp') diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index ca12caaa..0ee55060 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -386,6 +386,35 @@ MediaLink *MediaDevice::link(const MediaPad *source, const MediaPad *sink) return nullptr; } +/** + * \brief Disable all links in the media device + * + * Disable all the media device links, clearing the MEDIA_LNK_FL_ENABLED flag + * on links which are not flagged as IMMUTABLE. + * + * \return 0 on success, or a negative error code otherwise + */ +int MediaDevice::disableLinks() +{ + for (MediaEntity *entity : entities_) { + for (MediaPad *pad : entity->pads()) { + if (!(pad->flags() & MEDIA_PAD_FL_SOURCE)) + continue; + + for (MediaLink *link : pad->links()) { + if (link->flags() & MEDIA_LNK_FL_IMMUTABLE) + continue; + + int ret = link->setEnabled(false); + if (ret) + return ret; + } + } + } + + return 0; +} + /** * \var MediaDevice::objects_ * \brief Global map of media objects (entities, pads, links) keyed by their @@ -602,4 +631,50 @@ bool MediaDevice::populateLinks(const struct media_v2_topology &topology) return true; } +/** + * \brief Apply \a flags to a link between two pads + * \param link The link to apply flags to + * \param flags The flags to apply to the link + * + * This function applies the link \a flags (as defined by the MEDIA_LNK_FL_* + * macros from the Media Controller API) to the given \a link. It implements + * low-level link setup as it performs no checks on the validity of the \a + * flags, and assumes that the supplied \a flags are valid for the link (e.g. + * immutable links cannot be disabled). +* + * \sa MediaLink::setEnabled(bool enable) + * + * \return 0 on success, or a negative error code otherwise + */ +int MediaDevice::setupLink(const MediaLink *link, unsigned int flags) +{ + struct media_link_desc linkDesc = { }; + MediaPad *source = link->source(); + MediaPad *sink = link->sink(); + + linkDesc.source.entity = source->entity()->id(); + linkDesc.source.index = source->index(); + linkDesc.source.flags = MEDIA_PAD_FL_SOURCE; + + linkDesc.sink.entity = sink->entity()->id(); + linkDesc.sink.index = sink->index(); + linkDesc.sink.flags = MEDIA_PAD_FL_SINK; + + linkDesc.flags = flags; + + int ret = ioctl(fd_, MEDIA_IOC_SETUP_LINK, &linkDesc); + if (ret) { + ret = -errno; + LOG(Error) << "Failed to setup link: " << strerror(-ret); + return ret; + } + + LOG(Debug) << source->entity()->name() << "[" + << source->index() << "] -> " + << sink->entity()->name() << "[" + << sink->index() << "]: " << flags; + + return 0; +} + } /* namespace libcamera */ -- cgit v1.2.1