classdef bleLLDataChannelPDUConfig < comm.internal.ConfigBase
%bleLLDataChannelPDUConfig Bluetooth LE LL data channel PDU configuration
%parameters
%   CFGLLDATA = bleLLDataChannelPDUConfig creates a default Bluetooth(R)
%   low energy (LE) data channel protocol data unit (PDU) configuration
%   object.
%
%   CFGLLDATA = bleLLDataChannelPDUConfig(Name, Value) creates a Bluetooth
%   LE data channel PDU configuration object, CFGLLDATA, with the specified
%   property Name set to the specified Value. You can specify additional
%   name-value pair arguments in any order as (Name1, Value1, ..., NameN,
%   ValueN).
%
%   bleLLDataChannelPDUConfig properties:
%
%   LLID               - Link Layer identifier, indicates type of PDU
%   NESN               - Next expected sequence number
%   SequenceNumber     - Transmitting packet sequence number
%   MoreData           - Indication for more data
%   CRCInitialization  - CRC initialization value
%   ControlConfig      - Control PDU payload configuration object
%
%   % Example 1:
%   % Create a bleLLDataChannelPDUConfig object for a data PDU by assigning
%   % the LLID to 'Data (start fragment/complete)'.
%
%   cfgLLData = bleLLDataChannelPDUConfig;
%   cfgLLData.LLID = 'Data (start fragment/complete)'
%
%   % Example 2:
%   % Create a bleLLDataChannelPDUConfig object for a control PDU of type
%   % 'Channel map indication'.
%
%   cfgControl = bleLLControlPDUConfig('Opcode', 'Channel map indication')
%   cfgLLData = bleLLDataChannelPDUConfig('LLID', 'Control', ...
%                                         'ControlConfig', cfgControl)
%
%   % Example 3:
%   % Create a bleLLDataChannelPDUConfig object for a control PDU of type
%   % 'Version indication'.
%
%   cfgControl = bleLLControlPDUConfig('Opcode', 'Version indication');
%   cfgControl.VersionNumber    = '5.0';
%   cfgControl.SubVersionNumber = '008E'
%   cfgLLData = bleLLDataChannelPDUConfig('LLID', 'Control');
%   cfgLLData.ControlConfig = cfgControl
%
%   % Example 4:
%   % Link Layer data channel PDUs end-to-end workflow.
%
%   % Tx side
%   % Create a bleLLDataChannelPDUConfig object for a data PDU with LLID
%   % set to 'Data (start fragment/complete)'.
%   cfgTx = bleLLDataChannelPDUConfig;
%   cfgTx.LLID = 'Data (start fragment/complete)';
%   cfgTx.CRCInitialization = '123456'
%
%   %  Generate a data PDU with upper-layer payload '030004000A0100'.
%   pdu = bleLLDataChannelPDU(cfgTx, '030004000A0100');
%
%   % Rx side
%   crcInit = '123456'; % Received during association
%   % Decode a Link Layer data channel PDU.
%   [status, cfgRx, llPayload] = bleLLDataChannelPDUDecode(pdu, crcInit);
%   if strcmp(status, 'Success') % Decoding is successful
%       cfgRx
%       % Do upper-layer processing with llPayload.
%   else
%       fprintf('Link Layer decoding status ::');
%       status
%   end
%
%   See also bleLLControlPDUConfig, bleLLDataChannelPDU,
%   bleLLDataChannelPDUDecode.

%   Copyright 2018-2022 The MathWorks, Inc.

%#codegen

