Exemplo n.º 1
0
    def add_include(self, handle, included_service_att_handle,
                    end_group_handle, uuid):
        """
        Add a include service object to it's associated service on the device

        :param handle: Handle of characteristic
        :param included_service_att_handle: Handle of included service definition
        :param end_group_handle: End group handle of included service
        :param uuid: UUID of characteristic
        :type handle: int
        :type included_service_att_handle: int
        :type end_group_handle: int
        :type uuid: str
        :return: New include service
        :rtype: blesuite.entities.gatt_include
        """

        for service in self.services:
            start = service.start
            end = service.end
            if start <= handle <= end:
                incl = BLEInclude(handle, included_service_att_handle,
                                  end_group_handle, uuid)
                service.includes.append(incl)

        return incl
Exemplo n.º 2
0
    def add_include(
            self,
            handle,
            included_service_att_handle,
            end,
            uuid,
            attribute_type="2802",
            include_definition_attribute_properties=att_utils.ATT_PROP_READ,
            include_definition_attribute_read_permission=att_utils.
        ATT_SECURITY_MODE_OPEN,
            include_definition_attribute_write_permission=att_utils.
        ATT_SECURITY_MODE_NO_ACCESS,
            include_definition_attribute_require_authorization=False):
        """
        Add a service include object to the service class instance.

        :var handle: Handle of attribute
        :var included_service_att_handle: Start handle for included service
        :var end: End handle for included service
        :var uuid: UUID of included service
        :var attribute_type: Attribute type UUID (default "2802" - include service)
        :var include_definition_attribute_properties: Attribute properties (default blesuite.utils.att_utils.ATT_PROP_READ)
        :var include_definition_attribute_read_permission: Required security mode to read attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_OPEN)
        :var include_definition_attribute_write_permission: Required security mode to write to attribute (default blesuite.utils.att_utils.ATT_SECURITY_MODE_NO_ACCESS)
        :var include_definition_attribute_require_authorization: Flag to indicate that access of the attribute requires authorization (default False)
        :type handle: int
        :type included_service_att_handle: int
        :type end: int
        :type uuid: str
        :type attribute_type: str
        :type include_definition_attribute_properties: blesuite.utils.att_utils.ATT_PROP_*
        :type include_definition_attribute_read_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type include_definition_attribute_write_permission: blesuite.utils.att_utils.ATT_SECURITY_MODE_*
        :type include_definition_attribute_require_authorization: bool
        :return: Newly created service include
        :rtype: blesuite.entities.gatt_include
        """

        incl = BLEInclude(handle, included_service_att_handle, end, uuid,
                          attribute_type,
                          include_definition_attribute_properties,
                          include_definition_attribute_read_permission,
                          include_definition_attribute_write_permission,
                          include_definition_attribute_require_authorization)
        self.includes.append(incl)

        return incl
Exemplo 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)