// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved


// This software makes use of tools and libraries obtained from open source projects or released for

// use by relevant hardware manufactures. However, this software is NOT a part of any open source project.

// It is released only on a "need to know" basis for beta testers of the "RiceCNC Interpolation Engine".

// Recipents of this source code must respect the confidential nature of this software and prevent any

// distribution that could result in counterfeit copies of the "RiceCNC Interpolation Engine".


// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved

#include "Retransmit.h"

#include "SysTick.h"

#include <MySerial.h>



Retransmit::Retransmit() {

     retransmissions          = 0;

     payloadLength          = 0;

};



bool

     Retransmit::expired() {

     

     uint32_t elapsedTime = SysTick::microSecondTimer - microSeconds;

     return

          ( elapsedTime > 3000000 ); }; // 3.0 sec


void

     Retransmit::sendDebug() {


     char data[ 40 ];

     snprintf( data, 40,

          " tx>%d>%d>%d>%x>%d",

          packet.version,

          packet.sequence,

          packet.ack,

          packet.ackBitField,

          payloadLength );

     String msg = String( data );


     char* msgPtr = packet.payload;

     for ( int i = 0; i < packet.messageCount; i++ ) {

          msg          += String( ">" );

          msg          += String( msgPtr );

          while ( *msgPtr++ ); }; // advance to next string


     msg          += ">>";

     MySerial::sendMessageNonBlocking( &msg ); };


bool

     Retransmit::addMessage( String* msgPtr ) {

     

     size_t msgLength = msgPtr->length() + 1;

     if ( msgLength > capacity() )

          return false; // won't fit in this packet


     msgPtr->getBytes( // adds null termination

          (unsigned char*) &packet.payload[ payloadLength ],

          msgLength );


     packet.messageCount++;

     payloadLength     += msgLength;


     return true; };


size_t

     Retransmit::capacity() {

     

     return MaxPayloadSize - payloadLength; };