gusucode.com > vnt工具箱matlab源码程序 > vnt/vnt/+xcp/ODT.m

    classdef ODT < handle
% ODT Class that implements an XCP ODT.

% Authors: JDP
% Copyright 2012-2016 The MathWorks, Inc.

properties
    % Number - The absolute/relative ODT number for this ODT.
    Number
    % ODTEntryInfo - An array of structures of measurement information 
    %    for this ODT's measurements.
    ODTEntryInfo
    % A2LFileObject - An xcp.A2L file object.
    A2LFileObject
    % BytesFree - The number of bytes available for measurements in this ODT.
    BytesFree
    % STIMMessageObject - A message object for STIM ODTs.
    STIMMessageObject
end

properties (Dependent)
    % MeasurementNames - Quick reference to the names of all measurements
    %   used by this ODT as a cell array of strings.
    MeasurementNames
    % NumberOfODTEntries - Quick reference to the number of ODT entries in 
    %   this ODT.
    NumberOfODTEntries
end

methods
    
    function obj = ODT(a2lFileObject)
    % ODT Object constructor.
    
        % Set basic properties from the input.
        obj.A2LFileObject = a2lFileObject;
    
        % Set the number of bytes available in this ODT for data. We take
        % the maximum defined DTO size and subtract the following:
        %   - The number of bytes taken by the identification field.
        obj.BytesFree = double(obj.A2LFileObject.ProtocolLayerInfo.MaxDTO) - ...
            xcp.Utility.MAP_IDENTIFICATION_FIELD_TYPE_BYTES_USED( ...
            obj.A2LFileObject.DAQInfo.IdentificationFieldType);
        
        % Initialize an empty ODT entry structure.
        obj.ODTEntryInfo = struct([]);
    end    
    
    function result = addMeasurement(obj, measurementInfo)
    % addMeasurement Attempts to add a measurement to this ODT.    
    %
    %   measurementInfo - A structure of information about the measurement.
    %
    %   result - Boolean value indicating whether the measurement was added
    %       or does not fit into this ODT.
    %
    %   This method attempts to add the given measurement to this ODT. If
    %   the ODT has enough bytes available to fit the measurement, it
    %   stores the measurement information and returns true. Otherwise, it
    %   will return false.
        
        % Check if the measurement fits in the space remaining.
        if (obj.BytesFree - measurementInfo.SizeInBytes) < 0
            % Return false to indicate that this measurement does not fit
            % within this ODT.
            result = false;
            return;
        end
        
        % Adjust the number of bytes remaining.
        obj.BytesFree = obj.BytesFree - measurementInfo.SizeInBytes;
        
        % Calculate the start byte index for this entry in the ODT. In the
        % future, if timestamps are supported, this required adjustment.
        if obj.NumberOfODTEntries == 0
            % If this is the first ODT Entry being placed in this ODT,
            % start the positioning after the identifiction field.
            firstByte = 1 + xcp.Utility.MAP_IDENTIFICATION_FIELD_TYPE_BYTES_USED( ...
                obj.A2LFileObject.DAQInfo.IdentificationFieldType);
        else
            % Otherwise, start the positioning as the next byte after the 
            % last ODT Entry already placed in the ODT.
            firstByte = obj.ODTEntryInfo(end).LastByte + 1;
        end
        
        % Calculate the stop byte index for this entry in the ODT. Also
        % adjust for proper indexing by subtracting 1.
        lastByte = firstByte + measurementInfo.SizeInBytes - 1;
        
        % Set the byte locations into the measurement info.
        measurementInfo.FirstByte = firstByte;
        measurementInfo.LastByte = lastByte;
        
        % Add the measurement information to this ODT.
        obj.ODTEntryInfo = [obj.ODTEntryInfo measurementInfo];
        
        % Return true as the measurement was accepted.
        result = true;
    end
    
    function ODTEntryInfo = getODTEntryInfo(obj, measurement)
    % getODTEntryInfo Finds and returns an ODT entry information structure by name.
    %
    %   measurement - The name of the measurement/ODT entry.
    %
    %   ODTEntryInfo - A measurement/ODT entry information structure.
    %
    %   This method searches all the ODT entries in this ODT to find a
    %   match to the provided ODT entry by name. If no match is found, the
    %   method returns empty.   
        
        % Use logical indexing to find a match of the input measurement
        % name to the those configured in this ODT.
        index = strcmpi(measurement, {obj.ODTEntryInfo.Name});
        
        % Index for the output to return the ODT Entry information.
        ODTEntryInfo = obj.ODTEntryInfo(index);
    end
    
    function result = writeSTIMData(obj, measurement, value)
    % writeSTIMData Writes STIM data into the ODT message data.
    %
    %   measurement - The name of the measurement for which to update the
    %       data value.
    %   value - The new data value for the measurement.
    %
    %   result - Boolean value indicating success or failed write.
    %
    %   This method writes a new data value within the ODT STIM message for
    %   the specified measurement. If the write was a success, then method
    %   returns true, otherwise false. A write should only fail because
    %   this ODT does not contain the specified measurement. The data value
    %   is updated directly in the ODT STIM message.
        
        % Get the ODT entry information for the measurement.
        ODTEntryInfo = obj.getODTEntryInfo(measurement); %#ok<*PROP>
        
        % If the ODT entry was not found, then exit.
        if isempty(ODTEntryInfo)
            return;
        end
        
        % Convert the data value into bytes suitable for packing.
        byteValue = xcp.Utility.packMeasurement(ODTEntryInfo, value);

        % Put the new value bytes into the message data by overwriting the
        % existing bytes in those same locations.
        obj.STIMMessageObject.Data(ODTEntryInfo.FirstByte:ODTEntryInfo.LastByte) = byteValue;
        
        % Return true for success.
        result = true;
    end
    
end

    
methods % Custom get/set methods for properties.
        
    function value = get.MeasurementNames(obj)
        value = {obj.ODTEntryInfo.Name}';
    end
    
    function value = get.NumberOfODTEntries(obj)
        value = numel(obj.ODTEntryInfo);
    end
    
end % Custom get/set methods for properties.

end