Index: sources/update/MsgLink.h =================================================================== diff -u -r98d98e443f7f3605abfacf746e8a34ce3c0fc32b -r6338d2d734d71903fd4afc8371c58dc3facd432a --- sources/update/MsgLink.h (.../MsgLink.h) (revision 98d98e443f7f3605abfacf746e8a34ce3c0fc32b) +++ sources/update/MsgLink.h (.../MsgLink.h) (revision 6338d2d734d71903fd4afc8371c58dc3facd432a) @@ -35,9 +35,9 @@ #endif namespace SwUpdate { - + /*! - * Heuristic tuned value, typically it is 2x or more the wire + * Heuristic tuned value, typically it is 2x or more the wire * rate for most CAN systems. * * /note This is a tunable heuristic. @@ -47,16 +47,16 @@ /*! * The total amount of payload bytes to round trip data + response. */ -const uint32 TotalRoundTripDataSizeBytes = +const uint32 TotalRoundTripDataSizeBytes = (sizeof(SwUpdateDataBuffer) + sizeof(SwUpdateResponse)); /*! - * For each 8 bytes of payload, 114 wire bits are + * For each 8 bytes of payload, 114 wire bits are * used (header + payload + tail). */ const float BytesToCanBits = 114.0f / 8.0f; -/*! +/*! * Pause after a NACK before sending reset and retry. */ const std::chrono::duration g_ms_pause(20); @@ -68,25 +68,25 @@ */ const uint32 gAckSleepScale = 200; -/*! - * This is 2x the line speed transfer of a - * message relative to kbps of the line in milliseconds. +/*! + * This is 2x the line speed transfer of a + * message relative to kbps of the line in milliseconds. * * Example: * (sizeof(SwUpdateDataBuffer) + sizeof(SwUpdateResponse))/8) = 34. * 34 packets * 114 bits per packet = 3,876 bits. * / 250kpbs = 15.504 ms if full bandwidth for 250kbps. - * * 2 for collisions/normal stuff = + * * 2 for collisions/normal stuff = * Expected round trip within 31 ms else we should retry. * */ -const float RetryKbpsScaleFactor = +const float RetryKbpsScaleFactor = TotalRoundTripDataSizeBytes * BytesToCanBits * BandwidthFactor; /*! * \brief Send a command and wait for response. * - * Typical use is wait on bool sendOk(...), and if it failed then + * Typical use is wait on bool sendOk(...), and if it failed then * the effort timed out for retries. * * The numberRetries() call checks for too much cumulative failures @@ -150,48 +150,48 @@ _dataBuf({0,0,0,0, {0}}) { } - + /*! * \brief Virtual destructor. */ virtual ~MsgLink() { _expecting = false; } - - /*! + + /*! * \brief Get the number of message retries. */ uint32 numberRetries() { return _retries; } - - /*! + + /*! * \brief Reset the number of retries. */ void resetRetries() { _retries = 0; } - - /*! + + /*! * \brief Abort waiting. */ void abort() { _expecting = false; _ew.fireEvent(); } - /*! + /*! * \brief Send a message, wait as needed for retry/response. - * + * * \param msgId The message * \param msg The message bytes, already with security. * \param maxEffort Maximum number of 1ms retries. - * + * * \return True on success, False on retry failed or timeout. */ bool sendOk( uint16 msgId, - uint8 * msg, + uint8 * msg, uint16 maxEffort) { // We are expecting data. @@ -203,33 +203,33 @@ (msgId == SwUpdateCanMsgIds::DataBufferId_DG_FW) || (msgId == SwUpdateCanMsgIds::DataBufferId_DG_FPGA); - // Either command or data. if data use ResponseId, + // Either command or data. if data use ResponseId, // if a command and verify or version use VerifyId else ResponseId. - _expectedId = - _wasData ? + _expectedId = + _wasData ? SwUpdateCanMsgIds::ResponseId : - SwUpdate_getCmd(msg[1]) == SwUpdateCmdEnum::Verify ? - SwUpdateCanMsgIds::VerifyId : - SwUpdate_getCmd(msg[1]) == SwUpdateCmdEnum::Version ? - SwUpdateCanMsgIds::VerifyId : ResponseId; - + SwUpdate_getCmd(msg[1]) == SwUpdateCmdEnum::Verify ? + SwUpdateCanMsgIds::VerifyId : + SwUpdate_getCmd(msg[1]) == SwUpdateCmdEnum::Version ? + SwUpdateCanMsgIds::VerifyId : ResponseId; + _msgIdSlot = msg[0]; // For ack/nack matching. - + // Sending a command (8 bytes) or data (256). - uint32 length = _wasData ? - sizeof(SwUpdateDataBuffer) : sizeof(SwUpdateCommand); - + uint32 length = _wasData ? + sizeof(SwUpdateDataBuffer) : sizeof(SwUpdateCommand); + if (_wasData) { memcpy(&_dataBuf, msg, sizeof(SwUpdateDataBuffer)); } - + // _timeoutMode: // == 0 - Normal use. // 1 - Wait till time. // 2 - Was waiting for timeout but got an ACK. // 3 - Was waiting for timeout but got a NACK. _timeoutMode = TimeoutEnum::None; - for (uint32 effortSpent; + for (uint32 effortSpent = 0; // When None, keep going till maxEffort, when not none, it's event based. (effortSpent < maxEffort) || (TimeoutEnum::None != _timeoutMode); effortSpent++) { @@ -249,9 +249,9 @@ // Call indirectly to make this more testable. this->sendPacket(msgId, msg, length); } - + // Wait to either retry or success. - if (_ew.wait()) { + if (_ew.wait()) { if (TimeoutEnum::Waiting == _timeoutMode) { continue; @@ -276,7 +276,7 @@ } continue; } - + // If we're still expecting then we do a retry. // Before retry send a command that should clear us. if (_expecting && _wasData) { @@ -285,7 +285,7 @@ SwUpdateTargetEnum target = (SwUpdateTargetEnum)(((SwUpdateDataBuffer *)msg)->kind); resync.cmd = SwUpdate_FormCommand(target, SwUpdateCmdEnum::Resync); resync.rand = 0; - SwUpdate_createSecurity((uint8 *)&resync, sizeof(SwUpdateCommand)); + SwUpdate_createSecurity((uint8 *)&resync, sizeof(SwUpdateCommand)); std::this_thread::sleep_for(g_ms_pause); this->sendPacket(SwUpdateCanMsgIds::CommandId, (uint8*)&resync, 8); @@ -300,23 +300,23 @@ /*! * \brief Send a packet. - * + * * \param mstId Messge Id. * \param pData Message data. * \param length Message length. */ void sendPacket(uint16 msgId, uint8* pData, uint32 length) { _CanInterface.sendSWUpdateMsg(msgId, pData, length); } - + /*! * \brief Receive and check if matched. * * \param msgId Can message ID. - * \param pData Data pointer. + * \param pData Data pointer. * * \return True if we received the expected message. - */ + */ bool checkReceived(uint16 msgId, uint8 * pData) { bool rv = false; @@ -332,8 +332,8 @@ // F * * | T Its a verify/version // T F * | F Not for this thread // T T F | F For the thread not an ack. - // T T T | T For this thread and an ack. - if (msgId == SwUpdateCanMsgIds::ResponseId) { + // T T T | T For this thread and an ack. + if (msgId == SwUpdateCanMsgIds::ResponseId) { SwUpdateResponse *resp = (SwUpdateResponse *)pData; if (resp->id == _msgIdSlot) { @@ -361,12 +361,12 @@ rv = true; _timeoutMode = resp->ackNack == 0 ? TimeoutEnum::Failure : - resp->ackNack == 1 ? TimeoutEnum::Success : TimeoutEnum::Waiting; + resp->ackNack == 1 ? TimeoutEnum::Success : TimeoutEnum::Waiting; } } } else { rv = true; - } + } } } @@ -379,7 +379,7 @@ } -protected: +protected: EventWait _ew; ///< Wait for time or event. bool _expecting; ///< Expecting data. uint32 _retries; ///< Total number of retries.