/*++
*******************************************************************************
* Copyright (c) 1999-2000 Digi International Inc., All Rights Reserved        *
*                                                                             *
* This software contains proprietary and confidential information of Digi     *
* International Inc.  By accepting transfer of this copy, Recipient agrees    *
* to retain this software in confidence, to prevent disclosure to others,     *
* and to make no use of this software other than that for which it was        *
* delivered.  This is an unpublished copyrighted work of Digi International   *
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly   *
* prohibited.                                                                 *
*                                                                             *
* Restricted Rights Legend                                                    *
*                                                                             *
* Use, duplication, or disclosure by the Government is subject to             *
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in         *
* Technical Data and Computer Software clause at DFARS 252.227-7031 or        *
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -          *
* Restricted Rights at 48 CFR 52.227-19, as applicable.                       *
*                                                                             *
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343          *
*                                                                             *
*******************************************************************************

Module Name:

	pppdatasource.cpp

Abstract:

	PPP datasource implementation.

Author:

	Michael Zarns

--*/
#include "stdafx.h"
#include "Diagnostic.h"
#include "DataSource.h"
#include "Tracer.h"
#include "IsdnTrace.h"
#include "TraceStatView.h"
#include "Prime.h"
#include "StatDoc.h"
#include "typeinfo.h"
#include "pppdatasource.h"

void CPPPDataSourceBase::LoadSummaryInfo(const CString& frmvsn)
{
	CDataSource::LoadSummaryInfo(frmvsn);
	AddDriverInfo( "DigiPpp" );
}

void CDataSource::ClearTraceStats()
{ 
	if (IsTracing() && m_TraceData)
		m_TraceData->ClearTraceStats();
}

CPPPDataSourceBase::CPPPDataSourceBase(const HKEY hkLine, const CString& DeviceName) : 
	CDataSource(hkLine, DeviceName)
{
	m_Status = dssNotAvailable;
	m_TracePPPSNA = 1;
//	m_BaudRate = 64000;		// hardcoded to single timeslot max linespeed
}

const CSXB_USER_IOCTL *CPPPDataSourceBase::GetStatType(const type_info& Type, int index) const
{
	const CSXB_USER_IOCTL *result = CDataSource::GetStatType(Type, index);

	if (result == NULL)
	{
		if (typeid(CWANLINESTATUS) == Type)
			result =  &m_WanLineStatus;
	}

	return result;
}


BOOL CPPPDataSourceBase::UpdateDataSourceStats()
{
	CDataSource::UpdateDataSourceStats();

	if (GetData (&m_WanLineStatus) == dssRunning)
	{
		IncrementError(m_Err, m_WanLineStatus->wan_linestatus_stats.hc_noclock +
			m_WanLineStatus->wan_linestatus_stats.hc_rxorun +
			m_WanLineStatus->wan_linestatus_stats.hc_rxcrc +
			m_WanLineStatus->wan_linestatus_stats.hc_rxbiterr +
			m_WanLineStatus->wan_linestatus_stats.hc_rxoflow +
			m_WanLineStatus->wan_linestatus_stats.hc_rxabort);
	}

	return m_Status == dssRunning;
}

// Set ENABLE_PPP_TRACE to 1 to enable support via HDLC tracing in WAN module.  
// Currently, it seems firmware doesn't trace received packets, and doesn't allow
// multiple opens to the WANS device so starting a second trace stream fails on the 
// open with error OPENFAILED.
#define ENABLE_PPP_TRACE 0

CTracer *CPPPDataSourceBase::CreateTracer()
{ 
#if ENABLE_PPP_TRACE
	return new CHdlcTracer(this); 
#else
	return NULL; 
#endif
}

CTraceData *CPPPDataSourceBase::CreateTraceData(DWORD baud, CDWordArray& circuits)
{
#if ENABLE_PPP_TRACE
	return new CHdlcTraceData(baud, circuits); 
#else
	return NULL; 
#endif
}


///////////////////////////////////////////////////////////////////////////////
//
// SYNC Line PPP
//
///////////////////////////////////////////////////////////////////////////////

CPPPDataSource::CPPPDataSource(const HKEY hkLine, const CString& DeviceName) : CPPPDataSourceBase(hkLine, DeviceName)
{
	m_Status = dssNotAvailable;
}

CRuntimeClass* CPPPDataSource::GetStatDocClass() const
{
	return RUNTIME_CLASS(CPPPStatDoc);
}


///////////////////////////////////////////////////////////////////////////////
//
// CSU Line PPP
//
///////////////////////////////////////////////////////////////////////////////

CT1PPPDataSource::CT1PPPDataSource(const HKEY hkLine, const CString& DeviceName) : CPPPDataSourceBase(hkLine, DeviceName),
                                                                                   m_IfaceState(m_SNID - 'A'),
                                                                                   m_Rfc1406(m_SNID - 'A')
{
	m_Status = dssNotAvailable;
}


CRuntimeClass* CT1PPPDataSource::GetStatDocClass() const
{
	return RUNTIME_CLASS(CPPPCsuStatDoc);
}

const CSXB_USER_IOCTL *CT1PPPDataSource::GetStatType(const type_info& Type, int index) const
{
	const CSXB_USER_IOCTL *result = CDataSource::GetStatType(Type, index);

	if (result == NULL) 
	{
		if (typeid(m_IfaceState) == Type)
			result = &m_IfaceState;
		else if (typeid(m_Rfc1406) == Type)
			result = &m_Rfc1406;
	}
	return result;
}


BOOL CT1PPPDataSource::UpdateDataSourceStats()
{
	if ( m_Status == dssRunning )
	{
		GetData(&m_IfaceState);
		if ( m_Status == dssRunning )
		{
			switch ( ALARM_CONDITION( m_IfaceState->State ) )
			{
				case RED_ALARM_CONDITION:		m_Alarms = "Red";		break;
				case YELLOW_ALARM_CONDITION:	m_Alarms = "Yellow";	break;
				case BLUE_ALARM_CONDITION:		m_Alarms = "Blue";	break;
				case NO_ALARM_CONDITION:		
				default:								m_Alarms = "None";	break;
			}
		}
		else
			m_Alarms = "N/A";

		GetData(&m_Rfc1406);
		if ( m_Status == dssRunning )
		{
			// Add up the 24hour total errors
			__int64 Error = 
				m_Rfc1406->Total.ES + m_Rfc1406->Total.SES + m_Rfc1406->Total.SEFS + 
				m_Rfc1406->Total.UAS + m_Rfc1406->Total.CSS + m_Rfc1406->Total.PCV +
				m_Rfc1406->Total.LES + m_Rfc1406->Total.BES + m_Rfc1406->Total.DM + 
				m_Rfc1406->Total.LCV;
			// Then add in the current 15-minute interval errors
			Error += 
				m_Rfc1406->Current.ES + m_Rfc1406->Current.SES + m_Rfc1406->Current.SEFS + 
				m_Rfc1406->Current.UAS + m_Rfc1406->Current.CSS + m_Rfc1406->Current.PCV +
				m_Rfc1406->Current.LES + m_Rfc1406->Current.BES + m_Rfc1406->Current.DM + 
				m_Rfc1406->Current.LCV;
			m_Err = Error;	// Don't increment because these counters are already cumulative
		}
	}
	
	HandleClearStats(m_Err, m_ClearErr);
	return m_Status == dssRunning;
}
