Skip to content
Snippets Groups Projects
main.c 24.3 KiB
Newer Older
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <byteswap.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include "main.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>

//
#define PORT 8888
#define TIMELIST_SIZE 100
#define PPBUGGER_SIZE 30000
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed

// Define the structure to represent a packet
struct Packet {
    uint32_t header;                         // 0x48454144
    uint16_t trigger_type;                    // 0, normal trigger; 1 trigger in water
    uint16_t sn_alert;                        // 0, no alert; 1, alert
    uint64_t timestamp;                      // 8 ns timeStamp
    uint32_t trigger_counter;                // trigger count, server can check it
    uint16_t drop_trigger_count;             // drop_count cause network congestion
    uint16_t masked_trig_count;              // mask trigger caused dead time, dead time(1000 ns)
    uint32_t reserve;                        // reserve
    uint32_t tailer;                         // 0x5461696C
};

// Function declarations
uint64_t htonll(uint64_t value);
uint64_t ntohll(uint64_t value);
uint64_t gloabl_time_offset = 0x02f0000000000000;
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
// Global variables
struct Packet packets;

//Gl vector
int event_rd = 0;
int size = (PPBUGGER_SIZE + 1) * 16;
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
int file_num = 0;
int event_waitflag = 1;
int triggercnt = 0;
uint32_t last_trigger_time = 0;

// 初始化时间序列数组
double timeList[TIMELIST_SIZE] = {0};
// int timeIndex = 0;
int timeFilled = 0;

shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
// int last_event_rd = 0;
// first event
// 1 = 0x0 ~ 0x20000000 
// 0 = 0x20000000 ~ 0x40000000

// Function to convert a 64-bit unsigned integer from host byte order to network byte order
uint64_t htonll(uint64_t value) {
    // Check if the system is little-endian
    const int num = 42;
    if (*(const char *)&num == 42) {
    // Convert from little-endian to big-endian manually
    return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
    } else {
    // System is big-endian, no conversion needed
    return value;
    }
}

// Function to convert a 64-bit unsigned integer from network byte order to host byte order
uint64_t ntohll(uint64_t value) {
    // Check if the system is little-endian
    const int num = 42;
    if (*(const char *)&num == 42) {
    // Convert from big-endian to little-endian manually
    return ((uint64_t)ntohl(value & 0xFFFFFFFF) << 32) | ntohl(value >> 32);
    } else {
    // System is big-endian, no conversion needed
    return value;
    }
}

uint64_t combine_uint32(uint32_t high, uint32_t low) {
    return ((uint64_t)high << 32) | low;
}

double fitness(double N, double T){
    return N * log(N / T);
}