properties
    %LLID Link Layer identifier, indicates type of PDU
    %   Specify the LLID as one of 'Data (continuation fragment/empty)' |
    %   'Data (start fragment/complete)' | 'Control'. This property
    %   indicates the type of data channel PDU. The default value is 'Data
    %   (continuation fragment/empty)'.
    LLID
    
    %NESN Next expected sequence number
    %   Specify the NESN as 0 or 1. This property indicates either an
    %   acknowledgment for the last received packet or a request for
    %   resending the last transmitted packet from the peer. The default
    %   value is 0.
    NESN = 0
    
    %SequenceNumber Transmitting packet sequence number
    %   Specify the sequence number as 0 or 1. This property indicates the
    %   sequence number of the packet being transmitted. The default value
    %   is 0.
    SequenceNumber = 0
    
    %MoreData Indication for more data
    %   Specify this property as a scalar logical. A true value indicates
    %   that the sender has more data to send. The default value is false.
    MoreData (1, 1) logical = false
    
    %CRCInitialization CRC initialization value
    %   Specify the CRC initialization as a 6-element character vector or
    %   string scalar denoting a 3-octet hexadecimal value. This property
    %   is the initialization value for CRC calculation. The default value
    %   is '012345'.
    CRCInitialization = '012345'
    
    %ControlConfig Control PDU payload configuration object
    %   Specify this property as a configuration object of type <a
    %   href="matlab:help('bleLLControlPDUConfig')">bleLLControlPDUConfig</a>.
    %   . The control PDU payload fields are populated using settings of
    %   this configuration object. The default value is an object of type
    %   'bleLLControlPDUConfig' with all properties set to their default
    %   values.
    ControlConfig
end
  
properties(Hidden)
    %HideDisplay Hide the properties of Link Layer configuration object
    %   This is a hidden property used by <a
    %   href="matlab:help('bleLLDataChannelPDUDecode')">bleLLDataChannelPDUDecode</a>
    %   function to disable the display of all the properties when Link
    %   Layer decoding is failed.
    HideDisplay (1, 1) logical = false
end
  
properties(Constant, Hidden)
    LLID_Values = {...
        'Data (continuation fragment/empty)'; ...
        'Data (start fragment/complete)'; ...
        'Control'}
end
  
methods (Static)
    function props = matlabCodegenNontunableProperties(~)
        % List the properties that must be treated as nontunable during
        % code generation
        props = {'CRCInitialization'};
    end
end

methods
    function obj = bleLLDataChannelPDUConfig(varargin)
        % Setting default value for variable sized properties and the
        % object for code generation. As code generation doesn't support
        % object as an initial value for a property.
        obj@comm.internal.ConfigBase('LLID', ...
            'Data (continuation fragment/empty)', 'ControlConfig', ...
            bleLLControlPDUConfig, varargin{:});
    end
    
    function obj = set.LLID(obj, value)
        value = validatestring(value, obj.LLID_Values, ...
            mfilename, 'LLID');
        obj.LLID = '';
        obj.LLID = value;
    end
    
    function obj = set.NESN(obj, value)
        validateattributes(value, {'numeric', 'logical'}, {'scalar', ...
            'binary'}, mfilename, 'NESN');
        obj.NESN = value;
    end
    
    function obj = set.SequenceNumber(obj, value)
        validateattributes(value, {'numeric', 'logical'}, {'scalar', ...
            'binary'}, mfilename, 'SequenceNumber');
        obj.SequenceNumber = value;
    end
    
    function obj = set.CRCInitialization(obj, value)
        ble.internal.validateHex(value, 6, 'CRCInitialization');
        obj.CRCInitialization = char(value);
    end
    
    function obj = set.ControlConfig(obj, value)
        validateattributes(value, {'bleLLControlPDUConfig'}, {'scalar'}, ...
            mfilename, 'ControlConfig');
        obj.ControlConfig = value;
    end
end
  
methods(Access = protected)
    % Controls the conditional display of properties
    function flag = isInactiveProperty(obj, prop)
        flag = true;
        
        % Disable display
        if obj.HideDisplay
            return;
        end
        
        % Properties LLID, NESN, SequenceNumber, MoreData are applicable
        % for all data channel PDUs
        if any(strcmp(prop, {'LLID', 'NESN', 'SequenceNumber', 'MoreData', ...
                'CRCInitialization'}))
            flag = false;
            return;
        end
        
        % 'ControlConfig' is applicable for control PDUs
        if strcmp(prop, 'ControlConfig') && strcmp(obj.LLID, 'Control')
            flag = false;
            return;
        end
    end
end
end
