中级会员
- 积分
- 213
- 金钱
- 213
- 注册时间
- 2018-2-1
- 在线时间
- 40 小时
|
楼主 |
发表于 2018-3-6 21:54:05
|
显示全部楼层
[mw_shl_code=cpp,true]/**
* @brief Receive transmitting stage.
* @param None.
* @return None.
*/
void Ymodem::receiveStageTransmitting()
{
switch(receivePacket())
{
case CodeSoh:
{
uint16_t crc = ((uint16_t)(rxBuffer[YMODEM_PACKET_SIZE + YMODEM_PACKET_OVERHEAD - 2]) << 8) |
((uint16_t)(rxBuffer[YMODEM_PACKET_SIZE + YMODEM_PACKET_OVERHEAD - 1]) << 0);
if((rxBuffer[1] == (uint8_t)(dataCount)) && (rxBuffer[2] == (uint8_t)(0xFF - dataCount)) &&
(crc == crc16(&(rxBuffer[YMODEM_PACKET_HEADER]), YMODEM_PACKET_SIZE)))
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
txBuffer[0] = CodeAck;
txLength = 1;
write(txBuffer, txLength);
}
}
else if((rxBuffer[1] == (uint8_t)(dataCount + 1)) && (rxBuffer[2] == (uint8_t)(0xFE - dataCount)) &&
(crc == crc16(&(rxBuffer[YMODEM_PACKET_HEADER]), YMODEM_PACKET_SIZE)))
{
uint32_t dataLength = YMODEM_PACKET_SIZE;
if(callback(StatusTransmit, &(rxBuffer[YMODEM_PACKET_HEADER]), &dataLength) == CodeAck)
{
timeCount = 0;
errorCount = 0;
dataCount = dataCount + 1;
code = CodeNone;
stage = StageTransmitting;
txBuffer[0] = CodeAck;
txLength = 1;
write(txBuffer, txLength);
}
else
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
}
}
else
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
txBuffer[0] = CodeNak;
txLength = 1;
write(txBuffer, txLength);
}
}
break;
}
case CodeStx:
{
uint16_t crc = ((uint16_t)(rxBuffer[YMODEM_PACKET_1K_SIZE + YMODEM_PACKET_OVERHEAD - 2]) << 8) |
((uint16_t)(rxBuffer[YMODEM_PACKET_1K_SIZE + YMODEM_PACKET_OVERHEAD - 1]) << 0);
if((rxBuffer[1] == (uint8_t)(dataCount)) && (rxBuffer[2] == (uint8_t)(0xFF - dataCount)) &&
(crc == crc16(&(rxBuffer[YMODEM_PACKET_HEADER]), YMODEM_PACKET_1K_SIZE)))
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
txBuffer[0] = CodeAck;
txLength = 1;
write(txBuffer, txLength);
}
}
else if((rxBuffer[1] == (uint8_t)(dataCount + 1)) && (rxBuffer[2] == (uint8_t)(0xFE - dataCount)) &&
(crc == crc16(&(rxBuffer[YMODEM_PACKET_HEADER]), YMODEM_PACKET_1K_SIZE)))
{
uint32_t dataLength = YMODEM_PACKET_1K_SIZE;
if(callback(StatusTransmit, &(rxBuffer[YMODEM_PACKET_HEADER]), &dataLength) == CodeAck)
{
timeCount = 0;
errorCount = 0;
dataCount = dataCount + 1;
code = CodeNone;
stage = StageTransmitting;
txBuffer[0] = CodeAck;
txLength = 1;
write(txBuffer, txLength);
}
else
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
}
}
else
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
txBuffer[0] = CodeNak;
txLength = 1;
write(txBuffer, txLength);
}
}
break;
}
case CodeEot:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageFinishing;
txBuffer[0] = CodeNak;
txLength = 1;
write(txBuffer, txLength);
break;
}
case CodeA1:
case CodeA2:
case CodeCan:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
callback(StatusAbort, NULL, NULL);
break;
}
default:
{
timeCount++;
if((timeCount / (timeDivide + 1)) > timeMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else if((timeCount % (timeDivide + 1)) == 0)
{
txBuffer[0] = CodeNak;
txLength = 1;
write(txBuffer, txLength);
}
}
}
}
/**
* @brief Receive finishing stage.
* @param None.
* @return None.
*/
void Ymodem::receiveStageFinishing()
{
switch(receivePacket())
{
case CodeEot:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageFinished;
txBuffer[0] = CodeAck;
txBuffer[1] = CodeC;
txLength = 2;
write(txBuffer, txLength);
break;
}
case CodeA1:
case CodeA2:
case CodeCan:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
callback(StatusAbort, NULL, NULL);
break;
}
default:
{
timeCount++;
if((timeCount / (timeDivide + 1)) > timeMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else if((timeCount % (timeDivide + 1)) == 0)
{
txBuffer[0] = CodeNak;
txLength = 1;
write(txBuffer, txLength);
}
}
}
}
/**
* @brief Receive finished stage.
* @param None.
* @return None.
*/
void Ymodem::receiveStageFinished()
{
switch(receivePacket())
{
case CodeSoh:
{
uint16_t crc = ((uint16_t)(rxBuffer[YMODEM_PACKET_SIZE + YMODEM_PACKET_OVERHEAD - 2]) << 8) |
((uint16_t)(rxBuffer[YMODEM_PACKET_SIZE + YMODEM_PACKET_OVERHEAD - 1]) << 0);
if((rxBuffer[1] == 0x00) && (rxBuffer[2] == 0xFF) &&
(crc == crc16(&(rxBuffer[YMODEM_PACKET_HEADER]), YMODEM_PACKET_SIZE)))
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
txBuffer[0] = CodeAck;
txLength = 1;
write(txBuffer, txLength);
callback(StatusFinish, NULL, NULL);
}
else
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
txBuffer[0] = CodeNak;
txLength = 1;
write(txBuffer, txLength);
}
}
break;
}
case CodeEot:
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
txBuffer[0] = CodeAck;
txBuffer[1] = CodeC;
txLength = 2;
write(txBuffer, txLength);
}
break;
}
case CodeA1:
case CodeA2:
case CodeCan:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
callback(StatusAbort, NULL, NULL);
break;
}
default:
{
timeCount++;
if((timeCount / (timeDivide + 1)) > timeMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else if((timeCount % (timeDivide + 1)) == 0)
{
txBuffer[0] = CodeNak;
txLength = 1;
write(txBuffer, txLength);
}
}
}
}
/**
* @brief Transmit none stage.
* @param None.
* @return None.
*/
void Ymodem::transmitStageNone()
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageEstablishing;
}
/**
* @brief Transmit establishing stage.
* @param None.
* @return None.
*/
void Ymodem::transmitStageEstablishing()
{
switch(receivePacket())
{
case CodeC:
{
memset(&(txBuffer[YMODEM_PACKET_HEADER]), NULL, YMODEM_PACKET_SIZE);
if(callback(StatusEstablish, &(txBuffer[YMODEM_PACKET_HEADER]), &(txLength)) == CodeAck)
{
uint16_t crc = crc16(&(txBuffer[YMODEM_PACKET_HEADER]), txLength);
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageEstablished;
txBuffer[0] = CodeSoh;
txBuffer[1] = 0x00;
txBuffer[2] = 0xFF;
txBuffer[txLength + YMODEM_PACKET_OVERHEAD - 2] = (uint8_t)(crc >> 8);
txBuffer[txLength + YMODEM_PACKET_OVERHEAD - 1] = (uint8_t)(crc >> 0);
txLength = txLength + YMODEM_PACKET_OVERHEAD;
write(txBuffer, txLength);
}
else
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
}
break;
}
case CodeA1:
case CodeA2:
case CodeCan:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
callback(StatusAbort, NULL, NULL);
break;
}
default:
{
timeCount++;
if((timeCount / (timeDivide + 1)) > timeMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusTimeout, NULL, NULL);
}
}
}
}
/**
* @brief Transmit established stage.
* @param None.
* @return None.
*/
void Ymodem::transmitStageEstablished()
{
switch(receivePacket())
{
case CodeNak:
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
write(txBuffer, txLength);
}
break;
}
case CodeC:
{
errorCount++;
if(errorCount > errorMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else
{
timeCount = 0;
errorCount = 0;
dataCount = dataCount;
code = CodeNone;
stage = (Stage)(stage + dataCount);
write(txBuffer, txLength);
}
break;
}
case CodeAck:
{
memset(&(txBuffer[YMODEM_PACKET_HEADER]), NULL, YMODEM_PACKET_1K_SIZE);
switch(callback(StatusTransmit, &(txBuffer[YMODEM_PACKET_HEADER]), &(txLength)))
{
case CodeAck:
{
uint16_t crc = crc16(&(txBuffer[YMODEM_PACKET_HEADER]), txLength);
timeCount = 0;
errorCount = 0;
dataCount = 1;
code = CodeNone;
stage = StageEstablished;
txBuffer[0] = txLength > YMODEM_PACKET_SIZE ? CodeStx : CodeSoh;
txBuffer[1] = 0x01;
txBuffer[2] = 0xFE;
txBuffer[txLength + YMODEM_PACKET_OVERHEAD - 2] = (uint8_t)(crc >> 8);
txBuffer[txLength + YMODEM_PACKET_OVERHEAD - 1] = (uint8_t)(crc >> 0);
txLength = txLength + YMODEM_PACKET_OVERHEAD;
break;
}
case CodeEot:
{
timeCount = 0;
errorCount = 0;
dataCount = 2;
code = CodeNone;
stage = StageEstablished;
txBuffer[0] = CodeEot;
txLength = 1;
write(txBuffer, txLength);
break;
}
default:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
}
}
break;
}
case CodeA1:
case CodeA2:
case CodeCan:
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
callback(StatusAbort, NULL, NULL);
break;
}
default:
{
timeCount++;
if((timeCount / (timeDivide + 1)) > timeMax)
{
timeCount = 0;
errorCount = 0;
dataCount = 0;
code = CodeNone;
stage = StageNone;
for(txLength = 0; txLength < YMODEM_CODE_CAN_NUMBER; txLength++)
{
txBuffer[txLength] = CodeCan;
}
write(txBuffer, txLength);
callback(StatusError, NULL, NULL);
}
else if((timeCount % (timeDivide + 1)) == 0)
{
write(txBuffer, txLength);
}
}
}
}[/mw_shl_code]
|
|