#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <termios.h>


#define RETOUR_OK	0
#define RETOUR_ERROR	-1

int init_ser(char *ser_device);
int set_ser_param();
void flush_ser(void);
int shutdown_ser(void);
unsigned char get_1_char(void);

/* (fd == -1) shows that fd is not valid */
static int fd=-1;

int init_ser(char *ser_device)
{
    /* fd is static to this module but not to be seen outside */
    if ((fd=open(ser_device, O_RDWR | O_NOCTTY | O_NONBLOCK, 0)) == -1) {
        fprintf(stderr, "Error opening serial device !\n");
        return (RETOUR_ERROR);
    }
    else {
        if (set_ser_param(fd) == RETOUR_ERROR) {
            printf("Couldn't set serial parameters !\n");
            close(fd);
            fd=-1;
            return (RETOUR_ERROR);
        }
        else {
            /* printf("Sucessfully opened serial device !\n"); */
            return (RETOUR_OK);
        }
    }
}

int set_ser_param()
{
    int line;
    struct termios sgtty;

    if (tcgetattr(fd, &sgtty) < 0) {
        fprintf(stderr, "can't tcgetattr\n");
        fgetc(stdin);
        return (-1);
    }
    sgtty.c_iflag=(IMAXBEL | IGNBRK);
    sgtty.c_oflag=0;
    sgtty.c_lflag=0;
    sgtty.c_cflag=(CLOCAL | HUPCL | B9600 | CS8 | CREAD);
    sgtty.c_cflag &=~(PARENB | PARODD);
    sgtty.c_cc[VTIME]='\0';
    sgtty.c_cc[VMIN]='\0';
    if (tcsetattr(fd, TCSANOW, &sgtty) < 0) {
        fprintf(stderr, "can't tcsetattr\n");
        fgetc(stdin);
        return (RETOUR_ERROR);
    }

    flush_ser();

    return (RETOUR_OK);
}

void flush_ser(void)
{
#if 0
    ioctl(fd, TCFLSH, 0);
#endif
}

int shutdown_ser(void)
{
    if (close(fd) == -1) {
        fd=-1;
        return (RETOUR_ERROR);
    }
    else {
        fd=-1;
        return (RETOUR_OK);
    }
}

void put_1_char(unsigned char *data)
{
        write(fd, data, 1);
}

void put_n_chars(unsigned char *data, int n)
{
        write(fd, data, n);
}

unsigned char get_1_char(void)
{
    unsigned char recv_char;
    int r_code;

    do {
        /* wait for a char to arrive */
        r_code=read(fd, &recv_char, 1);
    }
    while (r_code != 1);

    return (recv_char);
}

static unsigned char buz_on[] ={'#', '0', '0', '1', 'B', 'U', 'Z', '1', 0xd};
static unsigned char buz_off[]={'#', '0', '0', '1', 'B', 'U', 'Z', '0', 0xd};

static unsigned char continous_on[]={'#', '0', '0', '1', 'C', 'O', 'N', '1', '1', '0', 0xd};

/* static unsigned char inventory[]={'#', '0', '0', '1', 'I', 'S', 'O', '0', '1', xxx, 0x0, 0x0, 0xd}; */

int main(int argc, char **argv)
{
	unsigned char received;
	unsigned char array_of_received_bytes[256];
	int nr_of_received_bytes;
	int i;

	init_ser(argv[1]);

#if 1
	/* printf("buz_on cmd has length: %d\n", sizeof(buz_on)); */
	printf("Switching buzzer on - press ENTER to switch off again !\n");
	put_n_chars(buz_on, sizeof(buz_on));

	fgetc(stdin);
	put_n_chars(buz_off, sizeof(buz_off));
	printf("Buzzer switched off again !\n");
#endif

#if 1
	printf("Device is now in continous mode !\n");
#endif

	put_n_chars(continous_on, sizeof(continous_on));

	nr_of_received_bytes=0;
	while (1) {
		received=get_1_char();
		array_of_received_bytes[nr_of_received_bytes]=received;
		nr_of_received_bytes++;
#if 0
		fprintf(stdout, "-%02x-", received);
#endif
		if (received == 0xd)
		{
#if 0
			printf("\n");
			printf("----------------|                              |--------\n");
#endif
			if (nr_of_received_bytes == 14)
			{
#if 0
				printf("This was a UID: ");
#endif
				printf("-");
				for (i=4; i < (4+7); i++)
				{
					fprintf(stdout, "%02x:", array_of_received_bytes[i]);
				}
				fprintf(stdout, "%02x-\n", array_of_received_bytes[4+7]);
			}
			nr_of_received_bytes=0;

		}
		fflush(stdout);
		
	}

	printf("Finishing now !\n");
	shutdown_ser();
}

