% helperBLEPlotConnectionTimeWithInterference Helper script for running
% multiple simulations (long running) by varying the advertising interval
% value and the interference levels (Loss probability) of advertising
% channels (37, 38 and 39) to observe the change in connection
% establishment time.
%
% The configuration parameters used in these simulations are:
%
%   -----------------------------------------------------------
%   |   Configuration parameter       |     Value              | 
%   -----------------------------------------------------------
%   |  Scan window                    |     100 milliseconds   |
%   |  Scan interval                  |     100 milliseconds   |
%   |  Scan duration                  |    1000 milliseconds   |
%   |  Advertising duration           |   10000 milliseconds   |
%   |  Advertising interval           |         variable       |
%   |  Interference (LossProbability) |         variable       |
%   -----------------------------------------------------------
%
% The connection establishment time is averaged over 100 simulations, for
% three advertising interval values and five interference levels.
%
% The workspace variables 'plotTwoStatsAdv30', 'plotTwoStatsAdv60' and
% 'plotTwoStatsAdv100' consists values of average connection establishment
% time with varying interference for advertising interval values 30, 60 and
% 100 milliseconds respectively.

% Copyright 2018-2019 The MathWorks, Inc.

% Open simulink model
open_system('BLELinkLayerStateMachineModel');

% Pre-allocate the connection establishment time vector with zeros
connectionTimes = zeros(1, 100);
% Interference values in percentage
interferenceValues = [10 20 30 50 70]; 
% 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 (default)
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 100 milliseconds for a scan interval of 100
% milliseconds (always active in scanning)
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', '100');
set_param('BLELinkLayerStateMachineModel/Master', 'ReducedScanWindow', '100');

% Advertising interval 30 milliseconds

% Pre-allocate connection establishment time vector with zeros
plotTwoStatsAdv30 = zeros(1, 5);

% Set advertising interval as 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 interference
% values
for i = 1:5
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch37LossProbability', num2str(interferenceValues(i)));
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch38LossProbability', num2str(interferenceValues(i)));
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch39LossProbability', num2str(interferenceValues(i)));
    
    % Simulate for 100 runs
    for index = 1:100
        SimOut = sim('BLELinkLayerStateMachineModel','ReturnWorkspaceOutputs','on') ;
        status = get_param(bdroot,'SimulationStatus');
        if strcmp(status, 'stopped')
          % Get connection establishment time for each simulation run
          connectionTimes(index) = timeTakenToEstablishConnection;
        end
    end
    % Average the results
    plotTwoStatsAdv30(i) = mean(connectionTimes);
end

% Advertising interval 60 milliseconds

% Pre-allocate connection establishment time vector with zeros
plotTwoStatsAdv60 = zeros(1, 5);

% Set advertising interval as 60 milliseconds
set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmax', '60');
set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmin', '60');
set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmax', '60');
set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmin', '60');

% Simulate the BLE link layer state machine model for five interference
% values
for i = 1:5
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch37LossProbability', num2str(interferenceValues(i)));
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch38LossProbability', num2str(interferenceValues(i)));
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch39LossProbability', num2str(interferenceValues(i)));
    
    % Simulate for 100 runs
    for index = 1:100
        SimOut = sim('BLELinkLayerStateMachineModel','ReturnWorkspaceOutputs','on') ;
        status = get_param(bdroot,'SimulationStatus');
        if strcmp(status, 'stopped')
          % Get connection establishment time for each simulation run
          connectionTimes(index) = timeTakenToEstablishConnection;
        end
    end
    % Average the results
    plotTwoStatsAdv60(i) = mean(connectionTimes);
end

% Advertising interval 100 milliseconds

% Pre-allocate connection establishment time vector with zeros
plotTwoStatsAdv100 = zeros(1, 5);

% Set advertising interval as 100 milliseconds
set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmax', '100');
set_param('BLELinkLayerStateMachineModel/Slave', 'FastAdvertisingIntervalmin', '100');
set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmax', '100');
set_param('BLELinkLayerStateMachineModel/Slave', 'ReducedAdvertisingIntervalmin', '100');

% Simulate the BLE link layer state machine model for five interference
% values
for i = 1:5
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch37LossProbability', num2str(interferenceValues(i)));
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch38LossProbability', num2str(interferenceValues(i)));
    set_param('BLELinkLayerStateMachineModel/Wireless Medium', 'ch39LossProbability', num2str(interferenceValues(i)));
    
    % Simulate for 100 runs
    for index = 1:100
        SimOut = sim('BLELinkLayerStateMachineModel','ReturnWorkspaceOutputs','on') ;
        status = get_param(bdroot,'SimulationStatus');
        if strcmp(status, 'stopped')
            % Get connection establishment time for each simulation run
            connectionTimes(index) = timeTakenToEstablishConnection;
        end
    end
    % Average the results
    plotTwoStatsAdv100(i) = mean(connectionTimes);
end

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