double* BayesianBlock(double* timeList, int evtNum, double priorGamma, int evtStepNum, int* changePointCount) {
    double prior = priorGamma;
    int stepNum = evtNum / evtStepNum;

    double* best = (double*)malloc(stepNum * sizeof(double));
    int* last = (int*)malloc(stepNum * sizeof(int));
    int* blocks = (int*)malloc(stepNum * sizeof(int));
    int i;
    for (i = 0; i < stepNum; i++) {
        best[i] = 0;
        last[i] = 0;
        blocks[i] = 0;
    }

    double beginTime = timeList[0];
    double AR, AR_max, Fr, N, T;
    int r_max, blockNum, blockNum_max;
    
    int R;
    for (R = 0; R < stepNum; R++) {
        if (R == 0) {
            N = (R + 1) * evtStepNum;
            T = (timeList[R * evtStepNum] + timeList[(R + 1) * evtStepNum]) / 2.0 - beginTime;
            AR_max = fitness(N, T) - prior;
            r_max = 0;
            blockNum_max = 1;
        } else if (R == stepNum - 1) {
            int r;
            for (r = 0; r < R; r++) {
                if (r == 0) {
                    N = (R + 0.5) * evtStepNum;
                    T = timeList[R * evtStepNum] - beginTime;
                    Fr = fitness(N, T);
                    AR = Fr - prior;
                    AR_max = AR;
                    blockNum = 1;
                    blockNum_max = blockNum;
                    r_max = r;
                } else {
                    N = (R + 0.5 - r) * evtStepNum;
                    T = timeList[R * evtStepNum] - (timeList[r * evtStepNum] + timeList[(r + 1) * evtStepNum]) / 2.0;
                    Fr = fitness(N, T);
                    AR = Fr + best[r - 1] - prior;
                    blockNum = blocks[r - 1] + 1;
                    if (AR > AR_max) {
                        AR_max = AR;
                        r_max = r;
                        blockNum_max = blockNum;
                    }
                }
            }
        } else {
            int r;
            for (r = 0; r < R; r++) {
                if (r == 0) {
                    N = (R + 1) * evtStepNum;
                    T = (timeList[R * evtStepNum] + timeList[(R + 1) * evtStepNum]) / 2.0 - beginTime;
                    Fr = fitness(N, T);
                    AR = Fr - prior;
                    AR_max = AR;
                    blockNum = 1;
                    blockNum_max = blockNum;
                    r_max = r;
                } else {
                    N = (R + 1 - r) * evtStepNum;
                    T = (timeList[R * evtStepNum] + timeList[(R + 1) * evtStepNum]) / 2.0 - (timeList[r * evtStepNum] + timeList[(r + 1) * evtStepNum]) / 2.0;
                    Fr = fitness(N, T);
                    AR = Fr + best[r - 1] - prior;
                    blockNum = blocks[r - 1] + 1;
                    if (AR > AR_max) {
                        AR_max = AR;
                        r_max = r;
                        blockNum_max = blockNum;
                    }
                }
            }
        }
        best[R] = AR_max;
        last[R] = r_max;
        blocks[R] = blockNum_max;
    }

    int maxChangePoints = stepNum + 1;
    double* changePoint = (double*)malloc(maxChangePoints * sizeof(double));
    *changePointCount = 0;
    changePoint[(*changePointCount)++] = timeList[stepNum * evtStepNum - 1];
    int lastChangePointStep = last[stepNum - 1];

    while (lastChangePointStep != 0) {
        double cp = (timeList[lastChangePointStep * evtStepNum] + timeList[(lastChangePointStep + 1) * evtStepNum]) / 2.0;
        changePoint[(*changePointCount)++] = cp;
        lastChangePointStep = last[lastChangePointStep];
    }
    changePoint[(*changePointCount)++] = timeList[0];
    
    int i2;
    for (i2 = 0; i2 < *changePointCount / 2; i2++) {
        double temp = changePoint[i2];
        changePoint[i2] = changePoint[*changePointCount - 1 - i2];
        changePoint[*changePointCount - 1 - i2] = temp;
    }

    free(best);
    free(last);
    free(blocks);

    return changePoint;
}

double* calculateRates(double* time, int timeSize, double* cp, int cpSize, int* ratesSize) {
    double* rates = (double*)malloc((cpSize - 1) * sizeof(double));
    *ratesSize = 0;
    
    int i;
    for (i = 0; i < cpSize - 1; ++i) {
        double numerator = 0.0;
        double denominator = cp[i + 1] - cp[i];
        
        int j;
        for (j = 0; j < timeSize; ++j) {
            if (time[j] >= cp[i] && time[j] < cp[i + 1]) {
                numerator += 1;
            }
        }

        if (denominator != 0) {
            double rate = numerator / denominator;
            rates[(*ratesSize)++] = rate;
        }
    }

    return rates;
}

int checkIncreasing(double* rates, int ratesSize) {
    if (ratesSize == 0) {
        return 0;
    }
    int i;
    for (i = 1; i < ratesSize; ++i) {
        if (rates[i] > rates[i - 1]) {
            return 1;
        }
    }
    return 0;
}
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed

