%

% Copyright 2006-2016 The MathWorks, Inc.

Block=gcb;

% Calculate packet type

switch mask_packet_type
case 'DM1'
    Packet_Type=4; 
case 'HV1'
    Packet_Type=6;
case 'HV2'
    Packet_Type=7;
case 'HV3'
    Packet_Type=8;
case 'SCORT'
    Packet_Type=13;
otherwise
    error(message('comm:commbluetoothfduplex_lib_init:UnsupportedPacket'))
end

% Define Variables Used in Block Parameters

Slot_Ts = (1/1600); % Slot Sample rate
Max_Slot_Symbols=625;
Slot_Symbols=366;

% GMSK Modulator and hopping variables
BT_Hop_Separation=1e6;
BT_Freq_Deviation=2*150e3;
Num_Hop_Frequencies=79;
BT_Samples_Per_Symbol=88;
Filter_Group_Delay=45;

% Tx mask variables
Tx_On_Samples=Slot_Symbols*BT_Samples_Per_Symbol;
Tx_Off_Samples=(Max_Slot_Symbols-Slot_Symbols)*BT_Samples_Per_Symbol;
Tx_Mask=[ones(Tx_On_Samples,1);zeros(Tx_Off_Samples,1)];

% Header Info
Slave_Address= [1 0 1]';
Packet_Type_Code= [1 0 1 0]';
Flow_Control = 1;
ARQ = 1;
Sequence = 1;
Access_Code=zeros(72,1); Access_Code(1:2:72)=1;

switch Device_Type
  case 1 % If device is the Master
    switch Packet_Type
	case 6 % HV1
            Tx_Ts=Slot_Ts*2;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=2;
            Num_Payload_Bits=80;
            Tx_Slot_Enable_Phase=0; 
            Rx_Slot_Enable_Phase=1;
	case 7 % HV2
            Tx_Ts=Slot_Ts*4;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=4;
            Num_Payload_Bits=160;
            if Slot_Pair == 3
                error(message('comm:commbluetoothfduplex_lib_init:InvalidSlot'));
            end
            Tx_Slot_Enable_Phase=Slot_Pair*2-2; 
            Rx_Slot_Enable_Phase=Tx_Slot_Enable_Phase+1;
	case 8 % HV3
            Tx_Ts=Slot_Ts*6;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=6;
            Num_Payload_Bits=240;
            Tx_Slot_Enable_Phase=Slot_Pair*2-2; 
            Rx_Slot_Enable_Phase=Tx_Slot_Enable_Phase+1;
	case 13 % EV1
            Tx_Ts=Slot_Ts*2;
            Frame_Ts=Slot_Ts*6;
            Num_Slots_Rate=2;
            Num_Payload_Bits=240;
            Tx_Slot_Enable_Phase=0; 
            Rx_Slot_Enable_Phase=Tx_Slot_Enable_Phase+1;
        case 4 % DM1
            Tx_Ts=Slot_Ts*2;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=2;
            Num_Payload_Bits=160;
            Tx_Slot_Enable_Phase=0; 
            Rx_Slot_Enable_Phase=1;
   end
  case 2 % If device is the Slave
    switch Packet_Type
	case 6 % HV1
            Tx_Ts=Slot_Ts*2;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=2;
            Num_Payload_Bits=80;
            Rx_Slot_Enable_Phase=0; 
            Tx_Slot_Enable_Phase=1;
	case 7 % HV2
            Tx_Ts=Slot_Ts*4;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=4;
            Num_Payload_Bits=160;
            if Slot_Pair == 3
                error(message('comm:commbluetoothfduplex_lib_init:InvalidSlot'));
            end
            Rx_Slot_Enable_Phase=Slot_Pair*2-2; 
            Tx_Slot_Enable_Phase=Rx_Slot_Enable_Phase+1;
	case 8 % HV3
            Tx_Ts=Slot_Ts*6;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=6;
            Num_Payload_Bits=240;
            Rx_Slot_Enable_Phase=Slot_Pair*2-2; 
            Tx_Slot_Enable_Phase=Rx_Slot_Enable_Phase+1;
	case 13 % EV1
            Tx_Ts=Slot_Ts*2;
            Frame_Ts=Slot_Ts*6;
            Num_Slots_Rate=2;
            Num_Payload_Bits=240;
            Rx_Slot_Enable_Phase=0; 
            Tx_Slot_Enable_Phase=Rx_Slot_Enable_Phase+1;
         case 4 % DM1
            Tx_Ts=Slot_Ts*2;
            Frame_Ts=Tx_Ts;
            Num_Slots_Rate=2;
            Num_Payload_Bits=160;
            Rx_Slot_Enable_Phase=0; 
            Tx_Slot_Enable_Phase=1;
    end
end


%set_param([Block,'/Audio source and speech coding/From Multimedia File'],'inputFileName',['commbluetoothfduplex_input' num2str(ID) '.wav']);
set_param([Block,'/Audio source and speech coding/Signal From Workspace'],'X',['bluetooth_fd.voice_input' num2str(ID)]);
set_param([Block,'/Speech decoding and audio sink/To Workspace'],'VariableName',['sigout' num2str(ID)]);

% Set Throughput workspace variable name
b=find_system(Block,'LookUnderMasks','on','Followlinks','on','Name','Throughput To Workspace');  
set_param(b{1},'VariableName',['Throughput' num2str(ID)]);

% Set ACL_BER workspace variable name
b=find_system(Block,'LookUnderMasks','on','Followlinks','on','Name','ACL To Workspace');  
set_param(b{1},'VariableName',['ACL_BER' num2str(ID)]);


% Set SCO_BER workspace variable name
b=find_system(Block,'LookUnderMasks','on','Followlinks','on','Name','To Workspace Raw');  
set_param(b{1},'VariableName',['Raw_BER' num2str(ID)]);
b=find_system(Block,'LookUnderMasks','on','Followlinks','on','Name','To Workspace Res');  
set_param(b{1},'VariableName',['Res_BER' num2str(ID)]);
b=find_system(Block,'LookUnderMasks','on','Followlinks','on','Name','To Workspace FER');  
set_param(b{1},'VariableName',['FER' num2str(ID)]);

