#ifndef CVEND_H_WNGPTXM7
#define CVEND_H_WNGPTXM7

#include <stdint.h>
#include <stdio.h>
enum message_type {
	MTYPE_VERSION = 0x02,
	MTYPE_VERSION_REPLY = 0x03,
	MTYPE_STATUS = 0x04,
	MTYPE_STATUS_REPLY = 0x05,
	MTYPE_HEARTBEAT = 0x07,
	MTYPE_STARTUP = 0x0f,
	MTYPE_RESET = 0x10,
	MTYPE_RESET_REPLY = 0x11,
	MTYPE_LEDS = 0x20,
	MTYPE_BUZZER = 0x22,
	MTYPE_CARD_RELEASE = 0x32,
	MTYPE_ABORT_CARD_HANDLING = 0x46,
	MTYPE_PUT_FILE = 0x96,
	MTYPE_PUT_FILE_REPLY = 0x97,
	MTYPE_GET_FILE = 0x98,
	MTYPE_GET_FILE_REPLY = 0x99,
	MTYPE_DELETE_FILE = 0x9a,
	MTYPE_DELETE_FILE_REPLY = 0x9b,
	MTYPE_FILE_INFO = 0x9c,
	MTYPE_FILE_INFO_REPLY = 0x9d,
	MTYPE_FILE_LIST = 0xa4,
	MTYPE_FILE_LIST_REPLY = 0xa5,
	MTYPE_SET_TIME = 0xaa,
	MTYPE_SET_TIME_REPLY = 0xab,
	MTYPE_ITSO_DATA = 0xac,
	MTYPE_ITSO_DATA_REPLY = 0xad,
	MTYPE_ITSO_CTRL = 0xae,
	MTYPE_ITSO_CTRL_REPLY = 0xaf,
	MTYPE_ITSO_READ = 0xb1,
	MTYPE_APDU_PROX = 0xb4,
	MTYPE_APDU_PROX_REPLY = 0xb5,
	MTYPE_SAM_CTRL = 0xb6,
	MTYPE_SAM_CTRL_REPLY = 0xb7,
	MTYPE_DESFIRE_READ = 0xb9,
	MTYPE_DESFIRE_STATUS = 0xba,
	MTYPE_DESFIRE_REMOVED = 0xbb,
	MTYPE_DESFIRE_COMMAND = 0xbc,
	MTYPE_DESFIRE_COMMAND_REPLY = 0xbd,
	MTYPE_UNHANDLED_CARD = 0xbe,
	MTYPE_EMV = 0xd0,
	MTYPE_EMV_STATUS = 0xd1,
	MTYPE_PROX_CARD_FUNCTION = 0xe4,
	MTYPE_PROX_CARD_FUNCTION_REPLY = 0xe5,
	MTYPE_SECURITY_SERVICES = 0xe8,
	MTYPE_SECURITY_SERVICES_REPLY = 0xe9,
	MTYPE_LOG = 0xed
};

enum card_type {
	CTYPE_VDV_KA = 0x04,
	CTYPE_MIFARE_CLASSIC = 0x05,
	CTYPE_ISO = 0x06,
	CTYPE_DESFIRE = 0x07,
	CTYPE_GIROGO = 0x08,
	CTYPE_ITSO = 0x09,
	CTYPE_ULTRALIGHT_C = 0x0a
};

enum function_status { FUNC_DISABLE = 0x00, FUNC_ENABLE = 0x01 };

typedef struct cvend_packet {
	uint8_t magic;
	uint8_t seq;
	uint8_t flags;
	uint8_t msg_type;
	uint16_t msg_len;
	uint8_t hdr_crc;
	uint8_t *msg_data;
	uint32_t msg_crc;
} cvend_packet;

const char *stringify_msg_type(uint8_t msg_type);

void cvend_init();
void cvend_lock();
void cvend_unlock();
cvend_packet *cvend_read(FILE *file);
cvend_packet *cvend_read_type(FILE *file, uint8_t msg_type);
void cvend_write(FILE *file, uint8_t msg_type, uint8_t *msg_data, uint16_t msg_len);
void cvend_free(cvend_packet *packet);
void print_packet(cvend_packet *packet);
void print_packet_short(char *brief, cvend_packet *packet);

#endif /* end of include guard: CVEND_H_WNGPTXM7 */
