function [nodesStats, classificationStats] = helperBluetoothFullDuplexStatistics(btNodes)
%helperBluetoothFullDuplexStatistics Helper for fetching statistics of the
%nodes in Bluetooth piconet
%
%   [NODESSTATS, CLASSIFICATIONSTATS] =
%   helperBluetoothFullDuplexStatistics(BTNODES) fetches statistics (Tx
%   packets, Rx packets, etc.) of the nodes in Bluetooth piconet, BTNODES.
%
%   BTNODES is a cell-array of objects of type
%   helperBluetoothFullDuplexNode.
%
%   NODESSTATS is a table having statistics of each node in the piconet.
%
%   CLASSIFICATIONSTATS is a table having the channel classification
%   statistics of each Master-Slave pair in the piconet.

%   Copyright 2020 The MathWorks, Inc.

    % Number of nodes in the piconet
    numNodes = numel(btNodes);
    % Initialize the names for the nodes
    nodeNames = cell(1, numNodes);
    nodeNames{1} = 'Master';
    for idx = 1:numNodes - 1
        nodeNames{idx+1} = ['Slave' num2str(idx)];
    end

    % Statistics from the node
    statisticsCategories = {'TotalRxPackets', 'TotalTxPackets', ...
        'TxACLPackets', 'TxACLOneSlotPackets', 'TxACLThreeSlotPackets', ...
        'TxACLFiveSlotPackets', 'RetransmittedACLPackets', 'TxSCOPackets', 'RxACLPackets', ...
        'RxACLOneSlotPackets', 'RxACLThreeSlotPackets', 'RxACLFiveSlotPackets', ...
        'RxSCOPackets', 'TotalRxFailedPackets', 'ACKedACLPackets', 'ACKedSCOPackets', ...
        'DroppedPackets', 'DroppedLMPMessages'};
    statisticsTypes = {'double', 'double', 'cell', 'cell', 'cell', 'cell', ...
        'cell', 'cell', 'cell', 'cell', 'cell', 'cell', ...
        'cell', 'double', 'cell', 'cell', 'double', 'double'};

    % Create a table for storing the statistics of each node
    nodesStats = table('Size', [numNodes, numel(statisticsCategories)], ...
        'RowNames', nodeNames, 'VariableNames', statisticsCategories, ...
        'VariableTypes', statisticsTypes);

    % Update the statistics of each node
    for idx = 1:numNodes
        nodesStats{idx, 'TotalRxPackets'} = btNodes{idx}.Baseband.RxPackets;
        nodesStats{idx, 'TotalTxPackets'} = btNodes{idx}.Baseband.TxPackets;
        nodesStats{idx, 'TxACLPackets'} = {[(1:7)' btNodes{idx}.Baseband.TxACLPackets]};
        nodesStats{idx, 'TxACLOneSlotPackets'} = {[(1:7)' btNodes{idx}.Baseband.TxACLOneSlotPackets]};
        nodesStats{idx, 'TxACLThreeSlotPackets'} = {[(1:7)' btNodes{idx}.Baseband.TxACLThreeSlotPackets]};
        nodesStats{idx, 'TxACLFiveSlotPackets'} = {[(1:7)' btNodes{idx}.Baseband.TxACLFiveSlotPackets]};
        nodesStats{idx, 'RetransmittedACLPackets'} = {[(1:7)' btNodes{idx}.Baseband.RetransmittedACLPackets]};
        nodesStats{idx, 'TxSCOPackets'} = {[(1:7)' btNodes{idx}.Baseband.TxSCOPackets]};
        nodesStats{idx, 'RxACLPackets'} = {[(1:7)' btNodes{idx}.Baseband.RxACLPackets]};
        nodesStats{idx, 'RxACLOneSlotPackets'} = {[(1:7)' btNodes{idx}.Baseband.RxACLOneSlotPackets]};
        nodesStats{idx, 'RxACLThreeSlotPackets'} = {[(1:7)' btNodes{idx}.Baseband.RxACLThreeSlotPackets]};
        nodesStats{idx, 'RxACLFiveSlotPackets'} = {[(1:7)' btNodes{idx}.Baseband.RxACLFiveSlotPackets]};
        nodesStats{idx, 'RxSCOPackets'} = {[(1:7)' btNodes{idx}.Baseband.RxSCOPackets]};
        nodesStats{idx, 'TotalRxFailedPackets'} = btNodes{idx}.Baseband.RxFailedPackets;
        nodesStats{idx, 'ACKedACLPackets'} = {[(1:7)' btNodes{idx}.Baseband.ACKedACLPackets]};
        nodesStats{idx, 'ACKedSCOPackets'} = {[(1:7)' btNodes{idx}.Baseband.ACKedSCOPackets]};
        nodesStats{idx, 'DroppedPackets'} = btNodes{idx}.Baseband.DroppedPackets;
        nodesStats{idx, 'DroppedLMPMessages'} = btNodes{idx}.Baseband.DroppedLMPMessages;
    end

    % Variable names for channel classification statistics
    variableNames = {'LTAddress', 'ClassificationCount', 'ChannelMap'};
    variableTypes = {'double', 'double', 'cell'};

    % Create a table for storing channel classification statistics
    classificationStats = table('Size', [btNodes{1}.Baseband.ClassificationTotalCount, numel(variableNames)], ...
        'VariableNames', variableNames, 'VariableTypes', variableTypes);

    % Update the channel classification statistics
    for idx = 1:btNodes{1}.Baseband.ClassificationTotalCount
        classificationStats{idx, 'LTAddress'} = btNodes{1}.Baseband.ChannelClassificationStats(idx, 1);
        classificationStats{idx, 'ClassificationCount'} = btNodes{1}.Baseband.ChannelClassificationStats(idx, 2);
        classificationStats{idx, 'ChannelMap'} = {btNodes{1}.Baseband.ChannelClassificationStats(idx, 3:end)};
    end
end

% LocalWords:  piconet ACL SCO LMP
