Example #1
0
    def on_connected(packet, future):
        self.logger.debug("Device connected event received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            device = packet.get_dict([
                'controller_id',
                'connection_handle',
                'address',
                ('address_type', lambda value: 'public' if value == 0 else 'random')
            ])

            self.register_callback(
                PacketUuid(payload_type=Payload.DeviceDisconnected,
                           controller_id=controller_id,
                           connection_handle=device['connection_handle']),
                callback=on_unexpected_disconnection
            )

            on_connected_cb(True, device, None, *on_connected_params)
            future.set_result(device)
        else:
            error = BaBLEException(packet, "Failed to connect", address=address)
            on_connected_cb(False, None, error, *on_connected_params)
            future.set_exception(error)
            return
Example #2
0
    def on_response_received(packet, future):
        self.logger.debug("Connect response received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code != StatusCode.Success:
            self.remove_callback(connected_event_uuid)
            error = BaBLEException(packet, "Failed to connect", address=address)
            on_connected_cb(False, None, error, *on_connected_params)
            future.set_exception(error)
Example #3
0
    def on_ack_received(packet, future):
        self.logger.debug("EmitNotification ack received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            future.set_result(True)
        else:
            error = BaBLEException(packet, "Failed to send EmitNotification packet",
                                   connection_handle=connection_handle,
                                   attribute_handle=attribute_handle)
            future.set_exception(error)
Example #4
0
    def on_response_received(packet, future):
        self.logger.debug("Cancel connection response received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            on_connection_cancelled_cb(True, None, None, *on_connection_cancelled_params)
            future.set_result(True)
        else:
            error = BaBLEException(packet, "Failed to cancel connection")
            on_connection_cancelled_cb(False, None, error, *on_connection_cancelled_params)
            future.set_exception(error)
Example #5
0
    def on_response_received(packet, future):
        self.logger.debug("Stop scan response received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            self.remove_callback(PacketUuid(payload_type=Payload.DeviceFound, controller_id=controller_id))
            on_scan_stopped_cb(True, packet.get_dict(['controller_id']), None, *on_scan_stopped_params)
            future.set_result(True)
        else:
            error = BaBLEException(packet, "Failed to stop scan")
            on_scan_stopped_cb(False, None, error, *on_scan_stopped_params)
            future.set_exception(error)
Example #6
0
    def on_response_received(packet, future):
        self.logger.debug("GATT table set with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            data = packet.get_dict(['controller_id'])

            on_set_cb(True, data, None, *on_set_params)
            future.set_result(data)
        else:
            error = BaBLEException(packet, "Failed to set GATT table")
            on_set_cb(False, None, error, *on_set_params)
            future.set_exception(error)
Example #7
0
    def on_response_received(packet, future):
        self.logger.debug("List of controllers received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            controllers = packet.get(
                name='controllers',
                format_function=lambda raw_ctrls: [Controller.from_flatbuffers(raw_ctrl) for raw_ctrl in raw_ctrls]
            )

            future.set_result(controllers)
        else:
            future.set_exception(BaBLEException(packet, "Failed to list controllers"))
Example #8
0
    def on_response_received(packet, future):
        self.logger.debug("List of connected devices received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            devices = packet.get(
                name='devices',
                format_function=lambda raw_devs: [Device.from_flatbuffers(raw_dev) for raw_dev in raw_devs]
            )

            future.set_result(devices)
        else:
            future.set_exception(BaBLEException(packet, "Failed to list connected devices"))
Example #9
0
 def _generate_error(controller_id,
                     message,
                     native_class='HCI',
                     native_status=0x0c,
                     status=StatusCode.StatusCode.Denied,
                     **kwargs):
     return BaBLEException(packet=Packet(
         payload_type=Payload.Payload.BaBLEError,
         controller_id=controller_id,
         native_class=native_class,
         native_status=native_status,
         status=status,
         message=message),
                           **kwargs)
Example #10
0
    def on_response_received(packet, future):
        self.logger.debug("Probe characteristics response received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            characteristics = packet.get_dict([
                'controller_id',
                'connection_handle',
                ('characteristics', lambda chars: [Characteristic.from_flatbuffers(char) for char in chars])
            ])
            on_characteristics_probed_cb(True, characteristics, None, *on_characteristics_probed_params)
            future.set_result(characteristics)
        else:
            error = BaBLEException(packet, "Failed to probe characteristics", connection_handle=connection_handle)
            on_characteristics_probed_cb(False, None, error, *on_characteristics_probed_params)
            future.set_exception(error)
Example #11
0
    def on_response_received(packet, future):
        self.logger.debug("Probe services response received with status={}".format(packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            services = packet.get_dict([
                'controller_id',
                'connection_handle',
                ('services', lambda services: [Service.from_flatbuffers(service) for service in services])
            ])
            on_services_probed_cb(True, services, None, *on_services_probed_params)
            future.set_result(services)
        else:
            error = BaBLEException(packet, "Failed to probe services", connection_handle=connection_handle)
            on_services_probed_cb(False, None, error, *on_services_probed_params)
            future.set_exception(error)
Example #12
0
    def on_response_received(packet, future):
        self.logger.debug("Write response received with status={}".format(
            packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            data = packet.get_dict(
                ['controller_id', 'connection_handle', 'attribute_handle'])

            on_written_cb(True, data, None, *on_written_params)

            future.set_result(data)
        else:
            error = BaBLEException(packet,
                                   "Failed to write value",
                                   connection_handle=connection_handle,
                                   attribute_handle=attribute_handle)
            on_written_cb(False, None, error, *on_written_params)
            future.set_exception(error)
Example #13
0
    def on_device_disconnected(packet, future):
        self.logger.debug(
            "Device disconnected event received with status={}".format(
                packet.status))
        self.remove_callback(packet.packet_uuid)

        if packet.status_code == StatusCode.Success:
            data = packet.get_dict(
                ['controller_id', 'connection_handle', 'reason', 'code'])

            self.remove_callback(PacketUuid(
                controller_id=controller_id,
                connection_handle=connection_handle),
                                 match_connection_only=True)
            on_disconnected_cb(True, data, None, *on_disconnected_params)
            future.set_result(data)
        else:
            error = BaBLEException(packet, "Failed to disconnect")
            on_disconnected_cb(False, None, error, *on_disconnected_params)
            future.set_exception(error)