/////////////////////////////// // ModLapLinkInterface2.cpp // Nate Jenkins /////////////////////////////// #include "ModLapLinkInterface2.h" ////////////////////////////////////////////////////////////////////////////// // Defualt Constructor CModLapLinkInterface2::CModLapLinkInterface2() : m_nPort(-1), m_nError(NOERRORS), m_ucByte(0xFF), m_ucDATA(0xFF), m_ucSTAT(0xFF), m_ucCTRL(0xFF) { } ////////////////////////////////////////////////////////////////////////////// // Destructor CModLapLinkInterface2::~CModLapLinkInterface2() { } ////////////////////////////////////////////////////////////////////////////// // Get exclusive access to parallel port int CModLapLinkInterface2::GetPort( int nPrtNo ) { char * chSysCmd = "insmod ppdev"; // we will attempt to load module char cDev[30]; // assume we will be successful in loading module and getting exclusive access to port m_nError = NOERRORS; if( !(m_nPort<0) ) // if there is a port already { m_nError = ALREADYHAVEPORT; } else // else we will attempt to open the parport { // load driver into kernel m_nError = system( chSysCmd ); #ifdef DEBUG_GETPORT printf("\"%s\" returned %d\n", chSysCmd, m_nError); #endif // figure out the desired name of the parport device node sprintf(cDev, "%s%d", ONEANDONLYPARPORT, nPrtNo); #ifdef DEBUG_GETPORT printf("Attempting to use %s . . .\n", cDev); #endif // open parallel port file descriptor m_nPort = open(cDev, O_RDWR|O_EXCL|O_NDELAY); if ( m_nPort < 0 ) { printf("OPEN attempt for \"%s\" -> failed\n", cDev); m_nError = WILLNOTOPEN; } // end of did not open else { #ifdef DEBUG_GETPORT printf("OPEN attempt \"%s\" -> succeded\n", cDev); #endif m_nError = ioctl(m_nPort, PPEXCL, 0); if( m_nError ) // if we can't setup the parport to claim only exclusively { printf("EXCL attempt -> failed with %d, errno = %d\n", m_nError, errno); m_nError = WILNOTEXCL; } else // else claim will only succeed if we can do so with exclusive access { #ifdef DEBUG_GETPORT printf("EXCL attempt -> succeded\n"); #endif m_nError = ioctl(m_nPort, PPCLAIM, 0); if( m_nError ) // if another process had it first { printf("CLAIM attempt -> failed with %d, errno = %d\n", m_nError, errno); m_nError = WILNOTCLAIM; } else // else we have exclusive access to parallel port device { #ifdef DEBUG_GETPORT printf("CLAIM attempt -> succeded\n"); #endif m_nError = NOERRORS; // remember what are in registers READ_DATA_REG(); READ_STAT_REG(); READ_CTRL_REG(); } } // end of exlusive setup for claim } // end of the parallel port device node opened fine // if we are not completely successfull, clean up the mess if( NOERRORS != m_nError ) ReleasePort(); } // end of getting new port return m_nError; } ////////////////////////////////////////////////////////////////////////////// // Relenquish shelfishness with respect to parallel port and unload ppdev module int CModLapLinkInterface2::ReleasePort( void ) { char * chSysCmd = "rmmod ppdev"; // we will attempt to unload module m_nError = NOERRORS; // assume we will be successful in releasing the port if( !(m_nPort<0) ) // if there is a port to release { close(m_nPort); // then release it m_nPort = -1; // indicate it has been released } // unload driver out of kernel m_nError = system( chSysCmd ); #ifdef DEBUG_RELEASEPORT printf("\"%s\" returned code %d\n", chSysCmd, m_nError); #endif return m_nError; } ////////////////////////////////////////////////////////////////////////////// int CModLapLinkInterface2::GetD( unsigned char * ucData ) { READ_DATA_REG(); if(NOERRORS==m_nError) { *ucData = m_ucByte; } return m_nError; } ////////////////////////////////////////////////////////////////////////////// int CModLapLinkInterface2::GetS( unsigned char * ucData ) { READ_STAT_REG(); if(NOERRORS==m_nError) { *ucData = m_ucByte; } return m_nError; } ////////////////////////////////////////////////////////////////////////////// int CModLapLinkInterface2::GetC( unsigned char * ucData ) { READ_CTRL_REG(); if(NOERRORS==m_nError) { *ucData = m_ucByte; } return m_nError; } ////////////////////////////////////////////////////////////////////////////// int CModLapLinkInterface2::SetD( unsigned char ucData ) { m_ucByte = ucData; WRITE_DATA_REG(); return m_nError; } ////////////////////////////////////////////////////////////////////////////// int CModLapLinkInterface2::SetC( unsigned char ucData ) { m_ucByte = ucData & 0x0F; WRITE_CTRL_REG(); return m_nError; } //////////////////////////////////////////////////////////////////////////////