WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
ExternalInterfaceModel.h
/******************************************************************************
* Copyright (c) ATTX LLC 2024. All Rights Reserved.
*
* This software and associated documentation (the "Software") are the 
* proprietary and confidential information of ATTX, LLC. The Software is 
* furnished under a license agreement between ATTX and the user organization 
* and may be used or copied only in accordance with the terms of the agreement.
* Refer to 'license/attx_license.adoc' for standard license terms.
*
* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION 
* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or 
* transmitted in any form or by any means, for any purpose, without the express 
* written permission of ATTX, LLC.
******************************************************************************/
/*
Metadata for MS GUI:
imdata = {"exclude" : True}
*/

#ifndef MODELS_SUPPORT_EXTERNAL_INTERFACE_MODEL
#define MODELS_SUPPORT_EXTERNAL_INTERFACE_MODEL

#include <vector>

#include "architecture/Time.h"
#include "flight/App.h"
#include "flight/FlightExecutive.h"
#include "simulation/SimulationSteps.h"
#include "utils/DataIOSocketRelay.h"
#include "utils/DataIOShmemRelay.h"

namespace warptwin {

    /**
     * @brief Manage all external interfaces to and from warptwin
     * 
     * The External Interface Model is designed to manage all interfaces
     * to and from WarpTwin. It is configured such that it manages the
     * creation, storage, and synchronization of DataIO interfaces to
     * shared memory and sockets, as created through connectExternalSocket
     * and connectExternalSharedMemory. These interfaces are shychronized 
     * twice per simulation step -- once in start step before any other
     * models run, and once in end step after all models have run. 
     * 
     * This model is designed to be created by the Sim Scheduler and should
     * not be created individually.
     * 
     * In addition to shmem and socket synchronization, this model manages the 
     * following:
     * - Synchronizing data passthroughs from socket to shared memory and vice-versa
     * - Receiving commands from external sources through the WarpTwin API
     */
    class ExternalInterfaceModel : public warpos::App {
    public:
        /// Model params
        ///         NAME                    TYPE                    DEFAULT VALUE
        START_PARAMS

        END_PARAMS

        /// Model inputs
        ///         NAME                    TYPE                    DEFAULT VALUE
        START_INPUTS

        END_INPUTS

        /// Model outputs
        ///         NAME                    TYPE                    DEFAULT VALUE
        START_OUTPUTS

        END_OUTPUTS

        /// @brief Connect a signal to an external socket
        /// @param signal The signal to connect externally
        /// @param mode The mode (must be INPUT or OUTPUT) on which the signal is connected
        /// @param ip The IP address of the connection
        /// @param port The port for the connection
        void connectExternalSocket(clockwerk::DataIOBase& signal, std::string mode, 
                                   const std::string &ip, int port);

        /// @brief Connect a signal to an external shared memory
        /// @param signal The signal to connect externally
        /// @param mode The mode (must be INPUT or OUTPUT) on which the signal is connected
        /// @param shmem_address The shared memory location on which the signal is connected
        void connectExternalSharedMemory(clockwerk::DataIOBase& signal, std::string mode,
                                         const std::string shmem_address);

        /// Model-specific implementations of startup and derivative
        /// Must be scheduled manually to derivative
        ExternalInterfaceModel(warpos::FlightExecutive &executive);
        virtual ~ExternalInterfaceModel();
    protected:
        int16 start() override;
        int16 execute() override; 

        /// @brief Synchronize all sockets to external
        /// @return Error code corresponding to success/failure
        int _syncSockets();
        /// @brief Synchronize all shared memory to external
        /// @return Error code corresponding to success/failure
        int _syncShmem();

        // Where our pointers to relays are held. Note these must be recorded
        // as DataIOBase
        std::vector<DataIOSocketRelay*> _socket_relays;
        std::vector<DataIOShmemRelay*> _shmem_relays;
    };

}

#endif