function varargout = commblkuserdefinedmapping(maskM, maskMapping, isRectQAMBlk)
% COMMBLKUSERDEFINEDMAPPING Communications Toolbox Baseband Mod and
% Demod block helper function used to check params when in 'User defined'
% mapping mode.

% Copyright 2005-2018 The MathWorks, Inc.

% Initialize output 
err.identifier  = '';
err.message = '';
varargout{1} = err;

if isRectQAMBlk
    if mod(log2(maskM),2)
        err.identifier = 'comm:commblkuserdefinedmapping:InvalidConstOrdering';
        err.message    = 'User-defined constellation ordering is only supported for Square QAM.';
        varargout{1}   = err;
        return;
    end
end

if ~isnumeric(maskMapping) || ~isvector(maskMapping) || ... 
        length(maskMapping) ~= maskM || ~all(floor(maskMapping) == maskMapping)
    err.identifier = 'comm:commblkuserdefinedmapping:InvalidConstMapping';
    err.message    = 'Constellation mapping must be an integer-valued vector of length M.';
    varargout{1}   = err;
    return;
end

if (any(maskMapping < 0) || any(maskMapping >= maskM))
    err.identifier = 'comm:commblkuserdefinedmapping:InvalidConstMappingRange';
    err.message    = 'Constellation mapping must be in the range [0, M-1].';
    varargout{1}   = err;
    return;
end

if (length(unique(maskMapping)) ~= maskM)
    err.identifier = 'comm:commblkuserdefinedmapping:ConstMappingNotUnique';
    err.message    = 'Constellation mapping must be a unique-valued vector.';
    varargout{1}   = err;
    return;
end

if ~isa(maskMapping,'double')
    err.identifier = 'comm:commblkuserdefinedmapping:ConstMappingNotDouble';
    err.message    = 'Constellation mapping must be a vector of data type double.';
    varargout{1}   = err;
    return;
end

% [EOF]
