Listen *timeout* seconds to become activated as *target*.

        .. note:: The :meth:`listen` method is intended for experts
                  with a good understanding of the commands and
                  responses exchanged during target activation (the
                  notion used for commands and responses follows the
                  NFC Forum Digital Specification). If the greater
                  level of control is not needed it is recommended to
                  use the :meth:`connect` method.

        The *target* argument is a :class:`LocalTarget` object that
        provides bitrate, technology type and response data
        attributes. The return value is either a :class:`LocalTarget`
        object with bitrate, technology type and request/response data
        attributes or :const:`None`.

        An **P2P Target** is selected when the ``atr_res`` attribute
        is set. The bitrate and technology type are decided by the
        Initiator and do not need to be specified. The ``sens_res``,
        ``sdd_res`` and ``sel_res`` attributes for Type A technology
        as well as the ``sensf_res`` attribute for Type F technolgy
        must all be set.

        When activated, the bitrate and type are set to the current
        communication values, the ``atr_req`` attribute contains the
        ATR_REQ received from the Initiator and the ``dep_req``
        attribute contains the first DEP_REQ received after
        activation. If the Initiator has changed communication
        parameters, the ``psl_req`` attribute holds the PSL_REQ that
        was received. The ``atr_res`` (and the ``psl_res`` if
        transmitted) are also made available.

        If the local target was activated in passive communication
        mode either the Type A response (``sens_res``, ``sdd_res``,
        ``sel_res``) or Type F response (``sensf_res``) attributes
        will be present.

        With a Nexus 5 on a reader connected via USB the following
        code should be working and produce similar output (the Nexus 5
        prioritizes active communication mode):

        >>> import nfc, nfc.clf
        >>> clf = nfc.ContactlessFrontend("usb")
        >>> atr_res = "d50101fe0102030405060708000000083246666d010110"
        >>> target = nfc.clf.LocalTarget()
        >>> target.sensf_res = bytearray.fromhex("0101FE"+16*"FF")
        >>> target.sens_res = bytearray.fromhex("0101")
        >>> target.sdd_res = bytearray.fromhex("08010203")
        >>> target.sel_res = bytearray.fromhex("40")
        >>> target.atr_res = bytearray.fromhex(atr_res)
        >>> print(clf.listen(target, timeout=2.5))
        424F atr_res=D50101FE0102030405060708000000083246666D010110 ...

        A **Type A Target** is selected when ``atr_res`` is not
        present and the technology type is ``A``. The bitrate should
        be set to 106 kbps, even if a driver supports higher bitrates
        they would need to be set after activation. The ``sens_res``,
        ``sdd_res`` and ``sel_res`` attributes must all be provided.

        >>> target = nfc.clf.Localtarget("106A")
        >>> target.sens_res = bytearray.fromhex("0101"))
        >>> target.sdd_res = bytearray.fromhex("08010203")
        >>> target.sel_res = bytearray.fromhex("00")
        >>> print(clf.listen(target, timeout=2.5))
        106A sdd_res=08010203 sel_res=00 sens_res=0101 tt2_cmd=3000

        A **Type B Target** is selected when ``atr_res`` is not
        present and the technology type is ``B``. Unfortunately none
        of the supported devices supports Type B technology for listen
        and an :exc:`nfc.clf.UnsupportedTargetError` exception will be
        the only result.

        >>> target = nfc.clf.LocalTarget("106B")
        >>> try: clf.listen(target, 2.5)
        ... except nfc.clf.UnsupportedTargetError: print("sorry")
        ...
        sorry

        A **Type F Target** is selected when ``atr_res`` is not
        present and the technology type is ``F``. The bitrate may be
        212 or 424 kbps. The ``sensf_res`` attribute must be provided.

        >>> idm, pmm, sys = "02FE010203040506", "FFFFFFFFFFFFFFFF", "12FC"
        >>> target = nfc.clf.LocalTarget("212F")
        >>> target.sensf_res = bytearray.fromhex("01" + idm + pmm + sys)
        >>> print(clf.listen(target, 2.5))
        212F sensf_req=00FFFF0003 tt3_cmd=0C02FE010203040506 ...

        **Exceptions**

        * :exc:`~exceptions.IOError` (ENODEV) when a local contacless
          communication device has not been opened or communication
          with the local device is no longer possible.

        * :exc:`nfc.clf.UnsupportedTargetError` if the single target
          supplied as input is not supported by the active driver.
          This exception is never raised when :meth:`sense` is called
          with multiple targets, those unsupported are then silently
          ignored.

