function [ampdu, frameLength] = helperAggregateMPDUs(cfgMAC, cfgPHY, msduList, sequenceNumberList, retryList)
%helperAggregateMPDUs Generate A-MPDU
%
%   This is an example helper function.
%
%   [AMPDU, FRAMELENGTH] = helperAggregateMPDUs(CFGMAC, CFGPHY, MSDULIST,
%   SEQUENCENUMBERLIST, RETRYLIST) generates an A-MPDU, by creating and
%   appending the MPDUs containing the MSDUs in the MSDULIST.
%
%   AMPDU is the aggregated MPDU, returned as a column vector of decimal
%   octets.
%
%   FRAMELENGTH is the PSDU length for an HT format A-MPDU and APEP length
%   for a VHT or HE format A-MPDU, returned as the number of octets.
%
%   CFGMAC is an object of type <a href="matlab:help('wlanMACFrameConfig')">wlanMACFrameConfig</a>.
%
%   CFGPHY is an object of type <a href="matlab:help('wlanHTConfig')">wlanHTConfig</a>, <a href="matlab:help('wlanVHTConfig')">wlanVHTConfig</a>, or
%   <a href="matlab:help('wlanHESUConfig')">wlanHESUConfig</a>.
%
%   MSDULIST is the list of MSDUs, specified as a cell array where each
%   element is a vector of decimal octets.
%
%   SEQUENCENUMBERLIST is the list of sequence numbers corresponding to
%   MSDUs provided in the MSDULIST, specified as a column vector where each
%   element is in the range of [0 - 4095].
%
%   RETRYLIST is the list of retry flags corresponding to MSDUs provided in
%   the MSDULIST, specified as a logical column vector.

%   Copyright 2019-2020 The MathWorks, Inc. 

%#codegen

numMPDUs = numel(msduList);
mpduList = cell(1, numMPDUs);

switch class(cfgPHY)
    case 'wlanNonHTConfig'
        error('Aggregation of Non-HT frames is not supported');
    
    case 'wlanHTConfig'
        cfgMAC.FrameFormat = 'HT-Mixed';
        
    case 'wlanVHTConfig'
        cfgMAC.FrameFormat = 'VHT';
        
    case 'wlanHESUConfig'
        cfgMAC.FrameFormat = 'HE-SU';
end

for i = 1:numMPDUs
    cfgMAC.SequenceNumber = sequenceNumberList(i);
    cfgMAC.Retransmission = retryList(i);
    mpduList{i} = wlan.internal.macGenerateMPDU(msduList{i}, cfgMAC);
end

[ampdu, frameLength] = wlan.internal.macGenerateAMPDU(mpduList, cfgMAC, cfgPHY);

end