Example #1
0
class BTStack(object):
    def __init__(self, pyusb_dev=None):
        if not pyusb_dev:
            pyusb_dev = \
                pyusb_bt_sockets.find_first_bt_adapter_pyusb_device_or_raise()
        self.hci_socket = pyusb_bt_sockets.PyUSBBluetoothHCISocket(pyusb_dev)
        self.hci = HCIThread(self.hci_socket)
        self.cb_thread = CallbackThread()
        self.cb_thread.register_with_rx_thread(self.hci)
        self.is_scannning_enabled = False
        self.connection_mgr = ConnectionManager(
            self.hci, self.cb_thread)
        self.address = None

    def start(self):
        LOG.debug("BTStack start()")

        # During reset, just ignore and eat all packets that might come in:
        ignore_queue = Queue.Queue()
        self.hci.add_packet_queue(lambda packet: True, ignore_queue)
        self.hci.start()
        self.hci.cmd_reset()
        self.hci.remove_packet_queue(ignore_queue)

        self.cb_thread.start()

        self.hci.cmd_set_event_filter_clear_all_filters()
        self.hci.cmd_set_event_mask()
        self.hci.cmd_le_host_supported()

        # "The LE_Read_Buffer_Size command must be issued by the Host before it
        #  sends any data to an LE Controller":
        self.hci.cmd_le_read_buffer_size()

        self.address = self.hci.cmd_read_bd_addr()

    def start_scan(self):
        assert self.is_scannning_enabled is False
        self.hci.cmd_le_scan_params()
        self.hci.cmd_le_scan_enable(True)
        self.is_scannning_enabled = True

    def stop_scan(self):
        assert self.is_scannning_enabled is True
        self.hci.cmd_le_scan_enable(False)
        self.is_scannning_enabled = False

    def connect(self, address):
        return self.connection_mgr.connect(address)

    def disconnect(self, connection):
        self.connection_mgr.disconnect(connection)

    def quit(self):
        LOG.debug("BTStack quit()")
        self.hci.cmd_reset()
        self.hci.kill()
        self.cb_thread.kill()
Example #2
0
class BTStack(object):
    def __init__(self, pyusb_dev=None):
        if not pyusb_dev:
            pyusb_dev = \
                pyusb_bt_sockets.find_first_bt_adapter_pyusb_device_or_raise()
        self.hci_socket = pyusb_bt_sockets.PyUSBBluetoothHCISocket(pyusb_dev)
        self.hci = HCIThread(self.hci_socket)
        self.cb_thread = CallbackThread()
        self.cb_thread.register_with_rx_thread(self.hci)
        self.is_scannning_enabled = False
        self.connection_mgr = ConnectionManager(self.hci, self.cb_thread)
        self.address = None

    def start(self):
        LOG.debug("BTStack start()")

        # During reset, just ignore and eat all packets that might come in:
        ignore_queue = Queue.Queue()
        self.hci.add_packet_queue(lambda packet: True, ignore_queue)
        self.hci.start()
        self.hci.cmd_reset()
        self.hci.remove_packet_queue(ignore_queue)

        self.cb_thread.start()

        self.hci.cmd_set_event_filter_clear_all_filters()
        self.hci.cmd_set_event_mask()
        self.hci.cmd_le_host_supported()

        # "The LE_Read_Buffer_Size command must be issued by the Host before it
        #  sends any data to an LE Controller":
        self.hci.cmd_le_read_buffer_size()

        self.address = self.hci.cmd_read_bd_addr()

    def start_scan(self):
        assert self.is_scannning_enabled is False
        self.hci.cmd_le_scan_params()
        self.hci.cmd_le_scan_enable(True)
        self.is_scannning_enabled = True

    def stop_scan(self):
        assert self.is_scannning_enabled is True
        self.hci.cmd_le_scan_enable(False)
        self.is_scannning_enabled = False

    def connect(self, address):
        return self.connection_mgr.connect(address)

    def disconnect(self, connection):
        self.connection_mgr.disconnect(connection)

    def quit(self):
        LOG.debug("BTStack quit()")
        self.hci.cmd_reset()
        self.hci.kill()
        self.cb_thread.kill()
Example #3
0
class DcpClient(object):

    def __init__(self, priority="medium"):
        self.lock = threading.Lock()
        self.rest = None
        self.connection = None
        self.priority = priority

    # Returns true is connections are successful
    def connect(self, host, port, bucket, user, pwd, handler):
        # Runs open connection on each node and sends control commands
        self.lock.acquire()
        if self.connection is not None:
            raise ConnectedException("Connection already established")

        self.rest = RestClient(host, port, user, pwd)
        self.rest.update()
        cluster_config = self.rest.get_nodes()
        bucket_config = self.rest.get_bucket(bucket)
        bucket_password = bucket_config['password'].encode('ascii')

        self.connection = ConnectionManager(handler)
        self.connection.connect(cluster_config, bucket_config)

        # Send the sasl auth message
        latch = CountdownLatch(len(self.rest.get_nodes()))
        op = SaslPlain(bucket, bucket_password, latch)
        self.connection.add_operation_all(op)
        # Todo: Check the value of get_result

        # Send the open connection message
        name = 'py_dcp:' + str(uuid.uuid4())[0:7] + str(uuid.uuid4())[0:7]
        latch = CountdownLatch(len(self.rest.get_nodes()))
        op = OpenConnection(FLAG_OPEN_PRODUCER, name, latch)
        self.connection.add_operation_all(op)
        # Todo: Check the value of get_result
        op.get_result()

        # Send the set priority control message
        latch = CountdownLatch(len(self.rest.get_nodes()))
        op = Control("set_priority", self.priority, latch)
        self.connection.add_operation_all(op)
        # Todo: Check the value of get_result

        # Todo: Add the ability to send control messages

        self.lock.release()

    # Returns true if the stream is successfully created
    def add_stream(self, vbucket, flags, start_seqno, end_seqno, vb_uuid,
                   snap_start, snap_end):
        self.lock.acquire()
        if self.connection is None:
            raise ConnectedException("Not connected")

        latch = CountdownLatch()
        op = StreamRequest(vbucket, flags, start_seqno, end_seqno, vb_uuid,
                           snap_start, snap_end, latch)
        self.connection.add_operation(op, vbucket)
        ret = op.get_result()

        self.lock.release()
        return ret

    # Returns true if the stream is closed successfully
    def close_stream(self):
        self.lock.acquire()
        if self.connection is None:
            raise ConnectedException("Not connected")
        raise NotImplementedError("Not impemented yet")
        self.lock.release()

    def close(self):
        self.lock.acquire()
        self.connection.close()
        self.client = None
        self.nodes = None
        self.buckets = None
        self.connection = None
        self.lock.release()