#include <gtk/gtk.h>
Go to the source code of this file.
Functions | |
void | frame_size () |
void | generate_data_frame () |
void * | udp_send_data () |
void * | pmu_udp () |
void * | tcp_send_data (void *newfd) |
void * | new_pmu_tcp (void *nfd) |
void * | pmu_tcp () |
void | start_server () |
void frame_size | ( | ) |
Definition at line 93 of file ServerFunction.c.
References c2i(), cfg_size, df_af, df_annmr, df_data_frm_size, df_data_rate, df_dgnmr, df_fdf, df_pf, df_phnmr, df_pmu_id, df_pn, df_temp, and MAX_STRING_SIZE.
Referenced by generate_data_frame(), new_pmu_tcp(), and pmu_udp().
00094 { 00095 /* Local variables */ 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 /* Open the 'cfg2.bin' file and read the last CFG frame */ 00102 file_1 = fopen (filename,"rb"); 00103 if (file_1 == NULL) 00104 { 00105 perror (filename); /* why didn't the file open? */ 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 /* Get the CFG size & store globally */ 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 /* Get the PMU ID from CFG FRM & store globally */ 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 /* Get the FORMAT word from CFG FRM */ 00126 df_temp[0] = cline[index++]; 00127 df_temp[1] = cline[index++]; 00128 format = c2i(df_temp); 00129 00130 /* Initialize the format bits as in his appropriate global variable */ 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 /* Get the PHNMR from CFG FRM & store globally */ 00197 df_temp[0] = cline[index++]; 00198 df_temp[1] = cline[index++]; 00199 df_phnmr = c2i(df_temp); 00200 00201 /* Get the df_annmr from CFG FRM & store globally */ 00202 df_temp[0] = cline[index++]; 00203 df_temp[1] = cline[index++]; 00204 df_annmr = c2i(df_temp); 00205 00206 /* Get the df_dgnmr from CFG FRM & store globally */ 00207 df_temp[0] = cline[index++]; 00208 df_temp[1] = cline[index++]; 00209 df_dgnmr = c2i(df_temp); 00210 00211 /* To escape the some of fields in cfg frame */ 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 /* Calculate the data frame size */ 00218 df_data_frm_size = 0; 00219 df_data_frm_size = df_data_frm_size + 18; /* 18 Bytes or 36 char is sum of all static fields in data frame */ 00220 00221 /* Calculate 4/8 bytes for each PHNMR & store globally */ 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 /* Calculate 2/4 bytes for each df_annmr & store globally */ 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 /* Calculate 2/4 bytes for both (FREQ + DFREQ) & store globally */ 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 /* Calculate 2 bytes for each DGNMR & store globally */ 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 } /* end of else of fopen*/ 00256 } /* end of function frame_size() */
void generate_data_frame | ( | ) |
Definition at line 265 of file ServerFunction.c.
References B_copy(), compute_CRC(), curnt_soc, data_frm, df_af, df_annmr, df_chk, df_data_frm_size, df_data_rate, df_dgnmr, df_fdf, df_pf, df_phnmr, df_pmu_id, df_pn, df_soc, df_temp, df_temp_1, f2c(), frame_size(), fsec, i2c(), indx, j, li2c(), pmuse, prev_soc, sa1, sa2, sa3, sa4, sa5, sc1, soc1, and soc2.
Referenced by tcp_send_data(), and udp_send_data().
00266 { 00267 /* local variables */ 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 /* At the starting always read from file "change.bin" and based on that word futher action would be taken */ 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 /* When Configuration has been changed call the function 'frame_size()' to initialize all the global variables 00286 "0" means No Changes in existing CFG FRAME & "5" means some modification has been done in existing CFG FRAME */ 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 /* Insert SYNC Word in data frame */ 00321 data_frm[indx++] = 0xAA; 00322 data_frm[indx++] = 0x01; 00323 00324 /* Insert data frame size in data frame */ 00325 i2c(df_data_frm_size, df_temp); 00326 B_copy(data_frm, df_temp, indx, 2); 00327 indx = indx + 2; 00328 00329 /* Insert PMU ID in data frame */ 00330 i2c(df_pmu_id, df_temp); 00331 B_copy(data_frm, df_temp, indx, 2); 00332 indx = indx + 2; 00333 00334 /* Insert SOC value in data frame */ 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 /* Insert Time Quality flag + fraction of second in data frame */ 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 /* Insert STAT Word in data frame Default or Changed */ 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 /* If not insert default STAT Word: 0000 */ 00433 data_frm[indx++] = 0x00; 00434 data_frm[indx++] = 0x00; 00435 } 00436 prev_soc = curnt_soc; 00437 pmuse = 1; 00438 00439 /* Insert Fix point phasor values in data frame */ 00440 if(df_pf == 0) 00441 { 00442 /* For rendom phasor values */ 00443 if(df_pn == 0) /* Rectangular */ 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 /* Polar */ 00478 { 00479 /* For rendom phasor values */ 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 /* Insert Floating point phasor values in data frame */ 00513 { 00514 if(df_pn == 0) /* Rectangular */ 00515 { 00516 /* For rendom phasor values */ 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 /* Polar */ 00550 { 00551 /* For rendom phasor values */ 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 /* Insert Fix point Frequency & DFrequency values in data frame */ 00586 if(df_fdf == 0) 00587 { 00588 /* For rendom values of FREQ & DFREQ */ 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 /* Insert Floating point Frequency & DFrequency values in data frame */ 00600 { 00601 /* For rendom values of FREQ & DFREQ */ 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 /* Insert Fix point Analog values in data frame */ 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 /* Insert Floating point Analog values in data frame */ 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 /* Insert Digital values in data frame */ 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 /* Calculate and insert the Checksum value in data frame (till now) */ 00645 df_chk = compute_CRC(data_frm,indx); 00646 data_frm[indx++] = (df_chk >> 8) & ~(~0<<8); /* CHKSUM high byte; */ 00647 data_frm[indx++] = (df_chk ) & ~(~0<<8); /* CHKSUM low byte; */ 00648 00649 /* Print the generated data frame */ 00650 printf("["); 00651 for(j=0; j<indx; j++) 00652 { 00653 printf("%x",data_frm[j]); 00654 } 00655 printf("]-(%d)\n", indx); 00656 00657 } /* end of function generate_data_frame() */
void* new_pmu_tcp | ( | void * | nfd | ) |
Definition at line 997 of file ServerFunction.c.
References c2i(), cfg_size, df_data_rate, df_pmu_id, df_temp, err, errno, frame_size(), hdr_size, MAX_STRING_SIZE, n, old_data_rate, pmuse, sa2, sa5, tcp_cfg_flag, tcp_data_flag, tcp_data_trans_off, tcp_send_data(), and tcp_send_thrd_id.
Referenced by pmu_tcp().
00998 { 00999 /* local variables */ 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) /* TCP data stream Received */ 01012 { 01013 memset(tcp_command,36,'\0'); 01014 ind = 2; 01015 01016 if((errno = recv(new_fd,tcp_command,36,0)) == -1) 01017 { 01018 /* Close the thread when connection will broke */ 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 /* Close the thread when connection will broke */ 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 /* New dat has been received */ 01037 { 01038 if (start_u == 0) 01039 { 01040 /* Call the function frame_size() to initialized all globals as Configuration frame */ 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) /* Check if it is a command frame from PDC */ 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) /* Check if it is coming from authentic PDC/iPDC */ 01056 { 01057 c = tcp_command[15]; 01058 if((c & 0x05) == 0x05) /* Command frame for Configuration Frame-1 request from PDC */ 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 /* Get the CFG size & store in global variable */ 01076 df_temp[0] = line[ind++]; 01077 df_temp[1] = line[ind]; 01078 cfg_size = c2i(df_temp); 01079 01080 /* Send Configuration frame to PDC Device */ 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 /* Cancle the data sending thread if data rate has changed */ 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) /* Command frame for Header frame request from PDC */ 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 /* Get the CFG size & store in global variable */ 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 /* Send Header frame to PDC Device */ 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) /* Command frame for Turn off transmission request from PDC */ 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) /* Command frame for Turn ON transmission request from PDC */ 01180 { 01181 printf("\n-> Turn ON data transmission request is Received.\n"); 01182 01183 /* Send data frames if and Only if cfg is sent to PDC */ 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 /* Create a Send data thread */ 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) /* Command frame for Configuration frame-1 request from PDC */ 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 /* Get the CFG size & store in global variable */ 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 /* If Received Command Frame Id code not matched */ 01240 { 01241 printf("\n-> Received Command Frame not from the Authentic PDC...!!!\n"); 01242 continue; 01243 } 01244 } /* end of processing with received Command frame */ 01245 01246 else /* If it is other than command frame */ 01247 { 01248 printf("\n-> Received Frame is not a command frame...!!!\n"); 01249 continue; 01250 } 01251 } /* end of if-else-if */ 01252 01253 } /*end of While */ 01254 pthread_exit(0); 01255 01256 } /* end of void* pmu_tcp(); */
void* pmu_tcp | ( | ) |
Definition at line 1264 of file ServerFunction.c.
References err, new_pmu_tcp(), TCP_addr, and TCP_sockfd.
Referenced by start_server().
01265 { 01266 /* local variables */ 01267 int new_tcp_fd, sin_size, err; 01268 pthread_t new_pmu; 01269 sin_size = sizeof(struct sockaddr_in); 01270 01271 /* A new thread is created for TCP connection in 'detached' mode. */ 01272 pthread_attr_t attr; 01273 pthread_attr_init(&attr); 01274 if( (err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) // In the detached state, the thread resources are 01275 { // immediately freed when it terminates, but 01276 // pthread_join(3) cannot be used to synchronize 01277 perror(strerror(err)); // on the thread termination. 01278 exit(1); 01279 } 01280 01281 if( (err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) /* Shed policy = SCHED_FIFO (realtime, first-in first-out) */ 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 /* New TCP connection has been received*/ 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 //pthread_join(new_pmu, NULL); 01305 close(new_tcp_fd); 01306 printf("1.1 Inside Accept\n"); 01307 //pthread_exit(0); 01308 }
void* pmu_udp | ( | ) |
Definition at line 699 of file ServerFunction.c.
References c2i(), cfg_size, df_data_rate, df_pmu_id, df_temp, err, frame_size(), hdr_size, MAX_STRING_SIZE, n, old_data_rate, pmuse, sa2, sa5, UDP_addr_len, udp_cfg_flag, udp_data_flag, udp_data_trans_off, udp_send_data(), udp_send_thrd_id, and UDP_sockfd.
Referenced by start_server().
00700 { 00701 /* local variables */ 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 /* Call the function frame_size() to initialized all globals as Configuration frame */ 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) /* UDP data Received */ 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 /* New datagram has been received */ 00736 { 00737 c = udp_command[1]; 00738 c <<= 1; 00739 c >>= 5; 00740 00741 if(c == 0x04) /* Check if it is a command frame from PDC */ 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) /* Check if it is coming from authentic PDC/iPDC */ 00748 { 00749 c = udp_command[15]; 00750 00751 if((c & 0x05) == 0x05) /* Command frame for Configuration Frame request from PDC */ 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 /* Get the CFG size & store in global variable */ 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 /* Send Configuration frame to PDC Device */ 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 /* Cancle the data sending thread if data rate is changed */ 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) /* Command frame for Header frame request from PDC */ 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 /* Get the CFG size & store in global variable */ 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 /* Send Header frame to PDC Device */ 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) /* Command frame for Turn off transmission request from PDC */ 00844 { 00845 printf("\n\n\n-> Turn OFF data transmission request is Received.\n"); 00846 00847 if (udp_data_flag == 1) 00848 { 00849 /* Cancle the data sending thread if data rate is changed */ 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) /* Command frame for Turn ON transmission request from PDC */ 00875 { 00876 printf("\n\n\n-> Turn ON data transmission request is Received.\n"); 00877 00878 /* Send data frames if and Only if cfg is sent to PDC */ 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 /* Create a Send data thread */ 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) /* Command frame for Configuration frame-1 request from PDC */ 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 /* Get the CFG size & store in global variable */ 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 /* If Received Command Frame Id code not matched */ 00935 { 00936 printf("\n-> Received Command Frame not from authentic PDC...!!!\n"); 00937 continue; 00938 } 00939 } /* end of processing with received Command frame */ 00940 00941 else /* If it is other than command frame */ 00942 { 00943 printf("\n-> Received Frame is not a command frame...!!!\n"); 00944 continue; 00945 } 00946 } /* end of if-else-if */ 00947 00948 } /* end of while */ 00949 } /* end of pmu_udp(); */
void start_server | ( | ) |
Definition at line 1316 of file ServerFunction.c.
References BACKLOG, err, pmu_tcp(), pmu_udp(), PORT, sa, sigchld_handler(), TCP_sockfd, TCP_thread, UDP_addr_len, UDP_sockfd, UDP_thread, and yes.
Referenced by main().
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) /* Create UDP socket */ 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) /* UDP socket */ 01360 { 01361 perror("setsockopt"); 01362 exit(1); 01363 } 01364 01365 UDP_my_addr.sin_family = AF_INET; /* host byte order */ 01366 UDP_my_addr.sin_port = htons(PORT); /* short, network byte order */ 01367 UDP_my_addr.sin_addr.s_addr = INADDR_ANY; /* inet_addr("127.0.0.1")/ INADDR_ANY-automatically fill with my IP */ 01368 memset(&(UDP_my_addr.sin_zero),'\0', 8); /* zero the rest of the struct */ 01369 01370 if (bind(UDP_sockfd, (struct sockaddr *)&UDP_my_addr, sizeof(struct sockaddr)) == -1) /* bind UDP socket to port */ 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 /* Create Thread for UDP on given port default mode */ 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 } /* end of UDP protocol */ 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) /* Create TCP socket */ 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) /* TCP socket */ 01411 { 01412 perror("setsockopt"); 01413 exit(1); 01414 } 01415 01416 TCP_my_addr.sin_family = AF_INET; /* host byte order */ 01417 TCP_my_addr.sin_port = htons(PORT); /* short, network byte order */ 01418 TCP_my_addr.sin_addr.s_addr = INADDR_ANY; /* ("127.0.0.1")/ INADDR_ANY-automatically fill with my IP */ 01419 memset(&(TCP_my_addr.sin_zero), '\0', 8); /* zero the rest of the struct */ 01420 01421 if (bind(TCP_sockfd, (struct sockaddr *)&TCP_my_addr, sizeof(struct sockaddr)) == -1) /* bind TCP socket to port */ 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) /* Listen on port on TCP socket */ 01432 { 01433 perror("listen"); 01434 exit(1); 01435 } 01436 01437 /* Signal handling for TCP Connection */ 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 /* Create Thread for TCP on given port default mode */ 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 } /* end of start_server() */
void* tcp_send_data | ( | void * | newfd | ) |
Definition at line 958 of file ServerFunction.c.
References df_data_rate, generate_data_frame(), i, tcp_data_flag, tcp_data_trans_off, and tcp_send_thrd_id.
Referenced by new_pmu_tcp().
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 /* Call the function generate_data_frame() to generate the Data Frame */ 00973 generate_data_frame(); 00974 00975 /* if (send(new_fd,data_frm, df_data_frm_size, 0) == 0) 00976 { 00977 perror("send"); 00978 tcp_data_flag = 0; 00979 tcp_data_trans_off = 0; 00980 tcp_send_thrd_id = 0; 00981 printf("1.2 Inside Accept\n"); 00982 pthread_exit(0); 00983 } 00984 */ i++; 00985 usleep(tcp_data_rate); 00986 } 00987 00988 } /* end of tcp_send_data() */
void* udp_send_data | ( | ) |
Definition at line 666 of file ServerFunction.c.
References data_frm, df_data_frm_size, df_data_rate, generate_data_frame(), i, udp_data_flag, udp_data_trans_off, udp_send_thrd_id, and UDP_sockfd.
Referenced by pmu_udp().
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 /* Call the function generate_data_frame() to generate the Data Frame */ 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 } /* end of function udp_send_data() */