// Read status register uint8_t Sensirion::readSR(uint8_t *result) { uint8_t val; uint8_t error = 0; #ifdef CRC_ENA _crc = bitrev(_stat_reg & SR_MASK); // Initialize CRC calculation #endif startTransmission(); if (error = putByte(STAT_REG_R)) { *result = 0xFF; return error; } #ifdef CRC_ENA calcCRC(STAT_REG_R, &_crc); // Include command byte in CRC calculation *result = getByte(ACK); calcCRC(*result, &_crc); val = getByte(noACK); val = bitrev(val); if (val != _crc) { *result = 0xFF; error = S_Err_CRC; } #else *result = getByte(noACK); #endif return error; }
/*! \brief crc */ static int getCrc (HANDLE_FDK_BITSTREAM hBs, ULONG NrBits) { int i; CRC_BUFFER CrcBuf; CrcBuf.crcState = SBR_CRC_START; CrcBuf.crcPoly = SBR_CRC_POLY; CrcBuf.crcMask = SBR_CRC_MASK; int CrcStep = NrBits>>MAXCRCSTEP_LD; int CrcNrBitsRest = (NrBits - CrcStep * MAXCRCSTEP); ULONG bValue; for (i = 0; i < CrcStep; i++) { bValue = FDKreadBits (hBs, MAXCRCSTEP); calcCRC (&CrcBuf, bValue, MAXCRCSTEP); } bValue = FDKreadBits (hBs, CrcNrBitsRest); calcCRC (&CrcBuf, bValue, CrcNrBitsRest); return (CrcBuf.crcState & SBR_CRC_RANGE); }
void RfMeshLeafBase::init() { node = new RfMeshNode(1, 10, 5, 5, 10); RfMeshTransceiverRfm70 *tc; tc = new RfMeshTransceiverRfm70(RFM70_PIN_SCLK, RFM70_PIN_MOSI, RFM70_PIN_MISO, RFM70_PIN_CSN, RFM70_PIN_CE); node->addTransceiver(tc); eeprom_read_block(&hardwareConfig, HARDWARE_CONFIG_ADDR, sizeof hardwareConfig); if (calcCRC(&hardwareConfig, sizeof hardwareConfig) != 0) reboot(); eeprom_read_block(&nodeConfig, NODE_CONFIG_ADDR, sizeof nodeConfig); if (calcCRC(&nodeConfig, sizeof nodeConfig) != 0) reboot(); if (!node->Init(nodeConfig.address)) reboot(); node->SendHeartbeat(nodeConfig.rootAddress); }
/************************************************************************************************** * @fn sbImgValid * * @brief Check validity of the run-code image. * * input parameters * * None. * * output parameters * * None. * * @return TRUE or FALSE for image valid. ************************************************************************************************** */ uint8 sbImgValid(void) { uint16 crc[2]; HalFlashRead(HAL_SB_CRC_ADDR / HAL_FLASH_PAGE_SIZE, HAL_SB_CRC_ADDR % HAL_FLASH_PAGE_SIZE, (uint8 *)crc, sizeof(crc)); if ((crc[0] == 0xFFFF) || (crc[0] == 0x0000)) { return FALSE; } if (crc[0] != crc[1]) { crc[1] = calcCRC(); HalFlashWrite((HAL_SB_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)crc, 1); HalFlashRead( HAL_SB_CRC_ADDR / HAL_FLASH_PAGE_SIZE, HAL_SB_CRC_ADDR % HAL_FLASH_PAGE_SIZE, (uint8 *)crc, sizeof(crc)); } return ((crc[0] == crc[1]) && (crc[0] != 0xFFFF) && (crc[0] != 0x0000)); }
// Initiate measurement. If blocking, wait for result uint8_t Sensirion::meas(uint8_t cmd, uint16_t *result, bool block) { uint8_t error, i; #ifdef CRC_ENA _crc = bitrev(_stat_reg & SR_MASK); // Initialize CRC calculation #endif startTransmission(); if (cmd == TEMP) cmd = MEAS_TEMP; else cmd = MEAS_HUMI; if (error = putByte(cmd)) return error; #ifdef CRC_ENA calcCRC(cmd, &_crc); // Include command byte in CRC calculation #endif // If non-blocking, save pointer to result and return if (!block) { _presult = result; return 0; } // Otherwise, wait for measurement to complete with 720ms timeout i = 240; while (digitalRead(_pinData)) { i--; if (i == 0) return S_Err_TO; // Error: Timeout delay(3); } error = getResult(result); return error; }
uint16_t ToSaMeshLeafBase::calcCRC (const void* ptr, uint16_t len) { return calcCRC(ptr, len, (uint16_t)~0); // uint16_t crc = ~0; // for (uint16_t i = 0; i < len; ++i) // crc = _crc16_update(crc, ((const char*) ptr)[i]); // return crc; }
//! calculate hashes for entire reconstructed picture Void SEIEncoder::initDecodedPictureHashSEI(SEIDecodedPictureHash *decodedPictureHashSEI, TComPic *pcPic, std::string &rHashString, const BitDepths &bitDepths) { assert (m_isInitialized); assert (decodedPictureHashSEI!=NULL); assert (pcPic!=NULL); if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 1) { decodedPictureHashSEI->method = SEIDecodedPictureHash::MD5; UInt numChar=calcMD5(*pcPic->getPicYuvRec(), decodedPictureHashSEI->m_pictureHash, bitDepths); rHashString = hashToString(decodedPictureHashSEI->m_pictureHash, numChar); } else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 2) { decodedPictureHashSEI->method = SEIDecodedPictureHash::CRC; UInt numChar=calcCRC(*pcPic->getPicYuvRec(), decodedPictureHashSEI->m_pictureHash, bitDepths); rHashString = hashToString(decodedPictureHashSEI->m_pictureHash, numChar); } else if(m_pcCfg->getDecodedPictureHashSEIEnabled() == 3) { decodedPictureHashSEI->method = SEIDecodedPictureHash::CHECKSUM; UInt numChar=calcChecksum(*pcPic->getPicYuvRec(), decodedPictureHashSEI->m_pictureHash, bitDepths); rHashString = hashToString(decodedPictureHashSEI->m_pictureHash, numChar); } }
AREXPORT bool ArLMS2xxPacket::verifyCRC(void) { int readLen = myReadLength; int len = myLength; ArTypes::Byte2 chksum; unsigned char c1, c2; myReadLength = myLength - 2; if (myReadLength < myHeaderLength) return false; c1 = bufToByte(); c2 = bufToByte(); myReadLength = readLen; chksum = (c1 & 0xff) | (c2 << 8); myLength = myLength - 2; if (chksum == calcCRC()) { myLength = len; return true; } else { myLength = len; return false; } }
UdpPermitToSendMsg() { hdr.length = hdr.cmd = hdr.destNodeIndex = hdr.max_data = 0; hdr.lastSequenceSeen = 0; hdr.missingCount = 0; #ifdef CRC_MESSAGES hdr.crc = calcCRC(); #endif }
/* \brief crc */ static int getCrc (HANDLE_BIT_BUFFER hBitBuf, unsigned long NrBits) { int i; int CrcStep = NrBits / MAXCRCSTEP; int CrcNrBitsRest = (NrBits - CrcStep * MAXCRCSTEP); unsigned long bValue; CRC_BUFFER CrcBuf; FLC_sub_start("getCrc"); DIV(1); MULT(1); ADD(1); /* counting previous operations */ MOVE(3); CrcBuf.crcState = SBR_CRC_START; CrcBuf.crcPoly = SBR_CRC_POLY; CrcBuf.crcMask = SBR_CRC_MASK; LOOP(1); for (i = 0; i < CrcStep; i++) { FUNC(2); bValue = getbits (hBitBuf, MAXCRCSTEP); PTR_INIT(1); FUNC(3); calcCRC (&CrcBuf, bValue, MAXCRCSTEP); } FUNC(2); bValue = getbits (hBitBuf, CrcNrBitsRest); PTR_INIT(1); FUNC(3); calcCRC (&CrcBuf, bValue, CrcNrBitsRest); LOGIC(1); /* counting post operation */ FLC_sub_end(); return (CrcBuf.crcState & SBR_CRC_RANGE); }
void MQ_GetS(int n) { byte command[6]; MERQURY* item = MerquryMeashure[n]; command[0] = item->address; command[1] = 0x08; command[2] = 0x16; command[3] = MQ_COMMANDS_CODE[ MQ_READS ]; calcCRC(command, 6); MQSendCommand(n, command, 6); MQ_COMMANDS[n] = MQ_READS; }
void MerquryOpen(int n) { MERQURY* item = MerquryMeashure[n]; byte command[11]; int len=11, i; command[0] = item->address; memcpy(command+1, item->password, 8); calcCRC(command, len); Print( "m -> %d\n\r", len ); MQSendCommand(n, command, len); MQ_COMMANDS[n] = MQ_OPEN; }
// Get measurement result from sensor (plus CRC, if enabled) uint8_t Sensirion::getResult(uint16_t *result) { uint8_t val; #ifdef CRC_ENA val = getByte(ACK); calcCRC(val, &_crc); *result = val; val = getByte(ACK); calcCRC(val, &_crc); *result = (*result << 8) | val; val = getByte(noACK); val = bitrev(val); if (val != _crc) { *result = 0xFFFF; return S_Err_CRC; } #else *result = getByte(ACK); *result = (*result << 8) | getByte(noACK); #endif return 0; }
void ExpandRequest::processPacket( PacketContainer* pack ) { const ExpandReplyPacket* reply = static_cast<ExpandReplyPacket*>( pack->getPacket() ); MC2_ASSERT( reply->getSubType() == Packet::PACKETTYPE_EXPAND_REPLY ); reply->get( *m_replyData ); m_status = reply->getStatus(); m_done = true; calcCRC(); }
MyString CDownload::getPersistentDir(const char* url) { MyString dir; // dir.Format("%s"ACTIVEGS_DIRECTORY_SEPARATOR"%s.%08X",getPersistentPath(),getfilenoext(url),calcCRC(url)); dir = getPersistentPath(); dir += ACTIVEGS_DIRECTORY_SEPARATOR; dir += getfilenoext(url) ; /* MyString urldir; getdir(url,urldir); */ MyString temp; temp.Format(".%08X",calcCRC(url)); dir += temp.c_str(); return dir ; }
int main(int argc, char *argv[]) { int fd; int len; char *buffer; if ((argc > 3) || (argc == 1)) { printf("Usage: %s -f filename OR %s string\n", argv[0], argv[0]); return 1; } /* read from file passed as cmd line arg */ if ((argc == 3) && (strcmp("-f",argv[1]) == 0)) { buffer = (char *)malloc(BUFFSIZE); if (buffer == NULL) { perror("malloc: "); return 2; } fd = open(argv[2],O_RDONLY); if (fd < 0) { perror("open:"); free(buffer); buffer = NULL; return 3; } len = read(fd,buffer,BUFFSIZE); close(fd); } else if (argc == 2) { //Read from cmd line buffer = argv[1]; len = strlen(argv[1]); } else { //handles if flag is not -f printf("Usage: %s -f filename OR %s string\n", argv[0], argv[0]); return 4; } printf("%.4x\n",calcCRC(buffer,len)); if (argc == 4) { free(buffer); } buffer = NULL; return 0; }
AREXPORT void ArLMS2xxPacket::finalizePacket(void) { int len = myLength; int chkSum; // put in the start of the packet myLength = 0; // toss in the header uByteToBuf(0x02); // now the laser we want to talk to uByteToBuf(mySendingAddress); // dump in the length uByte2ToBuf(len - myHeaderLength); myLength = len; // that lovely CRC chkSum = calcCRC(); byteToBuf(chkSum & 0xff ); byteToBuf((chkSum >> 8) & 0xff ); //printf("Sending "); //log(); }
int sb_image_check() { int fd; int rtnval = 0; // off_t offset = HAL_SB_CRC_ADDR - HAL_SB_IMG_ADDR; uint16_t *crc; uint8_t firmwareBuffer[HAL_SB_IMG_SIZE]; fd = open(SB_FIRMWARE_PERM_PATH, O_RDWR); if (fd < 0) { LOG_ERROR_MESSAGE("image file open failed"); return -1; } if (read(fd, (void *)firmwareBuffer, HAL_SB_IMG_SIZE) != HAL_SB_IMG_SIZE) { LOG_ERROR_MESSAGE("bad image size"); rtnval = -1; goto exit_sb_image_check; } // basic check crc first crc = (uint16_t *)(firmwareBuffer + HAL_SB_CRC_ADDR - HAL_SB_IMG_ADDR); if (crc[0] == 0xFFFF || crc[0] == 0x0000 || crc[0] != calcCRC(firmwareBuffer)) { LOG_ERROR_MESSAGE("bad image crc"); rtnval = -1; goto exit_sb_image_check; } exit_sb_image_check: close(fd); return rtnval; }
/** * Calculate and print hash for pic, compare to picture_digest SEI if * present in seis. seis may be NULL. Hash is printed to stdout, in * a manner suitable for the status line. Theformat is: * [Hash_type:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,(yyy)] * Where, x..x is the hash * yyy has the following meanings: * OK - calculated hash matches the SEI message * ***ERROR*** - calculated hash does not match the SEI message * unk - no SEI message was available for comparison */ static void calcAndPrintHashStatus(TComPicYuv& pic, const SEIDecodedPictureHash* pictureHashSEI) { /* calculate MD5sum for entire reconstructed picture */ UChar recon_digest[3][16]; Int numChar=0; const Char* hashType = "\0"; if (pictureHashSEI) { switch (pictureHashSEI->method) { case SEIDecodedPictureHash::MD5: { hashType = "MD5"; calcMD5(pic, recon_digest); numChar = 16; break; } case SEIDecodedPictureHash::CRC: { hashType = "CRC"; calcCRC(pic, recon_digest); numChar = 2; break; } case SEIDecodedPictureHash::CHECKSUM: { hashType = "Checksum"; calcChecksum(pic, recon_digest); numChar = 4; break; } default: { assert (!"unknown hash type"); } } } /* compare digest against received version */ const Char* ok = "(unk)"; Bool mismatch = false; if (pictureHashSEI) { ok = "(OK)"; for(Int yuvIdx = 0; yuvIdx < 3; yuvIdx++) { for (UInt i = 0; i < numChar; i++) { if (recon_digest[yuvIdx][i] != pictureHashSEI->digest[yuvIdx][i]) { ok = "(***ERROR***)"; mismatch = true; } } } } printf("[%s:%s,%s] ", hashType, digestToString(recon_digest, numChar), ok); if (mismatch) { g_md5_mismatch = true; printf("[rx%s:%s] ", hashType, digestToString(pictureHashSEI->digest, numChar)); } }
void Modem::readXChar(int) { uchar c; static uchar crc_hi, block, cblock; while(read(fd, (void *) &c, 1) == 1) { switch(xstate) { case 1: /* 1st 'C' sent */ case 2: /* 2nd 'C' sent */ case 3: /* 3rd 'C' sent */ case 4: /* 4th 'C' sent */ case 5: /* 1st <NAK> sent */ case 6: /* 2nd <NAK> sent */ case 7: /* 3rd <NAK> sent */ case 8: /* 4th <NAK> sent */ case 9: /* 5th <NAK> sent */ case 10: /* 6th <NAK> sent */ if(c == CSOH) { timerStart(1000); xsize = 128; xstate = 11; } else if(c == CSTX) { timerStart(1000); xsize = 1024; xstate = 11; } else if(c == CEOT) { timer->stop(); writeChar(CACK); xreset(); emit xmodemDone(true); } else timerStart(1000); break; case 11: /* <SOH> or <STX> received */ timerStart(1000); block = c; xstate++; break; case 12: /* block number received */ timerStart(1000); cblock = c; xstate++; bufpos = 0; break; case 13: /* complement block number received */ timerStart(1000); buffer[ bufpos++ ] = c; if(bufpos == xsize) { bufpos = 0; xstate++; if(!xcrc) xstate++; } break; case 14: /* data block received */ timerStart(1000); crc_hi = c; xstate++; break; case 15: /* crc high-byte received */ timerStart(10000); xstate = 4; if((uchar)(block ^ cblock) != 0xff) { writeChar(CNAK); break; } if(block + 1 == xblock) { writeChar(CACK); break; } if(block != xblock) { timer->stop(); writeChar(CCAN); xreset(); emit xmodemDone(false); break; } if(xcrc) { if(((ushort) crc_hi << 8 | (ushort) c) != calcCRC()) { writeChar(CNAK); break; } } else { if(c != calcChecksum()) { writeChar(CNAK); break; } } writeChar(CACK); xblock++; emit gotXBlock(buffer, xsize); break; default: break; } } }
uint16_t RfMeshLeafBase::calcCRC (const void* ptr, uint16_t len) { return calcCRC(ptr, len, (uint16_t)~0); }
bool CAPRSTransmit::run() { //First see if the packet is Icom supported... CAPRSPacket aprsPacket; if(!CAPRSParser::Parse(m_text, aprsPacket)){ wxLogWarning(wxT("Unsupported APRS Format, ignoring => ") + m_text.Trim(true)); return false; } wxString textWithCRC(aprsPacket.Raw()); wxLogMessage(wxT("Supported APRS Format => ") + textWithCRC.Trim(true)); //add nececessary stuff to text, but keep it the original textWithCRC.Replace(wxT("\n"), wxEmptyString); if(!textWithCRC.EndsWith(wxT("\r"))) textWithCRC.Append(wxT("\r")); wxString crc = wxString::Format(wxT("$$CRC%04X,"), calcCRC(textWithCRC)); textWithCRC.Prepend(crc); bool opened = m_socket.open(); if (!opened) return false; in_addr address = CUDPReaderWriter::lookup(wxT("127.0.0.1")); unsigned int id = CHeaderData::createId(); wxString callsignG = m_repeaterCallsign.Left(LONG_CALLSIGN_LENGTH - 1U); callsignG.Append(wxT("G")); CHeaderData header; header.setId(id); header.setMyCall1(m_APRSCallsign); header.setMyCall2(wxT("APRS")); header.setRptCall1(callsignG); header.setRptCall2(m_repeaterCallsign); header.setYourCall(wxT("CQCQCQ ")); header.setDestination(address, G2_DV_PORT); sendHeader(header); CSlowDataEncoder encoder; encoder.setHeaderData(header); encoder.setGPSData(textWithCRC); encoder.setTextData(wxT("APRS to DPRS")); CAMBEData data; data.setDestination(address, G2_DV_PORT); data.setId(id); wxStopWatch timer; timer.Start(); unsigned int out = 0U; unsigned int dataOut = 0U; unsigned int needed = (encoder.getInterleavedDataLength() / (DATA_FRAME_LENGTH_BYTES)) * 2U; while (dataOut < needed) { data.setSeq(out); unsigned char buffer[DV_FRAME_LENGTH_BYTES]; ::memcpy(buffer + 0U, NULL_AMBE_DATA_BYTES, VOICE_FRAME_LENGTH_BYTES); // Insert sync bytes when the sequence number is zero, slow data otherwise if (out == 0U) { ::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, DATA_SYNC_BYTES, DATA_FRAME_LENGTH_BYTES); } else { encoder.getInterleavedData(buffer + VOICE_FRAME_LENGTH_BYTES); dataOut++; } data.setData(buffer, DV_FRAME_LENGTH_BYTES); sendData(data); out++; if (out == 21U) out = 0U; } data.setData(END_PATTERN_BYTES, DV_FRAME_LENGTH_BYTES); data.setSeq(out >= 21U ? 0U : out); data.setEnd(true); sendData(data); m_socket.close(); return true; }
void addCRC() { tx_buffer.data[tx_buffer.size-1] = calcCRC(tx_buffer.data,tx_buffer.size-1); }
bool crcIsCorrect() { crc_t recCrc = rx_buffer.data[rx_buffer.size-1]; crc_t calcCrc = calcCRC(rx_buffer.data,rx_buffer.size-1); return (recCrc == calcCrc); }