Reworked driver code.

This commit is contained in:
2026-01-01 00:40:27 +01:00
parent 54e89e412a
commit 3ed3511f50
9 changed files with 201 additions and 212 deletions

View File

@@ -32,10 +32,12 @@
#include <QDebug>
#include <bcdrivertinycan.h>
#include <can_drv.h>
#include <expected>
BCDriverTinyCan::BCDriverTinyCan( QObject* parent )
: BCDriver(parent )
@@ -44,42 +46,72 @@ BCDriverTinyCan::BCDriverTinyCan( QObject* parent )
}
BCDriver::DriverState BCDriverTinyCan::loadDriver()
void BCDriverTinyCan::loadDriver()
{
//qDebug() << "CanBusControl " << cbc::Version << '\n' << "based on BigXionFlasher (c) 2011-2013 by Thomas Koenig <info@bigxionflasher.org> - www.bigxionflasher.org";
using Result=std::expected<DriverState,QString>;
struct ::TDeviceStatus canDevicestatus;
if( ::LoadDriver( NULL ) < 0 )
throw BCException( "Driver Error: 'LoadDriver'" );
setState(BCDriver::DriverState::Loaded);
if( ::CanInitDriver( NULL ) < 0 )
throw BCException( "Driver Error: 'InitDriver'" );
if( ::CanDeviceOpen( 0, NULL ) < 0 )
throw std::runtime_error( "Driver Error: 'DeviceOpen'" );
::CanSetSpeed( 0, CAN_125K_BIT );
::CanSetMode( 0, OP_CAN_START, CAN_CMD_ALL_CLEAR );
::CanGetDeviceStatus( 0, &canDevicestatus );
setState(BCDriver::DriverState::Initialized);
if( canDevicestatus.DrvStatus < DRV_STATUS_CAN_OPEN )
throw BCException( "Driver Error: could not open device." );
if( canDevicestatus.CanStatus == CAN_STATUS_BUS_OFF )
auto callLoadDriver = []() -> Result
{
::CanSetMode( 0, OP_CAN_RESET, CAN_CMD_NONE );
throw BCException( "Driver Error: CAN Status 'BusOff'" );
}
if( ::LoadDriver( NULL ) < 0 )
return std::unexpected(QString("Driver Error: 'LoadDriver'"));
return DriverState::Loaded;
};
setState(BCDriver::DriverState::Ready);
auto callInitDriver = [](DriverState state) -> Result
{
Q_UNUSED(state)
if( ::CanInitDriver( NULL ) < 0 )
return std::unexpected(QString("Driver Error: 'InitDriver'"));
return DriverState::Initialized;
};
return BCDriver::DriverState::Ready;
auto callOpenDevice = [](DriverState state) -> Result
{
Q_UNUSED(state)
if( ::CanDeviceOpen( 0, NULL ) < 0 )
return std::unexpected(QString("Driver Error: 'DeviceOpen'"));
return DriverState::Opended;
};
auto callSetDeviceMode = [](DriverState state) -> Result
{
Q_UNUSED(state)
::TDeviceStatus deviceStatus;
::CanSetSpeed( 0, CAN_125K_BIT );
::CanSetMode( 0, OP_CAN_START, CAN_CMD_ALL_CLEAR );
::CanGetDeviceStatus( 0, &deviceStatus );
if( deviceStatus.DrvStatus < DRV_STATUS_CAN_OPEN )
std::unexpected(QString("Driver Error: could not open device." ));
if( deviceStatus.CanStatus == CAN_STATUS_BUS_OFF )
{
::CanSetMode( 0, OP_CAN_RESET, CAN_CMD_NONE );
std::unexpected(QString("Driver Error: CAN Status 'BusOff'" ));
}
return BCDriver::DriverState::Ready;
};
auto newDriverState = callLoadDriver()
.and_then( callInitDriver )
.and_then( callOpenDevice )
.and_then( callSetDeviceMode );
_driverState = DriverState::Error;
QString message("Driver Ready.");
if(newDriverState)
_driverState = *newDriverState;
else
message = newDriverState.error();
emit driverStateChanged( _driverState, message );
}
void BCDriverTinyCan::initDriver()
{
}
@@ -87,21 +119,21 @@ BCDriver::DriverState BCDriverTinyCan::loadDriver()
/*
try
{
loadDriver();
loadAndInitDriver();
initBCDevice::ID::Console();
}
catch( std::exception& except )
{
::CanDownDriver();
::UnloadDriver();
::UnloadAndInitDriver();
// re-throw
throw BCException( except.what() );
}
*/
/*
BCDriver::DriverState BCDriverTinyCan::initDriver()
{
@@ -138,7 +170,7 @@ BCDriver::DriverState BCDriverTinyCan::initDriver()
}
*/
uint32_t BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t registerID ) const
{