#include "stdafx.h"
#include "Device.h"

int IoADevice::Attach( IoASerialInterface& SerialInterface )
{
    Detach( );  // make sure device is detached before attaching it

    IoASerialInterface *pSerialInterface = &SerialInterface;

    IoASerialProtocolEnum Protocol;
    int BaseAddress;

    if ( ! pSerialInterface->Attach( this, Protocol, BaseAddress ) ) return FALSE;

    m_Protocol = Protocol;

    m_Address = BaseAddress + m_Index;  // calculate device address

    m_pSerialInterface = pSerialInterface;

    return TRUE;
}

// Read + write functions for BSP protocol
int IoADevice::ReadBSP( int PreSelect, int Bits[ ], int nBits, int PostSelect ) const
{
    if ( ! IsAttached( ) ) return FALSE;

    if ( ! IsProtocol( eBSP ) ) return FALSE;

    if ( ! m_pSerialInterface->ReadBSP( Address( ), PreSelect, Bits, nBits, PostSelect ) ) return FALSE;

    return TRUE;
}

int IoADevice::WriteBSP( int PreSelect, const int Bits[ ], int nBits, int PostSelect ) const
{
    if ( ! IsAttached( ) ) return FALSE;

    if ( ! IsProtocol( eBSP ) ) return FALSE;

    if ( ! m_pSerialInterface->WriteBSP( Address( ), PreSelect, Bits, nBits, PostSelect ) ) return FALSE;

    return TRUE;
}