void makeFolder(char *folderName) {
    // 创建文件夹
    int status = mkdir(folderName, 0777); // 创建文件夹权限设置为777,即读、写、执行权限
    if (status == -1) {
    printf("创建文件夹失败\n");
    exit(1);

void read_data(int fd,int *c2h_align_mem,const char *file_name, int offset, int client_fd, int file_num, int delay_time, float prior)
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
{
    if (fd<0)
    {
        printf("open failed");
        printf("%d",fd);
    }
    else
    {
        printf("open c2h\n");
    }
    if (file_num > 0){
        FILE *record_fp = fopen(file_name, "wb");
        lseek(fd,offset,SEEK_SET);
        read(fd, c2h_align_mem, size);
        // fwrite(c2h_align_mem, size, 1, record_fp);
        // read(fd, c2h_align_mem, size);
        fwrite(c2h_align_mem + 4, size - 16, 1, record_fp);
        
        float triggerRate = 1000. * 1e9 / (8 * (c2h_align_mem[4 * 1001 + 1] - c2h_align_mem[4 * 1 + 1]));
        printf("trigger rate : %f Hz\n",triggerRate);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed

        packets.header = htonl(0x48454144);
        packets.trigger_type = htonl(0x0000);
        packets.sn_alert = htonl(0x0000);
        // packets.timestamp = htonll(125 * i * time_interval);
        // packets.trigger_counter = htonl(i);
        packets.drop_trigger_count = htonl(0x00000000);
        packets.masked_trig_count = htonl(0x0000);
        packets.reserve = htonl(0x00000000);
        packets.tailer = htonl(0x5441494c);

        int i;
        for (i = 1; i < PPBUGGER_SIZE + 1; i++)
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
        {   
            uint32_t lowtime = c2h_align_mem[4 * i + 1];
            int alert = 0;
            if ((lowtime - last_trigger_time) < 500){
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                continue;
            }else{
                uint32_t hightime = c2h_align_mem[4 * i + 2];
                // printf("%02X ",c2h_align_mem[4 * i - 1]);
                // packets.timestamp = htonll(c2h_align_mem[4 * i - 1]);
                // uint64_t time = combine_uint32(hightime, lowtime) - delay_time;
                // time = time + gloabl_time_offset;
                uint64_t time = combine_uint32(hightime, lowtime);
                // printf("time:%02lX\n",time);
                // printf("time: %lu\n",time);
                
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                time = time + gloabl_time_offset;
                time = time - (uint64_t)delay_time;
                double time_second = time *1e-9* 8;
                // timeList[timeIndex] = (double)time;
                // timeIndex = (timeIndex + 1) % TIMELIST_SIZE;
                // if (timeIndex == 0) {
                //     timeFilled = 1;
                // }

                if (timeFilled) {

                    int j0;
                    for (j0 = 0; j0 < TIMELIST_SIZE - 1; j0++){
                        timeList[j0] = timeList[ j0 + 1];
                    }
                    timeList[TIMELIST_SIZE - 1] = (double)time_second;
                    // 执行检测算法
                    int changePointCount;
                    double* changePoints = BayesianBlock(timeList, TIMELIST_SIZE, prior, 1, &changePointCount);

                    // 计算速率
                    int ratesSize;
                    double* rates = calculateRates(timeList, TIMELIST_SIZE, changePoints, changePointCount, &ratesSize);
                    
                    // int j;
                    // for (j = 0; j < ratesSize; j++){
                    //     printf("%f\n", rates[j]);
                    // }

                    // 检查速率是否递增
                    if (checkIncreasing(rates, ratesSize)) {
                        printf("block rate: \n");
                        int i0;
                        for (i0 = 0; i0 < ratesSize; i0++){
                            printf("%9f Hz\n", rates[i0]);
                        } 
                        alert = 1;
                        printf("Increasing rates detected!\n");
                    }

                    free(changePoints);
                    free(rates);
                }
                else{
                    timeList[triggercnt] = (double)time_second;
                    if (triggercnt == TIMELIST_SIZE){
                        timeFilled = 1;
                    }
                }
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                packets.timestamp = htonll(time);
                packets.trigger_counter = htonl(triggercnt);
                packets.sn_alert = htonl(alert);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                ssize_t bytes_sent = send(client_fd, &packets, sizeof(packets), 0);
                triggercnt = triggercnt + 1;
                last_trigger_time = lowtime;
            }
        }
    }
    else{
        lseek(fd,offset,SEEK_SET);
        read(fd, c2h_align_mem, size);
    }
    printf("\n%s\n",file_name);
}

void read_data_pure(int fd,int *c2h_align_mem,const char *file_name, int offset, int file_num, int delay_time, float prior)
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
{
    if (fd<0)
    {
        printf("open failed");
        printf("%d",fd);
    }
    else
    {
        printf("open c2h\n");
    }
    if (file_num > 0){
        FILE *record_fp = fopen(file_name, "wb");
        lseek(fd,offset,SEEK_SET);
        read(fd, c2h_align_mem, size);
        fwrite(c2h_align_mem + 4, size - 16, 1, record_fp);
        
        float triggerRate = 1000. * 1e9 / (8 * (c2h_align_mem[4 * 1001 + 1] - c2h_align_mem[4 * 1 + 1]));
        printf("trigger rate : %f Hz\n",triggerRate);

        int i;
        for (i = 1; i < PPBUGGER_SIZE + 1; i++)
        {   
            uint32_t lowtime = c2h_align_mem[4 * i + 1];
            int alert = 0;
            if ((lowtime - last_trigger_time) < 500){
                continue;
            }else{
                uint32_t hightime = c2h_align_mem[4 * i + 2];
                // printf("lowtime:%02X\n",lowtime);
                // printf("hightime:%02X\n",hightime);
                // packets.timestamp = htonll(c2h_align_mem[4 * i - 1]);

                uint64_t time = combine_uint32(hightime, lowtime);

                // printf("time:%02lX\n",time);
                // printf("time: %lu\n",time);
                
                time = time + gloabl_time_offset;
                time = time - (uint64_t)delay_time;

                double time_second = time *1e-9* 8;   
                // printf("time_second: %f\n",time_second);

                // timeList[timeIndex] = (double)time_second;
                // timeIndex = (timeIndex + 1) % TIMELIST_SIZE;
                // if (timeIndex == 0) {
                //     timeFilled = 1;
                // }

                if (timeFilled) {

                    int j0;
                    for (j0 = 0; j0 < TIMELIST_SIZE - 1; j0++){
                        timeList[j0] = timeList[ j0 + 1];
                    }
                    timeList[TIMELIST_SIZE - 1] = (double)time_second;

                    // 执行检测算法
                    int changePointCount;
                    double* changePoints = BayesianBlock(timeList, TIMELIST_SIZE, prior, 1, &changePointCount);

                    // printf("Check timelist\n");
                    // int j;
                    // for (j = 0; j < TIMELIST_SIZE; j++){
                    //     printf("%0.9f s \n", timeList[j]);
                    // }

                    // printf("Check changePoints\n");

                    // 计算速率
                    int ratesSize;
                    double* rates = calculateRates(timeList, TIMELIST_SIZE, changePoints, changePointCount, &ratesSize);
                    int i0;
                    //    for (i0 = 0; i0 < ratesSize; i0++){
                    //         printf("%9f \n", rates[i0]);
                    //     } 

                    // 检查速率是否递增
                    if (checkIncreasing(rates, ratesSize)) {
                        printf("block rate: \n");
                       for (i0 = 0; i0 < ratesSize; i0++){
                            printf("%9f Hz\n", rates[i0]);
                        } 
                        alert = 1;
                        printf("Increasing rates detected!\n");
                    }

                    free(changePoints);
                    free(rates);
                }else{
                    timeList[triggercnt] = (double)time_second;
                    if (triggercnt == TIMELIST_SIZE){
                        timeFilled = 1;
                    }
                }
                triggercnt++;
                last_trigger_time = lowtime;
            }
        }
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
        // int i;
        // for (i = 1; i < 10; i++)
        // {   
        //     // uint64_t time = (uint64_t)c2h_align_mem[4 * i - 1];
        //     printf("%02X ",c2h_align_mem[4 * i + 1]);
        // }
        printf("\n%s\n",file_name);
    }
    else{
        lseek(fd,offset,SEEK_SET);
        read(fd, c2h_align_mem, size);
    }
}

void *c2h_data_process(int fd_c2h,int fd_usr, int *c2h_align_mem, int client_fd, int delay_time, float prior)
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
{   

    
    //读取的数据写文件
    char file[256];
    char file_path[]="data/";
    char file_pack[]=".bin";
    sprintf(file,"%.100s%d%.30s",file_path,file_num,file_pack);
    int offset;

    // read restart
    char *reg_wr_0[]={"0","/dev/xdma0_user","0x4000","w","1"};
    char *reg_wr_1[]={"0","/dev/xdma0_user","0x4000","w","0"};

    //read event

    char *reg_rd[]={"0","/dev/xdma0_user","0x4000","w"};

    event_rd = reg_rw(4,reg_rd,fd_usr);
    switch(event_rd){
        case 1: 
            if (event_waitflag == 0){
                offset = 0x0;
                read_data(fd_c2h, c2h_align_mem, file, offset, client_fd, file_num, delay_time, prior);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                event_waitflag = 1;
                printf("read done ram1!\n");
                file_num = file_num + 1;
                break;
            }
            else{
                break;
            }
        case 0:
            if (event_waitflag == 1){
                offset = 0x800000;
                read_data(fd_c2h, c2h_align_mem, file, offset, client_fd, file_num, delay_time, prior);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                event_waitflag = 0;
                printf("read done ram2!\n");
                file_num = file_num + 1;
                break;
            }
        default: 
            break;
    }
}

void *c2h_data_process_pure(int fd_c2h,int fd_usr, int *c2h_align_mem, int delay_time, float prior)
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
{
    //读取的数据写文件
    char file[256];
    char file_path[]="data/";
    char file_pack[]=".bin";
    sprintf(file,"%.100s%d%.30s",file_path,file_num,file_pack);
    int offset;

    // read restart
    char *reg_wr_0[]={"0","/dev/xdma0_user","0x4000","w","1"};
    char *reg_wr_1[]={"0","/dev/xdma0_user","0x4000","w","0"};

    //read event

    char *reg_rd[]={"0","/dev/xdma0_user","0x4000","w"};

    event_rd = reg_rw(4,reg_rd,fd_usr);
    switch(event_rd){
        case 1: 
            if (event_waitflag == 0){
                offset = 0x0;
                read_data_pure(fd_c2h, c2h_align_mem, file, offset, file_num, delay_time, prior);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                event_waitflag = 1;
                printf("read done ram1!\n");
                file_num = file_num + 1;
                break;
            }
            else{
                break;
            }
        case 0:
            if (event_waitflag == 1){
                offset = 0x800000;
                read_data_pure(fd_c2h, c2h_align_mem, file, offset, file_num,delay_time, prior);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                event_waitflag = 0;
                printf("read done ram2!\n");
                file_num = file_num + 1;
                break;
            }
        default: 
            break;
    }
}


int main(int argc, char *argv[])
{   
    srand(time(NULL));
    if (argc < 4)
    {
        fprintf(stderr, "Usage: %s <daq_connect> <file_num> <delaytime> <prior(SN)>\n", argv[0]);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
        exit(EXIT_FAILURE);
    }

    int daq_connect = atoi(argv[1]);
    int File_NUM = atoi(argv[2]);
    int delay_time =atoi(argv[3]);
    float prior =atoi(argv[4]);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed

    if (daq_connect == 0){
        setbuf(stdout,NULL);
        int fd_c2h = open("/dev/xdma0_c2h_0",O_RDWR);
        int fd_usr = open("/dev/xdma0_user",O_RDWR);
        char *reg_restart0[]={"0","/dev/xdma0_user","0x4000","w","1"};
        char *reg_restart1[]={"0","/dev/xdma0_user","0x4000","w","0"};
        reg_rw(5, reg_restart0,fd_usr);
        reg_rw(5, reg_restart1,fd_usr);
        
        while(1) {
            if (file_num < File_NUM) {
                int *c2h_align_mem = (int*)malloc(size);
                c2h_data_process_pure(fd_c2h,fd_usr,c2h_align_mem,delay_time, prior);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                // printf ("c2h_align_mem[0]: %d", c2h_align_mem[0]);
                free(c2h_align_mem);
            } else {
                break;
            }
        }
        printf ("\nWrite done! %d files",file_num);
    }else if (daq_connect == 1){

        // // Instantiate the server address
        // struct sockaddr_in server_addr;
        // int client_fd;

        // // Create TCP socket
        // if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        //     perror("socket failed");
        //     exit(EXIT_FAILURE);
        // }

        // // Set the server address struct 
        // memset(&server_addr, 0, sizeof(server_addr));
        // server_addr.sin_family = AF_INET;
        // server_addr.sin_port = htons(PORT);


        // // // Set the server address
        // // if (inet_pton(AF_INET, "10.7.50.123", &server_addr.sin_addr) <= 0) {
        // //     perror("inet_pton failed");
        // // }

        // // Set the server address
        // if (inet_pton(AF_INET, "10.7.35.42", &server_addr.sin_addr) <=Quick Access 0) {
        //     perror("inet_pton failed");
        // }

        // // Connect to server
        // if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        //     perror("connect failed");
        //     exit(EXIT_FAILURE);
        // }

        // printf("Connect to server\n");

        // setbuf(stdout,NULL);

        // int fd_c2h = open("/dev/xdma0_c2h_0",O_RDWR);
        // int fd_usr = open("/dev/xdma0_user",O_RDWR);
        // char *reg_restart0[]={"0","/dev/xdma0_user","0x4000","w","1"};
        // char *reg_restart1[]={"0","/dev/xdma0_user","0x4000","w","0"};
        // reg_rw(5, reg_restart0,fd_usr);
        // reg_rw(5, reg_restart1,fd_usr);
        
        // while(1) {
        //     if (file_num < File_NUM) {
        //         int *c2h_align_mem = (int*)malloc(size);
        //         c2h_data_process(fd_c2h,fd_usr,c2h_align_mem, client_fd, delay_time);
        //         // printf ("c2h_align_mem[0]: %d", c2h_align_mem[0]);
        //         free(c2h_align_mem);
        //     } else {
        //         break;
        //     }
        // }
        // printf ("\nWrite done! %d files",file_num);
        // close(client_fd);


        // Instantiate the server address
        struct sockaddr_in server_addr, client_addr;
        int server_fd, client_fd;
        socklen_t client_len;

        // Create TCP socket
        if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
            perror("socket failed");
            exit(EXIT_FAILURE);
        }

        // Set the server address struct
        memset(&server_addr, 0, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = INADDR_ANY; // Listen on all available interfaces
        server_addr.sin_port = htons(PORT);

        // Bind socket to the address and port
        if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
            perror("bind failed");
            exit(EXIT_FAILURE);
        }

        // Put the socket in passive mode to listen for incoming connections
        if (listen(server_fd, 3) < 0) { // 3 is the maximum length of the pending connections queue
            perror("listen failed");
            exit(EXIT_FAILURE);
        }

        printf("Server listening on port %d\n", PORT);

        // Accept incoming connection
        client_len = sizeof(client_addr);
        if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len)) < 0) {
        perror("accept failed");
        exit(EXIT_FAILURE);
        }

        printf("Connection accepted from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

        setbuf(stdout,NULL);

        int fd_c2h = open("/dev/xdma0_c2h_0",O_RDWR);
        int fd_usr = open("/dev/xdma0_user",O_RDWR);
        char *reg_restart0[]={"0","/dev/xdma0_user","0x4000","w","1"};
        char *reg_restart1[]={"0","/dev/xdma0_user","0x4000","w","0"};
        reg_rw(5, reg_restart0,fd_usr);
        reg_rw(5, reg_restart1,fd_usr);
        
        while(1) {
            if (file_num < File_NUM) {
                int *c2h_align_mem = (int*)malloc(size);
                c2h_data_process(fd_c2h,fd_usr,c2h_align_mem, client_fd, delay_time, prior);
shishen_xian@sjtu.edu.cn's avatar
shishen_xian@sjtu.edu.cn committed
                // printf ("c2h_align_mem[0]: %d", c2h_align_mem[0]);
                free(c2h_align_mem);
            } else {
                break;
            }
        }
        printf ("\nWrite done! %d files",file_num);
        close(client_fd);
        close(server_fd);
    }else{
        perror("daq_connect error");
        exit(EXIT_FAILURE);
    }

    return 0;
}