Esempio n. 1
0
static int t1_resynchronize(ifd_protocol_t * p, int nad)
{
	t1_state_t *t1 = (t1_state_t *) p;
	unsigned char block[4];
	unsigned int retries = 3;

	if (p->reader && p->reader->device)
		ifd_device_flush(p->reader->device);

	while (retries--) {
		t1->ns = 0;
		t1->nr = 0;

		block[0] = nad;
		block[1] = T1_S_BLOCK | T1_S_RESYNC;
		block[2] = 0;
		t1_compute_checksum(t1, block, 3);

		if (t1_xcv(t1, block, 4, sizeof(block)) != 4) {
			ifd_debug(1, "fatal: transmit/receive failed");
			break;
		}

		if (!t1_verify_checksum(t1, block, 4)) {
			ifd_debug(1, "checksum failed");
			continue;
		}

		if (block[1] == (T1_S_BLOCK | T1_S_RESPONSE | T1_S_RESYNC))
			return 0;
	}

	t1->state = DEAD;
	return -1;
}
Esempio n. 2
0
static unsigned int t1_build(t1_state_t * t1, unsigned char *block,
			     unsigned char dad, unsigned char pcb,
			     ct_buf_t * bp, size_t * lenp)
{
	unsigned int len;

	len = bp ? ct_buf_avail(bp) : 0;
	if (len > t1->ifsc) {
		pcb |= T1_MORE_BLOCKS;
		len = t1->ifsc;
	}

	/* Add the sequence number */
	switch (t1_block_type(pcb)) {
	case T1_R_BLOCK:
		pcb |= t1->nr << T1_R_SEQ_SHIFT;
		break;
	case T1_I_BLOCK:
		pcb |= t1->ns << T1_I_SEQ_SHIFT;
		break;
	}

	block[0] = dad;
	block[1] = pcb;
	block[2] = len;

	if (len)
		memcpy(block + 3, ct_buf_head(bp), len);
	if (lenp)
		*lenp = len;

	return t1_compute_checksum(t1, block, len + 3);
}
unsigned int t1_build(t1_state_t * t1, unsigned char *block,
	unsigned char dad, unsigned char pcb,
	ct_buf_t *bp, size_t *lenp)
{
	unsigned int len;
	char more = FALSE;

	len = bp ? ct_buf_avail(bp) : 0;
	if (len > t1->ifsc) {
		pcb |= T1_MORE_BLOCKS;
		len = t1->ifsc;
		more = TRUE;
	}

	/* Add the sequence number */
	switch (t1_block_type(pcb)) {
	case T1_R_BLOCK:
		pcb |= t1->nr << T1_R_SEQ_SHIFT;
		break;
	case T1_I_BLOCK:
		pcb |= t1->ns << T1_I_SEQ_SHIFT;
		t1->more = more;
		DEBUG_COMM2("more bit: %d", more);
		break;
	}

	block[0] = dad;
	block[1] = pcb;
	block[2] = len;

	if (len)
		memcpy(block + 3, ct_buf_head(bp), len);
	if (lenp)
		*lenp = len;

	len = t1_compute_checksum(t1, block, len + 3);

	/* memorize the last sent block */
	/* only 4 bytes since we are only interesed in R-blocks */
	memcpy(t1->previous_block, block, 4);

	return len;
}