#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/sysmacros.h> /* major() minor() */
#include <signal.h>
#include <errno.h>
#include <limits.h>
// #include <assert.h>

#include <xpci3xxx.h>

#include "errorlib.h"
#include "findboards.h"
#include "errormsg.h"
#include "input.h"

//--------------------------------------------------------------------------------
int main (int argc, char** argv)
{
        /* detect available boards */
        int * xpci3xxx_card_fd;
        int xpci3xxx_card_number = 0;
        {
                xpci3xxx_card_number = apci_find_boards("xpci3xxx", &xpci3xxx_card_fd);
                printf("total: %d cards\n",xpci3xxx_card_number);
                if(xpci3xxx_card_number < 0)
                        exit(1);
        }

	{
                int i_ioctl_return = 0;
                int i = 0;
                uint32_t arg_dw_SerialNumber = 0;
		uint32_t arg_dw_FirmwareVersion = 0;

                for (i = 0; i < xpci3xxx_card_number; i++)
                {
			/* Read serial number of the board */
                        i_ioctl_return = call_ioctl(xpci3xxx_card_fd[i], CMD_xpci3xxx_ReadEepromHeader_SerialNumber, (long) &arg_dw_SerialNumber);

                        if (i_ioctl_return < 0)
                        {
                                perror("ioctl");
                                return 2;
                        }
                        else
                        {
                                if (i_ioctl_return > 0)
                                {
                                        printf("CMD_xpci3xxx_ReadEepromHeader_SerialNumber error: %d\n", i_ioctl_return);
                                        return 1;
                                }
                        }

			printf("\tSerial number of board %d: A-D %d | 0x%x\n", i, arg_dw_SerialNumber, arg_dw_SerialNumber);

			/* Get firmware version */
                        i_ioctl_return = call_ioctl(xpci3xxx_card_fd[i], CMD_xpci3xxx_GetFirmwareVersion, (long) &arg_dw_FirmwareVersion);

                        if (i_ioctl_return < 0)
                        {
                                perror("ioctl");
                                return 2;
                        }
                        else
                        {
                                if (i_ioctl_return > 0)
                                {
                                        printf("CMD_xpci3xxx_FirmwareVersion error: %d\n", i_ioctl_return);
                                        return 1;
                                }
                        }

			char car1 = (char) (arg_dw_FirmwareVersion >> 24);
                	char car2 = (char) (arg_dw_FirmwareVersion >> 16);
        	        char car3 = (char) (arg_dw_FirmwareVersion >> 8);
	                char car4 = (char) (arg_dw_FirmwareVersion);

                        printf("\tFirmware version of board %d (A-D %d): 0x%x | %c%c%c%c \n", i, arg_dw_SerialNumber, arg_dw_FirmwareVersion, car1, car2, car3, car4);
                }
        }

        return 0;
}


