%% 802.11 OFDM Beacon Frame Generation and Transmission with Test and Measurement Equipment
% This example shows how to generate packets containing MAC beacon frames
% suitable for baseband simulation or over-the-air transmission using WLAN
% Toolbox&trade;, Instrument Control Toolbox&trade; and Keysight Technologies&reg; RF signal generator.

% Copyright 2017-2020 The MathWorks, Inc. 

%% Introduction
% In this example WLAN Toolbox is used to create an IEEE&reg;
% 802.11&trade; beacon frame. Using Instrument Control Toolbox, the generated beacon frame is downloaded to Keysight 
% Technologies N517B signal generator for over-the-air transmission.
% Beacon frame is a type of management frame that identifies a basic 
% service set (BSS) formed by a number of 802.11 devices. The access point 
% of a BSS periodically transmits the beacon frame to establish and maintain the network. 
% A WiFi device can be used to view this beacon frame transmitted by the RF 
% Signal Generator.

%%
% <<../BeaconFrameGenerationRx.png>>
%
% For more information on beacon 
% frame generation using WLAN Toolbox, please refer 
% <https://www.mathworks.com/help/wlan/examples/802-11-ofdm-beacon-frame-generation.html
% 802.11 OFDM Beacon Frame Generation.>

%% Requirements
% To run this example you need:
% 
% * Keysight Technologies N5172B signal generator
% * Keysight VISA version 17.3
% * IVI-C driver for Keysight Technologies N5172B signal generator
% * National Instruments&trade; IVI&reg; compliance package version 16.0.1.2 or higher
% * WLAN Toolbox
% * Instrument Control Toolbox

%% Create IEEE 802.11 Beacon Frame
% The beacon packets are periodically transmitted as specified by the
% Target Beacon Transmission Time (TBTT) in the beacon interval field. The
% beacon interval represents the number of Time Units (TUs) between TBTT,
% where 1 TU represents 1024 microseconds. A beacon interval of 100 TU
% results in a 102.4 milliseconds time interval between successive beacons.
% A beacon frame is generated using the
% <docid:wlan_ref#mw_9f2e549d-99ee-49ae-b9c3-3a78e29a6c1a wlanMACFrame>
% function. This function consumes the MAC frame configuration object
% <docid:wlan_ref#mw_a004c76e-748e-42b5-84b2-360a063ea1f9
% wlanMACFrameConfig>. This object accepts
% <docid:wlan_ref#mw_083f69be-10d7-4935-9f2e-e487f2aa75a9
% wlanMACManagementConfig> as a property to configure the beacon
% frame-body.

SSID = 'TEST_BEACON'; % Network SSID
beaconInterval = 100; % In Time units (TU)
band = 5;             % Band, 5 or 2.4 GHz
chNum = 52;           % Channel number, corresponds to 5260MHz
bitsPerByte = 8;      % Number of bits in 1 byte

% Create Beacon frame-body configuration object
frameBodyConfig = wlanMACManagementConfig;
frameBodyConfig.BeaconInterval = beaconInterval;  % Beacon Interval in Time units (TUs)
frameBodyConfig.SSID = SSID;                      % SSID (Name of the network)
dsElementID = 3;                                  % DS Parameter IE element ID
dsInformation = dec2hex(chNum, 2);                % DS Parameter IE information
frameBodyConfig = frameBodyConfig.addIE(dsElementID, dsInformation);  % Add DS Parameter IE to the configuration

% Create Beacon frame configuration object
beaconFrameConfig = wlanMACFrameConfig('FrameType', 'Beacon');
beaconFrameConfig.ManagementConfig = frameBodyConfig;

% Generate Beacon frame bits
[mpduBits, mpduLength] = wlanMACFrame(beaconFrameConfig, 'OutputFormat', 'bits');

% Calculate center frequency for the given band and channel number
fc = helperWLANChannelFrequency(chNum, band);

%% Create IEEE 802.11 Beacon Packet
% A beacon packet is synthesized using <docid:wlan_ref#buwv1f3 
% wlanWaveformGenerator> with a non-HT format configuration object. In this
% example an object is configured to generate a beacon packet of 20 MHz
% bandwidth, 1 transmit antenna and BPSK rate 1/2 (MCS 0).

cfgNonHT = wlanNonHTConfig;              % Create a wlanNonHTConfig object
cfgNonHT.PSDULength = numel(mpduBits)/8; % Set the PSDU length in bits

% The idle time is the length in seconds of an idle period after each
% generated packet. The idle time is set to the beacon interval.
txWaveform = wlanWaveformGenerator(mpduBits, cfgNonHT, 'IdleTime', beaconInterval*1024e-6);
Rs = wlanSampleRate(cfgNonHT);           % Get the input sampling rate

%% Create a RF Signal Generator Object
% Quick-Control RF Signal Generator is used to download and transmit the
% baseband waveform, |txWaveform|, generated by WLAN Toolbox.
rf = rfsiggen();
%%
% Discover all the available instrument resources you can connect to, using the |resources| method.
rf.resources
%%
% Discover all the available instrument drivers, using |drivers| method.
rf.drivers

%% Connect to Signal Generator
% Set |Resource| and |Driver| properties before connecting to the object. The IP address of Keysight Technologies N5172B signal generator 
% is _172.31.165.249_ , hence the resource specified will be 'TCPIP0::172.31.165.249::inst0::INSTR'
rf.Resource = 'TCPIP0::172.31.165.249::inst0::INSTR';
rf.Driver = 'AgRfSigGen';
% Connect to the instrument
connect(rf);

%% Download Waveform
% Download the waveform, |txWaveform|, to the instrument with sampling rate
% |Rs|.
download(rf, transpose(txWaveform), Rs);

%% Transmit the Waveform
% Call |start| to start transmitting waveform using specified 
% centerFrequency, outputPower and loopCount. 
centerFrequency = fc;
outputPower = 0;
loopCount = Inf;
start(rf, centerFrequency, outputPower, loopCount);

%%
% Once the signal generator is transmitting the beacon, you can test by
% scanning for wireless network using a Wi-Fi device. You should now see a
% _TEST_BEACON_ SSID in the list of available networks.

%% Clean Up
% When you have finished transmitting, stop the waveform output, disconnect the 
%  |rfsiggen| object from the signal generator, and remove it from the workspace.
stop(rf);
disconnect(rf);
clear rf


%% Appendix
% This example uses the following helper functions:
%
% * <matlab:edit('helperWLANChannelFrequency.m') helperWLANChannelFrequency.m>
  
