- int byte_index = 0, card_size = 0, command_size = 0;
- uint16_t command_obfuscation = 0x57, response_obfuscation = 0;
- uint16_t tag_type = perform_setup_phase_rwd();
- switch_off_tag_rwd();
+ WDT_HIT();
+}
+/* calculate crc for a legic command */
+static int LegicCRC(int byte_index, int value) {
+ crc_clear(&legic_crc);
+ crc_update(&legic_crc, 1, 1); /* CMD_READ */
+ crc_update(&legic_crc, byte_index, 8);
+ crc_update(&legic_crc, value, 8);
+ return crc_finish(&legic_crc);
+}
+
+int legic_read_byte(int byte_index) {
+ int byte;
+
+ legic_prng_forward(4); /* we wait anyways */
+ while(timer->TC_CV < 387) ; /* ~ 258us + 100us*delay */
+
+ frame_send_rwd(1 | (byte_index << 1), 9);
+ frame_clean(¤t_frame);
+
+ frame_receive_rwd(¤t_frame, 12, 1);
+
+ byte = current_frame.data & 0xff;
+ if( LegicCRC(byte_index, byte) != (current_frame.data >> 8) )
+ Dbprintf("!!! crc mismatch: expected %x but got %x !!!", LegicCRC(byte_index, current_frame.data & 0xff), current_frame.data >> 8);
+
+ return byte;
+}
+
+/* legic_write_byte() is not included, however it's trivial to implement
+ * and here are some hints on what remains to be done:
+ *
+ * * assemble a write_cmd_frame with crc and send it
+ * * wait until the tag sends back an ACK ('1' bit unencrypted)
+ * * forward the prng based on the timing
+ */
+
+
+void LegicRfReader(int offset, int bytes) {
+ int byte_index=0;