classdef EthernetConnectivityConfig < rtw.connectivity.Config
% ETHERNETCONNECTIVITYCONFIG is an ethernet target connectivity configuration class
% it ties the launcher and builder together
%
%   Copyright 2021 The MathWorks, Inc.
    
    methods
        % Constructor
        function this = EthernetConnectivityConfig(componentArgs)
            % An executable framework specifies additional source files and
            % libraries required for building the PIL executable
            targetApplicationFramework = ...
                codertarget.armM4.pil.EthernetTargetApplicationFramework(componentArgs);
            
            % Create an instance of MakefileBuilder; this works in
            % conjunction with your template makefile to build the PIL
            % executable
            builder = rtw.connectivity.MakefileBuilder(componentArgs, ...
                targetApplicationFramework, ...
                '.out');
            
            % Launcher
            launcher = codertarget.armM4.pil.SerialLauncher(componentArgs, builder);

            % Host side rtiostream communication
            sharedLibExt = system_dependent('GetSharedLibExt');
            rtiostreamLib = ['libmwrtiostreamtcpip' sharedLibExt];
            hostCommunicator = rtw.connectivity.RtIOStreamHostCommunicator(...
                componentArgs, ...
                launcher, ...
                rtiostreamLib);
            
            launcher.Communicator = hostCommunicator;
            % For some targets it may be necessary to set a timeout value
            % for initial setup of the communications channel. For example,
            % the target processor may take a few seconds before it is
            % ready to open its side of the communications channel. If a
            % non-zero timeout value is set then the communicator will
            % repeatedly try to open the communications channel until the
            % timeout value is reached.
            hostCommunicator.setInitCommsTimeout(40); 
            
            % Configure a timeout period for reading of data by the host 
            % from the target. If no data is received with the specified 
            % period an error will be thrown.
            hostCommunicator.setTimeoutRecvSecs(60);
                       
            % Retrieve port from config set
            coderTargetData = componentArgs.getParam('CoderTargetData');
            
            % Set Ethernet settings
            rtIOStreamOpenArgs = {...
                '-hostname', coderTargetData.Ethernet.LocalIPAddress, ...
                '-client', '1', ...
                '-blocking', '0', ...
                '-port', '17725',...
                };
            
            hostCommunicator.setOpenRtIOStreamArgList(...              
                rtIOStreamOpenArgs);                                                                                           
                  
            % Call super class constructor to register components
            this@rtw.connectivity.Config(componentArgs,...
                                         builder,...
                                         launcher,...
                                         hostCommunicator);
            
            % Register timer functions
            clockRate = 1e6 * str2double(coderTargetData.Clocking.cpuClockRateMHz);
            timer = codertarget.arm_cortex_m.pil.Timer(clockRate);
            this.setTimer(timer);

             % Remove profiling overheads
            this.activateOverheadFiltering(true);
            this.runOverheadBenchmark(true);
            this.setOverheadBenchmarkSteps(100);
        end
    end
end

