bool isPacket(uint8_t *pkt, CircBuffer *recbuf) { bool ret=false; if(recbuf->Count>sizeof(Pakiet)) //Sprawdzamy zmienn¹ cout naszego bufora struktury CircBuffer, { //jeœli iloœæ bajtów jest mniejsza od struktury Pakiet to zwracamy flase pkt[0]=cb_Read(recbuf); if(pkt[0]==PKT_HEADER) //Sprawdzamy nag³ówek { uint16_t crc=_crc_xmodem_update(0xffff, pkt[0]); //Inicjujemy CRC uint16_t len=0; //USART_putchar(&USARTF0,pkt[0]); for(uint8_t pos=1; pos<(len+sizeof(Pakiet)); pos++) { pkt[pos]=cb_Read_Block(recbuf); //USART_putchar(&USARTF0,pkt[pos]); if(pos==offsetof(Pakiet, Len)) { len = (pkt[pos]<<8 | pkt[pos+1]); //W momencie dojœcia do danej odpowiedzialnej za d³ugoœæ pola danych Data[0], } //uaktualniana zostaje zmienna len, w ten sposób zanmy rozmiar pakietu crc=_crc_xmodem_update(crc, pkt[pos]); } if(crc==0) ret=true; } } return ret; }
// Send data from LCD to PC via RS-232 using the XMODEM protocol static inline void SendBMP(void) { uint8_t rx,data,Packet=1, n=0,i,j; uint16_t crc=0; // First Block send(SOH); send(Packet); // Send Packet number send(255-Packet); // Send Packet number 1's complement // Send BMP Header for(i=0; i<62; i++) { n++; data = pgm_read_byte_near(BMP+i); crc=_crc_xmodem_update(crc,data); send(data); } // Send LCD data, 64 lines for(i=63; i!=255; i--) { // Each LCD line consists of 16 bytes for(j=0; j<16; j++) { data=transpose(j*8,i); crc=_crc_xmodem_update(crc,data); send(data); n++; if(n==128) { // end of 128byte block n=0; send(hibyte(crc)); send(lobyte(crc)); // Wait for ACK rx = read(); if(rx!=ACK) return; // Error -> cancel transmission Packet++; send(SOH); send(Packet); send(255-Packet); crc=0; } } } // End of last block for(; n<128; n++) { // send remainder of block data= 0x1A; // pad with 0x1A which marks the end of file crc=_crc_xmodem_update(crc,data); send(data); } send(hibyte(crc)); send(lobyte(crc)); rx = read(); if(rx!=ACK) return; send(EOT); rx = read(); if(rx!=NAK) return; send(EOT); rx = read(); if(rx!=ACK) return; }
uint16_t calcCrc(uint8_t cmd, uint8_t* data) { uint16_t crc=0; crc = _crc_xmodem_update(crc,cmd); crc = _crc_xmodem_update(crc,data[3]); crc = _crc_xmodem_update(crc,data[2]); crc = _crc_xmodem_update(crc,data[1]); crc = _crc_xmodem_update(crc,data[0]); return crc; }
// Load board config. // Return true if config CRC is valid uint8_t board_load_config (void) { #ifndef SIMULATE uint8_t i; uint16_t crc=0; uint8_t *config_arr = (uint8_t*)&board_config; // read config from flash at45db_start_continuous_read(BOARD_CONFIG_ADDRESS); at45db_continuous_read_block(sizeof(board_config),(uint8_t*)(&board_config)); at45db_end_continuous_read(); // calculate CRC for (i=0;i<8;i++) crc = _crc_xmodem_update(crc,config_arr[i]); // check CRC return (crc==board_config.crc); #else printf("Skipping board config check...\n"); return 1; #endif }
uint16_t crccat(char *msg) { uint16_t x; for(x = 0xFFFF; *msg; msg++) x = _crc_xmodem_update(x, *msg); snprintf(msg, 8, "*%04X\n", x); return(x); }
uint16_t get_crc_of_buffer(msg_buffer_u *buffer, uint8_t length) { uint16_t calc_crc_16 = 0x0000; for (uint8_t i =0; i < length - 2; i++) { calc_crc_16 = _crc_xmodem_update(calc_crc_16, buffer->buffer[i]); } return calc_crc_16; }
//计算CRC16 static unsigned int calcrc(unsigned char *ptr, unsigned int count) { unsigned int crc = 0; while (count--) { crc =_crc_xmodem_update(crc,*ptr++); } return crc; }
uint16_t gps_CRC16_checksum (char *string){ size_t i; uint16_t crc; uint8_t c; crc = 0xFFFF; // Calculate checksum ignoring the first two $s for (i = 2; i < strlen(string); i++) { c = string[i]; crc = _crc_xmodem_update (crc, c); } return crc; }
uint16_t crccat(char *msg) { uint16_t x; /* Skip initial '$'s */ while(*msg == '$') msg++; /* Calculate the checksum */ for(x = 0xFFFF; *msg; msg++) x = _crc_xmodem_update(x, *msg); /* Append it to the string */ snprintf_P(msg, 8, PSTR("*%04X\n"), x); return(x); }
int main ( void ) { platform_init(); init_uart(); trigger_setup(); /* Uncomment this to get a HELLO message for debug */ /* putch('h'); putch('e'); putch('l'); putch('l'); putch('o'); putch('\n'); */ //Load Super-Secret key aes256_context ctx; uint8_t tmp32[32] = SECRET_KEY; aes256_init(&ctx, tmp32); //Load super-secret IV uint8_t iv[16] = IV; //Do this forever (we don't actually have a jumper to bootloader) uint8_t i; uint16_t crc; uint8_t c; while(1){ c = (uint8_t)getch(); if (c == 0){ //Initial Value crc = 0x0000; //Read 16 Bytes now for(i = 0; i < 16; i++){ c = (uint8_t)getch(); crc = _crc_xmodem_update(crc, c); //Save two copies, as one used for IV tmp32[i] = c; tmp32[i+16] = c; } //Validate CRC-16 uint16_t inpcrc = (uint16_t)getch() << 8; inpcrc |= (uint8_t)getch(); if (inpcrc == crc){ //CRC is OK - continue with decryption trigger_high(); aes256_decrypt_ecb(&ctx, tmp32); /* encrypting the data block */ trigger_low(); //Apply IV (first 16 bytes) for (i = 0; i < 16; i++){ tmp32[i] ^= iv[i]; } /* Comment this block out to always use initial IV, instead of performing actual CBC mode operation */ //Save IV for next time from original ciphertext for (i = 0; i < 16; i++){ iv[i] = tmp32[i+16]; } //Always send OK, done before checking //signature to ensure there is no timing //attack. This does mean user needs to //add some delay before sending next packet putch(COMM_OK); putch(COMM_OK); //Check the signature if ((tmp32[0] == SIGNATURE1) && (tmp32[1] == SIGNATURE2) && (tmp32[2] == SIGNATURE3) && (tmp32[3] == SIGNATURE4)){ //We now have 12 bytes of useful data to write to flash memory. //We don't actually write anything here though in real life would //probably do more than just delay a moment _delay_ms(1); } } else { putch(COMM_BADCHECKSUM); putch(COMM_BADCHECKSUM); } } } return 1; }
void process_serial(void) { if (zcl_ati()) { // ATI command response for (uint8_t i = 0; i < sizeof(ati_resp)-1; i++) { char c = pgm_get(ati_resp[i],byte); serial_send(c); } // MAC address, big endian, colon separated serial_send_hex(mac >> 56); serial_send(':'); serial_send_hex(mac >> 48); serial_send(':'); serial_send_hex(mac >> 40); serial_send(':'); serial_send_hex(mac >> 32); serial_send(':'); serial_send_hex(mac >> 24); serial_send(':'); serial_send_hex(mac >> 16); serial_send(':'); serial_send_hex(mac >> 8); serial_send(':'); serial_send_hex(mac >> 0); serial_send('\n'); // May continue to packet processing } if (zcl_own_fault()) { /* If buffer overflow or other internal error * happened, there is not much to do. TODO Maybe there * should be some internal flag? Or proper ZigBee * error? */ serial_send(ACK); zcl_receiver_reset(); return; } if (!zcl_packet_available()) return; // We have a packet. Checking CRC. uint16_t *msg_crc = (uint16_t *)(zcl.raw + zcl.packet.length + 2); uint16_t crc = 0xffff; for (uint16_t i = 0; i < zcl.packet.length; i++) { crc = _crc_xmodem_update(crc, zcl.raw[i+2]); } /* Answering ACK and processing the answer if it was * correct. Otherwise just send NAK and let the sender to * resend it later */ if (*msg_crc == crc) { serial_send(ACK); process_payload(); } else { serial_send(NAK); } // Re-enable the receiver zcl_receiver_reset(); }