// **********************************************************************************************************
//    coeEventFlag.cs
//    6/1/2022
//    NGI - Next Generation Interceptor
//    
//    Contract No. HQ0856-21-C-0003/1022000209
//    
//    THIS DOCUMENT DOES NOT CONTAIN TECHNOLOGY OR TECHNICAL DATA CONTROLLED UNDER EITHER THE U.S. 
//    INTERNATIONAL TRAFFIC IN ARMS REGULATIONS OR THE U.S. EXPORT ADMINISTRATION REGULATIONS.
//    
//    RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION PROPRIETARY TO RAYTHEON 
//    COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. 
//    DISCLOSURE TO UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO RAYTHEON 
//    COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS CONTENTS SHALL BE FURNISHED OR DISCLOSED 
//    TO OR COPIED OR USED BY PERSONS OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF 
//    RAYTHEON COMPANY.
//    
//    UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
//    
//    DESTRUCTION NOTICE: FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN DOD 5220.22-M, 
//    NATIONAL INDUSTRIAL SECURITY PROGRAM OPERATING MANUAL, FEBRUARY 2006, 
//    INCORPORATING CHANGE 1, MARCH 28, 2013, CHAPTER 5, SECTION 7, OR DODM 5200.01-VOLUME 3, 
//    DOD INFORMATION SECURITY PROGRAM: PROTECTION OF CLASSIFIED INFORMATION, ENCLOSURE 3, 
//    SECTION 17. FOR CONTROLLED UNCLASSIFIED INFORMATION FOLLOW THE PROCEDURES IN DODM 5200.01-VOLUME 4, 
//    INFORMATION SECURITY PROGRAM: CONTROLLED UNCLASSIFIED INFORMATION.
//    
//    CONTROLLED BY: MISSILE DEFENSE AGENCY
//    CONTROLLED BY: GROUND-BASED MIDCOURSE DEFENSE PROGRAM OFFICE
//    CUI CATEGORY: CTI 
//    DISTRIBUTION/DISSEMINATION CONTROL: F 
//    POC: Alex Kravchenko (1118268)
// **********************************************************************************************************

//\\<Unclassified>
//----------------------------------------------------------------------------//
//                                 UNCLASSIFIED                               //
//----------------------------------------------------------------------------//
//\\<\Unclassified>

//\\<UnlimitedRights>
//----------------------------------------------------------------------------//
// Copyright %(copyright)s Raytheon Company.                                  //
// This software was developed pursuant to Contract Number %(contractNumber)s //
// with the U.S. government. The U.S. government's rights in and to this      //
// copyrighted software are as specified in DFARS 252.227-7014 which was made //
// part of the above contract.                                                //
//----------------------------------------------------------------------------//
//\\<\UnlimitedRights>

//\\<EximUndetermined>
//----------------------------------------------------------------------------//
// WARNING - This document contains technical data and / or technology whose  //
// export or disclosure to Non-U.S. Persons, wherever located, is restricted  //
// by the International Traffic in Arms Regulations (ITAR) (22 C.F.R.         //
// Section 120-130) or the Export Administration Regulations (EAR) (15 C.F.R. //
// Section 730-774). This document CANNOT be exported (e.g., provided to a    //
// supplier outside of the United States) or disclosed to a Non-U.S. Person,  //
// wherever located, until a final jurisdiction and classification            //
// determination has been completed and approved by Raytheon, and any         //
// required U.S. Government approvals have been obtained. Violations are      //
// subject to severe criminal penalties.                                      //
//----------------------------------------------------------------------------//
//\\<\EximUndetermined>

using System;
using System.Runtime.InteropServices;

namespace Raytheon.Instruments.coeCSharp
{
    //
    //
    //
    // Event Flag
    //
    //
    //
    public class coeEventFlag : IDisposable
    {

        #region DLLImports
        [DllImport(coe.coeDLL, CallingConvention = CallingConvention.Cdecl, EntryPoint = "OE_Event_Flag_Create_Dynamic")]
        private static extern IntPtr OE_Event_Flag_Create_Dynamic(IntPtr Name,
                                                                  coe.ScopeType Scope,
                                                                  uint InitialMask,
                                                                  IntPtr ApplicationContext,
                                                                  out coe.Status Status);

        [DllImport(coe.coeDLL, CallingConvention = CallingConvention.Cdecl, EntryPoint = "OE_Event_Flag_Delete")]
        private static extern coe.Status OE_Event_Flag_Delete(IntPtr _obj);

        [DllImport(coe.coeDLL, CallingConvention = CallingConvention.Cdecl, EntryPoint = "OE_Event_Flag_Set")]
        private static extern coe.Status OE_Event_Flag_Set(IntPtr _obj,
                                                           uint Mask);

        [DllImport(coe.coeDLL, CallingConvention = CallingConvention.Cdecl, EntryPoint = "OE_Event_Flag_Clear")]
        private static extern coe.Status OE_Event_Flag_Clear(IntPtr _obj,
                                                             uint Mask);

        [DllImport(coe.coeDLL, CallingConvention = CallingConvention.Cdecl, EntryPoint = "OE_Event_Flag_Peek_Mask")]
        private static extern coe.Status OE_Event_Flag_Peek_Mask(IntPtr _obj,
                                                                 out uint Mask);

        [DllImport(coe.coeDLL, CallingConvention = CallingConvention.Cdecl, EntryPoint = "OE_Event_Flag_Wait")]
        private static extern coe.Status OE_Event_Flag_Wait(IntPtr _obj,
                                                            uint Mask,
                                                            uint ResetMask,
                                                            WaitPolicyType PolicyType,
                                                            int TimeInterval,
                                                            out uint CurrentMask);

        #endregion

        private bool _disposed = false;
        private IntPtr _handle = IntPtr.Zero;
        
        public enum WaitPolicyType : int
        {
            WAIT_FOR_ALL = 0,
            WAIT_FOR_ANY = 1
        }

        public coeEventFlag()
        {
            _handle = OE_Event_Flag_Create_Dynamic(IntPtr.Zero, coe.ScopeType.OE_Local, 0, IntPtr.Zero, out coe.Status oe_status);
            if (oe_status != coe.Status.SUCCESS)
            {
                _handle = IntPtr.Zero;
                throw new Exception("Unable to create OE_Event_Flag.  Error = " + oe_status);
            }
        }

        ~coeEventFlag()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (_disposed) 
                return;

            if (disposing)
            {
            }
            if (_handle != IntPtr.Zero)
            {
                OE_Event_Flag_Delete(_handle);
                _handle = IntPtr.Zero;
            }
            _disposed = true;
        }

        internal IntPtr Handle
        {
            get { return _handle; }
        }

        public coe.Status Set(uint mask)
        {
            return OE_Event_Flag_Set(_handle, mask);
        }

        public coe.Status Clear(uint mask)
        {
            return OE_Event_Flag_Clear(_handle, mask);
        }

        public coe.Status Peek(out uint mask)
        {
            coe.Status status;
            status = OE_Event_Flag_Peek_Mask(_handle, out uint currentMask);
            mask = currentMask;
            return status;
        }

        public coe.Status Wait(uint mask,
                               uint resetMask,
                               WaitPolicyType waitPolicy,
                               int timeout,
                               out uint currentMask)
        {
            coe.Status status;
            status = OE_Event_Flag_Wait(_handle, mask, resetMask, waitPolicy, timeout, out uint returnedMask);
            currentMask = returnedMask;
            return status;
        }
    }
}

//\\<Unclassified>
//----------------------------------------------------------------------------//
//                                 UNCLASSIFIED                               //
//----------------------------------------------------------------------------//
//\\<\Unclassified>

