function [retCode, dongleArray] = dcgetinfo(libName, typeMask, modelMask, prodcodeMask)
%DCGETINFO Get information about Dinkey Pro/FD dongles attached to this
%computer.
%   [RETCODE,DONGLEARRAY] =
%   DCGETINFO(LIBNAME,TYPEMASK,MODELMASK,PRODCODEMASK) calls the API via
%   the external library LIBNAME. TYPEMASK, MODELMASK and PRODCODEMASK can
%   be used to restrict the search to dongles with specific
%   characteristics. DONGLEARRAY is a vector of structures; each structure
%   contains information about a dongle attached to this computer. RETCODE
%   is the return code of the API call.

% Copyright Microcosm Ltd. DO NOT MODIFY THIS FILE.

    MAX_USB_DEVICES = 128;
    MAX_PRODCODE_LEN = 9;

    dongleArray = [];
    numberFound = int32(0);
    typeArray = zeros(1, MAX_USB_DEVICES, 'int32');
    modelArray = zeros(1, MAX_USB_DEVICES, 'int32');
    productCodeArray = zeros(1, MAX_USB_DEVICES * MAX_PRODCODE_LEN, 'uint8');
    dongleNumberArray = zeros(1, MAX_USB_DEVICES, 'uint32');
    updateNumberArray = zeros(1, MAX_USB_DEVICES, 'int32');

    [retCode, prodcodeMask, numberFound, typeArray, modelArray, productCodeArray, dongleNumberArray, updateNumberArray] = ...
        calllib(libName, 'DCGetInfo', typeMask, modelMask, prodcodeMask, MAX_USB_DEVICES, numberFound, typeArray, modelArray, productCodeArray, dongleNumberArray, updateNumberArray);

    for (i = 1:numberFound)
        productCode = productCodeArray(((i-1)*MAX_PRODCODE_LEN)+1 : i*MAX_PRODCODE_LEN);    % Get product codes out of array of bytes, 9 bytes at a time
        productCode = deblank(char(productCode));                                           % Convert to string and remove any trailing NULLs
        newDongle = createdonglestructure(typeArray(i), modelArray(i), productCode, dongleNumberArray(i), updateNumberArray(i));
        dongleArray = [dongleArray newDongle];
    end
end

function dongle = createdonglestructure(type, model, productCode, dongleNumber, updateNumber)
% Create a structure representing a dongle detected by dcgetinfo()

    dongle.type = type;
    switch type
        case 1
            dongle.type_as_string = 'Pro';
        case 2
            dongle.type_as_string = 'FD';
        otherwise
            dongle.type_as_string = '';   % Should never happen!
    end

    dongle.model = model;
    switch model
        case 1
            dongle.model_as_string = 'Lite';
        case 2
            dongle.model_as_string = 'Plus';
        case 3
            dongle.model_as_string = 'Net (1 user)';
        case 4
            dongle.model_as_string = 'Net (5 users)';
        case 5
            dongle.model_as_string = 'Net (10 users)';
        case 6
            dongle.model_as_string = 'Net (50 users)';
        case 7
            dongle.model_as_string = 'Net (unlimited users)';
        otherwise
            dongle.model_as_string = '';  % Should never happen!
    end

    dongle.product_code = char(productCode);
    dongle.dongle_number = uint32(dongleNumber);
    dongle.update_number = int32(updateNumber);
end
