Index: sources/canbus/CanInterface.cpp =================================================================== diff -u -r2ef03b2ce51b4dc507f66e9671953a8e0824bde9 -r99cf0dc3002c0395f0d10d1d4fb34e2052449fd6 --- sources/canbus/CanInterface.cpp (.../CanInterface.cpp) (revision 2ef03b2ce51b4dc507f66e9671953a8e0824bde9) +++ sources/canbus/CanInterface.cpp (.../CanInterface.cpp) (revision 99cf0dc3002c0395f0d10d1d4fb34e2052449fd6) @@ -25,7 +25,11 @@ #include "Logger.h" #include "MessageGlobals.h" #include "FrameInterface.h" +#include "UiSwUpdate.h" +#include +#include + // namespace using namespace Can; @@ -232,6 +236,67 @@ } /*! + * \brief Turn CAN driver SW mode on/off. + * + * \param on Turn the mode on (true) or off (false). + */ +void CanInterface::setSWUpdateMode(bool on) +{ + _SWUpdateMode = on; +} + +/*! + * \brief Send from UI. + * + * \param msgId The CAN message Id. + * \param pData The payload data. + * \param length Length in bytes. + */ +bool CanInterface::sendSWUpdateMsg(uint16_t msgId, uint8_t* pData, uint32_t length) +{ + bool ok = false; + + if (_canDevice && _SWUpdateMode) + { + int32_t slength = (int32_t)length; // Use signed math. + ok = true; + std::size_t offset = 0; + while (ok && (slength > 0)) + { + QCanBusFrame frame; + + // Always 8 bytes, zero the remaining. + QByteArray qb((qsizetype)8, (char)0); + uint32_t limit = slength > 8 ? 8 : slength; + + // Fill in the data. + for (std::size_t ii = 0; ii < limit; ii++) { + qb[(uint)ii] = pData[offset + ii]; + } + frame.setFrameId(msgId); + frame.setPayload(qb); + offset += 8; + slength -= 8; + + ok = _canDevice->writeFrame(frame); + { + if (ok) { + _txFrameCount++; + } else { + _erFrameCount++; + } + } + + // Delay 500 us between msg sends. + // 8 bytes is roughly 400us. + using namespace std::chrono_literals; + std::this_thread::sleep_for(500us); + } + } + return ok; +} + +/*! * \brief CanInterface status * \details Sets the Can interface status description * \param vDescription - Description about the CANBus Interface errors @@ -264,7 +329,11 @@ // disabled coco begin validated: Manually tested since required to disable and enable the CANBus if( !_canDevice ) return false; //disabled coco end - return _canDevice->writeFrame(vFrame); + if (_SWUpdateMode) { + return true; + } else { + return _canDevice->writeFrame(vFrame); + } } /*! @@ -412,10 +481,23 @@ const QCanBusFrame frame = _canDevice->readFrame(); rxCount(); // disabled coco begin validated: This code is only for debugging purposes and had been tested manually. - if ( _enableConsoleOut ) + if ( _enableConsoleOut ) { consoleOut(frame, QString("Rx:%1").arg(_rxFrameCount)); - // disabled coco end - emit didFrameReceive(frame); + } + + if (_SWUpdateMode) { + + uint16_t msgId = frame.frameId(); + QByteArray qb = frame.payload(); + + uint8_t* pData = (uint8 *)qb.data(); + + SwUpdate::UiSwUpdate::instance().receive(msgId, pData); + } else { + + // disabled coco end + emit didFrameReceive(frame); + } } } @@ -430,8 +512,9 @@ bool ok = transmit(vFrame); txCount(); // disabled coco begin validated: This code is only for debugging purposes and had been tested manually. - if ( _enableConsoleOut ) + if ( _enableConsoleOut ) { consoleOut(vFrame, QString("Tx:%1").arg(_txFrameCount)); + } // disabled coco end emit didFrameTransmit(ok); }