function lifetimeInDays = helperBLEMeshNodeLifetime(meshNode, simulationTime)
%helperBLEMeshLPNLifetime Compute Bluetooth mesh node lifetime
%   LIFETIMEINDAYS = helperBLEMeshLPNLifetime(MESHNODE, SIMULATIONTIME)
%   computes the lifetime of a Bluetooth mesh node corresponding to the
%   input node, MESHNODE, having statistics of that node after running the
%   Bluetooth mesh network simulation for SIMULATIONTIME (in ms).
%
%   LIFETIMEINDAYS returns the lifetime of a Bluetooth mesh node as a
%   double scalar value in days.
%
%   MESHNODE is a handle object of type <a
%   href="matlab:help('helperBLEMeshNode')">helperBLEMeshNode</a>.
%
%   SIMULATIONTIME is the time for which the Bluetooth mesh network is
%   simulated. It is in milliseconds of double scalar value.
%
%   Configuration: User can configure the battery capacity and current
%   parameters (ex: self-discharge current, transmission current etc.) of
%   the chip used in the Bluetooth mesh node, MESHNODE. The
%   characterization of current parameters at different states of a
%   Bluetooth node is calculated by conducting the hardware experiment. The
%   current parameters are obtained by integrating the current over the
%   given time interval. Example: The current consumed for transmitting a
%   mesh message on an advertising channel 37 is obtained by integrating
%   the current over time taken to transmit on channel 37. All the input
%   current parameters should be configured in mA.
%
%   The lifetime is calculated based on battery capacity, self-discharge
%   current and average current consumption of Bluetooth mesh node. You can
%   also consider state transition time while calculating the lifetime of a
%   Bluetooth mesh node.

%   Copyright 2019 The MathWorks, Inc.

% Battery capacity in mAh
batteryCapacity = 1200;

% Self-discharge current of the battery. It is a constant value as a
% function of time. Here, self-discharge rate is equal to 1%/year of its
% initial capacity i.e. self-discharge current = (0.01*1200 mAh)/(365*24 h)
iSelfDischarge = (0.01*batteryCapacity)/(365*24);

% Current consumed during transmission on three advertising channels in mA
iTxch37 = 7.57;
iTxch38 = 7.77;
iTxch39 = 7.70;

% Current consumed during listening in mA
iListen = 10.3;

% Current consumed during sleep state in mA
iSleep = 0.20;

% Current consumed during idle state in mA
iIdle = 1.19;

% Average transmission current
iTxAvg = (iTxch37 + iTxch38 + iTxch39)/3;

% Time taken to transmit a bit is 0.001 ms with 1 Mbps bit rate (phy mode:
% LE1M)
txTime = 0.001;

% Microseconds per millisecond
UsecPerMsec = 1e3;

% Bluetooth mesh node statistics
nTxBytes = meshNode.LLGAPBearer.BytesTransmitted;
nTxBits = nTxBytes*8;
listenTime = meshNode.LLGAPBearer.ListenTime/UsecPerMsec;
sleepTime = meshNode.LLGAPBearer.SleepTime/UsecPerMsec;
idleTime = meshNode.LLGAPBearer.IdleTime/UsecPerMsec;

% Calculate current consumption of Bluetooth mesh node
currentConsumptionlpn = (nTxBits*txTime*iTxAvg) + (listenTime*iListen) + ...
    (sleepTime*iSleep) + (idleTime*iIdle);

% Calculate average current consumption of Bluetooth mesh node
avgCurrentConsumptionlpn = currentConsumptionlpn/(simulationTime);

% Calculate lifetime of Bluetooth mesh node in hours
lifetimelpn = batteryCapacity/(avgCurrentConsumptionlpn + iSelfDischarge);

% Calculate lifetime of Bluetooth mesh node in days
lifetimeInDays = lifetimelpn/24;

% Hardware parameters of Bluetooth mesh node
hardwareParameterValues = [iSelfDischarge; iTxch37; iTxch38; ...
    iTxch39; iListen; iSleep; iIdle];
hardwareParameterNames = categorical({'Self-discharge'; ...
    'Transmission on channel 37'; 'Transmission on channel 38'; ...
    'Transmission on channel 39'; 'Listening'; 'Sleep'; 'Idle'});

% Display of hardware parameters and their configured values
fprintf('Configured hardware parameters for the battery capacity of %d mAh are:\n', batteryCapacity);
hardwareParameters = table(hardwareParameterNames, hardwareParameterValues, ...
    'VariableNames', {'Hardware parameters'; 'Configured values (mA)'})

% Display of transmit, listen and sleep times of Bluetooth mesh node
fprintf('Statistics at node "%d" are:\n', meshNode.Identifier);
statisticsAtNode = table(categorical({'Transmission time'; 'Listen time'; ...
    'Sleep time'; 'Idle time'}), [nTxBits*txTime; listenTime; ...
    sleepTime; idleTime], 'VariableNames', {'Time variables'; 'Time (ms)'})
end
