void TestWritemem() { DVD_SetDebugMode(); DVD_WaitImmediate(); u32 dwCheck = CalcChecksum(g_pXenoROM, FLASHSIZE); DbgPrint("\nChecksum original: %x", dwCheck); DbgPrint("\nReading memory..."); DVD_ReadDriveMemBlock(0xFF40D800, pBuffer, FLASHSIZE); // dcache_inv(pBuffer, FLASHSIZE); dwCheck = CalcChecksum(pBuffer, FLASHSIZE); DbgPrint("\nChecksum memory before: %x", dwCheck); DbgPrint("\nWriting memory..."); DVD_WriteDriveMemBlock(0xFF40D800, g_pXenoROM, FLASHSIZE); // GC_Sleep(8000); DVD_WaitImmediate(); DbgPrint("done"); DbgPrint("\nReading back..."); DVD_ReadDriveMemBlock(0xFF40D800, pBuffer, FLASHSIZE); // dcache_inv(pBuffer, FLASHSIZE); dwCheck = CalcChecksum(pBuffer, FLASHSIZE); DbgPrint("\nChecksum read back: %x", dwCheck); }
// Send packet to specified link (0 for broadcast). PUBLIC bool COM_SendPacket(int nlink, COM_PPacket p) { if (COM_Links == NULL || p == NULL || nlink < 0 || nlink > COM_MaxLinks) return FALSE; if (nlink > 0 && COM_Links[nlink-1].type == COMT_NONE) return FALSE; if (COM_Type == COMT_IPX) { IPX_PPacket q; if (nlink == 0) { q = IPX_PACKET(IPXPackets, NumIPXPackets-1); while (IPX_PACKET(q,0)->ecb.inuse); IPX_InitOutPacket(q, NULL, NULL, IPXAddress.socket); } else { q = COM_Links[nlink-1].ipx; while (IPX_PACKET(q,0)->ecb.inuse); IPX_InitOutPacket(q, NULL, &COM_Links[nlink-1].ipxadr, IPXAddress.socket); // p->time = COM_Links[nlink-1].nsend++; } memcpy(q->buf, p, p->len); IPX_SendPacket(&IPX_PACKET(q,0)->ecb); } else if (COM_Type == COMT_SERIAL) { p->checksum = CalcChecksum(p); SER_WriteBlock(&SerChannel, (void*)&p->len, p->len-sizeof(p->time)); // SER_WriteBlock(&SerChannel, (void*)&p->time, p->len); // if (p->time &1) SER_StreamTransmit(&SerChannel); } return TRUE; }
TEST(ErrorCheck, CalcChecksum_Data_ProcessMultiBytesSize) { uint8_t buffer[2]; buffer[0] = 1; buffer[1] = 2; TEST_ASSERT_EQUAL(3, CalcChecksum(buffer, 2)); }
uint8_t CompareChecksum(const uint8_t * buffer, uint32_t size, uint8_t checksum) { uint8_t calc = CalcChecksum(buffer, size); if ( calc == 0 ) { return 0; } else if ( calc == checksum ) { return 1; } else { return 0; } }
// Check for incoming packets, and if there is any, copy it and // return number of link it came from (0 for a broadcasted packet). PUBLIC int COM_GetPacket(COM_PPacket p) { int i, j; if (COM_Links == NULL || p == NULL) return -1; if (COM_Type == COMT_IPX) { while (TRUE) { IPX_PPacket q = NULL; dword mintime = 0xFFFFFFFFUL; // Search for the most ancient packet. for (i = 0; i < NUMINPACKETS; i++) if (!IPX_PACKET(IPXPackets,i)->ecb.inuse) { if (((COM_PPacket)IPX_PACKET(IPXPackets,i)->buf)->time < mintime) { mintime = ((COM_PPacket)(IPX_PACKET(IPXPackets,i)->buf))->time; q = IPX_PACKET(IPXPackets,i); } } // Was there any inbound packet? if (q == NULL) return -1; // Does it come from ourselves? if (memcmp(IPXAddress.node, q->h.source.node, sizeof(IPXAddress.node)) == 0) { // Discard it and search again. IPX_ListenForPacket(&q->ecb); continue; } // Accept it memcpy(p, q->buf, sizeof(*p)); // Search source address in active links. for (j = 0; j < COM_MaxLinks; j++) { if (COM_Links[j].type == COM_Type && memcmp(COM_Links[j].addr.addr, q->h.source.node, sizeof(COM_Links[j].addr.addr)) == 0) break; } IPX_ListenForPacket(&q->ecb); if (j >= COM_MaxLinks) return 0; // Unkown source. else return j + 1; } } else if (COM_Type == COMT_SERIAL) { if (SER_ReadBlock(&SerChannel, (void*)&p->len, sizeof(*p)-sizeof(p->time)) <= 0) // if (SER_ReadBlock(&SerChannel, (void*)&p->len, p->len-sizeof(p->time)) <= 0) // if (SER_ReadBlock(&SerChannel, (void*)&p->time, p->len) <= 0) return -1; if (CalcChecksum(p) != p->checksum) return -2; // p->time = COM_Links[0].nrec++; return COM_NLinks; } return -1; }
void SetupHeader( union u_header * pCMD, int nTypeID, int nCmdID, OTPayload & thePayload) { pCMD->fields.type_id = nTypeID; pCMD->fields.command_id = nCmdID; // pCMD->fields.size = thePayload.GetSize(); pCMD->fields.size = htonl(thePayload.GetSize()); // think this is causing problems pCMD->fields.checksum = CalcChecksum(pCMD->buf, OT_CMD_HEADER_SIZE-1); BYTE byChecksum = (BYTE)pCMD->fields.checksum; int nChecksum = byChecksum; uint32_t nTemp = thePayload.GetSize(); fprintf(stderr, "(Payload size %d, TYPE %d command, checksum: %d...)\n", nTemp, nTypeID, nChecksum); }
void SetupHeader( union u_header * pCMD, int nTypeID, int nCmdID, OTPayload & thePayload) { OT_ASSERT(NULL != pCMD); pCMD->fields.type_id = (nTypeID > 0) ? static_cast<BYTE>(nTypeID) : '\0'; pCMD->fields.command_id = (nCmdID > 0) ? static_cast<BYTE>(nCmdID) : '\0'; // pCMD->fields.size = thePayload.GetSize(); pCMD->fields.size = htonl(thePayload.GetSize()); // think this is causing problems pCMD->fields.checksum = CalcChecksum(pCMD->buf, OT_CMD_HEADER_SIZE-1); BYTE byChecksum = (BYTE)pCMD->fields.checksum; int nChecksum = byChecksum; uint32_t nTemp = thePayload.GetSize(); OTLog::vOutput(4, "(Payload size %d, TYPE %d command, checksum: %d...)\n", nTemp, nTypeID, nChecksum); }
uint8_t NazaGPS::CheckData() { #ifdef READ_NAZA if(Serial1.available()) { /* * Resets the current status pin states. */ digitalWrite(CHKSUM_ERROR_PIN, LOW); digitalWrite(PACKET_OK_PIN, LOW); if(buffpos < 2) { // If we dont have the header bytes, we will just read. buffer[buffpos] = Serial1.read(); buffpos ++; }else{ if(buffer[0] == 0x55 && buffer[1] == 0xAA) { // Checks if we have the correct heading if(buffpos < 4) { // We need 4 bytes. Head + ID + Size buffer[buffpos] = Serial1.read(); buffpos ++; }else{ payloadsize = buffer[3]; // Size is the 4th byte buffer[buffpos] = Serial1.read(); buffpos ++; if(buffpos == payloadsize+6) { // Ok, so we have all data CalcChecksum(); // Calculate the checksum if(CompareChecksum(&buffer[buffpos-2])) { // Checksum OK DecodeMessage(&buffer[4], buffer[2], buffer[3]);// Decode the message and save the data digitalWrite(PACKET_OK_PIN, HIGH); buffpos = 0; return 1; }else{ // Invalid Checksum. Discard all data. buffpos = 0; digitalWrite(CHKSUM_ERROR_PIN, HIGH); } } } }else{ // Wrong head, lets clean and restart buffpos = 0; buffer[buffpos] = Serial1.read(); buffpos++; } } } #endif return 0; }
/** * @brief Transmit a file using the ymodem protocol * @param p_buf: Address of the first byte * @param p_file_name: Name of the file sent * @param file_size: Size of the transmission * @retval COM_StatusTypeDef result of the communication */ COM_StatusTypeDef Ymodem_Transmit (uint8_t *p_buf, const uint8_t *p_file_name, uint32_t file_size) { uint32_t errors = 0, ack_recpt = 0, size = 0, pkt_size; uint8_t *p_buf_int; COM_StatusTypeDef result = COM_OK; uint32_t blk_number = 1; uint8_t a_rx_ctrl[2]; uint8_t i; #ifdef CRC16_F uint32_t temp_crc; #else /* CRC16_F */ uint8_t temp_chksum; #endif /* CRC16_F */ /* Prepare first block - header */ PrepareIntialPacket(aPacketData, p_file_name, file_size); while (( !ack_recpt ) && ( result == COM_OK )) { /* Send Packet */ HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT); /* Send CRC or Check Sum based on CRC16_F */ #ifdef CRC16_F temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE); Serial_PutByte(temp_crc >> 8); Serial_PutByte(temp_crc & 0xFF); #else /* CRC16_F */ temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE); Serial_PutByte(temp_chksum); #endif /* CRC16_F */ /* Wait for Ack and 'C' */ if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) { if (a_rx_ctrl[0] == ACK) { ack_recpt = 1; } else if (a_rx_ctrl[0] == CA) { if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA)) { HAL_Delay( 2 ); __HAL_UART_FLUSH_DRREGISTER(&UartHandle); result = COM_ABORT; } } } else { errors++; } if (errors >= MAX_ERRORS) { result = COM_ERROR; } } p_buf_int = p_buf; size = file_size; /* Here 1024 bytes length is used to send the packets */ while ((size) && (result == COM_OK )) { /* Prepare next packet */ PreparePacket(p_buf_int, aPacketData, blk_number, size); ack_recpt = 0; a_rx_ctrl[0] = 0; errors = 0; /* Resend packet if NAK for few times else end of communication */ while (( !ack_recpt ) && ( result == COM_OK )) { /* Send next packet */ if (size >= PACKET_1K_SIZE) { pkt_size = PACKET_1K_SIZE; } else { pkt_size = PACKET_SIZE; } HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], pkt_size + PACKET_HEADER_SIZE, NAK_TIMEOUT); /* Send CRC or Check Sum based on CRC16_F */ #ifdef CRC16_F temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], pkt_size); Serial_PutByte(temp_crc >> 8); Serial_PutByte(temp_crc & 0xFF); #else /* CRC16_F */ temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], pkt_size); Serial_PutByte(temp_chksum); #endif /* CRC16_F */ /* Wait for Ack */ if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == ACK)) { ack_recpt = 1; if (size > pkt_size) { p_buf_int += pkt_size; size -= pkt_size; if (blk_number == (USER_FLASH_SIZE / PACKET_1K_SIZE)) { result = COM_LIMIT; /* boundary error */ } else { blk_number++; } } else { p_buf_int += pkt_size; size = 0; } } else { errors++; } /* Resend packet if NAK for a count of 10 else end of communication */ if (errors >= MAX_ERRORS) { result = COM_ERROR; } } } /* Sending End Of Transmission char */ ack_recpt = 0; a_rx_ctrl[0] = 0x00; errors = 0; while (( !ack_recpt ) && ( result == COM_OK )) { Serial_PutByte(EOT); /* Wait for Ack */ if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) { if (a_rx_ctrl[0] == ACK) { ack_recpt = 1; } else if (a_rx_ctrl[0] == CA) { if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA)) { HAL_Delay( 2 ); __HAL_UART_FLUSH_DRREGISTER(&UartHandle); result = COM_ABORT; } } } else { errors++; } if (errors >= MAX_ERRORS) { result = COM_ERROR; } } /* Empty packet sent - some terminal emulators need this to close session */ if ( result == COM_OK ) { /* Preparing an empty packet */ aPacketData[PACKET_START_INDEX] = SOH; aPacketData[PACKET_NUMBER_INDEX] = 0; aPacketData[PACKET_CNUMBER_INDEX] = 0xFF; for (i = PACKET_DATA_INDEX; i < (PACKET_SIZE + PACKET_DATA_INDEX); i++) { aPacketData [i] = 0x00; } /* Send Packet */ HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT); /* Send CRC or Check Sum based on CRC16_F */ #ifdef CRC16_F temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE); Serial_PutByte(temp_crc >> 8); Serial_PutByte(temp_crc & 0xFF); #else /* CRC16_F */ temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE); Serial_PutByte(temp_chksum); #endif /* CRC16_F */ /* Wait for Ack and 'C' */ if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) { if (a_rx_ctrl[0] == CA) { HAL_Delay( 2 ); __HAL_UART_FLUSH_DRREGISTER(&UartHandle); result = COM_ABORT; } } }
bool XModemProcessByte(uint8_t Byte) { switch(State) { case STATE_RECEIVE_INIT: case STATE_RECEIVE_WAIT: if (Byte == BYTE_SOH) { /* Next frame incoming */ BufferIdx = 0; Checksum = CHECKSUM_INIT_VALUE; State = STATE_RECEIVE_FRAMENUM1; } else if (Byte == BYTE_EOT) { /* Transmission finished */ TerminalSendByte(BYTE_ACK); State = STATE_OFF; } else if ( (Byte == BYTE_CAN) || (Byte == BYTE_ESC) ) { /* Cancel transmission */ State = STATE_OFF; } else { /* Ignore other bytes */ } break; case STATE_RECEIVE_FRAMENUM1: /* Store frame number */ ReceivedFrameNumber = Byte; State = STATE_RECEIVE_FRAMENUM2; break; case STATE_RECEIVE_FRAMENUM2: if (Byte == (255 - ReceivedFrameNumber)) { /* frame-number check passed. */ State = STATE_RECEIVE_DATA; } else { /* Something went wrong. Try to recover by sending NAK */ TerminalSendByte(BYTE_NAK); State = STATE_RECEIVE_WAIT; } break; case STATE_RECEIVE_DATA: /* Process byte and update checksum */ TerminalBuffer[BufferIdx++] = Byte; if (BufferIdx == XMODEM_BLOCK_SIZE) { /* Block full */ State = STATE_RECEIVE_PROCESS; } break; case STATE_RECEIVE_PROCESS: if (ReceivedFrameNumber == CurrentFrameNumber) { /* This is the expected frame. Calculate and verify checksum */ if (CalcChecksum(TerminalBuffer, XMODEM_BLOCK_SIZE) == Byte) { /* Checksum is valid. Pass received data to callback function */ if (CallbackFunc(TerminalBuffer, BlockAddress, XMODEM_BLOCK_SIZE)) { /* Proceed to next frame and send ACK */ CurrentFrameNumber++; BlockAddress += XMODEM_BLOCK_SIZE; TerminalSendChar(BYTE_ACK); State = STATE_RECEIVE_WAIT; } else { /* Application signals to cancel the transmission */ TerminalSendByte(BYTE_CAN); TerminalSendByte(BYTE_CAN); State = STATE_OFF; } } else { /* Data seems to be damaged */ TerminalSendByte(BYTE_NAK); State = STATE_RECEIVE_WAIT; } } else if (ReceivedFrameNumber == (CurrentFrameNumber - 1)) { /* This is a retransmission */ TerminalSendByte(BYTE_ACK); State = STATE_RECEIVE_WAIT; } else { /* This frame is completely out of order. Just cancel */ TerminalSendByte(BYTE_CAN); State = STATE_OFF; } break; case STATE_SEND_INIT: /* Start sending on NAK */ if (Byte == BYTE_NAK) { CurrentFrameNumber = FIRST_FRAME_NUMBER - 1; Byte = BYTE_ACK; } else if (Byte == BYTE_ESC) { State = STATE_OFF; } /* Fallthrough */ case STATE_SEND_WAIT: if (Byte == BYTE_CAN) { /* Cancel */ TerminalSendByte(BYTE_ACK); State = STATE_OFF; } else if (Byte == BYTE_ACK) { /* Acknowledge. Proceed to next frame, get data and calc checksum */ CurrentFrameNumber++; if (CallbackFunc(TerminalBuffer, BlockAddress, XMODEM_BLOCK_SIZE)) { TerminalSendByte(BYTE_SOH); TerminalSendByte(CurrentFrameNumber); TerminalSendByte(255 - CurrentFrameNumber); TerminalSendBlock(TerminalBuffer, XMODEM_BLOCK_SIZE); TerminalSendByte(CalcChecksum(TerminalBuffer, XMODEM_BLOCK_SIZE)); BlockAddress += XMODEM_BLOCK_SIZE; } else { TerminalSendByte(BYTE_EOT); State = STATE_SEND_EOT; } } else if (Byte == BYTE_NAK){ /* Resend frame */ TerminalSendByte(BYTE_SOH); TerminalSendByte(CurrentFrameNumber); TerminalSendByte(255 - CurrentFrameNumber); TerminalSendBlock(TerminalBuffer, XMODEM_BLOCK_SIZE); TerminalSendByte(CalcChecksum(TerminalBuffer, XMODEM_BLOCK_SIZE)); } else { /* Ignore other chars */ } break; case STATE_SEND_EOT: /* Receive Ack */ State = STATE_OFF; break; default: return false; break; } return true; }
TEST(ErrorCheck, CalcChecksum_NULL_NonZeroSize) { TEST_ASSERT_EQUAL(0, CalcChecksum(NULL, 10)); }
unsigned Random<T_PRNG>::GetChecksum() const { return CalcChecksum(rng_); }