Newer
Older
#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 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;
// Global variables
struct Packet packets;
//Gl vector
int event_rd = 0;
int size = (PPBUGGER_SIZE + 1) * 16;
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;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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;
}
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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;
}
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)
{
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);
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++)
{
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("%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);
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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;
}
}
packets.timestamp = htonll(time);
packets.trigger_counter = htonl(triggercnt);
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)
{
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++)
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
{
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;
}
}
// 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)
{
//读取的数据写文件
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);
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);
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)
{
//读取的数据写文件
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);
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);
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]);
exit(EXIT_FAILURE);
}
int daq_connect = atoi(argv[1]);
int File_NUM = atoi(argv[2]);
int delay_time =atoi(argv[3]);
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);
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// 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);
// 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;
}