extern "C" int CanalGetStatistics( long handle, PCANALSTATISTICS pCanalStatistics ) #endif { CPeakObj *pdrvObj = theApp->getDriverObject( handle ); if ( NULL == pdrvObj ) return 0; return ( pdrvObj->getStatistics( pCanalStatistics ) ? CANAL_ERROR_SUCCESS : CANAL_ERROR_GENERIC ); }
extern "C" int CanalSetMask( long handle, unsigned long mask ) #endif { CPeakObj *pdrvObj = theApp->getDriverObject( handle ); if ( NULL == pdrvObj ) return 0; return ( pdrvObj->setMask( mask ) ? CANAL_ERROR_SUCCESS : CANAL_ERROR_GENERIC ); }
extern "C" int CanalReceive( long handle, PCANALMSG pCanalMsg ) #endif { CPeakObj *pdrvObj = theApp->getDriverObject( handle ); if ( NULL == pdrvObj ) return 0; return ( pdrvObj->readMsg( pCanalMsg ) ? CANAL_ERROR_SUCCESS : CANAL_ERROR_GENERIC ); }
extern "C" int CanalDataAvailable( long handle ) #endif { CPeakObj *pdrvObj = theApp->getDriverObject( handle ); if ( NULL == pdrvObj ) return 0; return pdrvObj->dataAvailable(); }
extern "C" int CanalSend( long handle, PCANALMSG pCanalMsg ) #endif { CPeakObj *pdrvObj = theApp->getDriverObject( handle ); if ( NULL == pdrvObj ) return 0; return pdrvObj->writeMsg( pCanalMsg ) ? CANAL_ERROR_SUCCESS : 0 ; }
CDllDrvObj::~CDllDrvObj() { LOCK_MUTEX( m_objMutex ); for ( int i = 0; i<CANAL_PEAK_DRIVER_MAX_OPEN; i++ ) { if ( NULL != m_drvObjArray[ i ] ) { CPeakObj *pdrvObj = getDriverObject( i ); if ( NULL != pdrvObj ) { pdrvObj->close(); delete m_drvObjArray[ i ]; m_drvObjArray[ i ] = NULL; } } } UNLOCK_MUTEX( m_objMutex ); #ifdef WIN32 CloseHandle( m_objMutex ); #else pthread_mutex_destroy( &m_objMutex ); #endif }
extern "C" int CanalClose( long handle ) #endif { int rv = 0; CPeakObj *pdrvObj = theApp->getDriverObject( handle ); if ( NULL == pdrvObj ) return 0; pdrvObj->close(); theApp->removeDriverObject( handle ); rv = 1; return CANAL_ERROR_SUCCESS; }
extern "C" long CanalOpen( const char *pDevice, unsigned long flags ) #endif { long h = 0; CPeakObj *pdrvObj = new CPeakObj(); if ( NULL != pdrvObj ) { if ( pdrvObj->open( pDevice, flags ) ){ if ( !( h = theApp->addDriverObject( pdrvObj ) ) ) { delete pdrvObj; } } else { delete pdrvObj; } } return h; }
void *workThreadReceive( void *pObject ) #endif { #ifdef WIN32 DWORD errorCode = 0; #else int rv = 0; #endif CPeakObj * pobj = ( CPeakObj *)pObject; if ( NULL == pobj ) { #ifdef WIN32 ExitThread( errorCode ); // Fail #else pthread_exit( &rv ); #endif } PeakCanMsg peakMsg; while ( pobj->m_bRun ) { // Noting to do if we should end... if ( !pobj->m_bRun ) continue; LOCK_MUTEX( pobj->m_peakMutex ); while ( 0 == ( pobj->m_procRead( &peakMsg ) & PEAK_CAN_ERR_QRCVEMPTY ) ) { // Check if this is a status message if ( PCAN_MSGTYPE_STATUS & peakMsg.msgType ) { continue; // TODO } // Write to the receive buffer if ( pobj->m_receiveList.nCount < PEAKDRV_MAX_RCVMSG ) { PCANALMSG pMsg = new canalMsg; pMsg->flags = 0; if ( NULL != pMsg ) { dllnode *pNode = new dllnode; if ( NULL != pNode ) { pMsg->timestamp = GetTickCount() * 1000; pMsg->id = peakMsg.id; pMsg->sizeData = peakMsg.len; memcpy( pMsg->data, peakMsg.data, pMsg->sizeData ); // If extended set extended flag if ( PCAN_MSGTYPE_EXTENDED & peakMsg.msgType ) { pMsg->flags |= CANAL_IDFLAG_EXTENDED; } // Check for RTS package if ( PCAN_MSGTYPE_RTR & peakMsg.msgType ) { pMsg->flags |= CANAL_IDFLAG_RTR; } pNode->pObject = pMsg; LOCK_MUTEX( pobj->m_receiveMutex ); dll_addNode( &pobj->m_receiveList, pNode ); UNLOCK_MUTEX( pobj->m_receiveMutex ); // Update statistics pobj->m_stat.cntReceiveData += pMsg->sizeData; pobj->m_stat.cntReceiveFrames += 1; } else { delete pMsg; } } } else { // Full buffer pobj->m_stat.cntOverruns++; } } // while rcv msg UNLOCK_MUTEX( pobj->m_peakMutex ); SLEEP( 1 ); } // while #ifdef WIN32 ExitThread( errorCode ); #else pthread_exit( &rv ); #endif }
void *workThreadTransmit( void *pObject ) #endif { #ifdef WIN32 DWORD errorCode = 0; #else int rv = 0; #endif CPeakObj * pobj = ( CPeakObj *)pObject; if ( NULL == pobj ) { #ifdef WIN32 ExitThread( errorCode ); // Fail #else pthread_exit( &rv ); #endif } while ( pobj->m_bRun ) { LOCK_MUTEX( pobj->m_peakMutex ); // Noting to do if we should end... if ( !pobj->m_bRun ) continue; // Is there something to transmit... int ret; while ( ( NULL != pobj->m_transmitList.pHead ) && ( NULL != pobj->m_transmitList.pHead->pObject ) ) { canalMsg msg; memcpy( &msg, pobj->m_transmitList.pHead->pObject, sizeof( canalMsg ) ); LOCK_MUTEX( pobj->m_transmitMutex ); dll_removeNode( &pobj->m_transmitList, pobj->m_transmitList.pHead ); UNLOCK_MUTEX( pobj->m_transmitMutex ); PeakCanMsg peakMsg; peakMsg.id = msg.id; peakMsg.msgType = 0; peakMsg.len = msg.sizeData; memcpy( peakMsg.data, msg.data, peakMsg.len ); // Check if RTR if ( ( msg.flags & CANAL_IDFLAG_RTR ) ) { peakMsg.msgType |= PCAN_MSGTYPE_RTR; } // Check if extended if ( ( msg.flags & CANAL_IDFLAG_EXTENDED ) ) { peakMsg.msgType |= PCAN_MSGTYPE_EXTENDED; } if ( PEAK_CAN_ERR_OK == ( ret = pobj->m_procWrite( &peakMsg ) ) ) { // Message sent successfully // Update statistics pobj->m_stat.cntTransmitData += msg.sizeData; pobj->m_stat.cntTransmitFrames += 1; } else { // Failed - put message back in queue front PCANALMSG pMsg = new canalMsg; if ( NULL != pMsg ) { // Copy in data memcpy ( pMsg, &msg, sizeof( canalMsg ) ); dllnode *pNode = new dllnode; if ( NULL != pNode ) { pNode->pObject = pMsg; LOCK_MUTEX( pobj->m_transmitMutex ); dll_addNodeHead( &pobj->m_transmitList, pNode ); UNLOCK_MUTEX( pobj->m_transmitMutex ); } else { delete pMsg; } } // unable to allocate storage } // faild to send message } // while data // No data to write UNLOCK_MUTEX( pobj->m_peakMutex ); SLEEP( 1 ); //} } // while #ifdef WIN32 ExitThread( errorCode ); #else pthread_exit( &rv ); #endif }