package com.mq.mylibrary.rfid.io.cmd;

import com.mq.mylibrary.rfid.io.service.FnIOException;
import com.mq.mylibrary.rfid.io.service.IOManager;

import java.io.IOException;


public class _14443AComm {

	static int TX_GENERAL_FRAME = 0;
	static int TX_APDU_FRAME = 1;

	public static int ISO14443A_Comm(TXFRAME frame, int nType)
			throws IOException {
		byte[] data = new byte[TXFRAME.TX_BUF_LEN];
		int len = 0;
		int ret = -1;

		len = TXFRAME.tx_make_frame_with_type(data, frame, nType);

		frame.timeout_count += 28;

		try {
			IOManager.INSTANCE.SendCmd(data, len);

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			byte[] recv1 = IOManager.INSTANCE.GetCommData();
			if (recv1 == null) {
				return -1;
			}
			ret = TXFRAME.tx_check_ack_nack(recv1, recv1.length, frame);
			if (ret != 0)
				return ret + _14443ACommApi.ISO14443A_BASE_ERROR;
//			Log.i("TXFRAME.tx_check_ack_nack", ret+"");
		} catch (FnIOException e) {
			System.out.print("aaa:"+e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}

	public static int ISO14443A_WriteReadFrame(TXFRAME frame) {
		try {
			return ISO14443A_Comm(frame, TX_GENERAL_FRAME);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
}
