classdef helperBLEMeshNetworkNode < handle
%helperBLEMeshNetworkNode Create an object for Bluetooth mesh node
%   MESHNODE = helperBLEMeshNetworkNode creates a Bluetooth mesh node
%   (with only network layer) with respective properties and methods.
%
%   MESHNODE = helperBLEMeshNetworkNode(Name, Value) creates a Bluetooth
%   mesh node with the specified property Name set to the specified Value.
%   You can specify additional name-value pair arguments in any order as
%   (Name1, Value1, ..., NameN, ValueN).
%
%   helperBLEMeshNetworkNode properties:
%
%   Identifier      - Unique identification for a node
%   State           - Indicating whether the node is power on/off
%   NetworkLayer    - Network layer functionality

%   Copyright 2019 The MathWorks, Inc.

%#codegen

properties
    %Identifier Unique identification for a node
    %   Specify the identifier as a positive integer. This property
    %   indicates unique identification for the node in a Bluetooth mesh
    %   network. The default value is 1.
    Identifier = 1
    
    %State Indicating whether the node is power on/off
    %   Specify the state property as a one of 'On' | 'Off'. This property
    %   indicates the state of a node i.e. switched 'On' or 'Off'. The
    %   default value is 'On'.
    State = 'On'
    
    %NetworkLayer Network layer functionality
    %   Specify this property as an object of type <a
    %   href="matlab:help('helperBLEMeshNetworkLayer')">helperBLEMeshNetworkLayer</a>.
    %   This object contains methods and properties related to mesh network
    %   layer.
    NetworkLayer
end

methods
    % Constructor
    function obj = helperBLEMeshNetworkNode(varargin)
        % Assign name-value pairs
        for idx = 1:2:nargin
            obj.(varargin{idx}) = varargin{idx+1};
        end
        % Assign network layer object
        obj.NetworkLayer = helperBLEMeshNetworkLayer();
    end
    
    % For auto completion
    function v = set(~, prop)
        if strcmp(prop, 'State')
            v = {'On';'Off'};
        end
    end
    
    % Set identifier
    function set.Identifier(obj, value)
        validateattributes(value, {'numeric'}, {'scalar', 'integer', ...
            '>=', 0}, mfilename, 'Identifier');
        obj.Identifier = value;
    end
    
    % Set state
    function set.State(obj, value)
        obj.State = validatestring(value, {'On';'Off'}, mfilename, 'State');
    end
    
    % Set network layer
    function set.NetworkLayer(obj, value)
        validateattributes(value, {'helperBLEMeshNetworkLayer'}, {'scalar'}, ...
            mfilename, 'NetworkLayer');
        obj.NetworkLayer = value;
    end
    
    % Run node (only network layer is simulated in this node)
    function txPDU = run(obj, elapsedTime, rxPDU)
        % Return if switched off
        if strcmp(obj.State, 'Off')
            txPDU = zeros(1, 0);
            return;
        end
        
        % Run network layer
        [~, txPDU] = obj.NetworkLayer.run(elapsedTime, rxPDU);
    end
end
end
