% helperBLEPlotConnectionTimeWithoutInterference Helper script for running
% multiple simulations (long running) by varying advertising interval to
% observe the change in connection establishment time.
%
% The configuration parameters used in these simulations are:
%
%   -----------------------------------------------------------
%   |   Configuration parameter       |     Value              | 
%   -----------------------------------------------------------
%   |  Scan window                    |      10 milliseconds   |
%   |  Scan interval                  |     100 milliseconds   |
%   |  Scan duration                  |    1000 milliseconds   |
%   |  Advertising duration           |   10000 milliseconds   |
%   |  Advertising interval           |         variable       |
%   |  Interference (LossProbability) |      not considered    |
%   -----------------------------------------------------------
%
% The connection establishment time between a Master and Slave is computed
% for five simulations by varying the advertising interval values. This
% value is stored in a workspace variable 'plotOneStatsVaryingAdvInterval'.

% Copyright 2018-2019 The MathWorks, Inc.

% Open simulink model
open_system('BLELinkLayerStateMachineModel');

% Pre-allocate connection establishment time vector with zeros
plotOneStatsVaryingAdvInterval = zeros(1, 5);
% Advertising interval values
advertisingIntervals = [100 200 400 600 900];
% Set simulation stop time in seconds
set_param('BLELinkLayerStateMachineModel', 'StopTime', '30');
% Set scan duration in milliseconds
set_param('BLELinkLayerStateMachineModel/Master', 'ScanDuration', '1000');
% Set advertising duration in milliseconds
set_param('BLELinkLayerStateMachineModel/Slave', 'AdvertisingDuration', '10000');
% Set interference to zero
set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch37LossProbability', '0');
set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch38LossProbability', '0');
set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch39LossProbability', '0');
% Set scan window as 10 milliseconds for a scan interval of 100
% milliseconds
set_param('BLELinkLayerStateMachineModel/Master', 'FastScanIntervalmax', '100');
set_param('BLELinkLayerStateMachineModel/Master', 'FastScanIntervalmin', '100');
set_param('BLELinkLayerStateMachineModel/Master', 'ReducedScanIntervalmin', '100');
set_param('BLELinkLayerStateMachineModel/Master', 'ReducedScanIntervalmax', '100');
set_param('BLELinkLayerStateMachineModel/Master', 'FastScanWindow', '10');
set_param('BLELinkLayerStateMachineModel/Master', 'ReducedScanWindow', '10');
% Set advertising interval to 30 milliseconds
set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmin', '30');
set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmax', '30');
set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmin', '30');
set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmax', '30');
    
% Simulate the BLE link layer state machine model for five advertising
% intervals
for i = 1:5
    set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmax', num2str(advertisingIntervals(i)));
    set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmin', num2str(advertisingIntervals(i)));
    set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmax', num2str(advertisingIntervals(i)));
    set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmin', num2str(advertisingIntervals(i)));
    SimOut = sim('BLELinkLayerStateMachineModel','ReturnWorkspaceOutputs','on') ;
    status = get_param(bdroot,'SimulationStatus');
    if strcmp(status, 'stopped')
        % Get connection establishment time for each simulation run
        plotOneStatsVaryingAdvInterval(i) = timeTakenToEstablishConnection;
    end
end

% Close simulink model without saving
close_system('BLELinkLayerStateMachineModel', false);
