+static void uuid_reversecpy(unsigned char* out, unsigned char* in, int len)
+{
+ int i = 0;
+ while (len > 0) {
+ out[i] = in[len];
+ len--;
+ i++;
+ }
+}
+
+static int
+rc632_iso15693_transceive_ac(struct rfid_asic_handle *handle,
+ const struct iso15693_anticol_cmd *acf,
+ unsigned int acf_len,
+ struct iso15693_anticol_resp *resp,
+ unsigned int *rx_len, unsigned char *bit_of_col)
+{
+ u_int8_t error_flag, boc;
+ //u_int8_t rx_len;
+
+ int ret, tx_len, mask_len_bytes;
+ unsigned int rate = ISO15693_T_SLOW;
+
+ if (acf->req.flags & RFID_15693_F_RATE_HIGH)
+ rate = ISO15693_T_FAST;
+
+ DEBUGP("acf = %s\n", rfid_hexdump(acf, acf_len));
+
+ ret = rc632_transceive(handle, (u_int8_t *)acf, acf_len,
+ (u_int8_t *) resp, rx_len,
+ iso15693_timing[rate][ISO15693_T1], 0);
+ if (ret == -ETIMEDOUT)
+ return ret;
+
+ /* determine whether there was a collission */
+ ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
+ if (ret < 0)
+ return ret;
+ DEBUGP_ERROR_FLAG(error_flag);
+
+ //FIXME: check for framing and crc errors...
+ if (error_flag & RC632_ERR_FLAG_COL_ERR) {
+ /* retrieve bit of collission */
+ ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
+ if (ret < 0)
+ return ret;
+ *bit_of_col = boc;
+ } else {
+ *bit_of_col = 0;
+ }
+
+ return 0;
+
+#if 0
+ *bit_of_col = 0;
+
+ mask_len_bytes = (acf->mask_len % 8) ? acf->mask_len/8+1 : acf->mask_len/8;
+
+ if (acf->current_slot == 0) {
+ /* first call: transmit Inventory frame */
+ DEBUGP("first_frame\n");
+
+ tx_len = sizeof(struct iso15693_request) + 1 + mask_len_bytes;
+
+ ret = rc632_transceive(handle, (u_int8_t *)&req, tx_len,
+ (u_int8_t *)&rx_buf, &rx_len, ISO15693_T1, 0);
+ acf->current_slot = 1;
+ DEBUGP("rc632_transceive ret: %d rx_len: %d\n",ret,rx_len);
+ /* if ((ret < 0)&&(ret != -ETIMEDOUT))
+ return ret; */
+
+ } else {
+ /* second++ call: end timeslot with EOFpulse and read */
+ DEBUGP("second++_frame\n");
+ if ((acf->current_slot > 16) ||
+ ((acf->flags & RFID_15693_F5_NSLOTS_1 == 0)
+ && (acf->current_slot > 1))) {
+
+ memset(uuid, 0, ISO15693_UID_LEN);
+ return -1;
+ }
+
+ /* reset EOF-pulse-bit to 0 */
+ ret = rc632_clear_bits(handle, RC632_REG_CODER_CONTROL,
+ RC632_CDRCTRL_15693_EOF_PULSE);
+ usleep(50);
+ /* generate EOF pulse */
+ ret = rc632_set_bits(handle, RC632_REG_CODER_CONTROL,
+ RC632_CDRCTRL_15693_EOF_PULSE);
+ if (ret < 0)
+ return ret;
+ // DEBUGP("waiting for EOF pulse\n");
+ // ret = rc632_wait_idle(handle, 10); //wait for idle
+
+ rx_len = sizeof(rx_buf);
+ ret = rc632_receive(handle, (u_int8_t*)&rx_buf, &rx_len, ISO15693_T3);
+ DEBUGP("rc632_receive ret: %d rx_len: %d\n", ret, rx_len);
+ acf->current_slot++;
+
+ /* if ((ret < 0)&&(ret != -ETIMEDOUT))
+ return ret; */
+ }
+
+ rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
+ DEBUGP_STATUS_FLAG(tmp);
+
+ if (ret == -ETIMEDOUT) {
+ /* no VICC answer in this timeslot*/
+ memset(uuid, 0, ISO15693_UID_LEN);
+ return -ETIMEDOUT;
+ } else {
+ /* determine whether there was a collission */
+ ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
+ DEBUGP_ERROR_FLAG(error_flag);
+ if (ret < 0)
+ return ret;
+
+ if (error_flag & RC632_ERR_FLAG_COL_ERR) {
+ /* retrieve bit of collission */
+ ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
+ if (ret < 0)
+ return ret;
+ *bit_of_col = boc;
+ memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
+ // uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
+ DEBUGP("Collision in slot %d bit %d\n",
+ acf->current_slot,boc);
+ return -ECOLLISION;
+ } else {
+ /* no collision-> retrieve uuid */
+ DEBUGP("no collision in slot %d\n", acf->current_slot);
+ memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
+ //uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
+ }
+ }
+
+ return 0;
+#endif
+}
+