function transportControlMessage = helperBLEMeshTransportControlMessage(msgType, input)
%helperBLEMeshTransportControlMessage Generate Bluetooth mesh transport
%control message
%   TRANSPORTCONTROLMESSAGE = helperBLEMeshTransportControlMessage(MSGTYPE,
%   FSN) generates a Bluetooth mesh transport control message (friend poll)
%   corresponding to the input parameters MSGTYPE and FSN (Friend Sequence
%   Number). Use this syntax to generate friend poll message.
%
%   TRANSPORTCONTROLMESSAGE = helperBLEMeshTransportControlMessage(MSGTYPE,
%   MD) generates a Bluetooth mesh transport control message (friend
%   update) corresponding to the input parameters MSGTYPE and MD (More
%   Data). Use this syntax to generate friend update message.
%
%   TRANSPORTCONTROLMESSAGE represents the output Bluetooth mesh transport
%   control message, returned as a column vector of decimal octets.
%
%   MSGTYPE is specified as 'Friend poll' | 'Friend update'.
%
%   FSN is either 0 or 1 which is used to acknowledge receipt of previous
%   messages from the Friend node to the Low Power node.
%
%   MD is a logical scalar which indicates the friend queue is empty or
%   not.

%   Copyright 2019 The MathWorks, Inc.

%#codegen

% Validate input parameters
msgType = validatestring(msgType, {'Friend poll', 'Friend update'}, ...
    mfilename, 'msgType');
validateattributes(input, {'logical', 'numeric'}, {'binary', 'scalar', ...
    'nonempty'}, mfilename, 'input');

% segmented: '0b0' (no segmentation)
segmented = 0;
    
% Generate friend poll message
if  strcmpi(msgType, 'Friend poll')
    
    % opcode: '0b0000001' (Opcode field determines the format of the
    % parameters field)
    opcode = [0 0 0 0 0 0 1];
    
    % padding: '0b0000000' (Padding to FSN)
    padding = [0 0 0 0 0 0 0];
    
    % fsn: '0b0'(fsn is used to acknowledge receipt of previous messages
    % from the Friend node to the Low Power node)
    fsn = input;
    
    % Concatenate all the fields of friend poll message
    transportControlMessage = ...
        [comm.internal.utilities.bi2deLeftMSB([segmented opcode], 2); ...
        comm.internal.utilities.bi2deLeftMSB([padding double(fsn)], 2)];
    
% Generate friend update message
else
    
    % opcode: '0b0000010' (Opcode field determines the format of the
    % parameters field)
    opcode = [0 0 0 0 0 1 0];
    
    % flags: '0b00000000' (Key refresh flag indicates whether the key
    % refresh procedure is in progress and IV update flag  indicates
    % whether the IV Update procedure is in progress )
    flags = 0;
    
    % ivindex: de2bi(0,32) (Current IV (Initialization Vector) index which
    % is a 4-octet value included in the security nonce in addition to
    % 24-bit sequence number)
    ivindex = comm.internal.utilities.de2biBase2LeftMSB(0, 32);
    
    % moredata: '0b00000000' (Set to 0 if friend queue is empty and set to
    % 1 if friend queue is not empty)
    moredata = input;
    
    % Concatenate all the fields of friend update message    
    transportControlMessage = ...
        [comm.internal.utilities.bi2deLeftMSB([segmented opcode], 2); ...
        flags; ...
        comm.internal.utilities.bi2deLeftMSB(ivindex(1:8), 2); ...
        comm.internal.utilities.bi2deLeftMSB(ivindex(9:16), 2); ...
        comm.internal.utilities.bi2deLeftMSB(ivindex(17:24), 2); ...
        comm.internal.utilities.bi2deLeftMSB(ivindex(25:32), 2); ...
        double(moredata)];
end
end