00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <stdlib.h>
00033 #include <pthread.h>
00034 #include <errno.h>
00035 #include <time.h>
00036 #include <math.h>
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <arpa/inet.h>
00041 #include <sys/signal.h>
00042 #include <unistd.h>
00043 #include <gtk/gtk.h>
00044 #include "function.h"
00045 #include "ServerFunction.h"
00046
00047 #define max_data_rate 200
00048 #define MAX_STRING_SIZE 5000
00049 #define BACKLOG 10
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 int df_pmu_id, df_fdf, df_af, df_pf, df_pn, df_phnmr, df_annmr, df_dgnmr, protocol_choice, PORT;
00073 int df_data_frm_size = 0, df_data_rate = 0, old_data_rate = 0, cfg_size, hdr_size=0;
00074 int sa1 = 0, sa2 = 0, sa3 = 0, sa4 = 0, sa5 = 0, count = 0, pmuse=0, sc1 = 0;
00075 int UDP_sockfd, TCP_sockfd, yes=1, TCP_sin_size, UDP_addr_len;
00076 int udp_cfg_flag = 0, tcp_cfg_flag = 0, tcp_data_flag = 0, udp_data_flag = 0;
00077 int err, errno, udp_data_trans_off = 1, tcp_data_trans_off = 1, stat_flag = 0;
00078 long int df_soc, fsec = 0, curnt_soc = 0, prev_soc = 0, udp_send_thrd_id = 0, tcp_send_thrd_id = 0,soc1,soc2;
00079 unsigned char data_frm[MAX_STRING_SIZE], df_temp[2], df_temp_1[4];
00080 struct sockaddr_in UDP_my_addr, TCP_my_addr, UDP_addr, TCP_addr;
00081 struct sigaction sa;
00082 pthread_t UDP_thread, TCP_thread;
00083 pthread_t pmu_server_thread;
00084 uint16_t df_chk;
00085
00086
00087
00088
00089
00090
00091
00092
00093 void frame_size()
00094 {
00095
00096 int index = 2, format;
00097 unsigned char cline[MAX_STRING_SIZE];
00098 static const char filename[] = "cfg2.bin";
00099 FILE *file_1;
00100
00101
00102 file_1 = fopen (filename,"rb");
00103 if (file_1 == NULL)
00104 {
00105 perror (filename);
00106 }
00107 else
00108 {
00109 memset(cline,'\0',sizeof(cline));
00110 fread(cline, sizeof(unsigned char), sizeof(cline), file_1);
00111 fclose(file_1);
00112
00113
00114 df_temp[0] = cline[index++];
00115 df_temp[1] = cline[index++];
00116 cfg_size = c2i(df_temp);
00117 cline[cfg_size] = '\0';
00118
00119
00120 df_temp[0] = cline[index++];
00121 df_temp[1] = cline[index++];
00122 df_pmu_id = c2i(df_temp);
00123 index = index + 32;
00124
00125
00126 df_temp[0] = cline[index++];
00127 df_temp[1] = cline[index++];
00128 format = c2i(df_temp);
00129
00130
00131 if(format == 15)
00132 {
00133 df_fdf=1, df_af=1, df_pf=1, df_pn=1;
00134 }
00135 else if(format == 14)
00136 {
00137 df_fdf=1, df_af=1, df_pf=1, df_pn=0;
00138 }
00139 else if(format == 13)
00140 {
00141 df_fdf=1, df_af=1, df_pf=0, df_pn=1;
00142 }
00143 else if(format == 12)
00144 {
00145 df_fdf=1, df_af=1, df_pf=0, df_pn=0;
00146 }
00147 else if(format == 11)
00148 {
00149 df_fdf=1, df_af=0, df_pf=1, df_pn=1;
00150 }
00151 else if(format == 10)
00152 {
00153 df_fdf=1, df_af=0, df_pf=1, df_pn=0;
00154 }
00155 else if(format == 9)
00156 {
00157 df_fdf=1, df_af=0, df_pf=0, df_pn=1;
00158 }
00159 else if(format == 8)
00160 {
00161 df_fdf=1, df_af=0, df_pf=0, df_pn=0;
00162 }
00163 else if(format == 7)
00164 {
00165 df_fdf=0, df_af=1, df_pf=1, df_pn=1;
00166 }
00167 else if(format == 6)
00168 {
00169 df_fdf=0, df_af=1, df_pf=1, df_pn=0;
00170 }
00171 else if(format == 5)
00172 {
00173 df_fdf=0, df_af=1, df_pf=0, df_pn=1;
00174 }
00175 else if(format == 4)
00176 {
00177 df_fdf=0, df_af=1, df_pf=0, df_pn=0;
00178 }
00179 else if(format == 3)
00180 {
00181 df_fdf=0, df_af=0, df_pf=1, df_pn=1;
00182 }
00183 else if(format == 2)
00184 {
00185 df_fdf=0, df_af=0, df_pf=1, df_pn=0;
00186 }
00187 else if(format == 1)
00188 {
00189 df_fdf=0, df_af=0, df_pf=0, df_pn=1;
00190 }
00191 else
00192 {
00193 df_fdf=0, df_af=0, df_pf=0, df_pn=0;
00194 }
00195
00196
00197 df_temp[0] = cline[index++];
00198 df_temp[1] = cline[index++];
00199 df_phnmr = c2i(df_temp);
00200
00201
00202 df_temp[0] = cline[index++];
00203 df_temp[1] = cline[index++];
00204 df_annmr = c2i(df_temp);
00205
00206
00207 df_temp[0] = cline[index++];
00208 df_temp[1] = cline[index++];
00209 df_dgnmr = c2i(df_temp);
00210
00211
00212 index = index + (16*df_phnmr) + (16*df_annmr) + (256*df_dgnmr) + (4*df_phnmr) + (4*df_annmr) + (4*df_dgnmr) + 4;
00213 df_temp[0] = cline[index++];
00214 df_temp[1] = cline[index++];
00215 df_data_rate = c2i(df_temp);
00216
00217
00218 df_data_frm_size = 0;
00219 df_data_frm_size = df_data_frm_size + 18;
00220
00221
00222 if (df_pf == 0)
00223 {
00224 df_data_frm_size = df_data_frm_size + (4*df_phnmr);
00225 }
00226 else
00227 {
00228 df_data_frm_size = df_data_frm_size + (8*df_phnmr);
00229 }
00230
00231
00232 if (df_af == 0)
00233 {
00234 df_data_frm_size = df_data_frm_size + (2*df_annmr);
00235 }
00236 else
00237 {
00238 df_data_frm_size = df_data_frm_size + (4*df_annmr);
00239 }
00240
00241
00242 if (df_fdf == 0)
00243 {
00244 df_data_frm_size = df_data_frm_size + 4;
00245 }
00246 else
00247 {
00248 df_data_frm_size = df_data_frm_size + 8;
00249 }
00250
00251
00252 df_data_frm_size = df_data_frm_size + (2*df_dgnmr);
00253 printf("\n-> Data Frame size = %d\n", df_data_frm_size);
00254
00255 }
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265 void generate_data_frame()
00266 {
00267
00268 int indx = 0, j, temp_i, freq, dfreq, dsw = 0, ka = 1;
00269 int analog[3] = {100, 1000, 10000}, rand_ph, rand_ang;
00270 long int freq_f, dfreq_f, analog_f;
00271 float phasor = 100.85, angle, result;
00272 char *a1 = "1", *a2 = "2", *a3 = "3", *a4 = "4", *a5 = "5", achar[2];
00273 static const char filename_1[] = "change.bin";
00274 FILE *file1;
00275
00276
00277 file1 = fopen (filename_1,"r");
00278 {
00279 memset(achar,'\0',sizeof(achar));
00280 fgets (achar, sizeof(achar), file1);
00281 achar[strlen(achar)] = '\0';
00282 }
00283 fclose (file1);
00284
00285
00286
00287 if (strcmp(achar, a1) == 0)
00288 {
00289 sa1 = 1;
00290 }
00291 else if (strcmp(achar, a2) == 0)
00292 {
00293 sa2 = 1;
00294 file1 = fopen (filename_1,"w");
00295 {
00296 fprintf(file1, "%s", "0");
00297 }
00298 fclose (file1);
00299 }
00300 else if (strcmp(achar, a3) == 0)
00301 {
00302 sa3 = 1;
00303 }
00304 else if (strcmp(achar, a4) == 0)
00305 {
00306 sa4 = 1;
00307 }
00308 else if (strcmp(achar, a5) == 0)
00309 {
00310 sa5 = 1;
00311 file1 = fopen (filename_1,"w");
00312 {
00313 fprintf(file1, "%s", "0");
00314 }
00315 fclose (file1);
00316 frame_size();
00317 }
00318 memset(data_frm,'\0',sizeof(df_data_frm_size));
00319
00320
00321 data_frm[indx++] = 0xAA;
00322 data_frm[indx++] = 0x01;
00323
00324
00325 i2c(df_data_frm_size, df_temp);
00326 B_copy(data_frm, df_temp, indx, 2);
00327 indx = indx + 2;
00328
00329
00330 i2c(df_pmu_id, df_temp);
00331 B_copy(data_frm, df_temp, indx, 2);
00332 indx = indx + 2;
00333
00334
00335 time_t tm = time(NULL);
00336 df_soc = tm;
00337 li2c(df_soc, df_temp_1);
00338 B_copy(data_frm, df_temp_1, indx, 4);
00339 indx = indx + 4;
00340
00341 if (sc1 == 0)
00342 {
00343 soc1 = df_soc;
00344 soc2 = df_soc;
00345 sc1 = 1;
00346 }
00347 else
00348 {
00349 soc2 = df_soc;
00350 }
00351
00352
00353 if ((soc2 > soc1) || fsec > ((df_data_rate-1) * (1000000/df_data_rate)))
00354 {
00355 fsec = 0;
00356 }
00357 li2c(fsec, df_temp_1);
00358 B_copy(data_frm, df_temp_1, indx, 4);
00359 indx = indx + 4;
00360 soc1 = soc2;
00361 fsec = fsec+(1000000/df_data_rate);
00362
00363
00364 time_t curnt_soc = time(NULL);
00365 if(pmuse == 0)
00366 {
00367 prev_soc = curnt_soc;
00368 }
00369 if ((sa5 == 1) || (sa5 == 2))
00370 {
00371 printf("\t-> STAT word Changed due to Configuration Change.");
00372 data_frm[indx++] = 0x04;
00373 data_frm[indx++] = 0x00;
00374 sa5 = 2;
00375 }
00376 else if ((sa2 == 1) || (sa2 == 2))
00377 {
00378 printf("\t-> STAT word Changed due to PMU Error.");
00379 data_frm[indx++] = 0x40;
00380 data_frm[indx++] = 0x00;
00381 sa2 = 2;
00382 }
00383 else if((curnt_soc-prev_soc) > 2)
00384 {
00385 printf("\t-> STAT word Changed due to PMU SYNC Error.");
00386 data_frm[indx++] = 0x20;
00387 data_frm[indx++] = 0x00;
00388 file1 = fopen (filename_1,"w");
00389 {
00390 fprintf(file1, "%s", "0");
00391 }
00392 fclose (file1);
00393 }
00394 else if (sa3 == 1)
00395 {
00396 printf("\t-> STAT word Changed due to Data sorting.");
00397 data_frm[indx++] = 0x10;
00398 data_frm[indx++] = 0x00;
00399 sa3 = 0;
00400 file1 = fopen (filename_1,"w");
00401 {
00402 fprintf(file1, "%s", "0");
00403 }
00404 fclose (file1);
00405 }
00406 else if (sa4 == 1)
00407 {
00408 printf("\t-> STAT word Changed due to PMU trigger detected.");
00409 data_frm[indx++] = 0x08;
00410 data_frm[indx++] = 0x00;
00411 sa4 = 0;
00412 file1 = fopen (filename_1,"w");
00413 {
00414 fprintf(file1, "%s", "0");
00415 }
00416 fclose (file1);
00417 }
00418 else if (sa1 == 1)
00419 {
00420 printf("\t-> STAT word Changed due to Data Invalidation.");
00421 data_frm[indx++] = 0x80;
00422 data_frm[indx++] = 0x00;
00423 sa1 = 0;
00424 file1 = fopen (filename_1,"w");
00425 {
00426 fprintf(file1, "%s", "0");
00427 }
00428 fclose (file1);
00429 }
00430 else
00431 {
00432
00433 data_frm[indx++] = 0x00;
00434 data_frm[indx++] = 0x00;
00435 }
00436 prev_soc = curnt_soc;
00437 pmuse = 1;
00438
00439
00440 if(df_pf == 0)
00441 {
00442
00443 if(df_pn == 0)
00444 {
00445 for(j=0; j<df_phnmr; j++)
00446 {
00447 rand_ph = rand() % 9 + 1;
00448 rand_ang = rand() % 29 + 1;
00449 angle = 120.89;
00450
00451 if(ka == 1)
00452 {
00453 phasor = phasor + rand_ph;
00454 angle = angle + rand_ang;
00455 ka = 0;
00456 }
00457 else
00458 {
00459 phasor = phasor - rand_ph;
00460 angle = angle - rand_ang;
00461 ka = 1;
00462 }
00463
00464 angle = (angle*3.1415)/180;
00465 result = cos(angle)*phasor;
00466 temp_i = 65535+result;
00467 i2c(temp_i, df_temp);
00468 B_copy(data_frm, df_temp, indx, 2);
00469 indx = indx + 2;
00470
00471 temp_i = sin(angle)*phasor;
00472 i2c(temp_i, df_temp);
00473 B_copy(data_frm, df_temp, indx, 2);
00474 indx = indx + 2;
00475 }
00476 }
00477 else
00478 {
00479
00480 for(j=0; j<df_phnmr; j++)
00481 {
00482 rand_ph = rand() % 9 + 1;
00483 rand_ang = rand() % 29 + 1;
00484 angle = 120.89;
00485
00486 if(ka == 1)
00487 {
00488 phasor = phasor + rand_ph;
00489 angle = angle + rand_ang;
00490 ka = 0;
00491 }
00492 else
00493 {
00494 phasor = phasor - rand_ph;
00495 angle = angle - rand_ang;
00496 ka = 1;
00497 }
00498
00499 angle = ((angle*3.1415)/180)*100000;
00500 temp_i = phasor*100000;
00501 i2c(temp_i, df_temp);
00502 B_copy(data_frm, df_temp, indx, 2);
00503 indx = indx + 2;
00504
00505 temp_i = angle;
00506 i2c(temp_i, df_temp);
00507 B_copy(data_frm, df_temp, indx, 2);
00508 indx = indx + 2;
00509 }
00510 }
00511 }
00512 else
00513 {
00514 if(df_pn == 0)
00515 {
00516
00517 for(j=0; j<df_phnmr; j++)
00518 {
00519 rand_ph = rand() % 9 + 1;
00520 rand_ang = rand() % 29 + 1;
00521 angle = 120.89;
00522
00523 if(ka == 1)
00524 {
00525 phasor = phasor + rand_ph;
00526 angle = angle + rand_ang;
00527 ka = 0;
00528 }
00529 else
00530 {
00531 phasor = phasor - rand_ph;
00532 angle = angle - rand_ang;
00533 ka = 1;
00534 }
00535
00536 angle = (angle*3.1415)/180;
00537 result = cos(angle)*phasor;
00538 result = 65535+result;
00539 f2c(result, df_temp_1);
00540 B_copy(data_frm, df_temp_1, indx, 4);
00541 indx = indx + 4;
00542
00543 result = sin(angle)*phasor;
00544 f2c(result, df_temp_1);
00545 B_copy(data_frm, df_temp_1, indx, 4);
00546 indx = indx + 4;
00547 }
00548 }
00549 else
00550 {
00551
00552 for(j=0; j<df_phnmr; j++)
00553 {
00554 rand_ph = rand() % 9 + 1;
00555 rand_ang = rand() % 29 + 1;
00556 angle = 120.89;
00557
00558 if(ka == 1)
00559 {
00560 phasor = phasor + rand_ph;
00561 angle = angle + rand_ang;
00562 ka = 0;
00563 }
00564 else
00565 {
00566 phasor = phasor - rand_ph;
00567 angle = angle - rand_ang;
00568 ka = 1;
00569 }
00570
00571 angle = (angle*3.1415)/180;
00572 result = phasor;
00573 f2c(result, df_temp_1);
00574 B_copy(data_frm, df_temp_1, indx, 4);
00575 indx = indx + 4;
00576
00577 result = angle;
00578 f2c(result, df_temp_1);
00579 B_copy(data_frm, df_temp_1, indx, 4);
00580 indx = indx + 4;
00581 }
00582 }
00583 }
00584
00585
00586 if(df_fdf == 0)
00587 {
00588
00589 freq = (rand() % 24 + 1)*100;
00590 i2c(freq, df_temp);
00591 B_copy(data_frm, df_temp, indx, 2);
00592 indx = indx + 2;
00593
00594 dfreq = 0;
00595 i2c(dfreq, df_temp);
00596 B_copy(data_frm, df_temp, indx, 2);
00597 indx = indx + 2;
00598 }
00599 else
00600 {
00601
00602 freq_f = (rand() % 24 + 1)*100;
00603 li2c(freq_f, df_temp_1);
00604 B_copy(data_frm, df_temp_1, indx, 4);
00605 indx = indx + 4;
00606
00607 dfreq_f = 0;
00608 i2c(dfreq_f, df_temp_1);
00609 B_copy(data_frm, df_temp_1, indx, 4);
00610 indx = indx + 4;
00611 }
00612
00613
00614 if(df_af == 0)
00615 {
00616 for(j=0, ka=0; ka<df_annmr; j++, ka++)
00617 {
00618 if (j == 3) j = 0;
00619 i2c(analog[j], df_temp);
00620 B_copy(data_frm, df_temp, indx, 2);
00621 indx = indx + 2;
00622 }
00623 }
00624 else
00625 {
00626 for(j=0, ka=0; ka<df_annmr; j++, ka++)
00627 {
00628 if (j == 3) j = 0;
00629 analog_f = analog[j];
00630 li2c(analog_f, df_temp_1);
00631 B_copy(data_frm, df_temp_1, indx, 4);
00632 indx = indx + 4;
00633 }
00634 }
00635
00636
00637 for(j=1; j<=df_dgnmr; j++)
00638 {
00639 i2c(dsw, df_temp);
00640 B_copy(data_frm, df_temp, indx, 2);
00641 indx = indx + 2;
00642 }
00643
00644
00645 df_chk = compute_CRC(data_frm,indx);
00646 data_frm[indx++] = (df_chk >> 8) & ~(~0<<8);
00647 data_frm[indx++] = (df_chk ) & ~(~0<<8);
00648
00649
00650 printf("[");
00651 for(j=0; j<indx; j++)
00652 {
00653 printf("%x",data_frm[j]);
00654 }
00655 printf("]-(%d)\n", indx);
00656
00657 }
00658
00659
00660
00661
00662
00663
00664
00665
00666 void* udp_send_data()
00667 {
00668 int udp_data_rate = 1000000/df_data_rate, i=1;
00669
00670 udp_data_flag = 1;
00671 udp_data_trans_off = 1;
00672 udp_send_thrd_id = pthread_self();
00673
00674 while(1)
00675 {
00676 printf("%d. ", i);
00677 if ( i == df_data_rate) i=0;
00678
00679
00680 generate_data_frame();
00681
00682 if (sendto(UDP_sockfd, data_frm, df_data_frm_size, 0, (struct sockaddr *)&UDP_addr,sizeof(UDP_addr)) == -1)
00683 {
00684 perror("sendto");
00685 }
00686 i++;
00687 usleep(udp_data_rate);
00688 }
00689
00690 }
00691
00692
00693
00694
00695
00696
00697
00698
00699 void* pmu_udp()
00700 {
00701
00702 int n, start1_u = 0, ind, id_pdc;
00703 char udp_command[18];
00704 unsigned char c, line [MAX_STRING_SIZE];
00705 static const char filename1[] = "cfg2.bin";
00706 static const char filename2[] = "header.bin";
00707 FILE *file_1;
00708 pthread_t udp_send;
00709
00710
00711 while(1)
00712 {
00713 usleep(1000);
00714 if((file_1=fopen("cfg2.bin", "rb"))!=NULL)
00715 {
00716 fclose(file_1);
00717 printf("CFG File Found.\n");
00718 break;
00719 }
00720 else
00721 continue;
00722 }
00723 frame_size();
00724
00725 while(1)
00726 {
00727 memset(udp_command,'\0',18);
00728 ind = 2;
00729
00730 if ((recvfrom(UDP_sockfd, udp_command, 18, 0, (struct sockaddr *)&UDP_addr, (socklen_t *)&UDP_addr_len)) == -1)
00731 {
00732 perror("recvfrom");
00733 exit(1);
00734 }
00735 else
00736 {
00737 c = udp_command[1];
00738 c <<= 1;
00739 c >>= 5;
00740
00741 if(c == 0x04)
00742 {
00743 df_temp[0] = udp_command[4];
00744 df_temp[1] = udp_command[5];
00745 id_pdc = c2i (df_temp);
00746
00747 if(id_pdc == df_pmu_id)
00748 {
00749 c = udp_command[15];
00750
00751 if((c & 0x05) == 0x05)
00752 {
00753 printf("\n\n\n-> Configuration Frame request is received.\n");
00754 file_1 = fopen (filename1,"rb");
00755
00756 if (file_1 == NULL)
00757 {
00758 perror (filename1);
00759 printf("\n-> Sorry, Configuration Frame is not available...!!!\n");
00760 exit(1);
00761 }
00762 else
00763 {
00764 memset(line,'\0',sizeof(line));
00765 fread(line, sizeof(unsigned char), sizeof(line), file_1);
00766 fclose(file_1);
00767
00768
00769 df_temp[0] = line[ind++];
00770 df_temp[1] = line[ind];
00771 cfg_size = c2i(df_temp);
00772 line[cfg_size] = '\0';
00773
00774
00775 if (sendto(UDP_sockfd,line, cfg_size, 0, (struct sockaddr *)&UDP_addr,sizeof(UDP_addr)) == -1)
00776 {
00777 perror("sendto");
00778 }
00779
00780 udp_cfg_flag = 1;
00781 sa5 = 0;
00782 sa2 = 0;
00783 ind = cfg_size - 4;
00784 df_temp[0] = line[ind++];
00785 df_temp[1] = line[ind];
00786 df_data_rate = c2i(df_temp);
00787
00788 if (start1_u > 0)
00789 {
00790 if(old_data_rate != df_data_rate)
00791 {
00792
00793 n = pthread_cancel(udp_send_thrd_id);
00794 if (n == 0)
00795 {
00796 if( (err = pthread_create(&udp_send,NULL,udp_send_data,NULL)))
00797 {
00798 perror(strerror(err));
00799 exit(1);
00800 }
00801 }
00802 }
00803 }
00804
00805 printf("\n-> The Configuration frame [%d Bytes] is sent to requested PDC.\n", cfg_size);
00806 memset(line,'\0',sizeof(line));
00807 old_data_rate = df_data_rate;
00808 start1_u = 1;
00809 }
00810 }
00811
00812 else if((c & 0x03) == 0x03)
00813 {
00814 printf("\n\n\n-> Header frame request is Received.\n");
00815 file_1 = fopen (filename2,"rb");
00816
00817 if (file_1 == NULL)
00818 {
00819 printf("\n-> Sorry, Header Frame is not available....!!!\n");
00820 }
00821 else
00822 {
00823 memset(line,'\0',sizeof(line));
00824 fread(line, sizeof(unsigned char), sizeof(line), file_1);
00825 fclose(file_1);
00826
00827
00828 df_temp[0] = line[2];
00829 df_temp[1] = line[3];
00830 hdr_size = c2i(df_temp);
00831 line[hdr_size] = '\0';
00832
00833
00834 if (sendto(UDP_sockfd,line, hdr_size, 0, (struct sockaddr *)&UDP_addr,sizeof(UDP_addr)) == -1)
00835 {
00836 perror("sendto");
00837 }
00838
00839 printf("\n-> The Header Frame is sent to requested PDC.\n");
00840 }
00841 }
00842
00843 else if((c & 0x01) == 0x01)
00844 {
00845 printf("\n\n\n-> Turn OFF data transmission request is Received.\n");
00846
00847 if (udp_data_flag == 1)
00848 {
00849
00850 n = pthread_cancel(udp_send_thrd_id);
00851 if (n == 0)
00852 {
00853 udp_data_flag = 0;
00854 udp_data_trans_off = 0;
00855 udp_send_thrd_id = 0;
00856 pmuse = 1;
00857 printf("\n-> Data Transmission OFF & Waiting.....\n");
00858 }
00859 else
00860 {
00861 printf("\n Data sending thread is unable to kill \n");
00862 }
00863 }
00864 else if (udp_cfg_flag == 0)
00865 {
00866 printf("\n-> Data Transmission not started yet...!!!\n");
00867 }
00868 else
00869 {
00870 printf("\n-> Data Transmission is Already in OFF mode...!!!\n");
00871 }
00872 }
00873
00874 else if((c & 0x02) == 0x02)
00875 {
00876 printf("\n\n\n-> Turn ON data transmission request is Received.\n");
00877
00878
00879 if((udp_data_trans_off == 0) || (udp_cfg_flag == 1 && udp_data_flag == 0))
00880 {
00881 if (udp_send_thrd_id == 0)
00882 {
00883
00884 if( (err = pthread_create(&udp_send,NULL,udp_send_data,NULL)))
00885 {
00886 perror(strerror(err));
00887 exit(1);
00888 }
00889 }
00890 else
00891 {
00892 printf("\n-> Data already in sending mode...!!!\n");
00893 }
00894 }
00895 else if(udp_data_flag == 1)
00896 {
00897 printf("\n-> Data already in sending mode...!!!\n");
00898 }
00899 else
00900 {
00901 printf("\n-> Data cannot be sent as Command frame for CFG is not received...!!!\n");
00902 }
00903 }
00904
00905 else if((c & 0x04) == 0x04)
00906 {
00907 printf("\n\n\n-> Configuration frame-1 request is Received.\n");
00908 file_1 = fopen (filename1,"rb");
00909
00910 if (file_1 == NULL)
00911 {
00912 printf("\n-> Sorry, Configuration Frame-1 is not available...!!!\n");
00913 }
00914 else
00915 {
00916 memset(line,'\0',sizeof(line));
00917 fread(line, sizeof(unsigned char), sizeof(line), file_1);
00918 fclose(file_1);
00919
00920
00921 df_temp[0] = line[ind++];
00922 df_temp[1] = line[ind++];
00923 cfg_size = c2i(df_temp);
00924 line[cfg_size] = '\0';
00925 if (sendto(UDP_sockfd,line, cfg_size, 0, (struct sockaddr *)&UDP_addr,sizeof(UDP_addr)) == -1)
00926 {
00927 perror("sendto");
00928 }
00929 printf("\n-> The Configuration Frame-1[%d] is sent to request PDC.\n", cfg_size);
00930 }
00931 }
00932 }
00933
00934 else
00935 {
00936 printf("\n-> Received Command Frame not from authentic PDC...!!!\n");
00937 continue;
00938 }
00939 }
00940
00941 else
00942 {
00943 printf("\n-> Received Frame is not a command frame...!!!\n");
00944 continue;
00945 }
00946 }
00947
00948 }
00949 }
00950
00951
00952
00953
00954
00955
00956
00957
00958 void* tcp_send_data(void * newfd)
00959 {
00960 int new_fd = (int) newfd, i=1;
00961 int tcp_data_rate = 1000000/df_data_rate;
00962
00963 tcp_data_flag = 1;
00964 tcp_data_trans_off = 1;
00965 tcp_send_thrd_id = pthread_self();
00966
00967 while(1)
00968 {
00969 printf("%d. ", i);
00970 if ( i == df_data_rate) i=0;
00971
00972
00973 generate_data_frame();
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984 i++;
00985 usleep(tcp_data_rate);
00986 }
00987
00988 }
00989
00990
00991
00992
00993
00994
00995
00996
00997 void* new_pmu_tcp(void * nfd)
00998 {
00999
01000 int new_fd,n,sin_size,start_t=0,ind,id_pdc,start_u=0;
01001 char tcp_command[18];
01002 unsigned char c, line[MAX_STRING_SIZE];
01003 static const char filename1[] = "cfg2.bin";
01004 static const char filename2[] = "header.bin";
01005 FILE *file_1;
01006 pthread_t tcp_send;
01007
01008 sin_size = sizeof(struct sockaddr_in);
01009 new_fd = (int)nfd;
01010
01011 while (1)
01012 {
01013 memset(tcp_command,36,'\0');
01014 ind = 2;
01015
01016 if((errno = recv(new_fd,tcp_command,36,0)) == -1)
01017 {
01018
01019 printf("\nPDC Connection Closed\n");
01020 tcp_data_flag = 0;
01021 tcp_data_trans_off = 0;
01022 tcp_send_thrd_id = 0;
01023 pthread_cancel(tcp_send_thrd_id);
01024 pthread_exit(0);
01025 }
01026 else if(errno == 0)
01027 {
01028
01029 printf("\nPDC Connection Closed.....PMU TCP Server Listening\n");
01030 pthread_cancel(tcp_send_thrd_id);
01031 tcp_data_flag = 0;
01032 tcp_data_trans_off = 0;
01033 tcp_send_thrd_id = 0;
01034 pthread_exit(0);
01035 }
01036 else
01037 {
01038 if (start_u == 0)
01039 {
01040
01041 frame_size();
01042 start_u = 1;
01043 }
01044
01045 c = tcp_command[1];
01046 c <<= 1;
01047 c >>= 5;
01048
01049 if(c == 0x04)
01050 {
01051 df_temp[0] = tcp_command[4];
01052 df_temp[1] = tcp_command[5];
01053 id_pdc = c2i (df_temp);
01054
01055 if(id_pdc == df_pmu_id)
01056 {
01057 c = tcp_command[15];
01058 if((c & 0x05) == 0x05)
01059 {
01060 printf("\n\n\n-> Configuration Frame request is received.\n");
01061 file_1 = fopen (filename1,"rb");
01062
01063 if (file_1 == NULL)
01064 {
01065 perror (filename1);
01066 printf("\n-> Sorry, Configuration Frame is not available...!!!\n");
01067 exit(1);
01068 }
01069 else
01070 {
01071 memset(line,'\0',sizeof(line));
01072 fread(line, sizeof(unsigned char), sizeof(line), file_1);
01073 fclose(file_1);
01074
01075
01076 df_temp[0] = line[ind++];
01077 df_temp[1] = line[ind];
01078 cfg_size = c2i(df_temp);
01079
01080
01081 if (send(new_fd,line, cfg_size, 0)== -1)
01082 {
01083 perror("sendto");
01084 }
01085
01086 tcp_cfg_flag = 1;
01087 sa5 = 0;
01088 sa2 = 0;
01089 ind = cfg_size - 4;
01090 df_temp[0] = line[ind++];
01091 df_temp[1] = line[ind];
01092 df_data_rate = c2i(df_temp);
01093
01094 if (start_t > 0)
01095 {
01096 if(old_data_rate != df_data_rate)
01097 {
01098
01099 n = pthread_cancel(tcp_send_thrd_id);
01100 if (n == 0)
01101 {
01102 if( (err = pthread_create(&tcp_send,NULL,tcp_send_data,(void *)new_fd)))
01103 {
01104 perror(strerror(err));
01105 exit(1);
01106 }
01107 }
01108 }
01109 }
01110
01111 printf("\n-> The Configuration frame [%d Bytes] is sent to requested PDC.\n", cfg_size);
01112 memset(line,'\0',sizeof(line));
01113 old_data_rate = df_data_rate;
01114 start_t = 1;
01115 }
01116 }
01117
01118 else if((c & 0x03) == 0x03)
01119 {
01120 printf("\n-> Header Frame request is received.\n");
01121 file_1 = fopen(filename2,"rb");
01122
01123 if (file_1 == NULL)
01124 {
01125 printf("\n-> Sorry, Header frame is not available....!!!\n");
01126 }
01127 else
01128 {
01129 memset(line,'\0',sizeof(line));
01130 fread(line, sizeof(unsigned char), sizeof(line), file_1);
01131 fclose(file_1);
01132
01133
01134 df_temp[0] = line[2];
01135 df_temp[1] = line[3];
01136 hdr_size = c2i(df_temp);
01137 line[hdr_size] = '\0';
01138
01139
01140 if (send(new_fd,line, hdr_size, 0)== -1)
01141 {
01142 perror("sendto");
01143 }
01144
01145 printf("\n-> The Headed frame is sent to requested PDC.\n");
01146 }
01147 }
01148
01149 else if((c & 0x01) == 0x01)
01150 {
01151 printf("\n\n\n-> Turn OFF data transmission request is Received.\n");
01152
01153 if (tcp_data_flag == 1)
01154 {
01155 n = pthread_cancel(tcp_send_thrd_id);
01156 if (n == 0)
01157 {
01158 tcp_data_flag = 0;
01159 tcp_data_trans_off = 0;
01160 tcp_send_thrd_id = 0;
01161 pmuse = 1;
01162 printf("\n-> Data Transmission OFF & Waiting.....\n");
01163 }
01164 else
01165 {
01166 printf("\n Data sending thread unable to kill \n");
01167 }
01168 }
01169 else if (tcp_cfg_flag == 0)
01170 {
01171 printf("\n-> Data Transmission not started yet...!!!\n");
01172 }
01173 else
01174 {
01175 printf("\n-> Data Transmission is Already in OFF mode...!!!\n");
01176 }
01177 }
01178
01179 else if((c & 0x02) == 0x02)
01180 {
01181 printf("\n-> Turn ON data transmission request is Received.\n");
01182
01183
01184 if ((tcp_data_trans_off == 0) || (tcp_cfg_flag == 1 && tcp_data_flag == 0))
01185 {
01186 if (tcp_send_thrd_id == 0)
01187 {
01188
01189 if( (err = pthread_create(&tcp_send,NULL,tcp_send_data,(void *)new_fd)))
01190 {
01191 perror(strerror(err));
01192 exit(1);
01193 }
01194 }
01195 else
01196 {
01197 printf("\n-> Data already in sending mode...!!!\n");
01198 }
01199 }
01200 else if(tcp_data_flag == 1)
01201 {
01202 printf("\n-> Data already in sending mode...!!!\n");
01203 }
01204 else
01205 {
01206 printf("\n-> Data cannot be sent as Command frame for CFG is not received...!!!\n");
01207 }
01208 }
01209
01210 else if((c & 0x04) == 0x04)
01211 {
01212 printf("\n-> Configuration frame-1 request is Received.\n");
01213 file_1 = fopen (filename1,"rb");
01214 if (file_1 == NULL)
01215 {
01216 printf("\n-> Sorry but Configuration Frame-1 is not available...!!!\n");
01217 }
01218 else
01219 {
01220 memset(line,'\0',sizeof(line));
01221 fread(line, sizeof(unsigned char), sizeof(line), file_1);
01222 fclose(file_1);
01223
01224
01225 df_temp[0] = line[ind++];
01226 df_temp[1] = line[ind++];
01227 cfg_size = c2i(df_temp);
01228
01229 if (send(new_fd,line, cfg_size, 0)== -1)
01230 {
01231 perror("sendto");
01232 }
01233
01234 printf("\n-> The Configuration Frame-1[%d Bytes] is sent to request PDC.\n", cfg_size);
01235 }
01236 }
01237 }
01238
01239 else
01240 {
01241 printf("\n-> Received Command Frame not from the Authentic PDC...!!!\n");
01242 continue;
01243 }
01244 }
01245
01246 else
01247 {
01248 printf("\n-> Received Frame is not a command frame...!!!\n");
01249 continue;
01250 }
01251 }
01252
01253 }
01254 pthread_exit(0);
01255
01256 }
01257
01258
01259
01260
01261
01262
01263
01264 void* pmu_tcp()
01265 {
01266
01267 int new_tcp_fd, sin_size, err;
01268 pthread_t new_pmu;
01269 sin_size = sizeof(struct sockaddr_in);
01270
01271
01272 pthread_attr_t attr;
01273 pthread_attr_init(&attr);
01274 if( (err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)))
01275 {
01276
01277 perror(strerror(err));
01278 exit(1);
01279 }
01280
01281 if( (err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO)))
01282 {
01283
01284 perror(strerror(err));
01285 exit(1);
01286 }
01287
01288 while(1)
01289 {
01290 if (((new_tcp_fd = accept(TCP_sockfd, (struct sockaddr *)&TCP_addr, (socklen_t *)&sin_size)) == -1))
01291 {
01292 perror("accept");
01293 }
01294 else
01295 {
01296 printf("Inside Accept\n");
01297 if( (err = pthread_create(&new_pmu,&attr,new_pmu_tcp,(void *)new_tcp_fd)))
01298 {
01299 perror(strerror(err));
01300 exit(1);
01301 }
01302 }
01303 }
01304
01305 close(new_tcp_fd);
01306 printf("1.1 Inside Accept\n");
01307
01308 }
01309
01310
01311
01312
01313
01314
01315
01316 void start_server()
01317 {
01318 char *pt, *p;
01319 char line[30], proto_choice[4];
01320 FILE *fp;
01321
01322 while(1)
01323 {
01324 usleep(1000);
01325
01326 if((fp = fopen("PmuServer.txt", "rb")) != NULL)
01327 {
01328 printf("PmuServer File Found.\n");
01329 break;
01330 }
01331 else
01332 continue;
01333 }
01334
01335 memset(line, '\0', 30);
01336 fgets(line, 30, fp);
01337 pt = strtok (line," ");
01338 p = strtok (NULL," ");
01339
01340 PORT = atoi(pt);
01341 strcpy(proto_choice, p);
01342
01343 if(!strncasecmp(proto_choice,"UDP",3))
01344 {
01345 printf("\n\t\t|-------------------------------------------------------|\n");
01346 printf("\t\t|\t\tWelcome to PMU SERVER - UDP\t\t|\n");
01347 printf("\t\t|-------------------------------------------------------|\n");
01348
01349 if ((UDP_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
01350 {
01351 perror("socket");
01352 exit(1);
01353 }
01354 else
01355 {
01356 printf("\n-> UDP Socket : Sucessfully created\n");
01357 }
01358
01359 if (setsockopt(UDP_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
01360 {
01361 perror("setsockopt");
01362 exit(1);
01363 }
01364
01365 UDP_my_addr.sin_family = AF_INET;
01366 UDP_my_addr.sin_port = htons(PORT);
01367 UDP_my_addr.sin_addr.s_addr = INADDR_ANY;
01368 memset(&(UDP_my_addr.sin_zero),'\0', 8);
01369
01370 if (bind(UDP_sockfd, (struct sockaddr *)&UDP_my_addr, sizeof(struct sockaddr)) == -1)
01371 {
01372 perror("bind");
01373 exit(1);
01374 }
01375 else
01376 {
01377 printf("\n-> UDP Socket Bind : Sucessfull\n");
01378 }
01379
01380 printf("\n-> UDP SERVER Listening on port: %d\n",PORT);
01381 UDP_addr_len = sizeof(struct sockaddr);
01382
01383
01384 if( (err = pthread_create(&UDP_thread,NULL,pmu_udp,NULL)))
01385 {
01386 perror(strerror(err));
01387 exit(1);
01388 }
01389 pthread_join(UDP_thread, NULL);
01390 close(UDP_sockfd);
01391
01392 }
01393
01394 else if (!strncasecmp(proto_choice,"TCP",3))
01395 {
01396 printf("\n\t\t|-------------------------------------------------------|\n");
01397 printf("\t\t|\t\tWelcome to PMU SERVER - TCP\t\t|\n");
01398 printf("\t\t|-------------------------------------------------------|\n");
01399
01400 if ((TCP_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
01401 {
01402 perror("socket");
01403 exit(1);
01404 }
01405 else
01406 {
01407 printf("\n-> TCP Socket : Sucessfully created\n");
01408 }
01409
01410 if (setsockopt(TCP_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
01411 {
01412 perror("setsockopt");
01413 exit(1);
01414 }
01415
01416 TCP_my_addr.sin_family = AF_INET;
01417 TCP_my_addr.sin_port = htons(PORT);
01418 TCP_my_addr.sin_addr.s_addr = INADDR_ANY;
01419 memset(&(TCP_my_addr.sin_zero), '\0', 8);
01420
01421 if (bind(TCP_sockfd, (struct sockaddr *)&TCP_my_addr, sizeof(struct sockaddr)) == -1)
01422 {
01423 perror("bind");
01424 exit(1);
01425 }
01426 else
01427 {
01428 printf("\n-> TCP Socket Bind : Sucessfull\n");
01429 }
01430
01431 if (listen(TCP_sockfd, BACKLOG) == -1)
01432 {
01433 perror("listen");
01434 exit(1);
01435 }
01436
01437
01438 sa.sa_handler = sigchld_handler;
01439 sigemptyset(&sa.sa_mask);
01440 sa.sa_flags = SA_RESTART;
01441 if (sigaction(SIGCHLD, &sa, NULL) == -1)
01442 {
01443 perror("sigaction");
01444 exit(1);
01445 }
01446
01447 printf("\n-> TCP SERVER Listening on port: %d\n",PORT);
01448
01449
01450 if( (err = pthread_create(&TCP_thread,NULL,pmu_tcp,NULL)))
01451 {
01452 perror(strerror(err));
01453 exit(1);
01454 }
01455 pthread_join(TCP_thread, NULL);
01456 close(TCP_sockfd);
01457
01458 }
01459 }
01460
01461