classdef RtIOStreamHostCommunicator < rtw.connectivity.Communicator
%RTIOSTREAMHOSTCOMMUNICATOR implements a host-side communicator
%
%   RTIOSTREAMHOSTCOMMUNICATOR(COMPONENTARGS, LAUNCHER, RTIOSTREAMLIB) creates
%   an instance of this class using the shared library
%   RTIOSTREAMLIB. RTIOSTREAMLIB must be an implementation of the rtiostream API
%   and provides the host-side of a communications channel. For more details on
%   rtiostream, run openExample('ecoder/PILTargetCommunicationExample').
% 
%   To create your own target connectivity configuration, you will only need to
%   used the methods of this class that are listed and hotlinked below. All
%   other properties and methods of this class are undocumented and likely to
%   change from release to release.
%
%   RtIOStreamHostCommunicator methods:
%
%       SETOPENRTIOSTREAMARGLIST - set an argument list for opening the channel
%       ADDOPENRTIOSTREAMARGS    - appends to the argument list
%       GETOPENRTIOSTREAMARGLIST - returns the argument list
%       SETINITCOMMSTIMEOUT      - sets an initCommunication timeout (in secs)
%
%   See also RTW.CONNECTIVITY.COMPONENTARGS, CODER.MYPIL.CONNECTIVITYCONFIG,
%   RTWDEMO_RTIOSTREAM

%   Copyright 2007-2020 The MathWorks, Inc.
%     

       
    methods
        % constructor
        function this = RtIOStreamHostCommunicator(componentArgs, ...
                launcher, rtiostreamLib)
            narginchk(3, 3);
            
            validateattributes(rtiostreamLib, {'char', 'string'}, {'scalartext'});
            rtiostreamLib = convertStringsToChars(rtiostreamLib);
            
            % maintain backwards compatibility with old name for the shared
            % library by add the prefix "libmw" if it is missing. We do
            % this for windows platforms only since for Linux, we were
            % already using the prefix.
            if ispc
                sharedLibExt=system_dependent('GetSharedLibExt');
                if any(strcmp(rtiostreamLib, {['rtiostreamtcpip' sharedLibExt],...
                        ['rtiostreamserial' sharedLibExt]}))
                    rtiostreamLib = ['libmw' rtiostreamLib];
                end
            end
            
            % create RtIOStreamWrapper
            rtIOStream = rtw.connectivity.RtIOStreamWrapper(rtiostreamLib);
            
            % includes to support rtIOStream shared library loading
            rtIOStream.setIncludeHeaders({'rtiostream_utils.h'});
            
            % call super class constructor
            this@rtw.connectivity.Communicator(componentArgs, launcher, rtIOStream);                                                
        end
        
        function setOpenRtIOStreamArgList(this, argList)
        %SETOPENRTIOSTREAMARGLIST sets an argument list for opening the channel
        %
        %   SETOPENRTIOSTREAMARGLIST(THIS,ARGLIST) sets the string ARGLIST as the
        %   argument list to be used when the rtIOStreamOpen function of the
        %   rtiostream shared library is called.
            
            this.getRtIOStream.setOpenRtIOStreamArgList(argList);        
        end

        function setOpenRtIOStreamArgPair(this, argPair)
        %SETOPENRTIOSTREAMARGPAIR sets an argument pair within the argument list
        %
        %   SETOPENRTIOSTREAMARGPAIR(THIS,ARGPAIR) sets the ARGPAIR within the
        %   argument list to be used when the rtIOStreamOpen function of the
        %   rtiostream shared library is called. ARGPAIR must be a cell array
        %   with length equal to two containing a name/value pair; if the name
        %   already exists in the argument list the corresponding value is
        %   replaced.

            this.getRtIOStream.setOpenRtIOStreamArgPair(argPair);            
        end        
        
        function addOpenRtIOStreamArgs(this, argList)
        %ADDOPENRTIOSTREAMARGS appends to the argument list
        %
        %   ADDOPENRTIOSTREAMARGS(THIS,ARGLIST) appends the string ARGLIST to
        %   the argument list to be used when the rtIOStreamOpen function of the
        %   rtiostream shared library is called.

            this.getRtIOStream.addOpenRtIOStreamArgs(argList);                    
        end
        
        function setTimeoutRecvSecs(this, recvTimeout)
        %SETTIMEOUTRECVSECS sets the timeout value for receiving data
        %
        %   SETTIMEOUTRECVSECS(THIS,TIMEOUT) configures the rtiostream 
        %   interface to time out if no new data is received for a period
        %   of greater than the specified recvTimeout seconds.
        
            this.getRtIOStream.setRecvTimeout(recvTimeout);
        end      
        
        function setTimeoutSendSecs(this, sendTimeout)
        %SETTIMEOUTSENDSECS sets the timeout value for sending data
        %
        %   SETTIMEOUTSENDSECS(THIS,TIMEOUT) configures the rtiostream 
        %   interface to time out if the whole data was not sent in 
        %   the specified sendTimeout seconds.
        
            this.getRtIOStream.setSendTimeout(sendTimeout);
        end   
        
        function argList = getOpenRtIOStreamArgList(this)
        %GETOPENRTIOSTREAMARGLIST returns the argument list
        %
        %   ARGLIST = ADDOPENRTIOSTREAMARGLIST(THIS) returns the string ARGLIST
        %   to be used when the rtIOStreamOpen function of the rtiostream shared
        %   library is called.

            argList = this.getRtIOStream.getOpenRtIOStreamArgList;
        end
        
        function setInitCommsTimeout( this, openTimeout )
        %SETINITCOMMSTIMEOUT sets an initCommunication timeout (in secs)
            this.getRtIOStream.setOpenTimeout(openTimeout);
        end                                                                           
        
        function stationID = getStationID(this)
            stationID = this.getRtIOStream.getStreamID;
        end
        
        function rtIOStreamLib = getRtIOStreamLib(this)
            rtIOStreamLib = this.getRtIOStream.getRtIOStreamLib;
        end
    end
end
