-
Notifications
You must be signed in to change notification settings - Fork 7
/
io_test.c
executable file
·732 lines (463 loc) · 12.5 KB
/
io_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <aio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#define PORT 0xBAC0
#define MAXLINE 1500
int stn=0;
uint8_t bqr_whois[]={
0x81,0x0a,0x00,0x0c,0x01,0x20,
0xff,0xff,0x00,0xff,0x10,0x08
};
uint8_t brp_im[]={
0x81,0x0b,0x00,0x19,0x01,0x20,0xff,0xff,0x00,0xff,0x10,0x00,0xc4,0x02,0x00,0x03,
0xf1,0x22,0x04,0x00,0x91,0x03,0x22,0x01,0x04
};
uint8_t bqr_rp[]={
0x81,0x0a,0x00,0x11,0x01,0x04,
0x00,0x03,0x00,0x0c,0x0c,0x02,
//0x3f,0xff,0xfe,0x19,0x2c
//0x00,0x27,0x19,0x19,0x2c
//0x00,0x03,0xee,0x19,0x2c
0x00,0x03,0xeb,0x19,0x2c
//0x00,0x03,0xe8,0x19,0x2c
//0x00,0x03,0xf0,0x19,0x2c
};
#define DEVICE_IDL req_read_object_list[14]
#define DEVICE_IDM req_read_object_list[13]
#define DEVICE_IDH req_read_object_list[12]
uint8_t req_read_object_list[]={
0x81,0x0a,0x00,0x11,0x01,0x04,
0x02,0x75,0x02,0x0c,0x0c,0x02,
//0x00,0x00,0x0a,0x19,0x4c
//0x00,0x00,0x08,0x19,0x4c
0x00,0x00,0x17,0x19,0x4c
//0x00,0x03,0xee,0x19,0x4c
//0x00,0x03,0xeb,0x19,0x4c,0x29,0x00
};
uint8_t segment_ack[]={
0x81,0x0a,0x00,0x0a,0x01,0x00,
0x40,0x01,0x00,0x01
};
uint8_t is_ready_object_list =0;
uint32_t cnt_object_list =0;
uint8_t array_object_list[1024][4]={0};
uint8_t cmd_object_list[] ={
0x81,0x0a,0x00,0x13,0x01,0x04,0x02,
0x75,0x03,0x0e,0x0c,
0x02,0x81,0x00,0x00,
0x1e,0x09,0x08,0x1f
};
extern int32_t make_internet_address(int8_t * hostname,int32_t port,struct sockaddr_in *addrp);
int sockfd;
char hname[128]={0};
uint8_t buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
struct hostent *hent=NULL;
char *sip=NULL;
int8_t cmd_str[1024];
int32_t make_internet_address(int8_t * hostname,int32_t port,struct sockaddr_in *addrp)
{
struct hostent *hp;
bzero((void*)addrp,sizeof(struct sockaddr_in));
hp = gethostbyname(hostname);
if(hp == NULL)
return -1;
bcopy((void*)hp->h_addr,(void*)&addrp->sin_addr,hp->h_length);
addrp->sin_port = htons(port);
addrp->sin_family= AF_INET;
return 0;
}
uint32_t vvcnt=0;
void on_timer(int signum)
{
signal(SIGALRM,on_timer);
make_internet_address(sip,PORT,&cliaddr);
if((stn = sendto(sockfd,bqr_rp,sizeof(bqr_rp),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
vvcnt++;
if(vvcnt == 100)
{
vvcnt=0;
sleep(100);
}
//printf("hello timer\n");
}
int set_timer(uint32_t nms)
{
struct itimerval ts;
ts.it_interval.tv_sec = nms/1000;
ts.it_interval.tv_usec = (nms%1000)*1000L;
ts.it_value.tv_sec = nms/1000;
ts.it_value.tv_usec = (nms%1000)*1000L;
return setitimer(ITIMER_REAL,&ts,NULL);
}
//=========================================================================================
//
uint8_t list_array[1024][4]={0};
uint32_t rv_cnt =0;
void *read_udp(void *arg_r)
{
int n=0;
int len=0;
uint32_t bid=0;
uint32_t acnt=0;
uint32_t bcnt=0;
uint32_t ccnt=0;
uint32_t vcnt=0;
len = sizeof(cliaddr); //len is value/resuslt
FILE *fp;
fp = fopen("object-list.log","w");
if(fp == NULL)
{
printf("failed open object-list.log");
exit(EXIT_FAILURE);
}
while(1)
{
n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, ( struct sockaddr *) &cliaddr, &len);
//n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, ( struct sockaddr *) &cliaddr, sizeof(struct sockaddr));
#if 0
// printf("==========rec len is %d\n",n);
printf("rec data is \r\n");
for(int i=0;i<n;i++)
{
printf("0x%02x ",buffer[i]);
if((i + 1)%8 == 0)
printf("\r\n");
}
printf("\r\n");
#endif
//printf("==========rec len is %d\n",n);
//printf("\n==ip %s\n",(char*)inet_ntoa(cliaddr.sin_addr));
//printf("\n==rec len is %d\n",n);
if(buffer[12]== 0xc4 && buffer[13] == 0x02)
{
bid=(uint32_t)(buffer[14]<<16|buffer[15]<<8|buffer[16]);
if(bid == 4194302)
{
acnt++;
printf("I am %d\t%d\n",bid,acnt);
}
if(bid == 12)
{
bcnt++;
printf("I am %d\t%d\n",bid,bcnt);
}
if(bid == 1000)
{
ccnt++;
printf("I am %d\t%d\n",bid,ccnt);
}
printf("I am %d\n",bid);
bid=0;
}
if(buffer[10]== 0x10 && buffer[11] == 0x08)
{
//printf("I am 5006\n");
}
if(strcmp(buffer,"whois\n") == 0)
{
//printf("I am: %d\n",1009);
//printf("%s\r\n","world");
//write(pfd[1],ts,strlen(ts));
}
if(buffer[16] == 0x3e && buffer[27]==0x3f)
{
vcnt++;
printf("%c%c%c%c%c%c%c\t%d\n",buffer[20],buffer[21],buffer[22],buffer[23],buffer[24],buffer[25],buffer[26],vcnt);
}
int moffset =0;
//int foffset =18;
int foffset =23;
if(buffer[4]== 0x01 && buffer[5]==0x04)
{
printf("******rec object list\n");
printf("result saved to object-list.log\n");
while(foffset < n)
{
printf("Command is %x %x %x %x ",buffer[foffset],buffer[foffset+1],buffer[foffset+2],buffer[foffset+3]);
array_object_list[cnt_object_list][0]= buffer[foffset + 0];
array_object_list[cnt_object_list][1]= buffer[foffset + 1];
array_object_list[cnt_object_list][2]= buffer[foffset + 2];
array_object_list[cnt_object_list][3]= buffer[foffset + 3];
foffset +=5;
cnt_object_list ++;
printf("No of cnt_object_list: %d\n",cnt_object_list);
}
// if(is_ready_object_list == 0)
{
if((stn = sendto(sockfd,segment_ack,sizeof(segment_ack),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
}
is_ready_object_list ++;
#if 0
if((stn = sendto(sockfd,segment_ack,sizeof(segment_ack),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
#endif
}
if(buffer[4]== 0x01 && buffer[5]==0x00)// receive results of object lists
{
rv_cnt++;
printf("result is %02x %02x %02x %02x %02x cnt is %d\n",buffer[6],buffer[10],buffer[11],buffer[12],buffer[13],rv_cnt);
fprintf(fp,"result is %02x %02x %02x %02x %02x\n",buffer[6],buffer[10],buffer[11],buffer[12],buffer[13]);
fflush(fp);
}
if(strcmp(buffer,"tx-quit\n") == 0)
{
printf("good-bye\n");
exit(EXIT_SUCCESS);
}
memset(buffer,0,sizeof(buffer));
// sleep(1);
}
}
uint8_t cmd_buf[128]={0};
void *write_udp(void *arg_w)
{
int nrcmd=0;
int fd;
int m=0;
make_internet_address(sip,PORT,&cliaddr);
fd = open("udp-fifo",O_RDONLY | O_NONBLOCK);
while(1)
{
//nrcmd = read(fd,cmd_buf,sizeof(cmd_buf));
nrcmd = read(fd,cmd_buf,PIPE_BUF);
//printf("====== nrcmd is %d\n",nrcmd);
//printf("cmd_buf is %s\n",cmd_buf);
//
//
if(nrcmd == 0)
{
// sleep(1);
}
if(strcmp(cmd_buf,"send rv\n") == 0)
{
if((stn = sendto(sockfd,bqr_rp,sizeof(bqr_rp),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
// printf("-------send rv\n");
m++;
#if 0
if(m>100)
exit(0);
#endif
// sleep(3);
}
if(strcmp(cmd_buf,"send rol\n") == 0)
{
if((stn = sendto(sockfd,req_read_object_list,sizeof(req_read_object_list),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
printf("-------send rol\n");
}
if(is_ready_object_list )
{
uint32_t icnt=0;
for(icnt=0;icnt <= cnt_object_list;icnt++)
{
cmd_object_list[11] = array_object_list[icnt][0];
cmd_object_list[12] = array_object_list[icnt][1];
cmd_object_list[13] = array_object_list[icnt][2];
cmd_object_list[14] = array_object_list[icnt][3];
if((stn = sendto(sockfd,cmd_object_list,sizeof(cmd_object_list),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
// printf("icnt is %d\n",icnt);
sleep(1);
//usleep(50000);
}
is_ready_object_list = 0;
}
memset(cmd_buf,0,sizeof(cmd_buf));
//sleep(1);
}
}
//=========================================================================================
int main(int argc, char* argv[])
{
if(argc <3)
{
printf("err:parameters\n");
exit(EXIT_FAILURE);
}
sip = argv[2];
uint32_t temp_id = atoi(argv[3]);
DEVICE_IDL= temp_id & 0x000000ff;
DEVICE_IDM= (temp_id & 0x0000ff00)>>8;
DEVICE_IDH= (temp_id & 0x00ff0000)>>16;
gethostname(hname,sizeof(hname));
hent = gethostbyname(hname);
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
int optval =1;
//setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST|SO_REUSEADDR,&optval,sizeof(int));
struct timeval timeout;
timeout.tv_sec = 6;
timeout.tv_usec = 0;
//setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
const char localip[]="192.168.2.10";
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
//servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr = INADDR_ANY;
//servaddr.sin_addr.s_addr = inet_addr(localip);
servaddr.sin_port = htons(PORT);
//servaddr.sin_port = PORT;
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
#if 0
set_timer(atoi(argv[1]));
signal(SIGALRM,on_timer);
#endif
memset(buffer,0,sizeof(buffer));
pthread_t thread_id_read;
pthread_t thread_id_write;
pthread_create(&thread_id_read,NULL,read_udp,NULL);
pthread_create(&thread_id_write,NULL,write_udp,NULL);
pthread_join(thread_id_read,NULL);
pthread_join(thread_id_write,NULL);
printf("after thread\r\n");
exit(0);
//==================================================================================================================
#if 0
int fpid;
int n=0;
fpid = fork();
if(fpid == -1)
{
perror("fork failed\n");
exit(EXIT_FAILURE);
}
else if(fpid == 0)
{
//execl("./myudp-tx.exe","myudp-tx.exe",(char*)0);
#if 1
struct timespec tp;
tp.tv_sec =0;
tp.tv_nsec =500000;
int cnt=0;
make_internet_address(sip,PORT,&cliaddr);
//make_internet_address("192.168.2.22",PORT,&cliaddr);
//
#if 0
if((stn = sendto(sockfd,req_read_object_list,sizeof(req_read_object_list),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
sleep(300);
#endif
sleep(3);
printf("start sending\n");
set_timer(atoi(argv[1]));
signal(SIGALRM,on_timer);
while(1);
#endif
}
#endif
//============================================================================================================================================
//===========================================================================================================================================
#if 0
char *ts="hello";
int xmcnt=0;
int rcnt=0;
uint32_t bid=0;
uint32_t acnt=0;
uint32_t bcnt=0;
uint32_t ccnt=0;
uint32_t vcnt=0;
while(1)
{
#if 1
rcnt = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, ( struct sockaddr *) &cliaddr, &len);
//n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, ( struct sockaddr *) &cliaddr, sizeof(struct sockaddr));
printf("\n==ip %s\n",(char*)inet_ntoa(cliaddr.sin_addr));
printf("\n==rec len is %d\n",rcnt);
if(buffer[12]== 0xc4 && buffer[13] == 0x02)
{
bid=(uint32_t)(buffer[14]<<16|buffer[15]<<8|buffer[16]);
if(bid == 4194302)
{
acnt++;
printf("I am %d\t%d\n",bid,acnt);
}
if(bid == 12)
{
bcnt++;
printf("I am %d\t%d\n",bid,bcnt);
}
if(bid == 1000)
{
ccnt++;
printf("I am %d\t%d\n",bid,ccnt);
}
printf("I am %d\n",bid);
bid=0;
}
#endif
if(buffer[10]== 0x10 && buffer[11] == 0x08)
{
//printf("I am 5006\n");
}
if(strcmp(buffer,"whois\n") == 0)
{
//printf("I am: %d\n",1009);
//printf("%s\r\n","world");
//write(pfd[1],ts,strlen(ts));
}
if(buffer[16] == 0x3e && buffer[27]==0x3f)
{
vcnt++;
printf("%c%c%c%c%c%c%c\t%d\n",buffer[20],buffer[21],buffer[22],buffer[23],buffer[24],buffer[25],buffer[26],vcnt);
}
if(strcmp(buffer,"tx-quit\n") == 0)
{
printf("good-bye\n");
exit(EXIT_SUCCESS);
}
memset(buffer,0,sizeof(buffer));
// sleep(1);
}
#endif
//======================================================================================================================================
}