function [master, slaves] = helperBLECreateLLConnection(master, slaves)
%helperBLECreateLLConnection Create connection between Master and Slave
%devices
%   [MASTER, SLAVES] = helperBLECreateLLConnection(MASTER, SLAVES) creates
%   a BLE Link Layer connection between the Master, MASTER and each Slave
%   in the SLAVES with default connection parameters such as connection
%   interval, access address.
%
%   MASTER is an object of type <a
%   href="matlab:help('helperBLEDeviceModel')">helperBLEDeviceModel</a>
%   acts as a Link Layer Master device.
%
%   SLAVES is a cell array of objects, each is of type <a
%   href="matlab:help('helperBLEDeviceModel')">helperBLEDeviceModel</a>
%   acts as a Link Layer Slave device.
%
%   Returns the MASTER and SLAVES after creating the connection between
%   Master and Slave devices.

%   Copyright 2018-2019 The MathWorks, Inc.

% Connection interval is a multiple of 1.25 milliseconds. Refer Bluetooth
% Core Specification v5.0, Vol 6, PART B, Section 4.5.1. This example
% models the connection events in such way that at least two packets (one
% from the Master and one from the Slave) are exchanged between Master and
% Slave in each connection event. Here, 2.5 milliseconds is considered to
% achieve a trade-off between the simulation time and accuracy.
minimumInterval = 2.5; % in milliseconds

% Number of Slaves
slavesCount = numel(slaves);

if slavesCount <= 3
    % Connection interval (must be multiple of 1.25 ms). Set to 7.5 ms for
    % all the connections as this is the minimum value as per the standard.
    % Refer Bluetooth Core Specification v5.0, Vol 6, PART B, Section
    % 4.5.1.
    connectionInterval = 7.5; % in milliseconds
else
    % Connection interval (must be multiple of 1.25 ms)
    connectionInterval = minimumInterval*slavesCount; % in milliseconds
end

% Connection offset in microseconds for all the connected peers
connectionOffset = floor((connectionInterval*1000)/slavesCount);

for idx = 1:slavesCount
    % Connection interval is a multiple of 1.25 milliseconds. Refer
    % Bluetooth Core Specification v5.0, Vol 6, PART B, Section 4.5.1.
    % Create connection event object for Master.
    connectionEventMaster = helperBLELLConnectionEvent('Role','Master', ...
        'ConnectionInterval',connectionInterval/1.25);
    
    % Update the connection event counter to define the number of
    % connection events completed. This example ignores the initial
    % connection event setup that used to select an anchor point. Refer
    % Bluetooth Core Specification v5.0, Vol 6, PART B, Section 4.5.4.
    updateEventCount(connectionEventMaster, 1);
    anchorPointSelected(connectionEventMaster);
    
    % Initialize the connection event object for Master
    init(connectionEventMaster);
    reset(connectionEventMaster);
    
    % Connection interval is a multiple of 1.25 milliseconds. Refer
    % Bluetooth Core Specification v5.0, Vol 6, PART B, Section 4.5.1.
    % Create connection event object for Slave.
    connectionEventSlave = helperBLELLConnectionEvent('Role','Slave', ...
        'ConnectionInterval',connectionInterval/1.25);
    
    % Update the connection event counter to define the number of
    % connection events completed. This example ignores the initial
    % connection event setup that used to select an anchor point. Refer
    % Bluetooth Core Specification v5.0, Vol 6, PART B, Section 4.5.4.
    updateEventCount(connectionEventSlave, 1);
    anchorPointSelected(connectionEventSlave);
    
    % Initialize the connection event object for Slave
    init(connectionEventSlave);
    reset(connectionEventSlave);
    
    % Link Layer connection configuration per each Master-Slave connection
    slaves(idx).LLConnectionConfigs(1) = helperBLEConnectionStateModel('AccessAddress', ...
                dec2hex((hex2dec(master.DefaultAccessAddress)+idx), 8), ...
                'ConnectionEvent',connectionEventSlave, ...
                'ConnectionInterval',connectionInterval/1.25, ...
                'ConnectionEventOffset',connectionOffset*(idx-1));
    % BLE Slave device initialization
    init(slaves(idx));
    
    % Link Layer connection configuration per each Master-Slave connection
    master.LLConnectionConfigs(idx) = helperBLEConnectionStateModel('AccessAddress', ...
                dec2hex((hex2dec(master.DefaultAccessAddress)+idx), 8), ...
                'ConnectionEvent',connectionEventMaster, ...
                'ConnectionInterval',connectionInterval/1.25, ...
                'ConnectionEventOffset',connectionOffset*(idx-1));
end

% BLE Master device initialization
init(master);
end

