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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 #include <unistd.h>
00058 #include <errno.h>
00059 #include <string.h>
00060 #include <sys/types.h>
00061 #include <sys/socket.h>
00062 #include <netinet/in.h>
00063 #include <arpa/inet.h>
00064 #include <sys/wait.h>
00065 #include <signal.h>
00066 #include <pthread.h>
00067 #include <errno.h>
00068 #include <time.h>
00069 #include <ctype.h>
00070 #include <gtk/gtk.h>
00071 #include "ipdcGui.h"
00072 #include "connections.h"
00073 #include "parser.h"
00074 #include "global.h"
00075 #include "new_pmu_or_pdc.h"
00076 #include "dallocate.h"
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 int add_PMU(char pmuid[], char ip[], char port[], char protocol[]) {
00087
00088 FILE *f_ip;
00089 int flag = 0;
00090 char *i,*p,*port_num,*id;
00091 int err;
00092
00093
00094 pthread_attr_t attr;
00095 pthread_attr_init(&attr);
00096 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
00097
00098 perror(strerror(err));
00099 exit(1);
00100
00101 }
00102
00103 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) {
00104
00105 perror(strerror(err));
00106 exit(1);
00107 }
00108
00109 f_ip = fopen("ipaddress.txt","r");
00110
00111 char line [40];
00112 memset(line,'\0',40);
00113
00114 if(f_ip != NULL) {
00115
00116 while (fgets (line, 40,f_ip) != NULL) {
00117
00118 id = strtok (line," ");
00119 i = strtok (NULL," ");
00120 port_num = strtok (NULL," ");
00121 p = strtok (NULL," ");
00122
00123 if(!strcmp(id,pmuid)) {
00124
00125 flag = 1;
00126 break;
00127
00128 } else {
00129
00130 memset(line,'\0',30);
00131 continue;
00132 }
00133 }
00134 fclose (f_ip);
00135 } else {
00136
00137 printf("File ipaddress.txt not present so we create it\n");
00138 f_ip = fopen("ipaddress.txt","w");
00139 fclose(f_ip);
00140 }
00141
00142
00143 printf("flag = %d\n",flag);
00144 if(flag) {
00145
00146 printf("%s %s is already in the ipaddress list Enter another PMU\n",pmuid,protocol);
00147 return 1;
00148
00149 } else if(!flag) {
00150
00151 f_ip = fopen("ipaddress.txt","a");
00152 fprintf(f_ip,"%s",pmuid);
00153 fputc(' ',f_ip);
00154 fprintf(f_ip,"%s",ip);
00155 fputc(' ',f_ip);
00156 fprintf(f_ip,"%s",port);
00157 fputc(' ',f_ip);
00158 fprintf(f_ip,"%s",protocol);
00159 fputc('\n',f_ip);
00160 fclose(f_ip);
00161
00162
00163 struct Lower_Layer_Details *temp_pmu;
00164 temp_pmu = malloc(sizeof(struct Lower_Layer_Details));
00165 if(!temp_pmu) {
00166
00167 printf("Not enough memory temp_pmu\n");
00168 exit(1);
00169 }
00170
00171 temp_pmu->pmuid = atoi(pmuid);
00172 strcpy(temp_pmu->ip,ip);
00173 temp_pmu->port = atoi(port);
00174 strcpy(temp_pmu->protocol,protocol);
00175 temp_pmu->protocol[3] = '\0';
00176 temp_pmu->up = 1;
00177 temp_pmu->data_transmission_off = 0;
00178 temp_pmu->pmu_remove = 0;
00179 temp_pmu->request_cfg_frame = 0;
00180 temp_pmu->next = NULL;
00181 temp_pmu->prev = NULL;
00182
00183 pthread_t t;
00184 if(!strncasecmp(protocol,"UDP",3)) {
00185
00186 if((err = pthread_create(&t,&attr,connect_pmu_udp,(void *)temp_pmu))) {
00187
00188 perror(strerror(err));
00189 exit(1);
00190 }
00191
00192 } else if(!strncasecmp(protocol,"TCP",3)) {
00193
00194 if((err = pthread_create(&t,&attr,connect_pmu_tcp,(void *)temp_pmu))) {
00195
00196 perror(strerror(err));
00197 exit(1);
00198 }
00199
00200 }
00201
00202 struct Upper_Layer_Details *temp_pdc = ULfirst;
00203 pthread_mutex_lock(&mutex_Upper_Layer_Details);
00204 while(temp_pdc != NULL ) {
00205
00206 temp_pdc->config_change = 1;
00207 temp_pdc = temp_pdc->next;
00208 }
00209 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
00210 return 0;
00211 }
00212 return 1;
00213 }
00214
00215
00216
00217
00218
00219
00220 void* connect_pmu_tcp(void *temp) {
00221
00222
00223 int tcp_sockfd,port_num,yes = 1;
00224 struct sockaddr_in PMU_addr;
00225 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp;
00226 unsigned char *tcp_BUF,*ptr,length[2];
00227 unsigned int flen;
00228 uint16_t cal_crc,frame_crc;
00229
00230 port_num = temp_pmu->port;
00231
00232 if ((tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00233 perror("socket");
00234 exit(1);
00235 }
00236
00237 if (setsockopt(tcp_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
00238 perror("setsockopt");
00239 exit(1);
00240 }
00241
00242 bzero(&PMU_addr,sizeof(PMU_addr));
00243 PMU_addr.sin_family = AF_INET;
00244 PMU_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip);
00245 PMU_addr.sin_port = htons(port_num);
00246 memset(&(PMU_addr.sin_zero), '\0', 8);
00247
00248
00249 temp_pmu->thread_id = pthread_self();
00250 bzero(&temp_pmu->llpmu_addr,sizeof(PMU_addr));
00251 temp_pmu->llpmu_addr.sin_family = AF_INET;
00252 temp_pmu->llpmu_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip);
00253 temp_pmu->llpmu_addr.sin_port = htons(port_num);
00254 memset(&(temp_pmu->llpmu_addr.sin_zero), '\0', 8);
00255 temp_pmu->sockfd = tcp_sockfd;
00256 temp_pmu->up = 1;
00257
00258 if (connect(tcp_sockfd, (struct sockaddr *)&PMU_addr,
00259 sizeof(PMU_addr)) == -1) {
00260 perror("connect");
00261 temp_pmu->up = 0;
00262 add_PMU_Node(temp_pmu);
00263 pthread_exit(NULL);
00264
00265 } else {
00266
00267 add_PMU_Node(temp_pmu);
00268
00269 tcp_BUF = malloc(MAXBUFLEN* sizeof(unsigned char));
00270
00271 int n,bytes_read;
00272 char *cmdframe = malloc(19);
00273 cmdframe[18] = '\0';
00274 create_command_frame(1,temp_pmu->pmuid,cmdframe);
00275 if ((n = send (tcp_sockfd,cmdframe,18,0) == -1)) {
00276
00277 perror("send");
00278
00279 } else {
00280
00281 free(cmdframe);
00282 while(1) {
00283
00284 memset(tcp_BUF, '\0', MAXBUFLEN * sizeof(unsigned char));
00285 bytes_read = recv (tcp_sockfd, tcp_BUF,MAXBUFLEN-1,0);
00286
00287 if(bytes_read == 0) {
00288
00289 printf("No data received Closing tcp socket %d\n",tcp_sockfd);
00290 temp_pmu->up = 0;
00291
00292 struct Upper_Layer_Details *temp_pdc = ULfirst;
00293 pthread_mutex_lock(&mutex_Upper_Layer_Details);
00294 while(temp_pdc != NULL ) {
00295
00296 temp_pdc->config_change = 1;
00297 temp_pdc = temp_pdc->next;
00298 }
00299 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
00300
00301
00302 pthread_exit(NULL);
00303
00304 } else if(bytes_read == -1) {
00305
00306 perror("recv");
00307 temp_pmu->up = 0;
00308
00309 struct Upper_Layer_Details *temp_pdc = ULfirst;
00310 pthread_mutex_lock(&mutex_Upper_Layer_Details);
00311 while(temp_pdc != NULL ) {
00312
00313 temp_pdc->config_change = 1;
00314 temp_pdc = temp_pdc->next;
00315 }
00316 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
00317
00318 } else {
00319
00320 ptr = tcp_BUF;
00321 ptr += 2;
00322 copy_cbyc(length,ptr,2);
00323 flen = to_intconvertor(length);
00324 cal_crc = compute_CRC(tcp_BUF,flen-2);
00325 ptr += flen -4;
00326 frame_crc = *ptr;
00327 frame_crc <<= 8;
00328 frame_crc |= *(ptr + 1);
00329 if(frame_crc != cal_crc) {
00330
00331 fprintf(fp_log,"CRC NOT MATCH\n");
00332 continue;
00333 }
00334 if (sendto(DB_sockfd,tcp_BUF, MAXBUFLEN-1, 0,
00335 (struct sockaddr *)&DB_Server_addr,sizeof(DB_Server_addr)) == -1) {
00336 perror("sendto");
00337
00338 }
00339
00340 tcp_BUF[bytes_read] = '\0';
00341 PMU_process_TCP(tcp_BUF,tcp_sockfd);
00342
00343 }
00344
00345 }
00346 }
00347 }
00348 close(tcp_sockfd);
00349 pthread_exit(NULL);
00350 }
00351
00352
00353
00354
00355
00356
00357
00358 void* connect_pmu_udp(void *temp) {
00359
00360 int udp_sockfd,port_num,addr_len,yes = 1;
00361 struct sockaddr_in PMU_addr,their_addr;
00362 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp;
00363 struct Lower_Layer_Details *t ;
00364 unsigned char *udp_BUF,*ptr,length[2];
00365 unsigned int flen;
00366 uint16_t cal_crc,frame_crc;
00367
00368 port_num = temp_pmu->port;
00369
00370 if ((udp_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
00371 perror("socket");
00372 exit(1);
00373 }
00374
00375 if (setsockopt(udp_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
00376 perror("setsockopt");
00377 exit(1);
00378 }
00379
00380 bzero(&PMU_addr,sizeof(PMU_addr));
00381 PMU_addr.sin_family = AF_INET;
00382 PMU_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip);
00383 PMU_addr.sin_port = htons(port_num);
00384 memset(&(PMU_addr.sin_zero), '\0', 8);
00385
00386
00387 temp_pmu->thread_id = pthread_self();
00388 bzero(&temp_pmu->llpmu_addr,sizeof(PMU_addr));
00389 temp_pmu->llpmu_addr.sin_family = AF_INET;
00390 temp_pmu->llpmu_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip);
00391 temp_pmu->llpmu_addr.sin_port = htons(port_num);
00392 memset(&(temp_pmu->llpmu_addr.sin_zero), '\0', 8);
00393 temp_pmu->sockfd = udp_sockfd;
00394 temp_pmu->up = 1;
00395
00396
00397 add_PMU_Node(temp_pmu);
00398
00399 udp_BUF = malloc(MAXBUFLEN* sizeof(unsigned char));
00400
00401
00402 addr_len = sizeof(struct sockaddr);
00403 int n,bytes_read;
00404 unsigned char *cmdframe = malloc(19);
00405 cmdframe[18] = '\0';
00406 create_command_frame(1,temp_pmu->pmuid,(char *)cmdframe);
00407
00408 if ((n = sendto(udp_sockfd,cmdframe, 18, 0,
00409 (struct sockaddr *)&PMU_addr,sizeof(PMU_addr)) == -1)) {
00410 perror("sendto");
00411
00412 } else {
00413
00414 free(cmdframe);
00415 fprintf(fp_log,"sent CMD for CFG\n");
00416
00417 while(1) {
00418
00419 memset(udp_BUF,'\0',MAXBUFLEN * sizeof(unsigned char));
00420 bytes_read = recvfrom (udp_sockfd, udp_BUF,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,(socklen_t *)&addr_len);
00421 if(bytes_read == -1) {
00422
00423 perror("recvfrom");
00424 exit(1);
00425
00426 } else {
00427
00428
00429 pthread_mutex_lock(&mutex_Lower_Layer_Details);
00430 int flag = 0;
00431 if(LLfirst == NULL) {
00432
00433 flag = 0;
00434
00435 } else {
00436
00437 t = LLfirst;
00438 while(t != NULL) {
00439
00440 if((!strcmp(t->ip,inet_ntoa(their_addr.sin_addr)))
00441 && (!strncasecmp(t->protocol,"UDP",3))) {
00442
00443 flag = 1;
00444 break;
00445
00446 } else {
00447
00448 t = t->next;
00449 }
00450 }
00451 }
00452 pthread_mutex_unlock(&mutex_Lower_Layer_Details);
00453
00454 if(flag) {
00455
00456 ptr = udp_BUF;
00457 ptr += 2;
00458 copy_cbyc(length,ptr,2);
00459 flen = to_intconvertor(length);
00460 cal_crc = compute_CRC(udp_BUF,flen-2);
00461 ptr += flen -4;
00462 frame_crc = *ptr;
00463 frame_crc <<= 8;
00464 frame_crc |= *(ptr + 1);
00465 if(frame_crc != cal_crc) {
00466
00467 fprintf(fp_log,"CRC NOT MATCH\n");
00468 continue;
00469 }
00470
00471
00472 int n;
00473 if ((n = sendto(DB_sockfd,udp_BUF, MAXBUFLEN-1, 0,
00474 (struct sockaddr *)&DB_Server_addr,sizeof(DB_Server_addr)) == -1)) {
00475 perror("sendto");
00476
00477 } else {
00478
00479 fprintf(fp_log,"DB socket %d \n",DB_sockfd);
00480 fprintf(fp_log,"DB Server %s \n",inet_ntoa(DB_Server_addr.sin_addr));
00481 fprintf(fp_log,"UDP_BUF len %d Sent FRAME %d bytes\n",strlen((const char *)udp_BUF),n);
00482 }
00483
00484 fprintf(fp_log,"\nUDP Server got packet from %s\n",inet_ntoa(their_addr.sin_addr));
00485 fprintf(fp_log,"packet is %d bytes long\n",bytes_read);
00486 udp_BUF[bytes_read] = '\0';
00487
00488
00489 PMU_process_UDP(udp_BUF,PMU_addr,udp_sockfd);
00490
00491 } else {
00492
00493 printf("Datagram PMU not authentic. We donot pass the buffer for further processing %s\n",inet_ntoa(their_addr.sin_addr));
00494
00495 }
00496 }
00497
00498 }
00499 }
00500
00501 close(udp_sockfd);
00502 pthread_exit(NULL);
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512 void add_PMU_Node(struct Lower_Layer_Details *temp_pmu) {
00513
00514 pthread_mutex_lock(&mutex_Lower_Layer_Details);
00515 if(LLfirst == NULL) {
00516
00517 temp_pmu->prev = NULL;
00518 LLfirst = temp_pmu;
00519
00520 } else {
00521
00522 LLlast->next = temp_pmu;
00523 temp_pmu->prev = LLlast;
00524 }
00525
00526 temp_pmu->next = NULL;
00527 LLlast = temp_pmu;
00528
00529 pthread_mutex_unlock(&mutex_Lower_Layer_Details);
00530
00531 }
00532
00533
00534
00535
00536
00537
00538
00539 int remove_Lower_Node(char pmuid[], char protocol[]) {
00540
00541 int flag = 0, err;
00542
00543
00544
00545 pthread_attr_t attr;
00546 pthread_attr_init(&attr);
00547 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
00548
00549 perror(strerror(err));
00550 exit(1);
00551
00552 }
00553
00554
00555 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) {
00556
00557 perror(strerror(err));
00558 exit(1);
00559 }
00560
00561 printf("#### Connection TABLE ####\n");
00562 printf("-----------------------------------------------------------------\n");
00563 printf("| PMU ID | IP | Port | Protocol |\n");
00564 printf("-----------------------------------------------------------------\n");
00565
00566 if(LLfirst == NULL) {
00567
00568 printf("No PMU Added\n");
00569 return 1;
00570
00571 } else {
00572
00573 flag = 1;
00574 struct Lower_Layer_Details *t = LLfirst;
00575
00576 while(t != NULL) {
00577
00578
00579 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\n",t->pmuid,t->ip,t->port,t->protocol);
00580 t = t->next;
00581 }
00582
00583 printf("-----------------------------------------------------------------\n");
00584
00585 }
00586
00587 if(flag) {
00588
00589 int match = 0;
00590 struct Lower_Layer_Details *temp_pmu = LLfirst;
00591
00592 while(temp_pmu != NULL) {
00593
00594 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) {
00595
00596 match = 1;
00597 break;
00598
00599 } else {
00600
00601 temp_pmu = temp_pmu->next;
00602
00603 }
00604
00605 }
00606 if(match) {
00607
00608 pthread_t t;
00609 if((err = pthread_create(&t,&attr,remove_llnode,(void *)temp_pmu))) {
00610
00611 perror(strerror(err));
00612 exit(1);
00613 }
00614
00615 struct Upper_Layer_Details *temp_pdc = ULfirst;
00616 pthread_mutex_lock(&mutex_Upper_Layer_Details);
00617 while(temp_pdc != NULL ) {
00618
00619 temp_pdc->config_change = 1;
00620 temp_pdc = temp_pdc->next;
00621 }
00622 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
00623 return 0;
00624
00625 } else {
00626
00627 fprintf(fp_log,"No Match\n");
00628 return 1;
00629 }
00630 }
00631 return 1;
00632 }
00633
00634
00635
00636
00637
00638 void* remove_llnode(void* temp) {
00639
00640 int rc,result;
00641 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp;
00642 char pmuid[6];
00643 pthread_t tid = temp_pmu->thread_id;
00644
00645
00646
00647 FILE *fnew_ip = fopen("ipaddress1.txt","w");
00648 FILE *f_ip = fopen("ipaddress.txt","r");
00649 if (f_ip != NULL) {
00650
00651 char s[40];
00652 memset(s,'\0',sizeof(s));
00653 while (fgets (s,sizeof(s),f_ip) != NULL) {
00654
00655 memset(pmuid,'\0',6);
00656 sprintf(pmuid,"%d",temp_pmu->pmuid);
00657 if((strstr(s,pmuid))&&(strstr(s,temp_pmu->protocol))) {
00658
00659 break;
00660
00661 } else {
00662
00663
00664 fputs(s,fnew_ip);
00665 memset(s,'\0',40);
00666 memset(pmuid,'\0',6);
00667 }
00668 }
00669 memset(s,'\0',sizeof(s));
00670
00671 while (fgets (s, sizeof(s),f_ip) != NULL) {
00672
00673 fputs(s,fnew_ip);
00674 memset(s,'\0',40);
00675 }
00676
00677 fclose(f_ip);
00678 fclose(fnew_ip);
00679
00680 if( remove( "ipaddress.txt" ) != 0 )
00681 perror( "Error deleting file" );
00682 result = rename("ipaddress1.txt","ipaddress.txt");
00683 if ( result == 0 )
00684 fputs ("File successfully renamed",fp_log);
00685 else
00686 perror("Error renaming file" );
00687
00688 } else {
00689
00690 perror("ipaddress.txt");
00691 }
00692
00693
00694
00695 int ind = 0,match = 0;
00696 struct cfg_frame *temp_cfg = cfgfirst,*tprev_cfg;
00697 unsigned char id_CODE[2];
00698 tprev_cfg = temp_cfg;
00699
00700 id_CODE[0] = temp_pmu->pmuid >> 8;
00701 id_CODE[1] = temp_pmu->pmuid ;
00702
00703 while(temp_cfg != NULL){
00704 if(!ncmp_cbyc(id_CODE,temp_cfg->idcode,2)) {
00705
00706 match = 1;
00707 break;
00708
00709 } else {
00710
00711 ind++;
00712 tprev_cfg = temp_cfg;
00713 temp_cfg = temp_cfg->cfgnext;
00714
00715 }
00716 }
00717
00718 if(match) {
00719
00720 pthread_mutex_lock(&mutex_cfg);
00721 if(!ind) {
00722
00723
00724 cfgfirst = cfgfirst->cfgnext;
00725 free_cfgframe_object(temp_cfg);
00726 fprintf(fp_log,"Out of FIRST free_cfgframe_object()\n");
00727
00728 pthread_mutex_lock(&mutex_file);
00729 remove_cfg_from_file(id_CODE);
00730 pthread_mutex_unlock(&mutex_file);
00731
00732 } else {
00733
00734 tprev_cfg->cfgnext = temp_cfg->cfgnext;
00735 free_cfgframe_object(temp_cfg);
00736 fprintf(fp_log,"Out of free_cfgframe_object()\n");
00737 pthread_mutex_lock(&mutex_file);
00738 remove_cfg_from_file(id_CODE);
00739 pthread_mutex_unlock(&mutex_file);
00740 }
00741
00742 pthread_mutex_unlock(&mutex_cfg);
00743
00744 }
00745
00746 pthread_mutex_lock(&mutex_Lower_Layer_Details);
00747
00748 if(temp_pmu->prev == NULL) {
00749
00750 LLfirst = temp_pmu->next;
00751
00752 if(LLfirst != NULL)
00753 LLfirst->prev = NULL;
00754
00755
00756 } else {
00757
00758 if (temp_pmu == LLlast)
00759 temp_pmu->prev->next = NULL;
00760
00761 else
00762 temp_pmu->prev->next = temp_pmu->next;
00763 }
00764
00765 if(temp_pmu->next == NULL) {
00766
00767 LLlast = temp_pmu->prev;
00768 } else {
00769 if(temp_pmu->prev != NULL)
00770 temp_pmu->prev->next = temp_pmu->next;
00771
00772 }
00773
00774 pthread_mutex_unlock(&mutex_Lower_Layer_Details);
00775
00776 rc = pthread_cancel(tid);
00777 if(rc == 0)
00778 fprintf(fp_log,"Cancelled thread %ld\n",tid);
00779 close(temp_pmu->sockfd);
00780 free(temp_pmu);
00781
00782 pthread_exit(NULL);
00783 }
00784
00785
00786
00787
00788
00789
00790 void remove_cfg_from_file(unsigned char idcode[]) {
00791
00792 FILE *file,*newfile;
00793 int result;
00794 unsigned char id[3],*line,*s,frame_len[2];
00795 unsigned long fileLen;
00796 unsigned int framesize;
00797
00798 file = fopen("cfg.bin","rb");
00799 newfile = fopen("ncfg.bin","wb");
00800
00801 if (file != NULL) {
00802
00803
00804 fseek(file, 0, SEEK_END);
00805 fileLen = ftell(file);
00806 fseek(file, 0, SEEK_SET);
00807
00808 fprintf(fp_log,"File len %ld\n",fileLen);
00809 while (fileLen != 0) {
00810
00811 fseek (file,2 ,SEEK_CUR);
00812 fread(frame_len, sizeof(unsigned char),2, file);
00813 fseek (file,-4 ,SEEK_CUR);
00814
00815 framesize = to_intconvertor(frame_len);
00816 line = malloc(framesize*sizeof(unsigned char));
00817 memset(line,'\0',sizeof(line));
00818 fread(line, sizeof(unsigned char),framesize,file);
00819 s = line;
00820 s += 4;
00821
00822 copy_cbyc (id,s,2);
00823 id[2] = '\0';
00824 if(!ncmp_cbyc(id,idcode,2)) {
00825
00826 fprintf(fp_log,"MATCHED the IDCODE file cfg.bin\n");
00827 break;
00828
00829 } else {
00830
00831
00832 fwrite(line, sizeof(unsigned char),framesize,newfile);
00833 free(line);
00834 fileLen -= framesize;
00835 }
00836 }
00837
00838 fileLen -= framesize;
00839
00840 while (fileLen != 0) {
00841
00842 fseek (file,2 ,SEEK_CUR);
00843 fread(frame_len, sizeof(unsigned char),2, file);
00844 fseek (file,-4 ,SEEK_CUR);
00845
00846 framesize = to_intconvertor(frame_len);
00847 line = malloc(framesize*sizeof(unsigned char));
00848 memset(line,'\0',sizeof(line));
00849 fread(line, sizeof(unsigned char),framesize,file);
00850
00851 fwrite(line, sizeof(unsigned char),framesize,newfile);
00852 free(line);
00853 fileLen -= framesize;
00854 }
00855
00856
00857 fclose (file);
00858 fclose(newfile);
00859
00860 if( remove( "cfg.bin" ) != 0 )
00861 perror( "Error deleting file" );
00862 result= rename("ncfg.bin","cfg.bin");
00863 if ( result == 0 )
00864 fputs ("File successfully renamed",fp_log);
00865 else
00866 perror( "Error renaming file" );
00867
00868 } else {
00869 perror ("cfg.bin");
00870 }
00871 }
00872
00873
00874
00875
00876
00877 int put_data_transmission_off(char pmuid[], char protocol[]) {
00878
00879 int flag = 0,err;
00880
00881
00882 pthread_attr_t attr;
00883 pthread_attr_init(&attr);
00884 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
00885
00886 perror(strerror(err));
00887 exit(1);
00888
00889 }
00890
00891
00892 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) {
00893
00894 perror(strerror(err));
00895 exit(1);
00896 }
00897
00898 printf("#### Connection TABLE ####\n");
00899 printf("-----------------------------------------------------------------\n");
00900 printf("| PMU ID | IP | Port | Protocol |\n");
00901 printf("-----------------------------------------------------------------\n");
00902
00903 pthread_mutex_lock(&mutex_Lower_Layer_Details);
00904
00905 if(LLfirst == NULL) {
00906
00907 printf("No PMU Added\n");
00908 return 1;
00909
00910 } else {
00911
00912 flag = 1;
00913 struct Lower_Layer_Details *t = LLfirst;
00914
00915 while(t != NULL) {
00916
00917
00918 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\n",t->pmuid,t->ip,t->port,t->protocol);
00919 t = t->next;
00920 }
00921
00922 printf("-----------------------------------------------------------------\n");
00923
00924 }
00925
00926 pthread_mutex_unlock(&mutex_Lower_Layer_Details);
00927
00928 if(flag) {
00929
00930 int match = 0;
00931 struct Lower_Layer_Details *temp_pmu = LLfirst;
00932
00933 while(temp_pmu != NULL) {
00934
00935 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) {
00936
00937 match = 1;
00938 break;
00939
00940 } else {
00941
00942 temp_pmu = temp_pmu->next;
00943
00944 }
00945
00946 }
00947 if(match) {
00948
00949 pthread_t t;
00950 temp_pmu->data_transmission_off = 1;
00951 if((err = pthread_create(&t,&attr,data_off_llnode,(void *)temp_pmu))) {
00952
00953 perror(strerror(err));
00954 exit(1);
00955 }
00956 return 0;
00957
00958 } else {
00959
00960 fprintf(fp_log,"No Match\n");
00961 return 1;
00962 }
00963 }
00964 return 1;
00965 }
00966
00967
00968
00969
00970
00971
00972 void* data_off_llnode(void* temp) {
00973
00974 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp;
00975
00976 char *cmdframe = malloc(19);
00977 create_command_frame(3,temp_pmu->pmuid,cmdframe);
00978 cmdframe[18] = '\0';
00979
00980 if(!strncasecmp(temp_pmu->protocol,"UDP",3)) {
00981
00982 int n;
00983 if ((n = sendto(temp_pmu->sockfd,cmdframe, 18,
00984 0,(struct sockaddr *)&temp_pmu->llpmu_addr,sizeof(temp_pmu->llpmu_addr)) == -1)) {
00985 perror("sendto");
00986 } else {
00987
00988 printf("Sent CmD to put off data transmission\n");
00989
00990 }
00991
00992 } else if(!strncasecmp(temp_pmu->protocol,"TCP",3)){
00993
00994 int n;
00995 if(temp_pmu->up == 1) {
00996 if ((n = send(temp_pmu->sockfd,cmdframe, 18,0) == -1)) {
00997
00998 perror("send");
00999
01000 } else {
01001
01002 printf("Sent CmD to put off data transmission\n");
01003
01004 }
01005 }
01006 }
01007 free(cmdframe);
01008 pthread_exit(NULL);
01009 }
01010
01011
01012
01013
01014
01015
01016 int put_data_transmission_on(char pmuid[], char protocol[]) {
01017
01018 int flag = 0,err;
01019
01020
01021 pthread_attr_t attr;
01022 pthread_attr_init(&attr);
01023 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
01024
01025 perror(strerror(err));
01026 exit(1);
01027
01028 }
01029
01030
01031 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) {
01032
01033 perror(strerror(err));
01034 exit(1);
01035 }
01036
01037 printf("Lower Layer PMU/PDC whose data transmiision is off\n");
01038
01039 printf("#### Connection TABLE ####\n");
01040 printf("-----------------------------------------------------------------\n");
01041 printf("| PMU ID | IP | Port | Protocol |\n");
01042 printf("-----------------------------------------------------------------\n");
01043
01044 pthread_mutex_lock(&mutex_Lower_Layer_Details);
01045
01046 if(LLfirst == NULL) {
01047
01048 printf("No PMU Added\n");
01049 return 1;
01050
01051 } else {
01052
01053 flag = 1;
01054 struct Lower_Layer_Details *t = LLfirst;
01055
01056 while(t != NULL) {
01057
01058 if(t->data_transmission_off == 1) {
01059 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\n",t->pmuid,t->ip,t->port,t->protocol);
01060 }
01061 t = t->next;
01062 }
01063
01064 printf("-----------------------------------------------------------------\n");
01065
01066 }
01067
01068 pthread_mutex_unlock(&mutex_Lower_Layer_Details);
01069
01070 if(flag) {
01071
01072 int match = 0;
01073 struct Lower_Layer_Details *temp_pmu = LLfirst;
01074
01075 while(temp_pmu != NULL) {
01076
01077 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) {
01078
01079 match = 1;
01080 break;
01081
01082 } else {
01083
01084 temp_pmu = temp_pmu->next;
01085
01086 }
01087
01088 }
01089 if(match) {
01090
01091 temp_pmu->data_transmission_off = 0;
01092 pthread_t t;
01093 if((err = pthread_create(&t,&attr,data_on_llnode,(void *)temp_pmu))) {
01094
01095 perror(strerror(err));
01096 exit(1);
01097 }
01098 return 0;
01099
01100 } else {
01101
01102 printf("No Match\n");
01103 return 1;
01104 }
01105 }
01106 return 1;
01107 }
01108
01109
01110
01111
01112
01113
01114 void* data_on_llnode(void* temp) {
01115
01116 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp;
01117
01118 char *cmdframe = malloc(19);
01119 create_command_frame(2,temp_pmu->pmuid,cmdframe);
01120 cmdframe[18] = '\0';
01121 if(!strncasecmp(temp_pmu->protocol,"UDP",3)) {
01122
01123 int n;
01124 if ((n = sendto(temp_pmu->sockfd,cmdframe, 18,
01125 0,(struct sockaddr *)&temp_pmu->llpmu_addr,sizeof(temp_pmu->llpmu_addr)) == -1)) {
01126 perror("sendto");
01127 } else {
01128
01129 printf("Sent CmD to put on data transmission.\n");
01130
01131 }
01132
01133 } else if(!strncasecmp(temp_pmu->protocol,"TCP",3)){
01134
01135 int n;
01136 if(temp_pmu->up == 1) {
01137
01138 if ((n = send(temp_pmu->sockfd,cmdframe, 18,0) == -1)) {
01139
01140 perror("send");
01141
01142 } else {
01143
01144 printf("Sent CmD to put on data transmission.\n");
01145
01146 }
01147 }
01148 }
01149 free(cmdframe);
01150 pthread_exit(NULL);
01151 }
01152
01153
01154
01155
01156
01157 int configuration_request(char pmuid[], char protocol[]) {
01158
01159 int flag = 0,err;
01160
01161
01162 pthread_attr_t attr;
01163 pthread_attr_init(&attr);
01164 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
01165
01166 perror(strerror(err));
01167 exit(1);
01168
01169 }
01170 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) {
01171
01172 perror(strerror(err));
01173 exit(1);
01174 }
01175
01176 pthread_mutex_lock(&mutex_Lower_Layer_Details);
01177
01178 if(LLfirst == NULL) {
01179
01180 printf("No PMU Added\n");
01181 return 1;
01182
01183 } else {
01184
01185 flag = 1;
01186 }
01187
01188 pthread_mutex_unlock(&mutex_Lower_Layer_Details);
01189
01190 if(flag) {
01191
01192 int match = 0;
01193 struct Lower_Layer_Details *temp_pmu = LLfirst;
01194
01195 while(temp_pmu != NULL) {
01196
01197 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) {
01198
01199 match = 1;
01200 break;
01201
01202 } else {
01203 temp_pmu = temp_pmu->next;
01204 }
01205 }
01206 if(match) {
01207
01208
01209 pthread_t t;
01210 if((err = pthread_create(&t,&attr,config_request,(void *)temp_pmu))) {
01211
01212 perror(strerror(err));
01213 exit(1);
01214 }
01215 return 0;
01216
01217 } else {
01218
01219 printf("No Match\n");
01220 return 1;
01221 }
01222 }
01223 return 1;
01224 }
01225
01226
01227
01228
01229
01230 void* config_request(void* temp) {
01231
01232 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp;
01233 char *cmdframe = malloc(19);
01234 int err;
01235
01236 pthread_attr_t attr;
01237 pthread_attr_init(&attr);
01238 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
01239
01240 perror(strerror(err));
01241 exit(1);
01242
01243 }
01244
01245 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) {
01246
01247 perror(strerror(err));
01248 exit(1);
01249 }
01250
01251 if(!strncasecmp(temp_pmu->protocol,"UDP",3)) {
01252
01253 cmdframe[18] = '\0';
01254 create_command_frame(1,temp_pmu->pmuid,cmdframe);
01255 int n;
01256 if ((n = sendto(temp_pmu->sockfd,cmdframe, 18,
01257 0,(struct sockaddr *)&temp_pmu->llpmu_addr,sizeof(temp_pmu->llpmu_addr)) == -1)) {
01258 perror("sendto");
01259 } else {
01260
01261 printf("CmD to send CFG = %x\t%x\n", cmdframe[0], cmdframe[1]);
01262 free(cmdframe);
01263 temp_pmu->data_transmission_off = 0;
01264
01265 }
01266
01267 } else if(!strncasecmp(temp_pmu->protocol,"TCP",3)){
01268
01269 int n;
01270
01271 if(temp_pmu->up == 0) {
01272
01273 pthread_t t;
01274 if((err = pthread_create(&t,&attr,connect_pmu_tcp,(void *)temp_pmu))) {
01275
01276 perror(strerror(err));
01277 exit(1);
01278 }
01279
01280 struct Upper_Layer_Details *temp_pdc = ULfirst;
01281 pthread_mutex_lock(&mutex_Upper_Layer_Details);
01282 while(temp_pdc != NULL ) {
01283
01284 temp_pdc->config_change = 1;
01285 temp_pdc = temp_pdc->next;
01286 }
01287 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
01288
01289
01290 } else {
01291
01292 cmdframe[18] = '\0';
01293 create_command_frame(1,temp_pmu->pmuid,cmdframe);
01294 if ((n = send(temp_pmu->sockfd,cmdframe, 18,0) == -1)) {
01295
01296 printf("temp_pmu->sockfd %d \n",temp_pmu->sockfd);
01297 perror("send");
01298
01299 } else {
01300
01301 printf("CmD to send CFG \n");
01302 free(cmdframe);
01303 temp_pmu->data_transmission_off = 0;
01304 }
01305 }
01306 }
01307 pthread_exit(NULL);
01308 }
01309
01310
01311
01312
01313
01314
01315
01316 int add_PDC(char ip[], char protocol[]) {
01317
01318 FILE *f_ip;
01319 int flag = 0;
01320 char port_num[6], line [30];
01321 char *p,*i,*port;
01322
01323 f_ip = fopen("upperpdc_ip.txt","r");
01324
01325 memset(line,'\0',30);
01326
01327 if(f_ip != NULL) {
01328
01329 while (fgets (line, 30,f_ip) != NULL) {
01330
01331 i = strtok (line," ");
01332 port = strtok (NULL," ");
01333 p = strtok (NULL," ");
01334
01335 if(!strcmp(i,ip) && (!strncasecmp(p,protocol,3))){
01336
01337 flag = 1;
01338 printf("MATCH\n");
01339 return 1;
01340 }
01341 }
01342 fclose (f_ip);
01343 } else {
01344
01345 printf("file upperpdc_ip.txt not present\n Created upperpdc_ip.txt\n");
01346 f_ip = fopen("upperpdc_ip.txt","w");
01347 fclose(f_ip);
01348 }
01349
01350 printf("flag = %d\n",flag);
01351 f_ip = fopen("upperpdc_ip.txt","a");
01352 fprintf(f_ip,"%s",ip);
01353 fputc(' ',f_ip);
01354 if(!strncasecmp(protocol,"TCP",3)) {
01355
01356 sprintf(port_num,"%d",TCPPORT);
01357 fprintf(f_ip,"%s",port_num);
01358 fputc(' ',f_ip);
01359
01360 } else {
01361
01362 sprintf(port_num,"%d",UDPPORT);
01363 fprintf(f_ip,"%s",port_num);
01364 fputc(' ',f_ip);
01365
01366 }
01367
01368 fprintf(f_ip,"%s",protocol);
01369 fputc('\n',f_ip);
01370 fclose(f_ip);
01371
01372
01373 struct Upper_Layer_Details *temp_pdc;
01374 temp_pdc = malloc(sizeof(struct Upper_Layer_Details));
01375 if(!temp_pdc) {
01376
01377 printf("Not enough memory temp_pdc\n");
01378 exit(1);
01379
01380 }
01381
01382 strcpy(temp_pdc->ip,ip);
01383 if(!strncasecmp(protocol,"UDP",3)) {
01384
01385 temp_pdc->port = UDPPORT;
01386
01387 } else {
01388
01389 temp_pdc->port = TCPPORT;
01390 }
01391
01392 strncpy(temp_pdc->protocol,protocol,3);
01393 temp_pdc->protocol[3] = '\0';
01394
01395 bzero(&temp_pdc->pdc_addr,sizeof(temp_pdc->pdc_addr));
01396 temp_pdc->pdc_addr.sin_family = AF_INET;
01397 temp_pdc->pdc_addr.sin_addr.s_addr = inet_addr(temp_pdc->ip);
01398 temp_pdc->pdc_addr.sin_port = htons(temp_pdc->port);
01399 memset(&(temp_pdc->pdc_addr.sin_zero), '\0', 8);
01400 temp_pdc->config_change = 0;
01401 temp_pdc->tcpup = 1;
01402 temp_pdc->UL_upper_pdc_cfgsent = 0;
01403 temp_pdc->UL_data_transmission_off = 1;
01404 temp_pdc->address_set = 0;
01405
01406 printf("PDC IP ADRESS = %s\n",inet_ntoa(temp_pdc->pdc_addr.sin_addr));
01407
01408 pthread_mutex_lock(&mutex_Upper_Layer_Details);
01409 if(ULfirst == NULL) {
01410
01411 ULfirst = temp_pdc;
01412 temp_pdc->prev = NULL;
01413
01414 } else {
01415
01416
01417 ULlast->next = temp_pdc;
01418 temp_pdc->prev = ULlast;
01419
01420 }
01421
01422 ULlast = temp_pdc;
01423 temp_pdc->next = NULL;
01424
01425 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
01426 return 0;
01427
01428
01429 }
01430
01431
01432
01433
01434
01435
01436 int remove_PDC(char ip[], char port_num[], char protocol[]) {
01437
01438 char *p,*i,*port;
01439 int flag = 0,result;
01440
01441 if(ULfirst == NULL) {
01442
01443 printf("No PDC Added\n");
01444 return 1;
01445
01446 } else {
01447
01448 struct Upper_Layer_Details *t = ULfirst;
01449 while(t != NULL) {
01450
01451 if(!strcmp(t->ip,ip)) {
01452 if(((!strncasecmp(t->protocol,"UDP",3)) && (t->port == UDPPORT)) || ((!strncasecmp(t->protocol,"TCP",3)) && (t->port == TCPPORT))) {
01453
01454 flag = 1;
01455 break;
01456 }
01457 }
01458
01459 t = t->next;
01460 }
01461
01462 if(flag) {
01463 pthread_mutex_lock(&mutex_Upper_Layer_Details);
01464
01465 if(t->prev == NULL) {
01466
01467 ULfirst = t->next;
01468 if(ULfirst != NULL) ULfirst->prev = NULL;
01469
01470 } else {
01471
01472 t->prev->next = t->next;
01473 }
01474
01475 if(t->next == NULL) {
01476
01477 ULlast = t->prev;
01478
01479 } else {
01480 if(t->prev != NULL)
01481 t->prev->next = t->next;
01482 }
01483
01484 free(t);
01485 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
01486
01487 FILE *fnew_ip = fopen("upperpdc_ip1.txt","w");
01488 FILE *f_ip = fopen("upperpdc_ip.txt","r");
01489
01490 if (f_ip != NULL) {
01491
01492 flag = 0;
01493 char s[40], s1[40];
01494 memset(s,'\0',40);
01495 memset(s1,'\0',40);
01496 while (fgets (s, 40,f_ip) != NULL) {
01497
01498 strcpy(s1, s);
01499 i = strtok (s," ");
01500 port = strtok (NULL," ");
01501 p = strtok (NULL," ");
01502
01503 if(!strcmp(i,ip) && (!strncasecmp(p,protocol,3))){
01504
01505 flag = 1;
01506 printf("MATCH\n");
01507 break;
01508
01509 } else {
01510
01511
01512 fputs(s1,fnew_ip);
01513 memset(s,'\0',40);
01514 memset(s1,'\0',40);
01515 }
01516 }
01517 memset(s,'\0',sizeof(s));
01518
01519 while (fgets (s, sizeof(s),f_ip) != NULL) {
01520
01521 fputs(s,fnew_ip);
01522 memset(s,'\0',40);
01523 }
01524
01525 fclose(f_ip);
01526 fclose(fnew_ip);
01527
01528 if( remove( "upperpdc_ip.txt" ) != 0 )
01529 perror( "Error deleting file" );
01530 result = rename("upperpdc_ip1.txt","upperpdc_ip.txt");
01531 if ( result != 0 )
01532 perror("Error renaming file" );
01533
01534 return 0;
01535 } else {
01536
01537 perror("upperpdc_ip.txt");
01538 return 1;
01539 }
01540
01541 } else {
01542
01543 printf("\nNo Match\n");
01544 return 1;
01545 }
01546 }
01547 }
01548
01549
01550
01551
01552
01553 void display_CT() {
01554
01555 printf("#### CONNECTION TABLE OF SOURCE DEVICES ####\n");
01556 printf("--------------------------------------------------------------------------------\n");
01557 printf("| PMU ID | IP | Port | Protocol | Up |\n");
01558 printf("--------------------------------------------------------------------------------\n");
01559
01560 pthread_mutex_lock(&mutex_Lower_Layer_Details);
01561
01562 if(LLfirst == NULL) {
01563
01564 printf("No PMU Added\n");
01565
01566
01567 } else {
01568
01569 struct Lower_Layer_Details *t = LLfirst;
01570
01571 while(t != NULL) {
01572
01573 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\t%d\t|\n",t->pmuid,t->ip,t->port,t->protocol,t->up);
01574 t = t->next;
01575 }
01576
01577 printf("--------------------------------------------------------------------------------\n");
01578
01579 }
01580
01581 pthread_mutex_unlock(&mutex_Lower_Layer_Details);
01582
01583 printf("#### CONNECTION TABLE OF DESTINATION DEVICES ####\n");
01584 printf("-------------------------------------------------\n");
01585 printf("| IP | Port | Protocol |\n");
01586 printf("-------------------------------------------------\n");
01587
01588 if(ULfirst == NULL) {
01589
01590 printf("No PDC Added\n");
01591
01592
01593 } else {
01594
01595 struct Upper_Layer_Details *t = ULfirst;
01596
01597 while(t != NULL) {
01598
01599 printf("|%s\t|\t%d\t|\t%s\t|\n",t->ip,t->port,t->protocol);
01600 t = t->next;
01601 }
01602
01603 printf("-------------------------------------------------\n");
01604
01605 }
01606 }
01607
01608
01609
01610
01611
01612 void create_command_frame(int type,int pmu_id,char cmdframe[]) {
01613
01614 fprintf(fp_log,"Inside create_command_frame()\n");
01615 memset(cmdframe,'\0',19);
01616 unsigned char fsize[2],pmuid[2],soc[4],fracsec[4];
01617 long int sec,frac = 0;
01618 int f = 18;
01619 uint16_t chk;
01620
01621 memset(fsize,'\0',2);
01622 int_to_ascii_convertor(f,fsize);
01623 int_to_ascii_convertor(pmu_id,pmuid);
01624
01625 sec = (long int)time (NULL);
01626 long_int_to_ascii_convertor(sec,soc);
01627 long_int_to_ascii_convertor(frac,fracsec);
01628
01629 int index = 0;
01630 switch(type) {
01631
01632 case 1 : byte_by_byte_copy((unsigned char *)cmdframe,CMDSYNC,index,2);
01633 index += 2;
01634 byte_by_byte_copy((unsigned char *)cmdframe,fsize,index,2);
01635 index += 2;
01636 byte_by_byte_copy((unsigned char *)cmdframe,pmuid,index,2);
01637 index += 2;
01638 byte_by_byte_copy((unsigned char *)cmdframe,soc,index,4);
01639 index += 4;
01640 byte_by_byte_copy((unsigned char *)cmdframe,fracsec,index,4);
01641 index += 4;
01642 byte_by_byte_copy((unsigned char *)cmdframe,CMDCFGSEND,index,2);
01643 index += 2;
01644 chk = compute_CRC((unsigned char *)cmdframe,index);
01645 cmdframe[index++] = (chk >> 8) & ~(~0<<8);
01646 cmdframe[index] = (chk ) & ~(~0<<8);
01647 fprintf(fp_log,"Case 1\n");
01648 break;
01649
01650 case 2 : byte_by_byte_copy((unsigned char *)cmdframe,CMDSYNC,index,2);
01651 index += 2;
01652 byte_by_byte_copy((unsigned char *)cmdframe,fsize,index,2);
01653 index += 2;
01654 byte_by_byte_copy((unsigned char *)cmdframe,pmuid,index,2);
01655 index += 2;
01656 byte_by_byte_copy((unsigned char *)cmdframe,soc,index,4);
01657 index += 4;
01658 byte_by_byte_copy((unsigned char *)cmdframe,fracsec,index,4);
01659 index += 4;
01660 byte_by_byte_copy((unsigned char *)cmdframe,CMDDATASEND,index,2);
01661 index += 2;
01662 chk = compute_CRC((unsigned char *)cmdframe,index);
01663 cmdframe[index++] = (chk >> 8) & ~(~0<<8);
01664 cmdframe[index] = (chk ) & ~(~0<<8);
01665 fprintf(fp_log,"Case 2\n");
01666 break;
01667
01668 case 3 : byte_by_byte_copy((unsigned char *)cmdframe,CMDSYNC,index,2);
01669 index += 2;
01670 byte_by_byte_copy((unsigned char *)cmdframe,fsize,index,2);
01671 index += 2;
01672 byte_by_byte_copy((unsigned char *)cmdframe,pmuid,index,2);
01673 index += 2;
01674 byte_by_byte_copy((unsigned char *)cmdframe,soc,index,4);
01675 index += 4;
01676 byte_by_byte_copy((unsigned char *)cmdframe,fracsec,index,4);
01677 index += 4;
01678 byte_by_byte_copy((unsigned char *)cmdframe,CMDDATAOFF,index,2);
01679 index += 2;
01680 chk = compute_CRC((unsigned char *)cmdframe,index);
01681 cmdframe[index++] = (chk >> 8) & ~(~0<<8);
01682 cmdframe[index] = (chk ) & ~(~0<<8);
01683 fprintf(fp_log,"Case 3\n");
01684 break;
01685
01686 default: fprintf(fp_log,"Enter a Valid Request\n");
01687 break;
01688 }
01689 }
01690
01691
01692
01693
01694
01695 int checkip(char ip[]) {
01696
01697 struct sockaddr_in sa;
01698 int result = inet_pton(AF_INET, ip, &(sa.sin_addr));
01699 return result;
01700 }