#include "cvend.h"

int main(int argc, char *argv[])
{
	char *file = argc > 1 ? argv[1] : "/dev/ttymxc3";
	uint8_t desfire_enable[] = { 0x00, CTYPE_DESFIRE, 0x01, FUNC_ENABLE };
	// uint8_t desfire_get_version[] = { 0x60 };
	uint8_t desfire_select_application[] = { 0x5a, 0xF2, 0x10, 0xE0 };
	uint8_t desfire_fid_list[] = { 0xf5, 0x00 };
	uint8_t desfire_read[] = { 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	uint8_t desfire_free[] = { 0x6e };

	cvend_init(file);
	cvend_write(MTYPE_PROX_CARD_FUNCTION, desfire_enable, 4);

	while (1) {
		cvend_packet *packet;
		packet = cvend_read();

		// if (packet->msg_type != MTYPE_HEARTBEAT)
		print_packet(packet);

		if (packet->msg_type == MTYPE_DESFIRE_READ) {
			//cvend_write(MTYPE_DESFIRE_INTERACT, desfire_get_version, 1);
			cvend_write(MTYPE_DESFIRE_COMMAND, desfire_select_application, 4);
			cvend_packet *response;
			do {
				response = cvend_read();
			} while (!response);
			print_packet(response);
			cvend_write(MTYPE_DESFIRE_COMMAND, desfire_fid_list, 1);
			cvend_free(response);
			cvend_write(MTYPE_DESFIRE_COMMAND, desfire_read, 8);
			cvend_write(MTYPE_DESFIRE_COMMAND, desfire_free, 1);
		}

		cvend_free(packet);
	}
	return 0;
}
