/*! * \file CInterfaceManager.cpp * \author S. Eisenhauer * \date 25.10.2011 * \brief Implementation of CInterfaceManager */ #include "CInterfaceManager.h" #include "x2e/CAN.hpp" #include CInterfaceManager::CInterfaceManager() :m_pxDrv( x2e::System::OpenDriver() ) ,m_enTimer(x2e::System::Timer::TIMER_2) { x2e::status_t enRetCode; if( !m_pxDrv) { ERROR_PRINT("Error getting Driver! Exiting..."); exit(1); } enRetCode = x2e::System::Timer::Configure(m_pxDrv, m_enTimer, nTIMER_INTERVAL_US, true, true); if( enRetCode != x2e::OK ) { ERROR_PRINT("Error configuring timer %d: %d", m_enTimer, enRetCode); exit(1); } x2e::System::SetLEDState(m_pxDrv,x2e::System::LED_AUX,x2e::System::LED_ON); enEnableAllInterfaces(); } CInterfaceManager::~CInterfaceManager() { DEBUG_PRINT("entry"); DEBUG_PRINT("exit"); } x2e::status_t CInterfaceManager::enWriteCanMessage( int32_t i32InterfaceHandle, uint32_t u32MsgId, bool boIsExtended, bool boIsRtrFrame, const uint8_t* pu8Data, uint32_t u32DataLength) { if( !m_pxDrv ) { return x2e::ERR_NOT_INITIALIZED; } return x2e::CAN::WriteMessage( m_pxDrv, i32InterfaceHandle, u32MsgId, boIsExtended, boIsRtrFrame, pu8Data, u32DataLength); } x2e::status_t CInterfaceManager::enGetQueueFillLevel(x2e::Log::queueFillLevel& stFillLevel) { if( !m_pxDrv ) { return x2e::ERR_NOT_INITIALIZED; } return x2e::Log::RetrieveFillLevel(m_pxDrv,stFillLevel); } x2e::status_t CInterfaceManager::enPeekMessage( x2e::Log::queueFillLevel& stFillLevel, x2e::Log::messageDescription* pstMsgDescr, x2e::Log::framePayload* pstMsgPayload) { if( !m_pxDrv ) { return x2e::ERR_NOT_INITIALIZED; } return x2e::Log::PeekNextMessage(m_pxDrv,stFillLevel,pstMsgDescr,pstMsgPayload); } x2e::status_t CInterfaceManager::enEnableInterface(int32_t i32RequestedInterface) { if( !m_pxDrv ) { return x2e::ERR_NOT_INITIALIZED; } int32_t i32Handle = i32RequestedInterface; bool boSendAbility = true; x2e::CAN::transceiver enTransceiver= x2e::CAN::TRANSCEIVER_HIGHSPEED; x2e::Log::InterfaceId::ifid_t tLogIf; x2e::status_t enRes; uint32_t u32BaudRate = x2e::CAN::BAUDRATE_1000K; //uint32_t u32BaudRate = x2e::CAN::BAUDRATE_500K; m_tenState enInterfaceState = nenOn; uint32_t u32Slot = 0; uint32_t u32SlotPos = 0; uint32_t u32AttrSize = 0; enRes = x2e::CAN::GetInterface(m_pxDrv,&i32Handle,&enTransceiver,&boSendAbility, &tLogIf); if( x2e::OK != enRes) { ERROR_PRINT("Error getting interface %d: %d",i32Handle,enRes); exit(1); } if( i32Handle != i32RequestedInterface ) { ERROR_PRINT("Request interface %d, got %d. Exiting",i32RequestedInterface,i32Handle); exit(1); } DEBUG_PRINT("Got interface %d",i32Handle); if( !boSendAbility ) { ERROR_PRINT("Interface %d is not able to send",i32Handle); exit(1); } DEBUG_PRINT("Interface %d can send",i32Handle); enRes = x2e::CAN::SetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_BAUDRATE, &u32BaudRate, sizeof(u32BaudRate)); if( enRes != x2e::OK ) { ERROR_PRINT("Error setting baudrate for interface %d",i32Handle); exit(1); } // u32AttrSize = sizeof(uint32_t); // enRes = x2e::CAN::GetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_BAUDRATE,(void*) &u32BaudRate, &u32AttrSize); // if( enRes != x2e::OK ) // { // ERROR_PRINT("Error getting baudrate for interface %d: %d",i32Handle,enRes); // exit(1); // } // if( u32BaudRate == x2e::CAN::BAUDRATE_1000K ) // { // DEBUG_PRINT("set baudrate to 1MBit/s on interface %d",i32Handle); // } // else // { // DEBUG_PRINT("baudrate interface %d is %d",i32Handle, u32BaudRate); // } enRes = SetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_INTERFACE_STATUS, &enInterfaceState, sizeof(m_tenState)); if( enRes != x2e::OK ) { ERROR_PRINT("error enabling CAN interface %d: %d",i32Handle,enRes); exit(1); } DEBUG_PRINT("enabled interface %d",i32Handle); u32AttrSize = sizeof(uint32_t); enRes = x2e::CAN::GetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_SLOT,(void*) &u32Slot, &u32AttrSize); if( enRes != x2e::OK ) { ERROR_PRINT("Error getting Slot for interface %d: %d",i32Handle,enRes); exit(1); } u32AttrSize = sizeof(uint32_t); enRes = x2e::CAN::GetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_POSITION_ON_SLOT,(void*) &u32SlotPos, &u32AttrSize); if( enRes != x2e::OK ) { ERROR_PRINT("Error getting position on Slot for interface %d: %d",i32Handle,enRes); exit(1); } DEBUG_PRINT("Interface %d is on Slot %d pos %d",i32Handle,u32Slot,u32SlotPos); return x2e::OK; } x2e::status_t CInterfaceManager::enEnableAllInterfaces() { x2e::status_t enRes; if( !m_pxDrv ) { return x2e::ERR_NOT_INITIALIZED; } for( uint32_t u32ActInterface = static_cast(x2e::CAN::INTERFACE_1); u32ActInterface < static_cast(x2e::CAN::INTERFACE_7); u32ActInterface++) { enRes = this->enEnableInterface(static_cast(u32ActInterface)); if( enRes != x2e::OK ) { ERROR_PRINT("Error enabling interface %d: %d" , static_cast(u32ActInterface),enRes); return enRes; } } return enRes; } x2e::status_t CInterfaceManager::enGetTimestamp( uint32_t* pu32TsHigh, uint32_t* pu32TsLow ) { if( !m_pxDrv ) { return x2e::ERR_NOT_INITIALIZED; } return x2e::System::GetCentralTimeStamp(m_pxDrv,pu32TsHigh,pu32TsLow); } x2e::status_t CInterfaceManager::enWait() { if( !m_pxDrv ) { return x2e::ERR_NOT_INITIALIZED; } return x2e::System::Timer::Wait(m_pxDrv, m_enTimer); } void CInterfaceManager::vDeinit() { if( !m_pxDrv ) { return; } x2e::status_t enRes; DEBUG_PRINT("entry"); for( int32_t i32ActInterface = x2e::CAN::INTERFACE_1; i32ActInterface < x2e::CAN::INTERFACE_7; i32ActInterface++) { enRes = enDisableInterface( i32ActInterface ); } x2e::System::SetLEDState(m_pxDrv,x2e::System::LED_AUX,x2e::System::LED_OFF); x2e::System::CloseDriver(m_pxDrv); m_pxDrv = NULL; DEBUG_PRINT("exit"); } x2e::status_t CInterfaceManager::enDisableInterface(int32_t i32Interface) { x2e::status_t enRes = x2e::OK; m_tenState enInterfaceState = nenOff; DEBUG_PRINT("disabling interface %d",i32Interface); enRes = SetAttribute( m_pxDrv, i32Interface, x2e::CAN::ATTR_INTERFACE_STATUS, &enInterfaceState, sizeof(m_tenState) ); if( enRes != x2e::OK ) { ERROR_PRINT("Error disabling interface %d: %d" , i32Interface,enRes); } enRes = SetAttribute( m_pxDrv, i32Interface, x2e::CAN::ATTR_TRANSCEIVER_STATUS, &enInterfaceState, sizeof(m_tenState) ); if( enRes != x2e::OK ) { ERROR_PRINT("Error switching tranceiver %d off: %d" , i32Interface,enRes); } enRes = SetAttribute( m_pxDrv, i32Interface, x2e::CAN::ATTR_LED_STATUS, &enInterfaceState, sizeof(m_tenState) ); if( enRes != x2e::OK ) { ERROR_PRINT("Error switching led %d off: %d" , i32Interface,enRes); } return enRes; }