Ejemplo n.º 1
0
    def import_include_from_dictionary(self, include_dictionary):
        """
        Populate include attributes from a dictionary containing included service information.
        This is complimentary to export_include_to_dictionary .

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

        include_attributes = include_dictionary.keys()

        if 'included_service_uuid' in include_attributes:
            uuid = validator.validate_attribute_uuid(
                include_dictionary['included_service_uuid'])
            self.included_service_uuid = uuid
        else:
            raise validator.InvalidUUIDException(None)

        self.determine_type()

        if 'handle' in include_attributes:
            handle = validator.validate_int_att_handle(
                include_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.included_service_att_handle = 0x00

        if 'included_service_att_handle' in include_attributes:
            included_service_att_handle = validator.validate_int_att_handle(
                include_dictionary['included_service_att_handle'])
            self.included_service_att_handle = included_service_att_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.included_service_att_handle = 0x00

        if 'included_service_end_group_handle' in include_attributes:
            end = validator.validate_int_att_handle(
                include_dictionary['included_service_end_group_handle'])
            self.included_service_end_group_handle = end
        else:
            self.included_service_end_group_handle = 0x00

        if 'include_definition_attribute_properties' in include_attributes:
            att_properties = include_dictionary[
                'include_definition_attribute_properties']

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

        if 'include_definition_attribute_read_permission' in include_attributes:
            permission_dictionary = include_dictionary[
                'include_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.include_definition_attribute_read_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'include_definition_attribute_write_permission' in include_attributes:
            permission_dictionary = include_dictionary[
                'include_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.include_definition_attribute_write_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'include_definition_attribute_require_authorization' in include_attributes:
            require_auth = include_dictionary[
                'include_definition_attribute_require_authorization']
            if require_auth is not None:
                self.include_definition_attribute_require_authorization = require_auth
        return
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def import_service_from_dictionary(self, service_dictionary):
        """
        Populate service attributes from a dictionary containing service information. This is complimentary to
        export_service_to_dictionary .

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

        service_attributes = service_dictionary.keys()

        if 'uuid' in service_attributes:
            uuid = validator.validate_attribute_uuid(
                service_dictionary['uuid'])
            self.uuid = uuid
        else:
            raise validator.InvalidUUIDException(None)

        self.determine_type()

        if 'start_handle' in service_attributes:
            start = validator.validate_int_att_handle(
                service_dictionary['start_handle'])
            self.start = start
        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.start = 0x00

        if 'end_handle' in service_attributes:
            end = validator.validate_int_att_handle(
                service_dictionary['end_handle'])
            self.end = end
        else:
            self.end = 0x00

        if 'attribute_type' in service_attributes:
            att_type = validator.validate_attribute_uuid(
                service_dictionary['attribute_type'])
            self.attribute_type = att_type
            # If not present, it will default to a primary service

        if 'service_definition_attribute_properties' in service_attributes:
            att_properties = service_dictionary[
                'service_definition_attribute_properties']
            self.service_definition_attribute_properties = 0
            for att_property in att_properties:

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

        if 'service_definition_attribute_read_permission' in service_attributes:
            permission_dictionary = service_dictionary[
                'service_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.service_definition_attribute_read_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'service_definition_attribute_write_permission' in service_attributes:
            permission_dictionary = service_dictionary[
                'service_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.service_definition_attribute_write_permission = att_utils.get_att_security_mode_from_mode_and_level(
                mode, level)

        if 'service_definition_attribute_require_authorization' in service_attributes:
            require_auth = service_dictionary[
                'service_definition_attribute_require_authorization']
            if require_auth is not None:
                self.service_definition_attribute_require_authorization = require_auth

        if 'characteristics' in service_attributes:
            characteristic_list = service_dictionary['characteristics']
            for characteristic_dictionary in characteristic_list:
                # value_handle, handle, uuid, gatt_properties, service_uuid
                gatt_characteristic = BLECharacteristic(
                    None, None, None, None, None)
                gatt_characteristic.import_characteristic_from_dictionary(
                    characteristic_dictionary)
                self.characteristics.append(gatt_characteristic)

        if 'includes' in service_attributes:
            include_list = service_dictionary['includes']
            for include_dictionary in include_list:
                # handle, included_service_att_handle, end, uuid
                gatt_include = BLEInclude(None, None, None, None)
                gatt_include.import_include_from_dictionary(include_dictionary)
                self.includes.append(gatt_include)