<s> from btcommon import * <EOL> import socket <EOL> import struct <EOL> import threading <EOL> import os <EOL> import _widcomm <EOL> DEFAULT_MTU = <NUM_LIT> <EOL> def dbg ( * args ) : <EOL> return <EOL> sys . stdout . write ( * args ) <EOL> sys . stdout . write ( "<STR_LIT:\n>" ) <EOL> def BD_ADDR_to_str ( bda ) : <EOL> return "<STR_LIT>" % ( ord ( bda [ <NUM_LIT:0> ] ) , ord ( bda [ <NUM_LIT:1> ] ) , ord ( bda [ <NUM_LIT:2> ] ) , <EOL> ord ( bda [ <NUM_LIT:3> ] ) , ord ( bda [ <NUM_LIT:4> ] ) , ord ( bda [ <NUM_LIT:5> ] ) ) <EOL> def str_to_BD_ADDR ( s ) : <EOL> digits = [ int ( c , <NUM_LIT:16> ) for c in s . split ( "<STR_LIT::>" ) ] <EOL> return struct . pack ( "<STR_LIT>" , * digits ) <EOL> class WCInquirer : <EOL> DEVST_DOWN = <NUM_LIT:0> <EOL> DEVST_UP = <NUM_LIT:1> <EOL> DEVST_ERROR = <NUM_LIT:2> <EOL> DEVST_UNLOADED = <NUM_LIT:3> <EOL> DEVST_RELOADED = <NUM_LIT:4> <EOL> def __init__ ( self ) : <EOL> self . _wcinq = _widcomm . _WCInquirer ( ) <EOL> port = self . _wcinq . get_sockport ( ) <EOL> self . readsock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . readsock . connect ( ( "<STR_LIT:127.0.0.1>" , port ) ) <EOL> self . _wcinq . accept_client ( ) <EOL> self . recently_discovered = [ ] <EOL> self . inquiry_in_progress = False <EOL> self . sdp_query_in_progress = False <EOL> def fileno ( ) : <EOL> return self . readsock . fileno ( ) <EOL> def start_inquiry ( self ) : <EOL> self . recently_discovered = [ ] <EOL> self . inquiry_in_progress = self . _wcinq . start_inquiry ( ) <EOL> def read_msg ( self ) : <EOL> intsize = struct . calcsize ( "<STR_LIT>" ) <EOL> msg_type = struct . unpack ( "<STR_LIT>" , self . readsock . recv ( intsize ) ) [ <NUM_LIT:0> ] <EOL> if msg_type == _widcomm . INQ_DEVICE_RESPONDED : <EOL> fmt = "<STR_LIT>" <EOL> data = self . readsock . recv ( struct . calcsize ( fmt ) ) <EOL> bda , devclass , bdname , connected = struct . unpack ( fmt , data ) <EOL> bdaddr = BD_ADDR_to_str ( bda ) <EOL> bdname = bdname . strip ( "<STR_LIT>" ) <EOL> self . recently_discovered . append ( ( bdaddr , devclass , bdname , <EOL> connected ) ) <EOL> elif msg_type == _widcomm . INQ_INQUIRY_COMPLETE : <EOL> fmt = "<STR_LIT>" <EOL> data = self . readsock . recv ( struct . calcsize ( fmt ) ) <EOL> success , num_responses = struct . unpack ( fmt , data ) <EOL> self . inquiry_in_progress = False <EOL> elif msg_type == _widcomm . INQ_DISCOVERY_COMPLETE : <EOL> self . sdp_query_in_progress = False <EOL> elif msg_type == _widcomm . INQ_STACK_STATUS_CHANGE : <EOL> fmt = "<STR_LIT>" <EOL> data = self . readsock . recv ( struct . calcsize ( fmt ) ) <EOL> new_status = struct . unpack ( fmt , data ) [ <NUM_LIT:0> ] <EOL> def start_discovery ( self , addr , uuid = None ) : <EOL> bd_addr = str_to_BD_ADDR ( addr ) <EOL> if uuid is not None : <EOL> self . sdp_query_in_progress = self . _wcinq . start_discovery ( bd_addr , to_full_uuid ( uuid ) ) <EOL> else : <EOL> self . sdp_query_in_progress = self . _wcinq . start_discovery ( bd_addr ) <EOL> self . sdp_query_in_progress = True <EOL> def read_discovery_records ( self , addr , uuid = None ) : <EOL> if not is_valid_address ( addr ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> bd_addr = str_to_BD_ADDR ( addr ) <EOL> if uuid is not None : <EOL> dbg ( "<STR_LIT>" % ( addr , uuid ) ) <EOL> return self . _wcinq . read_discovery_records ( bd_addr , <EOL> to_full_uuid ( uuid ) ) <EOL> else : <EOL> return self . _wcinq . read_discovery_records ( bd_addr ) <EOL> def is_device_ready ( self ) : <EOL> return self . _wcinq . is_device_ready ( ) <EOL> def get_local_device_address ( self ) : <EOL> return self . _wcinq . get_local_device_address ( ) <EOL> inquirer = WCInquirer ( ) <EOL> def discover_devices ( duration = <NUM_LIT:8> , flush_cache = True , lookup_names = False ) : <EOL> inquirer . start_inquiry ( ) <EOL> while inquirer . inquiry_in_progress : <EOL> inquirer . read_msg ( ) <EOL> discovered = inquirer . recently_discovered [ : ] <EOL> if not lookup_names : <EOL> return [ tup [ <NUM_LIT:0> ] for tup in discovered ] <EOL> if lookup_names : <EOL> result = [ ] <EOL> for bdaddr , devClass , bdName , bConnected in discovered : <EOL> if bdName : <EOL> result . append ( ( bdaddr , bdName ) ) <EOL> else : <EOL> result . append ( ( bdAddr , None ) ) <EOL> return result <EOL> def lookup_name ( address , timeout = <NUM_LIT:10> ) : <EOL> discover_devices ( ) <EOL> for bdaddr , devClass , bdName , bConnected in inquirer . recently_discovered : <EOL> if bdaddr == address : <EOL> return bdName <EOL> def advertise_service ( sock , name , service_id = "<STR_LIT>" , service_classes = [ ] , profiles = [ ] , provider = "<STR_LIT>" , description = "<STR_LIT>" , protocols = [ ] ) : <EOL> sock . _advertise_service ( name , service_id , service_classes , <EOL> profiles , provider , description , protocols ) <EOL> def stop_advertising ( sock ) : <EOL> sock . _stop_advertising ( ) <EOL> def find_service ( name = None , uuid = None , address = None ) : <EOL> if address : <EOL> if address == "<STR_LIT:localhost>" : raise NotImplementedError <EOL> if not is_valid_address ( address ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> addresses = [ address ] <EOL> else : <EOL> addresses = discover_devices ( ) <EOL> if uuid and not is_valid_uuid ( uuid ) : <EOL> raise ValueError ( "<STR_LIT>" , uuid ) <EOL> results = [ ] <EOL> for addr in addresses : <EOL> inquirer . start_discovery ( addr , uuid ) <EOL> while inquirer . sdp_query_in_progress : <EOL> inquirer . read_msg ( ) <EOL> results . extend ( inquirer . read_discovery_records ( addr , uuid ) ) <EOL> return results <EOL> def _port_return_code_to_str ( code ) : <EOL> k = { _widcomm . RFCOMM_SUCCESS : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_ALREADY_OPENED : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_NOT_OPENED : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_HANDLE_ERROR : "<STR_LIT>" "<STR_LIT>" , <EOL> _widcomm . RFCOMM_LINE_ERR : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_START_FAILED : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_PAR_NEG_FAILED : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_PORT_NEG_FAILED : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_PEER_CONNECTION_FAILED : "<STR_LIT>" "<STR_LIT>" , <EOL> _widcomm . RFCOMM_PEER_TIMEOUT : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_INVALID_PARAMETER : "<STR_LIT>" , <EOL> _widcomm . RFCOMM_UNKNOWN_ERROR : "<STR_LIT>" } <EOL> if code in k : <EOL> return k [ code ] <EOL> else : <EOL> return "<STR_LIT>" % str ( code ) <EOL> def _port_ev_code_to_str ( code ) : <EOL> d = { _widcomm . PORT_EV_RXFLAG : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_TXEMPTY : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_CTS : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_DSR : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_RLSD : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_BREAK : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_ERR : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_RING : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_CTSS : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_DSRS : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_RLSDS : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_OVERRUN : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_TXCHAR : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_CONNECTED : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_CONNECT_ERR : "<STR_LIT>" "<STR_LIT>" , <EOL> _widcomm . PORT_EV_FC : "<STR_LIT>" , <EOL> _widcomm . PORT_EV_FCS : "<STR_LIT>" } <EOL> result = [ ] <EOL> for k , v in d . items ( ) : <EOL> if code & k : <EOL> result . append ( v ) <EOL> if len ( result ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" % code <EOL> else : <EOL> return "<STR_LIT:\n>" . join ( result ) <EOL> def _sdp_checkraise ( code ) : <EOL> if code == _widcomm . SDP_OK : return <EOL> elif code == _widcomm . SDP_COULD_NOT_ADD_RECORD : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> elif code == _widcomm . SDP_INVALID_RECORD : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> elif code == _widcomm . SDP_INVALID_PARAMETERS : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> raise RuntimeError ( "<STR_LIT>" % code ) <EOL> class BluetoothSocket : <EOL> def __init__ ( self , proto = RFCOMM , _sockdata = None ) : <EOL> if not proto in [ RFCOMM , L2CAP ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . proto = proto <EOL> if proto == RFCOMM : <EOL> self . bind = self . rfcomm_bind <EOL> self . listen = self . rfcomm_listen <EOL> self . accept = self . rfcomm_accept <EOL> self . connect = self . rfcomm_connect <EOL> self . send = self . rfcomm_send <EOL> self . recv = self . rfcomm_recv <EOL> self . close = self . rfcomm_close <EOL> self . getsockname = self . rfcomm_getsockname <EOL> self . setblocking = self . rfcomm_setblocking <EOL> self . settimeout = self . rfcomm_settimeout <EOL> self . gettimeout = self . rfcomm_gettimeout <EOL> self . dup = self . rfcomm_dup <EOL> self . makefile = self . rfcomm_makefile <EOL> self . fileno = self . rfcomm_fileno <EOL> self . __make_cobjects = self . __rfcomm_make_cobjects <EOL> self . _advertise_service = self . __rfcomm_advertise_service <EOL> if _sockdata : <EOL> self . _wc , self . _if , self . readsock = _sockdata <EOL> else : <EOL> self . __make_cobjects ( ) <EOL> self . connected = self . _wc . is_connected ( ) <EOL> elif proto == L2CAP : <EOL> dbg ( "<STR_LIT>" ) <EOL> self . bind = self . l2cap_bind <EOL> self . listen = self . l2cap_listen <EOL> self . accept = self . l2cap_accept <EOL> self . connect = self . l2cap_connect <EOL> self . send = self . l2cap_send <EOL> self . recv = self . l2cap_recv <EOL> self . close = self . l2cap_close <EOL> self . getsockname = self . l2cap_getsockname <EOL> self . setblocking = self . l2cap_setblocking <EOL> self . settimeout = self . l2cap_settimeout <EOL> self . gettimeout = self . l2cap_gettimeout <EOL> self . dup = self . l2cap_dup <EOL> self . makefile = self . l2cap_makefile <EOL> self . fileno = self . l2cap_fileno <EOL> self . __make_cobjects = self . __l2cap_make_cobjects <EOL> self . _advertise_service = self . __l2cap_advertise_service <EOL> if _sockdata : <EOL> self . _wc , self . _if , self . readsock = _sockdata <EOL> self . connected = True <EOL> else : <EOL> self . __make_cobjects ( ) <EOL> self . connected = False <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> self . nonblocking = False <EOL> self . connecting = False <EOL> self . listening = False <EOL> self . bound = False <EOL> self . received_data = [ ] <EOL> self . last_event_code = None <EOL> self . port = <NUM_LIT:0> <EOL> self . _sdpservice = None <EOL> def _stop_advertising ( self ) : <EOL> if not self . _sdpservice : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> self . _sdpservice = None <EOL> def __rfcomm_make_cobjects ( self ) : <EOL> self . _wc = _widcomm . _WCRfCommPort ( ) <EOL> self . _if = _widcomm . _WCRfCommIf ( ) <EOL> self . readsock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . readsock . connect ( ( "<STR_LIT:127.0.0.1>" , self . _wc . get_sockport ( ) ) ) <EOL> self . _wc . accept_client ( ) <EOL> def rfcomm_read_msg ( self ) : <EOL> intsize = struct . calcsize ( "<STR_LIT>" ) <EOL> msg_type_data = self . readsock . recv ( intsize ) <EOL> msg_type = struct . unpack ( "<STR_LIT>" , msg_type_data ) [ <NUM_LIT:0> ] <EOL> if msg_type == _widcomm . RFCOMM_DATA_RECEIVED : <EOL> datalen_fmt = "<STR_LIT>" <EOL> datalen_data = self . readsock . recv ( struct . calcsize ( datalen_fmt ) ) <EOL> datalen = struct . unpack ( datalen_fmt , datalen_data ) [ <NUM_LIT:0> ] <EOL> self . received_data . append ( self . readsock . recv ( datalen ) ) <EOL> elif msg_type == _widcomm . RFCOMM_EVENT_RECEIVED : <EOL> fmt = "<STR_LIT>" <EOL> data = self . readsock . recv ( struct . calcsize ( fmt ) ) <EOL> code = struct . unpack ( fmt , data ) [ <NUM_LIT:0> ] <EOL> dbg ( "<STR_LIT>" % code ) <EOL> if code & _widcomm . PORT_EV_CONNECTED : <EOL> self . connecting = False <EOL> self . listening = False <EOL> self . connected = True <EOL> if code & _widcomm . PORT_EV_CONNECT_ERR : <EOL> self . connecting = False <EOL> self . listening = False <EOL> self . connected = False <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_RXFLAG : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_TXEMPTY : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_CTS : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_DSR : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_RLSD : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_BREAK : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_ERR : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_RING : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_CTSS : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_DSRS : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_RLSDS : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_OVERRUN : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_TXCHAR : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_FC : <EOL> dbg ( "<STR_LIT>" ) <EOL> if code & _widcomm . PORT_EV_FCS : <EOL> dbg ( "<STR_LIT>" ) <EOL> self . last_event_code = code <EOL> def rfcomm_bind ( self , addrport ) : <EOL> addr , port = addrport <EOL> if len ( addr ) : <EOL> raise ValueError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> result = self . _if . assign_scn_value ( RFCOMM_UUID , port ) <EOL> if not result : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> self . bound = True <EOL> self . port = self . _if . get_scn ( ) <EOL> def rfcomm_listen ( self , backlog ) : <EOL> if self . connected : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if self . listening : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if backlog != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> port = self . _if . get_scn ( ) <EOL> self . _if . set_security_level ( "<STR_LIT>" , _widcomm . BTM_SEC_NONE , True ) <EOL> if not port : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> result = self . _wc . open_server ( port , DEFAULT_MTU ) <EOL> if result != _widcomm . RFCOMM_SUCCESS : <EOL> raise BluetoothError ( _port_return_code_to_str ( result ) ) <EOL> self . listening = True <EOL> def rfcomm_accept ( self ) : <EOL> if self . connected : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> while self . listening and not self . connected : <EOL> dbg ( "<STR_LIT>" ) <EOL> self . rfcomm_read_msg ( ) <EOL> if self . connected : <EOL> port = self . _if . get_scn ( ) <EOL> client_bdaddr = BD_ADDR_to_str ( self . _wc . is_connected ( ) ) <EOL> client_port = <NUM_LIT:0> <EOL> _sockdata = self . _wc , self . _if , self . readsock <EOL> clientsock = BluetoothSocket ( RFCOMM , _sockdata ) <EOL> self . __rfcomm_make_cobjects ( ) <EOL> return clientsock , ( client_bdaddr , client_port ) <EOL> def rfcomm_connect ( self , addrport ) : <EOL> addr , port = addrport <EOL> dbg ( "<STR_LIT>" % ( addr , port ) ) <EOL> if not is_valid_address ( addr ) : <EOL> raise ValueError ( "<STR_LIT>" % addr ) <EOL> self . _if . assign_scn_value ( RFCOMM_UUID , port ) <EOL> self . _if . set_security_level ( "<STR_LIT>" , _widcomm . BTM_SEC_NONE , False ) <EOL> result = self . _wc . open_client ( port , str_to_BD_ADDR ( addr ) , DEFAULT_MTU ) <EOL> if result != _widcomm . RFCOMM_SUCCESS : <EOL> raise BluetoothError ( _port_return_code_to_str ( result ) ) <EOL> self . connecting = True <EOL> while self . connecting : <EOL> self . rfcomm_read_msg ( ) <EOL> if not self . _wc . is_connected ( ) : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> def rfcomm_send ( self , data ) : <EOL> dbg ( "<STR_LIT>" % data ) <EOL> status , written = self . _wc . write ( data ) <EOL> if status == _widcomm . RFCOMM_SUCCESS : <EOL> dbg ( "<STR_LIT>" ) <EOL> return written <EOL> else : <EOL> raise BluetoothError ( _port_return_code_to_str ( status ) ) <EOL> def rfcomm_recv ( self , numbytes ) : <EOL> if self . nonblocking and not self . received_data : <EOL> return None <EOL> while not self . received_data and self . _wc . is_connected ( ) : <EOL> self . rfcomm_read_msg ( ) <EOL> if self . received_data : <EOL> data = self . received_data . pop ( <NUM_LIT:0> ) <EOL> if len ( data ) > numbytes : <EOL> self . received_data . insert ( <NUM_LIT:0> , data [ numbytes : ] ) <EOL> return data [ : numbytes ] <EOL> else : <EOL> return data <EOL> def rfcomm_close ( self ) : <EOL> self . _wc . close ( ) <EOL> self . _wc = None <EOL> self . bound = False <EOL> self . connecting = False <EOL> self . listening = False <EOL> self . connected = False <EOL> def rfcomm_getsockname ( self ) : <EOL> if not self . bound : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> addr = inquirer . get_local_device_address ( ) <EOL> port = self . _if . get_scn ( ) <EOL> return addr , port <EOL> def rfcomm_setblocking ( self , blocking ) : <EOL> self . nonblocking = not blocking <EOL> self . readsock . setblocking ( blocking ) <EOL> def rfcomm_settimeout ( self , timeout ) : <EOL> raise NotImplementedError <EOL> pass <EOL> def rfcomm_gettimeout ( self ) : <EOL> raise NotImplementedError <EOL> def rfcomm_fileno ( self ) : <EOL> return self . readsock . fileno ( ) <EOL> def rfcomm_dup ( self ) : <EOL> raise NotImplementedError <EOL> def rfcomm_makefile ( self ) : <EOL> raise NotImplementedError <EOL> def __rfcomm_advertise_service ( self , name , service_id , <EOL> service_classes , profiles , provider , description , <EOL> protocols ) : <EOL> if self . _sdpservice is not None : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if not self . listening : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if protocols : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> self . _sdpservice = _widcomm . _WCSdpService ( ) <EOL> if service_classes : <EOL> service_classes = [ to_full_uuid ( s ) for s in service_classes ] <EOL> _sdp_checkraise ( self . _sdpservice . add_service_class_id_list ( service_classes ) ) <EOL> _sdp_checkraise ( self . _sdpservice . add_rfcomm_protocol_descriptor ( self . port ) ) <EOL> if profiles : <EOL> for uuid , version in profiles : <EOL> uuid = to_full_uuid ( uuid ) <EOL> _sdp_checkraise ( self . _sdpservice . add_profile_descriptor_list ( uuid , version ) ) <EOL> _sdp_checkraise ( self . _sdpservice . add_service_name ( name ) ) <EOL> _sdp_checkraise ( self . _sdpservice . make_public_browseable ( ) ) <EOL> def __l2cap_make_cobjects ( self ) : <EOL> dbg ( "<STR_LIT>" ) <EOL> self . _wc = _widcomm . _WCL2CapConn ( ) <EOL> self . _if = _widcomm . _WCL2CapIf ( ) <EOL> self . readsock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . readsock . connect ( ( "<STR_LIT:127.0.0.1>" , self . _wc . get_sockport ( ) ) ) <EOL> self . _wc . accept_client ( ) <EOL> def l2cap_read_msg ( self ) : <EOL> intsize = struct . calcsize ( "<STR_LIT>" ) <EOL> msg_type_data = self . readsock . recv ( intsize ) <EOL> msg_type = struct . unpack ( "<STR_LIT>" , msg_type_data ) [ <NUM_LIT:0> ] <EOL> if msg_type == _widcomm . L2CAP_DATA_RECEIVED : <EOL> datalen_fmt = "<STR_LIT>" <EOL> datalen_data = self . readsock . recv ( struct . calcsize ( datalen_fmt ) ) <EOL> datalen = struct . unpack ( datalen_fmt , datalen_data ) [ <NUM_LIT:0> ] <EOL> self . received_data . append ( self . readsock . recv ( datalen ) ) <EOL> elif msg_type == _widcomm . L2CAP_INCOMING_CONNECTION : <EOL> result = self . _wc . accept ( ) <EOL> if not result : raise BluetoothError ( "<STR_LIT>" ) <EOL> elif msg_type == _widcomm . L2CAP_REMOTE_DISCONNECTED : <EOL> dbg ( "<STR_LIT>" ) <EOL> self . connecting = False <EOL> self . listening = False <EOL> self . connected = False <EOL> elif msg_type == _widcomm . L2CAP_CONNECTED : <EOL> self . connecting = False <EOL> self . listening = False <EOL> self . connected = True <EOL> def l2cap_bind ( self , addrport ) : <EOL> dbg ( "<STR_LIT>" % str ( addrport ) ) <EOL> addr , port = addrport <EOL> if len ( addr ) : <EOL> raise ValueError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> result = self . _if . assign_psm_value ( L2CAP_UUID , port ) <EOL> if not result : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> self . bound = True <EOL> self . port = self . _if . get_psm ( ) <EOL> result = self . _if . register ( ) <EOL> if not result : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> def l2cap_listen ( self , backlog ) : <EOL> dbg ( "<STR_LIT>" % backlog ) <EOL> if self . connected : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if self . listening : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if backlog != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> port = self . _if . get_psm ( ) <EOL> self . _if . set_security_level ( "<STR_LIT>" , _widcomm . BTM_SEC_NONE , True ) <EOL> if not port : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> result = self . _wc . listen ( self . _if ) <EOL> if not result : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> self . listening = True <EOL> def l2cap_accept ( self ) : <EOL> dbg ( "<STR_LIT>" ) <EOL> if self . connected : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> while self . listening and not self . connected : <EOL> dbg ( "<STR_LIT>" ) <EOL> self . l2cap_read_msg ( ) <EOL> if self . connected : <EOL> port = self . _if . get_psm ( ) <EOL> client_bdaddr = BD_ADDR_to_str ( self . _wc . remote_bd_addr ( ) ) <EOL> client_port = <NUM_LIT:0> <EOL> _sockdata = self . _wc , self . _if , self . readsock <EOL> clientsock = BluetoothSocket ( L2CAP , _sockdata ) <EOL> self . __l2cap_make_cobjects ( ) <EOL> return clientsock , ( client_bdaddr , client_port ) <EOL> def l2cap_connect ( self , addrport ) : <EOL> addr , port = addrport <EOL> dbg ( "<STR_LIT>" % ( addr , port ) ) <EOL> if not is_valid_address ( addr ) : <EOL> raise ValueError ( "<STR_LIT>" % addr ) <EOL> if not self . _if . assign_psm_value ( L2CAP_UUID , port ) : <EOL> raise BluetoothError ( "<STR_LIT>" % port ) <EOL> if not self . _if . set_security_level ( "<STR_LIT>" , _widcomm . BTM_SEC_NONE , False ) : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if not self . _if . register ( ) : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> self . connecting = True <EOL> if not self . _wc . connect ( self . _if , str_to_BD_ADDR ( addr ) ) : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> while self . connecting : <EOL> self . l2cap_read_msg ( ) <EOL> if not self . connected : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> def l2cap_send ( self , data ) : <EOL> dbg ( "<STR_LIT>" % data ) <EOL> status , written = self . _wc . write ( data ) <EOL> if status : <EOL> dbg ( "<STR_LIT>" ) <EOL> return written <EOL> else : <EOL> raise BluetoothError ( _port_return_code_to_str ( status ) ) <EOL> def l2cap_recv ( self , numbytes ) : <EOL> if self . nonblocking and not self . received_data : <EOL> return None <EOL> while not self . received_data and self . connected : <EOL> self . l2cap_read_msg ( ) <EOL> if self . received_data : <EOL> data = self . received_data . pop ( <NUM_LIT:0> ) <EOL> if len ( data ) > numbytes : <EOL> self . received_data . insert ( <NUM_LIT:0> , data [ numbytes : ] ) <EOL> return data [ : numbytes ] <EOL> else : <EOL> return data <EOL> def l2cap_close ( self ) : <EOL> self . _wc . disconnect ( ) <EOL> self . _if . deregister ( ) <EOL> self . _wc = None <EOL> self . bound = False <EOL> self . connecting = False <EOL> self . listening = False <EOL> self . connected = False <EOL> def l2cap_getsockname ( self ) : <EOL> if not self . bound : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> addr = inquirer . get_local_device_address ( ) <EOL> port = self . _if . get_psm ( ) <EOL> return addr , port <EOL> def l2cap_setblocking ( self , blocking ) : <EOL> self . nonblocking = not blocking <EOL> self . readsock . setblocking ( blocking ) <EOL> def l2cap_settimeout ( self , timeout ) : <EOL> raise NotImplementedError <EOL> def l2cap_gettimeout ( self ) : <EOL> raise NotImplementedError <EOL> def l2cap_fileno ( self ) : <EOL> return self . readsock . fileno ( ) <EOL> def l2cap_dup ( self ) : <EOL> raise NotImplementedError <EOL> def l2cap_makefile ( self ) : <EOL> raise NotImplementedError <EOL> def __l2cap_advertise_service ( self , name , service_id , <EOL> service_classes , profiles , provider , description , <EOL> protocols ) : <EOL> if self . _sdpservice is not None : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if not self . listening : <EOL> raise BluetoothError ( "<STR_LIT>" ) <EOL> if protocols : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> self . _sdpservice = _widcomm . _WCSdpService ( ) <EOL> if service_classes : <EOL> service_classes = [ to_full_uuid ( s ) for s in service_classes ] <EOL> _sdp_checkraise ( self . _sdpservice . add_service_class_id_list ( service_classes ) ) <EOL> _sdp_checkraise ( self . _sdpservice . add_l2cap_protocol_descriptor ( self . port ) ) <EOL> if profiles : <EOL> for uuid , version in profiles : <EOL> uuid = to_full_uuid ( uuid ) <EOL> _sdp_checkraise ( self . _sdpservice . add_profile_descriptor_list ( uuid , version ) ) <EOL> _sdp_checkraise ( self . _sdpservice . add_service_name ( name ) ) <EOL> _sdp_checkraise ( self . _sdpservice . make_public_browseable ( ) ) <EOL> class DeviceDiscoverer : <EOL> def __init__ ( self ) : <EOL> raise NotImplementedError </s>
