void immob_check_state(struct ecudata_t* d) { uint8_t i = 0, crc = 0; uint8_t key[8]; if (!(d->param.bt_flags & _BV(BTF_USE_IMM))) return; //immibilizer was not activated onewire_save_io_registers(); if (!onewire_reset()) goto lock_system; //not device present, lock the system! //Read 64-bit key onewire_write_byte(OWCMD_READ_ROM); for(; i < 8; ++i) key[i] = onewire_read_byte(); //validate CRC8, all bytes except CRC8 byte for(i = 0; i < 7; ++i) crc = update_crc8(key[i], crc); if (crc != key[7]) goto lock_system; //crc doesn't match, lock the system! //validate read key, skip family code and CRC8 bytes if (!validate_key(d, key+1, IBTN_KEY_SIZE)) goto lock_system; //read and stored keys don't match, lock the system! onewire_restore_io_registers(); return; //ok, system is unlocked lock_system: onewire_restore_io_registers(); d->sys_locked = 1; //set locking flag }
uint8_t calculate_crc8(uint8_t *p, unsigned int length) { uint8_t crc; unsigned int i; crc = 0; for (i=0; i < length; i++) { crc = update_crc8(crc,*p++); } return crc; }
void __attribute__((__interrupt__, no_auto_psv)) _U2RXInterrupt(void) { unsigned char c; while(U2STAbits.URXDA) { c = U2RXREG; // If we just received the second character, set packet length if (in_pkt_idx_ == 1) { in_pkt_len_ = c; } // If there has been a parse error, reset packet buffer if ((in_pkt_idx_ == 0 && c != PKT_START_CHAR) || in_pkt_len_ < sizeof(header_t)+1 ) { in_pkt_idx_ = 0; in_pkt_len_ = MAX_PACKET_LENGTH; in_pkt_crc_ = 0; // Store byte in packet buffer and update CRC } else { in_pkt_->raw[in_pkt_idx_++] = c; in_pkt_crc_ = update_crc8(in_pkt_crc_, c); } // If we just received the last character and CRC is good, // swap with last_packet and reset packet buffer if (in_pkt_idx_ == in_pkt_len_) { if (in_pkt_crc_ == 0) { last_bldc_packet_is_new = 1; LED_2 ^= 1; LED_3 = 1; if(last_bldc_packet == &(pkt_buf_0_)) { last_bldc_packet = &(pkt_buf_1_); in_pkt_ = &(pkt_buf_0_); } else { last_bldc_packet = &(pkt_buf_0_); in_pkt_ = &(pkt_buf_1_); } } in_pkt_idx_ = 0; in_pkt_len_ = MAX_PACKET_LENGTH; in_pkt_crc_ = 0; } } if(U2STAbits.OERR) { U2STAbits.OERR = 0; } _U2RXIF = 0; }
static uint8_t get_crc8(uint8_t *Buf, uint8_t BufLen) { uint8_t crc = 0; for(int i=0; i<BufLen; i++) crc = update_crc8(Buf[i], crc); return (crc); }