Exemple #1
0
    def add_user_description_descriptor(self, handle, name,
                                        characteristic_descriptor_attribute_properties=att_utils.ATT_PROP_READ,
                                        characteristic_descriptor_attribute_read_permission=att_utils.ATT_SECURITY_MODE_OPEN,
                                        characteristic_descriptor_attribute_write_permission=att_utils.ATT_SECURITY_MODE_NO_ACCESS,
                                        characteristic_descriptor_attribute_require_authorization=False):
        """
        Add a user description descriptor to the characteristic class instance.

        :var handle: Handle of descriptor
        :var name: Value stored in descriptor
        :var characteristic_descriptor_attribute_properties: Attribute properties (default blesuite.utils.att_utils.ATT_PROP_READ)
        :var characteristic_descriptor_attribute_read_permission: Required security mode to read attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_OPEN)
        :var characteristic_descriptor_attribute_write_permission: Required security mode to write to attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_NO_ACCESS)
        :var characteristic_descriptor_attribute_require_authorization: Flag to indicate that access of the attribute requires authorization (default False)
        :type handle: int
        :type name: str
        :type characteristic_descriptor_attribute_properties: blesuite.utils.att_utils.ATT_PROP_*
        :type characteristic_descriptor_attribute_read_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type characteristic_descriptor_attribute_write_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type characteristic_descriptor_attribute_require_authorization: bool
        :return: User description descriptor
        :rtype: blesuite.entities.gatt_descriptor
        """
        descriptor = BLEDescriptor(handle, "2901", name,
                                   characteristic_descriptor_attribute_properties,
                                   characteristic_descriptor_attribute_read_permission,
                                   characteristic_descriptor_attribute_write_permission,
                                   characteristic_descriptor_attribute_require_authorization)
        self.descriptors.append(descriptor)
        return descriptor
Exemple #2
0
    def add_client_characteristic_configuration_descriptor(self, handle,
                                                           characteristic_descriptor_attribute_properties=att_utils.ATT_PROP_READ|att_utils.ATT_PROP_WRITE,
                                                           characteristic_descriptor_attribute_read_permission=att_utils.ATT_SECURITY_MODE_OPEN,
                                                           characteristic_descriptor_attribute_write_permission=att_utils.ATT_SECURITY_MODE_OPEN,
                                                           characteristic_descriptor_attribute_require_authorization=False):
        """
        Add a client characteristic configuration (CCC) descriptor to the characteristic class instance. This is required
        for characteristics configured to support indication or notification. Default value set to \x00\x00
        (notifications and indications disabled).

        :var handle: Handle of descriptor
        :var characteristic_descriptor_attribute_properties: Attribute properties (default blesuite.utils.att_utils.ATT_PROP_READ|blesuite.utils.att_utils.ATT_PROP_WRITE)
        :var characteristic_descriptor_attribute_read_permission: Required security mode to read attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_OPEN)
        :var characteristic_descriptor_attribute_write_permission: Required security mode to write to attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_OPEN)
        :var characteristic_descriptor_attribute_require_authorization: Flag to indicate that access of the attribute requires authorization (default False)
        :type handle: int
        :type characteristic_descriptor_attribute_properties: blesuite.utils.att_utils.ATT_PROP_*
        :type characteristic_descriptor_attribute_read_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type characteristic_descriptor_attribute_write_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type characteristic_descriptor_attribute_require_authorization: bool
        :return: CCC descriptor
        :rtype: blesuite.entities.gatt_descriptor
        """
        descriptor = BLEDescriptor(handle, "2902", "\x00\x00",
                                   characteristic_descriptor_attribute_properties,
                                   characteristic_descriptor_attribute_read_permission,
                                   characteristic_descriptor_attribute_write_permission,
                                   characteristic_descriptor_attribute_require_authorization)
        self.descriptors.append(descriptor)
        return descriptor
