#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <time.h>
#include "../output/include/pcie_ecat.h"



void print_usage(const char *program_name) {
    printf("Usage: %s -d <index> [options]\n", program_name);
    printf("Options:\n");
    printf("  -d <index>  Device index (0-3, required)\n");
    printf("  -c <index>  CAN channel index (0-1, default: 0)\n");
    printf("  -h          Show this help message\n");
    printf("\nExamples:\n");
    printf("  %s -d 0         # Use device 0, channel 0\n", program_name);
    printf("  %s -d 1         # Use device 1, channel 0\n", program_name);
    printf("  %s -d 2 -c 1    # Use device 2, channel 1\n", program_name);
}

int main(int argc, char *argv[]) {
    printf("=== CANFD Library Interface Test ===\n\n");
    
    int ret = 0;
    int DevIndex = -1;    // 设备索引（初始化为-1表示未设置）
    int CanIndex = 0;     // CAN通道索引
    int dev_index_specified = 0; // 标记是否指定了设备索引

    int opt;
    while ((opt = getopt(argc, argv, "d:h")) != -1) {
        switch (opt) {
            case 'd':
                DevIndex = atoi(optarg);
                dev_index_specified = 1;
                if (DevIndex < 0 || DevIndex > 3) {
                    printf("Error: Device index must be between 0 and 3\n");
                    return -1;
                }
                break;
            case 'h':
                print_usage(argv[0]);
                return 0;
            default:
                print_usage(argv[0]);
                return -1;
        }
    }
    
    // 检查是否指定了设备索引
    if (!dev_index_specified) {
        printf("Error: Device index is required. Use -d option to specify.\n\n");
        print_usage(argv[0]);
        return -1;
    }
    
    // 再次验证设备索引的有效性
    if (DevIndex < 0 || DevIndex > 3) {
        printf("Error: Invalid device index %d. Must be between 0 and 3.\n\n", DevIndex);
        print_usage(argv[0]);
        return -1;
    }
    
    printf("Device Index: %d\n", DevIndex);
    printf("CAN Channel: %d\n\n", CanIndex);

    
    // 测试1: 打开设备
    printf("VCI_OpenDevice...\n");
    ret = ECAT_OpenDevice(DevIndex);
    if (ret != true) {
        printf("  Result: FAILED (no hardware or driver not loaded)\n\n");
        return -1;
    }
    printf(" Result: SUCCESS (device opened)\n\n");

    ECAT_CFG_T cfg = {0};
    printf("ECAT_GetDevCfg...\n");
    ret = ECAT_GetDevCfg(DevIndex, &cfg);
    if (ret != true) {
        printf("  Result: FAILED (no hardware or driver not loaded)\n\n");
        return -1;
    }
    printf(" Result: SUCCESS (app:%s  fw:%s)\n\n", cfg.app_Version, cfg.fw_Version);

    int addr = 0;
    int size = 4;
    uint8_t buff[256] = {0};
    printf("ECAT_GetInputBuff...\n");
    ret = ECAT_GetInputBuff(DevIndex, addr, size, buff);
    if (ret != 0) {
        printf("  Result: FAILED (no hardware or driver not loaded)\n\n");
    }
    printf(" Result: SUCCESS (%d bytes)\n\n", ret);
    printf("Data: ");
    for (int i = 0; i < ret; i++) {
        printf("%02x ", buff[i]);
    }
    printf("\n\n");

    printf("ECAT_SetOutputBuff...\n");
    ret = ECAT_SetOutputBuff(DevIndex, addr, size, buff);
    if (ret != 0) {
        printf("  Result: FAILED (no hardware or driver not loaded)\n\n");
        return -1;
    }
    printf(" Result: SUCCESS\n\n");
    printf("Data: ");
    for (int i = 0; i < size; i++) {
        printf("%02x ", buff[i]);
    }
    printf("\n\n");

    printf("ECAT_CloseDevice...\n");
    ret = ECAT_CloseDevice(DevIndex);
    if (ret != 0) {
        printf("  Result: FAILED (no hardware or driver not loaded)\n\n");
        return -1;
    }
    printf(" Result: SUCCESS\n\n");

    printf("=== Tests Completed ===\n");

    return 0;
}
