//
//  Copyright © 2003, RESON Inc. All Rights Reserved.
//
//  No part of this file may be reproduced or transmitted in any form or by
//  any means, electronic or mechanical, including photocopy, recording, or
//  information storage or retrieval system, without permission in writing
//  from RESON Inc.
//
//  Filename:   SubsystemIndex.cpp
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    Defines CSubsystemIndex class to assist with defining the association between
//              the Sensor Index and the 7k doublet used for id.
//
//  Notes:      
//

#include "StdAfx.h"
#include "SubsystemIndex.h"

///////////////////////////////////////////////////////////////////////////////
// CSubsystemIndex class implementation.

CSubsystemIndex::CSubsystemIndex(   PFN_SUBSYSTEMCALLBACK   pfnCallback,
                                    void                   *pvParam,
                                    const int              &riSensorIndex,
                                    const int              &riSystemIndex,
                                    const unsigned long    &rulDeviceId,
                                    const unsigned short   &runSystemEnumerator )
{
    m_pfnCallback        = pfnCallback;
    m_pvParam            = pvParam;
    m_iSensorIndex       = riSensorIndex;
    m_iSystemIndex       = riSystemIndex;
    m_ulDeviceId         = rulDeviceId;
    m_unSystemEnumerator = runSystemEnumerator;
}

CSubsystemIndex::~CSubsystemIndex( void )
{
    m_pfnCallback        = NULL;
    m_pvParam            = NULL;
    m_iSensorIndex       = 0;
    m_iSystemIndex       = 0;
    m_ulDeviceId         = 0UL;
    m_unSystemEnumerator = 0;
}

CSubsystemIndex::CSubsystemIndex( const CSubsystemIndex &rRhs )
{
    m_pfnCallback        = rRhs.m_pfnCallback;
    m_pvParam            = rRhs.m_pvParam;
    m_iSensorIndex       = rRhs.m_iSensorIndex;
    m_iSystemIndex       = rRhs.m_iSystemIndex;
    m_ulDeviceId         = rRhs.m_ulDeviceId;
    m_unSystemEnumerator = rRhs.m_unSystemEnumerator;
}

CSubsystemIndex & CSubsystemIndex::operator = ( const CSubsystemIndex &rRhs )
{
    m_pfnCallback        = rRhs.m_pfnCallback;
    m_pvParam            = rRhs.m_pvParam;
    m_iSensorIndex       = rRhs.m_iSensorIndex;
    m_iSystemIndex       = rRhs.m_iSystemIndex;
    m_ulDeviceId         = rRhs.m_ulDeviceId;
    m_unSystemEnumerator = rRhs.m_unSystemEnumerator;

    return *this;
}

bool CSubsystemIndex::SetDeviceIndicies(    const int              &riSensorIndex,
                                            const int              &riSystemIndex,
                                            const unsigned long    &rulDeviceId,
                                            const unsigned short   &runSystemEnumerator )
{
    m_iSensorIndex       = riSensorIndex;
    m_iSystemIndex       = riSystemIndex;
    m_ulDeviceId         = rulDeviceId;
    m_unSystemEnumerator = runSystemEnumerator;

    return true;
}

bool CSubsystemIndex::SetCallback(  PFN_SUBSYSTEMCALLBACK   pfnCallback,
                                    void                   *pvParam )
{
    m_pfnCallback = pfnCallback;
    m_pvParam     = pvParam;

    return true;
}

bool CSubsystemIndex::Is7kSubsystem(    const unsigned long    &rulDeviceId,
                                        const unsigned short   &runSystemEnumerator ) const
{
    return ( ( rulDeviceId         == m_ulDeviceId         ) &&
             ( runSystemEnumerator == m_unSystemEnumerator )  );
}

bool CSubsystemIndex::operator == ( const int &riSensorIndex ) const
{
    return ( riSensorIndex == m_iSensorIndex );
}

bool CSubsystemIndex::operator < ( const CSubsystemIndex &rRhs ) const
{
    // List of sensors and their doublets are to be priority sorted as follows:
    // a) Device id,
    // b) system enumerator and then
    // c) subsystem id.

    bool bLHSIsLess = false;
    
    if ( m_ulDeviceId < rRhs.m_ulDeviceId )
    {
        bLHSIsLess = true;
    }
    else if ( m_unSystemEnumerator < rRhs.m_unSystemEnumerator )
    {
        bLHSIsLess = true;
    }

    return bLHSIsLess;
}

void CSubsystemIndex::operator()    (   const BYTE             *pbyData,
                                        const unsigned long    &rulBytes,
                                        const unsigned long    &rulTimeStamp )
{
    try
    {
        if ( m_pfnCallback != NULL )
        {
            ( m_pfnCallback ) ( m_pvParam, pbyData, rulBytes, rulTimeStamp );
        }
    }
    catch ( ... )
    {
        TRACE ( _T( "CSubsystemIndex::operator(), Unspecified exception caught!\n" ) );
    }
}

int CSubsystemIndex::SensorIndex( void ) const
{
    return m_iSensorIndex;
}

int CSubsystemIndex::SystemIndex( void ) const
{
    return m_iSystemIndex;
}

void CSubsystemIndex::SystemIndex( const int &riSystemIndex )
{
    m_iSystemIndex = riSystemIndex;
}

unsigned long  CSubsystemIndex::DeviceId( void ) const
{
    return m_ulDeviceId;
}

unsigned short CSubsystemIndex::SystemEnumerator( void ) const
{
    return m_unSystemEnumerator;
}

