function commwlan80211BeaconModelParams(mode)
%commwlan80211BeaconModelParams Manage 802.11 WLAN Beacon Frame model
  
%   Copyright 2011-2019 The MathWorks, Inc.
    
switch mode
  case 'init'
    % Block name for model dialog parameters
    settingsBlkName = 'Model Parameters';
    
    % Get parameters from model settings block.
    settingsBlk = [bdroot '/' settingsBlkName];
    
    % Model name
    Modelname=bdroot;
    [beaconInterval, ssidValue, rates, sendCFParamsStr, cfpCount, ...
      cfpPeriod, cfpMaxDuration, cfpDurRemain, channelNumber, EsNo, ...
      freqOffset, delay] =  ...
      getparamvals(settingsBlk,...
      'beaconInterval', ...
      'ssidValue', ...
      'rates', ...
      'sendCFParams', ...
      'cfpCount', ...
      'cfpPeriod', ...
      'cfpMaxDuration', ...
      'cfpDurRemain', ...
      'channelNumber', ...
      'EsNo', ...
      'freqOffset', ...
      'delay');
    
    validateattributes(beaconInterval, {'numeric'}, ...
      {'real','positive','integer','scalar','<',2^16-1}, ...
      'commwlan80211BeaconModelParams', '''Beacon Interval (TU)''');

    ssidValue = convertStringsToChars(ssidValue); 
    validateattributes(ssidValue, {'char'}, ...
      {'row'}, ...
      'commwlan80211BeaconModelParams', 'SSID');
    if length(ssidValue) > 32
      error(message('comm:commwlan80211BeaconModelParams:SSID'));
    end
    
    validateattributes(rates, {'numeric'}, ...
      {'real','positive','integer','row'}, ...
      'commwlan80211BeaconModelParams', '''Supported Rates''');
    if length(rates) > 8
      error(message('comm:commwlan80211BeaconModelParams:Rates'));
    end

    validateattributes(cfpCount, {'numeric'}, ...
      {'real','positive','integer','scalar'}, ...
      'commwlan80211BeaconModelParams', '''CFP Count''');

    validateattributes(cfpPeriod, {'numeric'}, ...
      {'real','positive','integer','scalar'}, ...
      'commwlan80211BeaconModelParams', '''CFP Period''');

    validateattributes(cfpMaxDuration, {'numeric'}, ...
      {'real','positive','integer','scalar'}, ...
      'commwlan80211BeaconModelParams', '''CFP MaxDuration (TU)''');

    validateattributes(cfpDurRemain, {'numeric'}, ...
      {'real','positive','integer','scalar'}, ...
      'commwlan80211BeaconModelParams', '''CFP DurRemaining (TU)''');

    validateattributes(EsNo, {'numeric'}, ...
      {'real','nonnan','scalar'}, ...
      'commwlan80211BeaconModelParams', '''EsNo (dB)''');

    validateattributes(freqOffset, {'numeric'}, ...
      {'real','nonnegative','nonnan','finite','scalar'}, ...
      'commwlan80211BeaconModelParams', '''Frequency offset (Hz)''');

    validateattributes(delay, {'numeric'}, ...
      {'real','nonnegative','nonnan','scalar','<',1024e-6}, ...
      'commwlan80211BeaconModelParams', '''Delay (s)''');

    % Set channel parameters
    set_param([Modelname '/Channel'], 'EsNo', num2str(EsNo))
    set_param([Modelname '/Channel'], 'freqOffset', num2str(freqOffset))
    set_param([Modelname '/Channel'], 'delay', num2str(delay))
    
    if strcmp(sendCFParamsStr, 'on')
      sendCFParams = true;
    else
      sendCFParams = false;
    end
    
    p80211 = commwlan80211BeaconInit(beaconInterval, ssidValue, rates, ...
      channelNumber, sendCFParams, cfpCount, cfpPeriod, cfpMaxDuration, ...
      cfpDurRemain);
    
    assignin('base', 'p80211', p80211);
  case 'cbParamSet'
    maskEnables = get_param(gcb,'MaskEnables');
    maskVisibilities = get_param(gcb,'MaskVisibilities');
    maskNames = get_param(gcb, 'MaskNames');
    beaconParams = {'beaconInterval','ssidValue', 'rates', ...
      'channelNumber','sendCFParams','cfpCount','cfpPeriod',...
      'cfpMaxDuration','cfpDurRemain'};
    chanParams = {'EsNo','freqOffset','delay'};
    beconVisible = ...
      strcmp(get_param(gcb,'paramSet'),'Beacon frame parameters');
    chanVisible = ...
      strcmp(get_param(gcb,'paramSet'),'Channel parameters');
    for p=1:length(beaconParams)
      idx = strcmp(maskNames, beaconParams{p});
      if beconVisible
        maskEnables{idx} = 'on';
        maskVisibilities{idx} = 'on';
      else
        maskEnables{idx} = 'off';
        maskVisibilities{idx} = 'off';
      end
    end
    for p=1:length(chanParams)
      idx = strcmp(maskNames, chanParams{p});
      if chanVisible
        maskEnables{idx} = 'on';
        maskVisibilities{idx} = 'on';
      else
        maskEnables{idx} = 'off';
        maskVisibilities{idx} = 'off';
      end
    end
    set_param(gcb,'MaskEnables',maskEnables)
    set_param(gcb,'MaskVisibilities',maskVisibilities)
  case 'cbSendCFParams'
    maskEnables = get_param(gcb,'MaskEnables');
    maskVisibilities = get_param(gcb,'MaskVisibilities');
    maskNames = get_param(gcb, 'MaskNames');
    cfpCountIdx = strcmp(maskNames, 'cfpCount');
    cfpPeriodIdx = strcmp(maskNames, 'cfpPeriod');
    cfpMaxDurationIdx = strcmp(maskNames, 'cfpMaxDuration');
    cfpDurRemainIdx = strcmp(maskNames, 'cfpDurRemain');
    if strcmp(get_param(gcb,'paramSet'),'Beacon frame parameters') && ...
        strcmp(get_param(gcb,'sendCFParams'),'on')
      maskEnables{cfpCountIdx} = 'on';
      maskVisibilities{cfpCountIdx} = 'on';
      maskEnables{cfpPeriodIdx} = 'on';
      maskVisibilities{cfpPeriodIdx} = 'on';
      maskEnables{cfpMaxDurationIdx} = 'on';
      maskVisibilities{cfpMaxDurationIdx} = 'on';
      maskEnables{cfpDurRemainIdx} = 'on';
      maskVisibilities{cfpDurRemainIdx} = 'on';
    else
      maskEnables{cfpCountIdx} = 'off';
      maskVisibilities{cfpCountIdx} = 'off';
      maskEnables{cfpPeriodIdx} = 'off';
      maskVisibilities{cfpPeriodIdx} = 'off';
      maskEnables{cfpMaxDurationIdx} = 'off';
      maskVisibilities{cfpMaxDurationIdx} = 'off';
      maskEnables{cfpDurRemainIdx} = 'off';
      maskVisibilities{cfpDurRemainIdx} = 'off';
    end
    set_param(gcb,'MaskEnables',maskEnables)
    set_param(gcb,'MaskVisibilities',maskVisibilities)
end

function varargout = getparamvals(blk, varargin)
% Get parameter values from block.  This process uses the 'Evaluate' check
% box (in the mask) to determine whether the parameter string should be
% evaluated in the base workspace.
h = get_param(blk, 'Handle');
maskVars = get(h, 'MaskVariables');
for n = 1:length(varargin)
    paramName = varargin{n};
    paramValStr = get(h, paramName);
    evalParam = maskVars(strfind(maskVars, paramName) ...
        + length(paramName) + 1) == '@';
    if evalParam
        varargout{n} = evalin('base', paramValStr); %#ok<AGROW>
    else
        varargout{n} = paramValStr; %#ok<AGROW>
    end
end