def listen(self, target, timeout):
        """Listen *timeout* seconds to become activated as *target*.

        .. note:: The :meth:`listen` method is intended for experts
                  with a good understanding of the commands and
                  responses exchanged during target activation (the
                  notion used for commands and responses follows the
                  NFC Forum Digital Specification). If the greater
                  level of control is not needed it is recommended to
                  use the :meth:`connect` method.

        The *target* argument is a :class:`LocalTarget` object that
        provides bitrate, technology type and response data
        attributes. The return value is either a :class:`LocalTarget`
        object with bitrate, technology type and request/response data
        attributes or :const:`None`.

        An **P2P Target** is selected when the ``atr_res`` attribute
        is set. The bitrate and technology type are decided by the
        Initiator and do not need to be specified. The ``sens_res``,
        ``sdd_res`` and ``sel_res`` attributes for Type A technology
        as well as the ``sensf_res`` attribute for Type F technolgy
        must all be set.

        When activated, the bitrate and type are set to the current
        communication values, the ``atr_req`` attribute contains the
        ATR_REQ received from the Initiator and the ``dep_req``
        attribute contains the first DEP_REQ received after
        activation. If the Initiator has changed communication
        parameters, the ``psl_req`` attribute holds the PSL_REQ that
        was received. The ``atr_res`` (and the ``psl_res`` if
        transmitted) are also made available.

        If the local target was activated in passive communication
        mode either the Type A response (``sens_res``, ``sdd_res``,
        ``sel_res``) or Type F response (``sensf_res``) attributes
        will be present.

        With a Nexus 5 on a reader connected via USB the following
        code should be working and produce similar output (the Nexus 5
        prioritizes active communication mode):

        >>> import nfc, nfc.clf
        >>> clf = nfc.ContactlessFrontend("usb")
        >>> atr_res = "d50101fe0102030405060708000000083246666d010110"
        >>> target = nfc.clf.LocalTarget()
        >>> target.sensf_res = bytearray.fromhex("0101FE"+16*"FF")
        >>> target.sens_res = bytearray.fromhex("0101")
        >>> target.sdd_res = bytearray.fromhex("08010203")
        >>> target.sel_res = bytearray.fromhex("40")
        >>> target.atr_res = bytearray.fromhex(atr_res)
        >>> print(clf.listen(target, timeout=2.5))
        424F atr_res=D50101FE0102030405060708000000083246666D010110 ...

        A **Type A Target** is selected when ``atr_res`` is not
        present and the technology type is ``A``. The bitrate should
        be set to 106 kbps, even if a driver supports higher bitrates
        they would need to be set after activation. The ``sens_res``,
        ``sdd_res`` and ``sel_res`` attributes must all be provided.

        >>> target = nfc.clf.Localtarget("106A")
        >>> target.sens_res = bytearray.fromhex("0101"))
        >>> target.sdd_res = bytearray.fromhex("08010203")
        >>> target.sel_res = bytearray.fromhex("00")
        >>> print(clf.listen(target, timeout=2.5))
        106A sdd_res=08010203 sel_res=00 sens_res=0101 tt2_cmd=3000

        A **Type B Target** is selected when ``atr_res`` is not
        present and the technology type is ``B``. Unfortunately none
        of the supported devices supports Type B technology for listen
        and an :exc:`nfc.clf.UnsupportedTargetError` exception will be
        the only result.

        >>> target = nfc.clf.LocalTarget("106B")
        >>> try: clf.listen(target, 2.5)
        ... except nfc.clf.UnsupportedTargetError: print("sorry")
        ...
        sorry

        A **Type F Target** is selected when ``atr_res`` is not
        present and the technology type is ``F``. The bitrate may be
        212 or 424 kbps. The ``sensf_res`` attribute must be provided.

        >>> idm, pmm, sys = "02FE010203040506", "FFFFFFFFFFFFFFFF", "12FC"
        >>> target = nfc.clf.LocalTarget("212F")
        >>> target.sensf_res = bytearray.fromhex("01" + idm + pmm + sys)
        >>> print(clf.listen(target, 2.5))
        212F sensf_req=00FFFF0003 tt3_cmd=0C02FE010203040506 ...

        **Exceptions**

        * :exc:`~exceptions.IOError` (ENODEV) when a local contacless
          communication device has not been opened or communication
          with the local device is no longer possible.

        * :exc:`nfc.clf.UnsupportedTargetError` if the single target
          supplied as input is not supported by the active driver.
          This exception is never raised when :meth:`sense` is called
          with multiple targets, those unsupported are then silently
          ignored.

        """
        def listen_tta(target, timeout):
            return self.device.listen_tta(target, timeout)

        def listen_ttb(target, timeout):
            return self.device.listen_ttb(target, timeout)

        def listen_ttf(target, timeout):
            return self.device.listen_ttf(target, timeout)

        def listen_dep(target, timeout):
            target = self.device.listen_dep(target, timeout)
            if target and target.atr_req:
                try:
                    assert len(target.atr_req) >= 16, "less than 16 byte"
                    assert len(target.atr_req) <= 64, "more than 64 byte"
                    return target
                except AssertionError as error:
                    log.debug("atr_req is %s", str(error))

        assert isinstance(target, LocalTarget), \
            "invalid target argument type: %r" % target

        with self.lock:
            if self.device is None:
                raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))

            self.target = None  # forget captured target
            self.device.mute()  # deactivate the rf field

            info = "listen %.3f seconds for %s"
            if target.atr_res is not None:
                log.debug(info, timeout, "DEP")
                self.target = listen_dep(target, timeout)
            elif target.brty in ('106A', '212A', '424A'):
                log.debug(info, timeout, target)
                self.target = listen_tta(target, timeout)
            elif target.brty in ('106B', '212B', '424B', '848B'):
                log.debug(info, timeout, target)
                self.target = listen_ttb(target, timeout)
            elif target.brty in ('212F', '424F'):
                log.debug(info, timeout, target)
                self.target = listen_ttf(target, timeout)
            else:
                errmsg = "unsupported bitrate technology type {}"
                raise ValueError(errmsg.format(target.brty))

            return self.target