#NEWLINE# def connect( self , **options ) : #NEWLINE# #INDENT# 'Connect with a contactless target or become connected as a\n        contactless target. The calling thread is blocked until a\n        single activation and deactivation has completed or a callback\n        function supplied as the keyword argument ``terminate``\n        returned True. The result of the terminate function also\n        applies to the loop run after activation, so the example below\n        will make :meth:`~connect()` return after 10 seconds from\n        either waiting for a peer device or when connected. ::\n\n        >>> import nfc, time\n        >>> clf = nfc.ContactlessFrontend(\'usb\')\n        >>> after5s = lambda: time.time() - started > 5\n        >>> started = time.time(); clf.connect(llcp={}, terminate=after5s)\n\n        Connect options are given as keyword arguments with dictionary\n        values. Possible options are:\n        \n        * ``rdwr={key: value, ...}`` - options for reader/writer operation\n        * ``llcp={key: value, ...}`` - options for peer to peer mode operation\n        * ``card={key: value, ...}`` - options for card emulation operation\n\n        **Reader/Writer Options**\n\n        \'targets\': sequence\n          A list of target specifications with each target of either\n          type :class:`~nfc.clf.TTA`, :class:`~nfc.clf.TTB`, or\n          :class:`~nfc.clf.TTF`. A default set is choosen if \'targets\'\n          is not provided.\n          \n        \'on-startup\': function\n          A function that will be called with the list of targets\n          (from \'targets\') to search for. Must return a list of\n          targets or :const:`None`. Only the targets returned are\n          finally considered.\n          \n        \'on-connect\': function\n          A function object that will be called with an activated\n          :class:`~nfc.tag.Tag` object.\n        \n        >>> import nfc\n        >>> def connected(tag):\n        ...     print tag\n        ...     return True\n        ...\n        >>> clf = nfc.ContactlessFrontend()\n        >>> clf.connect(rdwr={\'on-connect\': connected})\n        Type3Tag IDm=01010501b00ac30b PMm=03014b024f4993ff SYS=12fc\n        True\n        \n        **Peer To Peer Options**\n\n        \'on-startup\': function\n          A function that is called before an attempt is made to\n          establish peer to peer communication. The function receives\n          the initialized :class:`~nfc.llcp.llc.LogicalLinkController`\n          instance as parameter, which may then be used to allocate\n          and bind communication sockets for service applications. The\n          return value must be either the\n          :class:`~nfc.llcp.llc.LogicalLinkController` instance or\n          :const:`None` to effectively remove llcp from the options\n          considered.\n          \n        \'on-connect\': function\n          A function that is be called when peer to peer communication\n          was established. The function receives the connected\n          :class:`~nfc.llcp.llc.LogicalLinkController` instance as\n          parameter, which may then be used to allocate communication\n          sockets with\n          :meth:`~nfc.llcp.llc.LogicalLinkController.socket` and spawn\n          working threads to perform communication. The callback must\n          return more or less immediately with :const:`True` unless\n          the logical link controller run loop is handled within the\n          callback.\n          \n        \'role\': string\n          Defines which role the local LLC shall take for the data\n          exchange protocol activation. Possible values are\n          \'initiator\' and \'target\'.  The default is to alternate\n          between both roles until communication is established.\n          \n        \'miu\': integer\n          Defines the maximum information unit size that will be\n          supported and announced to the remote LLC. The default value\n          is 128.\n          \n        \'lto\': integer\n          Defines the link timeout value (in milliseconds) that will\n          be announced to the remote LLC. The default value is 100\n          milliseconds.\n          \n        \'agf\': boolean\n          Defines if the local LLC performs PDU aggregation and may\n          thus send Aggregated Frame (AGF) PDUs to the remote LLC. The\n          dafault is to use aggregation.\n        \n        >>> import nfc\n        >>> import threading\n        >>> def worker(socket):\n        ...     socket.sendto("Hi there!", address=16)\n        ...     socket.close()\n        ...\n        >>> def connected(llc):\n        ...     socket = llc.socket(nfc.llcp.LOGICAL_DATA_LINK)\n        ...     threading.Thread(target=worker, args=(socket,)).start()\n        ...     return True\n        ...\n        >>> clf = nfc.ContactlessFrontend()\n        >>> clf.connect(llcp={\'on-connect\': connected})\n        \n        **Card Emulation Options**\n\n        \'targets\': sequence\n          A list of target specifications with each target of either\n          type :class:`~nfc.clf.TTA`, :class:`~nfc.clf.TTB`, or\n          :class:`~nfc.clf.TTF`. The list of targets is processed\n          sequentially. Defaults to an empty list.\n          \n        \'on-startup\': function\n          A function that will be called with the list of targets\n          (from \'targets\') to emulate. Must return a list of one\n          target choosen or :const:`None`.\n          \n        \'on-connect\': function\n          A function that will be called with an activated\n          :class:`~nfc.tag.TagEmulation` instance as first parameter and\n          the first command received as the second parameter.\n\n        \'on-release\': function A function that will be called when the\n          activated tag has been released by it\'s Initiator, basically\n          that is when the tag has been removed from the Initiator\'s\n          RF field.\n\n        \'timeout\': integer\n          The timeout in seconds to wait for for each target to become\n          initialized. The default value is 1 second.\n\n        >>> import nfc\n        >>>\n        >>> def connected(tag, command):\n        ...     print tag\n        ...     print str(command).encode("hex")\n        ...\n        >>> clf = nfc.ContactlessFrontend()\n        >>> idm = bytearray.fromhex("01010501b00ac30b")\n        >>> pmm = bytearray.fromhex("03014b024f4993ff")\n        >>> sys = bytearray.fromhex("12fc")\n        >>> target = nfc.clf.TTF(212, idm, pmm, sys)\n        >>> clf.connect(card={\'targets\': [target], \'on-connect\': connected})\n        Type3TagEmulation IDm=01010501b00ac30b PMm=03014b024f4993ff SYS=12fc\n        100601010501b00ac30b010b00018000\n        True\n        \n        Connect returns :const:`None` if no options were to execute,\n        :const:`False` if interrupted by a :exc:`KeyboardInterrupt`,\n        or :const:`True` if terminated normally and the \'on-connect\'\n        callback function had returned :const:`True`. If the\n        \'on-connect\' callback had returned :const:`False` the return\n        value of connect() is the same parameters as were provided to\n        the callback function.\n\n        Connect raises :exc:`IOError(errno.ENODEV)` if called before a\n        contactless reader was opened.\n        ' #NEWLINE# if ( self . dev is None ) : #NEWLINE# #INDENT# raise IOError ( errno . ENODEV , os . strerror ( errno . ENODEV ) ) #NEWLINE# #UNINDENT# log . debug ( 'connect({0})' . format ( options ) ) #NEWLINE# terminate = options . get ( 'terminate' , ( lambda : False ) ) #NEWLINE# rdwr_options = options . get ( 'rdwr' ) #NEWLINE# llcp_options = options . get ( 'llcp' ) #NEWLINE# card_options = options . get ( 'card' ) #NEWLINE# if isinstance ( rdwr_options , dict ) : #NEWLINE# #INDENT# rdwr_options . setdefault ( 'targets' , [ TTA ( br = 106 , cfg = None , uid = None ) , TTB ( br = 106 ) , TTF ( br = 424 , idm = None , pmm = None , sys = None ) , TTF ( br = 212 , idm = None , pmm = None , sys = None ) ] ) #NEWLINE# if ( 'on-startup' in rdwr_options ) : #NEWLINE# #INDENT# targets = rdwr_options . get ( 'targets' ) #NEWLINE# targets = rdwr_options [ 'on-startup' ] ( self , targets ) #NEWLINE# if ( targets is None ) : #NEWLINE# #INDENT# rdwr_options = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# rdwr_options [ 'targets' ] = targets #NEWLINE# #UNINDENT# #UNINDENT# if ( rdwr_options is not None ) : #NEWLINE# #INDENT# if ( not ( 'on-connect' in rdwr_options ) ) : #NEWLINE# #INDENT# rdwr_options [ 'on-connect' ] = ( lambda tag : True ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( rdwr_options is not None ) : #NEWLINE# #INDENT# raise TypeError ( 'argument *rdrw* must be a dictionary' ) #NEWLINE# #UNINDENT# #UNINDENT# if isinstance ( llcp_options , dict ) : #NEWLINE# #INDENT# llc = nfc . llcp . llc . LogicalLinkController ( recv_miu = llcp_options . get ( 'miu' , 128 ) , send_lto = llcp_options . get ( 'lto' , 100 ) , send_agf = llcp_options . get ( 'agf' , True ) , symm_log = llcp_options . get ( 'symm-log' , True ) ) #NEWLINE# if ( 'on-startup' in llcp_options ) : #NEWLINE# #INDENT# llc = llcp_options [ 'on-startup' ] ( self , llc ) #NEWLINE# if ( llc is None ) : #NEWLINE# #INDENT# llcp_options = None #NEWLINE# #UNINDENT# #UNINDENT# if ( llcp_options is not None ) : #NEWLINE# #INDENT# if ( not ( 'on-connect' in llcp_options ) ) : #NEWLINE# #INDENT# llcp_options [ 'on-connect' ] = ( lambda llc : True ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( llcp_options is not None ) : #NEWLINE# #INDENT# raise TypeError ( 'argument *llcp* must be a dictionary' ) #NEWLINE# #UNINDENT# #UNINDENT# if isinstance ( card_options , dict ) : #NEWLINE# #INDENT# if ( 'on-startup' in card_options ) : #NEWLINE# #INDENT# targets = card_options . get ( 'targets' , [ ] ) #NEWLINE# targets = card_options [ 'on-startup' ] ( self , targets ) #NEWLINE# if ( targets is None ) : #NEWLINE# #INDENT# card_options = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# card_options [ 'targets' ] = targets #NEWLINE# #UNINDENT# #UNINDENT# if ( card_options is not None ) : #NEWLINE# #INDENT# if ( not card_options . get ( 'targets' ) ) : #NEWLINE# #INDENT# log . error ( 'a target must be specified to connect as tag' ) #NEWLINE# return None #NEWLINE# #UNINDENT# if ( not ( 'on-connect' in card_options ) ) : #NEWLINE# #INDENT# card_options [ 'on-connect' ] = ( lambda tag , command : True ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( card_options is not None ) : #NEWLINE# #INDENT# raise TypeError ( 'argument *card* must be a dictionary' ) #NEWLINE# #UNINDENT# #UNINDENT# some_options = ( rdwr_options or llcp_options or card_options ) #NEWLINE# if ( not some_options ) : #NEWLINE# #INDENT# log . warning ( 'no options left to connect' ) #NEWLINE# return None #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# while ( not terminate ( ) ) : #NEWLINE# #INDENT# if llcp_options : #NEWLINE# #INDENT# result = self . _llcp_connect ( llcp_options , llc , terminate ) #NEWLINE# if ( bool ( result ) is True ) : #NEWLINE# #INDENT# return result #NEWLINE# #UNINDENT# #UNINDENT# if rdwr_options : #NEWLINE# #INDENT# result = self . _rdwr_connect ( rdwr_options , terminate ) #NEWLINE# if ( bool ( result ) is True ) : #NEWLINE# #INDENT# return result #NEWLINE# #UNINDENT# #UNINDENT# if card_options : #NEWLINE# #INDENT# result = self . _card_connect ( card_options , terminate ) #NEWLINE# if ( bool ( result ) is True ) : #NEWLINE# #INDENT# return result #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except KeyboardInterrupt as error : #NEWLINE# #INDENT# log . debug ( error ) #NEWLINE# return False #NEWLINE# #UNINDENT# except IOError as error : #NEWLINE# #INDENT# log . error ( error ) #NEWLINE# return False