Exemple #3
0
    def add_descriptor_with_data(self, handle, uuid, data):
        """
        Create a descriptor object, set the object's lastReadValue, and
        add it to the descriptors list.

        :param handle: Handle of descriptor
        :param uuid: UUID of descriptor
        :param data: Data received after reading from descriptor handle
        :type handle: int - base 10
        :type uuid: str
        :type data: list of strings
        :return: Descriptor
        :rtype: blesuite.entities.gatt_descriptor
        """
        descriptor = BLEDescriptor(handle, uuid, data)
        if self.value_handle == handle:
            descriptor.type_string = "Characteristic Value Declaration"
        self.descriptors.append(descriptor)
        return descriptor
    def add_descriptor_with_data(
            self,
            handle,
            uuid,
            data,
            characteristic_descriptor_attribute_properties=att_utils.
        ATT_PROP_READ | att_utils.ATT_PROP_WRITE,
            characteristic_descriptor_attribute_read_permission=att_utils.
        ATT_SECURITY_MODE_OPEN,
            characteristic_descriptor_attribute_write_permission=att_utils.
        ATT_SECURITY_MODE_OPEN,
            characteristic_descriptor_attribute_require_authorization=False):
        """
        Create a descriptor object, set the object's lastReadValue, and
        add it to the descriptors list.

        :param handle: Handle of descriptor
        :param uuid: UUID of descriptor
        :param data: Data received after reading from descriptor handle
        :param characteristic_descriptor_attribute_properties: Attribute properties (default blesuite.utils.att_utils.ATT_PROP_READ)
        :param characteristic_descriptor_attribute_read_permission: Required security mode to read attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_OPEN)
        :param characteristic_descriptor_attribute_write_permission: Required security mode to write to attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_NO_ACCESS)
        :param characteristic_descriptor_attribute_require_authorization: Flag to indicate that access of the attribute requires authorization (default False)
        :type handle: int - base 10
        :type uuid: str
        :type data: list of strings
        :type characteristic_descriptor_attribute_properties: blesuite.utils.att_utils.ATT_PROP_*
        :type characteristic_descriptor_attribute_read_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type characteristic_descriptor_attribute_write_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type characteristic_descriptor_attribute_require_authorization: bool
        :return: Descriptor
        :rtype: blesuite.entities.gatt_descriptor
        """
        descriptor = BLEDescriptor(
            handle, uuid, data, characteristic_descriptor_attribute_properties,
            characteristic_descriptor_attribute_read_permission,
            characteristic_descriptor_attribute_write_permission,
            characteristic_descriptor_attribute_require_authorization)
        if self.value_handle == handle:
            descriptor.type_string = "Characteristic Value Declaration"
        self.descriptors.append(descriptor)
        return descriptor
    def import_characteristic_from_dictionary(self, characteristic_dictionary):
        """
        Populate characteristic attributes from a dictionary containing characteristic information.
        This is complimentary to export_characteristic_to_dictionary .

        :param characteristic_dictionary: Dictionary containing characteristic information
        :type characteristic_dictionary: dict
        :return:
        :rtype:
        :raises blesuite.pybt.gatt.InvalidUUIDException: if the provided characteristic dictionary contains a characteristic with an invalid UUID
        :raises blesuite.utils.validators.InvalidATTHandle: if the provided characteristic dictionary contains a characteristic with an invalid handle
        :raises blesuite.utils.validators.InvalidGATTProperty: if the provided characteristic dictionary contains a characteristic with an invalid GATT property
        :raises blesuite.utils.validators.InvalidATTProperty: if the provided characteristic dictionary contains a characteristic with an invalid attribute property
        :raises blesuite.utils.validators.InvalidATTSecurityMode: if the provided characteristic dictionary contains a characteristic with an invalid attribute permission
        """
        import blesuite.utils.validators as validator

        characteristic_attributes = characteristic_dictionary.keys()

        if 'uuid' in characteristic_attributes:
            uuid = validator.validate_attribute_uuid(
                characteristic_dictionary['uuid'])
            self.uuid = uuid
        else:
            return validator.InvalidUUIDException(None)

        self.determine_type()

        if 'handle' in characteristic_attributes:
            handle = validator.validate_int_att_handle(
                characteristic_dictionary['handle'])
            self.handle = handle
        else:
            # This will allow us to disregard adding handles to our import JSON file and we can calculate during
            # the gatt_server creation that uses the BLEDevice (flag enabled by default)
            self.handle = 0x00

        if 'value_handle' in characteristic_attributes:
            handle = validator.validate_int_att_handle(
                characteristic_dictionary['value_handle'])
            self.value_handle = handle
        else:
            self.value_handle = 0x00

        if 'value' in characteristic_attributes:
            self.value = characteristic_dictionary['value']

        gatt_properties = 0
        if 'gatt_properties' in characteristic_attributes:
            property_list = characteristic_dictionary['gatt_properties']
            for gatt_property in property_list:
                validated_property = validator.validate_gatt_property(
                    gatt_property)
                translated_property = Permissions.permission_dictionary_lookup_by_name[
                    validated_property]
                gatt_properties |= translated_property
        self.gatt_properties = gatt_properties

        # Characteristic Definition (aka Characteristic Declaration)

        if 'characteristic_definition_attribute_properties' in characteristic_attributes:
            att_properties = characteristic_dictionary[
                'characteristic_definition_attribute_properties']

            for att_property in att_properties:
                self.characteristic_definition_attribute_properties = 0
                validated_att_property = validator.validate_att_property(
                    att_property)
                if validated_att_property == "read":
                    self.characteristic_definition_attribute_properties |= att_utils.ATT_PROP_READ
                elif validated_att_property == "write":
                    self.characteristic_definition_attribute_properties |= att_utils.ATT_PROP_WRITE

        if 'characteristic_definition_attribute_read_permission' in characteristic_attributes:
            permission_dictionary = characteristic_dictionary[
                'characteristic_definition_attribute_read_permission']
            permission_keys = permission_dictionary.keys()
            if "security_mode" not in permission_keys:
                mode = None
            else:
                mode = permission_dictionary['security_mode']
            if "security_level" not in permission_keys:
                level = None
            else:
                level = permission_dictionary['security_level']

            mode, level = validator.validate_att_security_mode(mode, level)
            self.characteristic_definition_attribute_read_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'characteristic_definition_attribute_write_permission' in characteristic_attributes:
            permission_dictionary = characteristic_dictionary[
                'characteristic_definition_attribute_write_permission']
            permission_keys = permission_dictionary.keys()
            if "security_mode" not in permission_keys:
                mode = None
            else:
                mode = permission_dictionary['security_mode']
            if "security_level" not in permission_keys:
                level = None
            else:
                level = permission_dictionary['security_level']

            mode, level = validator.validate_att_security_mode(mode, level)
            self.characteristic_definition_attribute_write_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'characteristic_definition_attribute_require_authorization' in characteristic_attributes:
            require_auth = characteristic_dictionary[
                'characteristic_definition_attribute_require_authorization']
            if require_auth is not None:
                self.characteristic_definition_attribute_require_authorization = require_auth

        # Characteristic Value Declaration

        if 'characteristic_value_attribute_properties' in characteristic_attributes:
            att_properties = characteristic_dictionary[
                'characteristic_value_attribute_properties']
            self.characteristic_value_attribute_properties = 0
            for att_property in att_properties:

                validated_att_property = validator.validate_att_property(
                    att_property)
                if validated_att_property == "read":
                    self.characteristic_value_attribute_properties |= att_utils.ATT_PROP_READ
                elif validated_att_property == "write":
                    self.characteristic_value_attribute_properties |= att_utils.ATT_PROP_WRITE

        if 'characteristic_value_attribute_read_permission' in characteristic_attributes:
            permission_dictionary = characteristic_dictionary[
                'characteristic_value_attribute_read_permission']
            permission_keys = permission_dictionary.keys()
            if "security_mode" not in permission_keys:
                mode = None
            else:
                mode = permission_dictionary['security_mode']
            if "security_level" not in permission_keys:
                level = None
            else:
                level = permission_dictionary['security_level']

            mode, level = validator.validate_att_security_mode(mode, level)
            self.characteristic_value_attribute_read_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'characteristic_value_attribute_write_permission' in characteristic_attributes:
            permission_dictionary = characteristic_dictionary[
                'characteristic_value_attribute_write_permission']
            permission_keys = permission_dictionary.keys()
            if "security_mode" not in permission_keys:
                mode = None
            else:
                mode = permission_dictionary['security_mode']
            if "security_level" not in permission_keys:
                level = None
            else:
                level = permission_dictionary['security_level']

            mode, level = validator.validate_att_security_mode(mode, level)
            self.characteristic_value_attribute_write_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'characteristic_value_attribute_require_authorization' in characteristic_attributes:
            require_auth = characteristic_dictionary[
                'characteristic_value_attribute_require_authorization']
            if require_auth is not None:
                self.characteristic_value_attribute_require_authorization = require_auth

        if 'descriptors' in characteristic_attributes:
            descriptor_list = characteristic_dictionary['descriptors']
            for descriptor_dictionary in descriptor_list:
                # value_handle, handle, uuid, gatt_properties, service_uuid
                gatt_characteristic = BLEDescriptor(None, None)
                gatt_characteristic.import_descriptor_from_dictionary(
                    descriptor_dictionary)
                self.descriptors.append(gatt_characteristic)