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
00056
00057 #include <stdio.h>
00058 #include <string.h>
00059 #include <stdlib.h>
00060 #include <pthread.h>
00061 #include "parser.h"
00062 #include "global.h"
00063 #include "dallocate.h"
00064 #include "align_sort.h"
00065 #include "connections.h"
00066 #include "recreate.h"
00067 #include <stdint.h>
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 void cfgparser(unsigned char st[]){
00078
00079 fprintf(fp_log,"Inside cfgparser()\n");
00080 unsigned char *s;
00081 unsigned char sync[3];
00082 unsigned int num_pmu,phn,ann,dgn;
00083 int i,j,dgchannels,match = 0;
00084 struct cfg_frame *cfg;
00085 struct channel_names *cn;
00086 FILE *fp;
00087
00088
00089
00090 pthread_mutex_lock(&mutex_file);
00091
00092 fp = fopen("cfg.bin","ab");
00093
00094 if (fp == NULL)
00095 fprintf(fp_log,"File doesn't exist\n");
00096
00097 cfg = malloc(sizeof(struct cfg_frame));
00098 if(!cfg) {
00099 fprintf(fp_log,"No enough memory for cfg\n");
00100 }
00101
00102 fprintf(fp_log,"Inside cfgparser()\n");
00103 s = st;
00104
00105
00106
00107 cfg->framesize = malloc(3*sizeof(unsigned char));
00108 if(!cfg->framesize) {
00109 printf("No enough memory for cfg->framesize\n");
00110 }
00111
00112
00113 cfg->idcode = malloc(3*sizeof(unsigned char));
00114 if(!cfg->idcode) {
00115 fprintf(fp_log,"No enough memory for cfg->idcode\n");
00116 }
00117
00118
00119 cfg->soc = malloc(5*sizeof(unsigned char));
00120 if(!cfg->soc) {
00121 fprintf(fp_log,"Not enough memory for cfg->soc\n");
00122 }
00123
00124
00125 cfg->fracsec = malloc(5*sizeof(unsigned char));
00126 if(!cfg->fracsec) {
00127 fprintf(fp_log,"Not enough memory for cfg->fracsec\n");
00128 }
00129
00130
00131 cfg->time_base = malloc(5*sizeof(unsigned char));
00132 if(!cfg->time_base) {
00133 fprintf(fp_log,"Not enough memory for cfg->time_base\n");
00134 }
00135
00136
00137 cfg->time_base = malloc(5*sizeof(unsigned char));
00138 if(!cfg->time_base) {
00139 fprintf(fp_log,"No enough memory for cfg->time_base\n");
00140 }
00141
00142
00143 cfg->num_pmu = malloc(3*sizeof(unsigned char));
00144 if(!cfg->num_pmu) {
00145 fprintf(fp_log,"No enough memory for cfg->num_pmu\n");
00146 }
00147
00148
00149 cfg->data_rate = malloc(3*sizeof(unsigned char));
00150 if(!cfg->data_rate) {
00151 fprintf(fp_log,"No enough memory for cfg->data_rate\n");
00152 }
00153
00154
00155 copy_cbyc(sync,(unsigned char *)s,2);
00156 sync[2] = '\0';
00157 s = s + 2;
00158
00159
00160 copy_cbyc(cfg->framesize,(unsigned char *)s,2);
00161 cfg->framesize[2] = '\0';
00162 unsigned int framesize;
00163 framesize = to_intconvertor(cfg->framesize);
00164 s = s + 2;
00165
00166
00167 fwrite(st, sizeof(unsigned char),framesize, fp);
00168
00169
00170 copy_cbyc(cfg->idcode,(unsigned char *)s,2);
00171 cfg->idcode[2] = '\0';
00172 int id = to_intconvertor(cfg->idcode);
00173 fprintf(fp_log,"ID Code %d\n",id);
00174 s = s + 2;
00175
00176
00177 remove_id_from_status_change_list(cfg->idcode);
00178
00179
00180 copy_cbyc(cfg->soc,(unsigned char *)s,4);
00181 cfg->soc[4] = '\0';
00182 s =s + 4;
00183
00184
00185 copy_cbyc(cfg->fracsec,(unsigned char *)s,4);
00186 cfg->fracsec[4] = '\0';
00187 s = s + 4;
00188
00189
00190 copy_cbyc (cfg->time_base,(unsigned char *)s,4);
00191 cfg->time_base[4]='\0';
00192 s = s + 4;
00193
00194
00195 copy_cbyc (cfg->num_pmu,(unsigned char *)s,2);
00196 cfg->num_pmu[2] = '\0';
00197 s = s + 2;
00198
00199 num_pmu = to_intconvertor(cfg->num_pmu);
00200 fprintf(fp_log,"Number of PMU's %d\n",num_pmu);
00201
00202
00203 cfg->pmu = malloc(num_pmu* sizeof(struct for_each_pmu *));
00204 if(!cfg->pmu) {
00205 fprintf(fp_log,"Not enough memory pmu[][]\n");
00206 exit(1);
00207 }
00208
00209 for (i = 0; i < num_pmu; i++) {
00210 cfg->pmu[i] = malloc(sizeof(struct for_each_pmu));
00211 }
00212
00213 j = 0;
00214
00216 while(j<num_pmu) {
00217
00218
00219 cfg->pmu[j]->stn = malloc(17*sizeof(unsigned char));
00220 if(!cfg->pmu[j]->stn) {
00221 fprintf(fp_log,"Not enough memory cfg->pmu[j]->stn\n");
00222 exit(1);
00223 }
00224
00225
00226 cfg->pmu[j]->idcode = malloc(3*sizeof(unsigned char));
00227 if(!cfg->pmu[j]->idcode) {
00228 fprintf(fp_log,"Not enough memory cfg->pmu[j]->idcode\n");
00229 exit(1);
00230 }
00231
00232
00233 cfg->pmu[j]->data_format = malloc(3*sizeof(unsigned char));
00234 if(!cfg->pmu[j]->data_format) {
00235 fprintf(fp_log,"Not enough memory cfg->pmu[j]->data_format\n");
00236 exit(1);
00237 }
00238
00239
00240 cfg->pmu[j]->phnmr = malloc(3*sizeof(unsigned char));
00241 if(!cfg->pmu[j]->phnmr) {
00242 fprintf(fp_log,"Not enough memory cfg->pmu[j]->phnmr\n");
00243 exit(1);
00244 }
00245
00246
00247 cfg->pmu[j]->annmr = malloc(3*sizeof(unsigned char));
00248 if(!cfg->pmu[j]->annmr) {
00249 fprintf(fp_log,"Not enough memory cfg->pmu[j]->annmr\n");
00250 exit(1);
00251 }
00252
00253
00254 cfg->pmu[j]->dgnmr = malloc(3*sizeof(unsigned char));
00255 if(!cfg->pmu[j]->dgnmr) {
00256 fprintf(fp_log,"Not enough memory cfg->pmu[j]->dgnmr\n");
00257 exit(1);
00258 }
00259
00260
00261 cfg->pmu[j]->fnom = malloc(3*sizeof(unsigned char));
00262 if(!cfg->pmu[j]->fnom) {
00263 fprintf(fp_log,"Not enough memory cfg->pmu[j]->fnom\n");
00264 exit(1);
00265 }
00266
00267
00268 cfg->pmu[j]->cfg_cnt = malloc(3*sizeof(unsigned char));
00269 if(!cfg->pmu[j]->cfg_cnt) {
00270 fprintf(fp_log,"Not enough memory cfg->pmu[j]->cfg_cnt\n");
00271 exit(1);
00272 }
00273
00274
00275 copy_cbyc (cfg->pmu[j]->stn,(unsigned char *)s,16);
00276 cfg->pmu[j]->stn[16] = '\0';
00277 s = s + 16;
00278
00279
00280 copy_cbyc (cfg->pmu[j]->idcode,(unsigned char *)s,2);
00281 cfg->pmu[j]->idcode[2] = '\0';
00282 s = s + 2;
00283
00284
00285 copy_cbyc (cfg->pmu[j]->data_format,(unsigned char *)s,2);
00286 cfg->pmu[j]->data_format[2]='\0';
00287 s = s + 2;
00288
00289 unsigned char hex = cfg->pmu[j]->data_format[1];
00290 hex <<= 4;
00291
00292
00293 cfg->pmu[j]->fmt = malloc(sizeof(struct format));
00294 if((hex & 0x80) == 0x80) cfg->pmu[j]->fmt->freq = '1'; else cfg->pmu[j]->fmt->freq = '0';
00295 if((hex & 0x40) == 0x40 ) cfg->pmu[j]->fmt->analog = '1'; else cfg->pmu[j]->fmt->analog = '0';
00296 if((hex & 0x20) == 0x20) cfg->pmu[j]->fmt->phasor = '1'; else cfg->pmu[j]->fmt->phasor = '0';
00297 if((hex & 0x10) == 0x10) cfg->pmu[j]->fmt->polar = '1'; else cfg->pmu[j]->fmt->polar = '0';
00298
00299
00300 copy_cbyc (cfg->pmu[j]->phnmr,(unsigned char *)s,2);
00301 cfg->pmu[j]->phnmr[2]='\0';
00302 s = s + 2;
00303
00304 phn = to_intconvertor(cfg->pmu[j]->phnmr);
00305
00306
00307 copy_cbyc (cfg->pmu[j]->annmr,(unsigned char *)s,2);
00308 cfg->pmu[j]->annmr[2]='\0';
00309 s = s + 2;
00310
00311 ann = to_intconvertor(cfg->pmu[j]->annmr);
00312
00313
00314 copy_cbyc (cfg->pmu[j]->dgnmr,(unsigned char *)s,2);
00315 cfg->pmu[j]->dgnmr[2]='\0';
00316 s = s + 2;
00317
00318 dgn = to_intconvertor(cfg->pmu[j]->dgnmr);
00319
00320 fprintf(fp_log,"Phasor %d Analogs %d Digitals %d\n",phn,ann,dgn);
00321
00322 cn = malloc(sizeof(struct channel_names));
00323 cn->first = NULL;
00324
00326 if(phn != 0){
00327 cn->phnames = malloc(phn*sizeof(unsigned char*));
00328 if(!cn->phnames) {
00329 fprintf(fp_log,"Not enough memory cfg->pmu[j]->cn->phnames[][]\n");
00330 exit(1);
00331 }
00332
00333 for (i = 0; i < phn; i++) {
00334
00335 cn->phnames[i] = malloc(17*sizeof(unsigned char));
00336
00337 }
00338
00339 i = 0;
00340 while(i<phn){
00341 copy_cbyc (cn->phnames[i],(unsigned char *)s,16);
00342 cn->phnames[i][16]='\0';
00343 fprintf(fp_log,"Phnames %s\n",cn->phnames[i]);
00344 s = s + 16;
00345 i++;
00346
00347 }
00348 }
00349
00350
00351 if(ann != 0){
00352 cn->angnames = malloc(ann*sizeof(unsigned char*));
00353 if(!cn->angnames) {
00354
00355 fprintf(fp_log,"Not enough memory cfg->pmu[j]->cn->phnames[][]\n");
00356 exit(1);
00357 }
00358
00359 for (i = 0; i < ann; i++) {
00360
00361 cn->angnames[i] = malloc(17*sizeof(unsigned char));
00362 }
00363
00364
00365 i=0;
00366 while(i<ann){
00367
00368 copy_cbyc (cn->angnames[i],(unsigned char *)s,16);
00369 cn->angnames[i][16]='\0';
00370 fprintf(fp_log,"ANGNAMES %s\n",cn->angnames[i]);
00371 s = s + 16;
00372 i++;
00373 }
00374 }
00375
00376 struct dgnames *q;
00377 i = 0;
00378 while(i < dgn) {
00379 struct dgnames *temp1 = malloc(sizeof(struct dgnames));
00380 temp1->dgn = malloc(16*sizeof(unsigned char *));
00381 if(!temp1->dgn) {
00382
00383 fprintf(fp_log,"Not enough memory temp1->dgn\n");
00384 exit(1);
00385 }
00386
00387 for (i = 0; i < 16; i++) {
00388
00389 temp1->dgn[i] = malloc(17*sizeof(unsigned char));
00390
00391 }
00392
00393 temp1->dg_next = NULL;
00394 for(dgchannels = 0;dgchannels < 16; dgchannels++){
00395
00396 copy_cbyc (temp1->dgn[dgchannels],(unsigned char *)s,16);
00397 temp1->dgn[dgchannels][16]='\0';
00398 s += 16;
00399 fprintf(fp_log,"%s\n",temp1->dgn[dgchannels]);
00400
00401 }
00402
00403 if(cn->first == NULL){
00404 cn->first = q = temp1;
00405
00406
00407 } else {
00408
00409 while(q->dg_next!=NULL){
00410 q = q->dg_next;
00411 }
00412 q->dg_next = temp1;
00413 }
00414
00415 i++;
00416 }
00417
00418 cfg->pmu[j]->cnext = cn;
00419
00421 if(phn != 0){
00422
00423 cfg->pmu[j]->phunit = malloc(phn*sizeof(unsigned char*));
00424 if(!cfg->pmu[j]->phunit) {
00425 fprintf(fp_log,"Not enough memory cfg->pmu[j]->phunit[][]\n");
00426 exit(1);
00427 }
00428
00429 for (i = 0; i < phn; i++) {
00430 cfg->pmu[j]->phunit[i] = malloc(5);
00431 }
00432
00433 i = 0;
00434 while(i<phn){
00435
00436 copy_cbyc (cfg->pmu[j]->phunit[i],(unsigned char *)s,4);
00437 cfg->pmu[j]->phunit[i][4] = '\0';
00438 s = s + 4;
00439 i++;
00440 }
00441 }
00442
00443
00444 if(ann != 0){
00445
00446 cfg->pmu[j]->anunit = malloc(ann*sizeof(unsigned char*));
00447 if(!cfg->pmu[j]->anunit) {
00448 fprintf(fp_log,"Not enough memory cfg->pmu[j]->anunit[][]\n");
00449 exit(1);
00450 }
00451
00452 for (i = 0; i < ann; i++) {
00453 cfg->pmu[j]->anunit[i] = malloc(5);
00454 }
00455
00456 i = 0;
00457 while(i<ann){
00458
00459 copy_cbyc (cfg->pmu[j]->anunit[i],(unsigned char *)s,4);
00460 cfg->pmu[j]->anunit[i][4] = '\0';
00461 s = s + 4;
00462 i++;
00463 }
00464
00465 }
00466
00467 if(dgn != 0){
00468
00469 cfg->pmu[j]->dgunit = malloc(dgn*sizeof(unsigned char*));
00470 if(!cfg->pmu[j]->dgunit) {
00471
00472 fprintf(fp_log,"Not enough memory cfg->pmu[j]->dgunit[][]\n");
00473 exit(1);
00474 }
00475
00476 for (i = 0; i < dgn; i++) {
00477
00478 cfg->pmu[j]->dgunit[i] = malloc(5);
00479 }
00480
00481 i=0;
00482 while(i<dgn){
00483 copy_cbyc (cfg->pmu[j]->dgunit[i],(unsigned char *)s,4);
00484 cfg->pmu[j]->dgunit[i][4] = '\0';
00485 s = s + 4;
00486 i++;
00487 }
00488 }
00489
00490
00491 copy_cbyc (cfg->pmu[j]->fnom,(unsigned char *)s,2);
00492 cfg->pmu[j]->fnom[2]='\0';
00493 s = s + 2;
00494
00495 copy_cbyc (cfg->pmu[j]->cfg_cnt,(unsigned char *)s,2);
00496 cfg->pmu[j]->cfg_cnt[2]='\0';
00497 s = s + 2;
00498 j++;
00499 }
00500
00501
00502 copy_cbyc (cfg->data_rate,(unsigned char *)s,2);
00503 cfg->data_rate[2] = '\0';
00504 s += 2;
00505 cfg->cfgnext = NULL;
00506
00507
00508
00509
00510 pthread_mutex_lock(&mutex_cfg);
00511
00512
00513 int index = 0;
00514
00515 if (cfgfirst == NULL) {
00516
00517 cfgfirst = cfg;
00518 fclose(fp);
00519
00520 } else {
00521
00522 struct cfg_frame *temp_cfg = cfgfirst,*tprev_cfg;
00523 tprev_cfg = temp_cfg;
00524
00525
00526 while(temp_cfg!=NULL){
00527 if(!ncmp_cbyc(cfg->idcode,temp_cfg->idcode,2)) {
00528
00529 match = 1;
00530 break;
00531
00532 } else {
00533
00534 index++;
00535 tprev_cfg = temp_cfg;
00536 temp_cfg = temp_cfg->cfgnext;
00537
00538 }
00539 }
00540
00541 if(match) {
00542
00543 if(!index) {
00544
00545
00546 cfg->cfgnext = cfgfirst->cfgnext;
00547 free_cfgframe_object(cfgfirst);
00548 cfgfirst = cfg;
00549
00550 fclose(fp);
00551 remove_old_cfg(cfg->idcode,st);
00552
00553 } else {
00554
00555
00556 tprev_cfg->cfgnext = cfg;
00557 cfg->cfgnext = temp_cfg->cfgnext;
00558 free_cfgframe_object(temp_cfg);
00559 fclose(fp);
00560 remove_old_cfg(cfg->idcode,st);
00561 }
00562
00563 } else {
00564
00565 tprev_cfg->cfgnext = cfg;
00566 fclose(fp);
00567 }
00568
00569 }
00570
00571 pthread_mutex_unlock(&mutex_cfg);
00572 pthread_mutex_unlock(&mutex_file);
00573
00574 }
00575
00576
00577
00578
00579
00580
00581
00582
00583 void remove_old_cfg(unsigned char idcode[],unsigned char frame[]) {
00584
00585 FILE *file,*newfile;
00586 int result;
00587 unsigned char id[3],*line,*s,frame_len[2];
00588 unsigned long fileLen;
00589 unsigned int framesize;
00590
00591 file = fopen("cfg.bin","rb");
00592 newfile = fopen("ncfg.bin","wb");
00593
00594 if (file != NULL) {
00595
00596
00597 fseek(file, 0, SEEK_END);
00598 fileLen = ftell(file);
00599 fseek(file, 0, SEEK_SET);
00600
00601
00602 while (fileLen != 0) {
00603
00604 fseek (file,2 ,SEEK_CUR);
00605 fread(frame_len, sizeof(unsigned char),2, file);
00606 fseek (file,-4 ,SEEK_CUR);
00607
00608 framesize = to_intconvertor(frame_len);
00609 line = malloc(framesize*sizeof(unsigned char));
00610 memset(line,'\0',sizeof(line));
00611 fread(line, sizeof(unsigned char),framesize,file);
00612 s = line;
00613 s += 4;
00614
00615 copy_cbyc (id,s,2);
00616 id[2] = '\0';
00617 if(!ncmp_cbyc(id,idcode,2)) {
00618
00619 fprintf(fp_log,"MATCHED the new cfg with old cfg in file cfg.bin\n");
00620 break;
00621
00622 } else {
00623
00624
00625 fwrite(line, sizeof(unsigned char),framesize,newfile);
00626 free(line);
00627 fileLen -= framesize;
00628 }
00629 }
00630
00631
00632 unsigned int len;
00633 unsigned char *p = frame;
00634 p += 2;
00635 copy_cbyc (frame_len,p,2);
00636 len = to_intconvertor(frame_len);
00637 fwrite(frame, sizeof(unsigned char),len,newfile);
00638
00639 fileLen -= framesize;
00640
00641 while (fileLen != 0) {
00642
00643 fseek (file,2 ,SEEK_CUR);
00644 fread(frame_len, sizeof(unsigned char),2, file);
00645 fseek (file,-4 ,SEEK_CUR);
00646
00647 framesize = to_intconvertor(frame_len);
00648 line = malloc(framesize*sizeof(unsigned char));
00649 memset(line,'\0',sizeof(line));
00650 fread(line, sizeof(unsigned char),framesize,file);
00651
00652 if(!ncmp_cbyc(line,frame,framesize)) {
00653
00654
00655
00656 break;
00657
00658 } else {
00659
00660 fwrite(line, sizeof(unsigned char),framesize,newfile);
00661 free(line);
00662 fileLen -= framesize;
00663 }
00664
00665 }
00666
00667
00668 fclose (file);
00669 fclose(newfile);
00670
00671 if( remove( "cfg.bin" ) != 0 )
00672 perror( "Error deleting file" );
00673 result= rename("ncfg.bin","cfg.bin");
00674 if ( result == 0 )
00675 fputs ("File successfully renamed",fp_log);
00676 else
00677 perror( "Error renaming file" );
00678
00679 } else {
00680 perror ("cfg.bin");
00681 }
00682
00683 }
00684
00685
00686
00687
00688
00689
00690
00691 int dataparser(unsigned char data[]) {
00692
00693 int match=0,i,j=0;
00694 int stat_status,config_change = 0;
00695 unsigned int num_pmu,phnmr,annmr,dgnmr;
00696 unsigned char framesize[3],idcode[3],stat[3],outer_stat[3],*d;
00697 struct cfg_frame *temp_cfg;
00698 struct data_frame *df;
00699
00700 fprintf(fp_log,"Inside Data Parser\n");
00701 d = data;
00702
00703 d += 2;
00704
00705
00706 copy_cbyc (framesize,d,2);
00707 framesize[2]='\0';
00708 d += 2;
00709
00710
00711 copy_cbyc (idcode,d,2);
00712 idcode[2]='\0';
00713
00714 unsigned int id = to_intconvertor(idcode);
00715 fprintf(fp_log,"ID Code %d\n",id);
00716
00717 pthread_mutex_lock(&mutex_cfg);
00718
00719 temp_cfg = cfgfirst;
00720
00721 while(temp_cfg != NULL){
00722
00723 if(!ncmp_cbyc(idcode,temp_cfg->idcode,2)) {
00724
00725 match = 1;
00726 break;
00727
00728 } else {
00729
00730 temp_cfg = temp_cfg->cfgnext;
00731
00732 }
00733 }
00734 pthread_mutex_unlock(&mutex_cfg);
00735
00736
00737 if(match){
00738
00739
00740
00741
00742 df = malloc(sizeof(struct data_frame));
00743 if(!df) {
00744 fprintf(fp_log,"Not enough memory df\n");
00745 exit(1);
00746 }
00747 df->dnext = NULL;
00748
00749
00750 df->framesize = malloc(3*sizeof(unsigned char));
00751 if(!df->framesize) {
00752 fprintf(fp_log,"Not enough memory df->idcode\n");
00753 exit(1);
00754 }
00755
00756
00757 df->idcode = malloc(3*sizeof(unsigned char));
00758 if(!df->idcode) {
00759 fprintf(fp_log,"Not enough memory df->idcode\n");
00760 exit(1);
00761 }
00762
00763
00764 df->soc = malloc(5*sizeof(unsigned char));
00765 if(!df->soc) {
00766 fprintf(fp_log,"Not enough memory df->soc\n");
00767 exit(1);
00768 }
00769
00770
00771 df->fracsec = malloc(5*sizeof(unsigned char));
00772 if(!df->fracsec) {
00773 fprintf(fp_log,"Not enough memory df->fracsec\n");
00774 exit(1);
00775 }
00776
00777
00778 num_pmu = to_intconvertor(temp_cfg->num_pmu);
00779 d += 12;
00780
00781 if(num_pmu > 1 ) {
00782
00783 copy_cbyc (outer_stat,d,2);
00784 if((outer_stat[0] & 0x04) == 0x04) {
00785
00786 add_id_to_status_change_list(idcode);
00787 return 14;
00788 }
00789
00790 }
00791 df->dpmu = malloc(num_pmu* sizeof(struct data_for_each_pmu *));
00792 if(!df->dpmu) {
00793 fprintf(fp_log,"Not enough memory df->dpmu[][]\n");
00794 exit(1);
00795 }
00796
00797 for (i = 0; i < num_pmu; i++) {
00798 df->dpmu[i] = malloc(sizeof(struct data_for_each_pmu));
00799 }
00800
00801
00802
00803
00804 d -= 14;
00805 copy_cbyc (df->framesize,d,2);
00806 df->framesize[2] = '\0';
00807 d += 2;
00808
00809
00810 copy_cbyc (df->idcode,idcode,2);
00811 df->idcode[2] = '\0';
00812 d += 2;
00813
00814
00815 copy_cbyc (df->soc,d,4);
00816 df->soc[4] = '\0';
00817 d += 4;
00818
00819
00820 copy_cbyc (df->fracsec,d,4);
00821 df->fracsec[4] = '\0';
00822 d += 4;
00823
00824
00825 df->num_pmu = num_pmu;
00826
00827 if(num_pmu > 1 )
00828 d += 2;
00829
00830
00831 while(j<num_pmu) {
00832
00833 copy_cbyc (stat,d,2);
00834 stat[2] = '\0';
00835 d += 2;
00836
00837
00838 stat_status = check_statword(stat);
00839
00840
00841 if(stat_status == 16) {
00842
00843 df->dpmu[j]->stat = malloc(3*sizeof(unsigned char));
00844 if(!df->dpmu[j]->stat) {
00845 fprintf(fp_log,"Not enough memory for df->dpmu[j]->stat\n");
00846 }
00847 copy_cbyc(df->dpmu[j]->stat,stat,2);
00848 df->dpmu[j]->stat[2] = '\0';
00849 memset(stat,'\0',3);
00850 j++;
00851 continue;
00852
00853 } else if((stat_status == 14)||(stat_status == 10)) {
00854
00855
00856 add_id_to_status_change_list(idcode);
00857
00858
00859 df->dpmu[j]->stat = malloc(3*sizeof(unsigned char));
00860 if(!df->dpmu[j]->stat) {
00861 fprintf(fp_log,"Not enough memory for df->dpmu[j]->stat\n");
00862 }
00863 copy_cbyc(df->dpmu[j]->stat,stat,2);
00864 df->dpmu[j]->stat[2] = '\0';
00865 memset(stat,'\0',3);
00866
00867 config_change = stat_status;
00868 j++;
00869 continue;
00870
00871 }
00872
00873
00874 phnmr = to_intconvertor(temp_cfg->pmu[j]->phnmr);
00875 annmr = to_intconvertor(temp_cfg->pmu[j]->annmr);
00876 dgnmr = to_intconvertor(temp_cfg->pmu[j]->dgnmr);
00877
00878
00879
00880
00881
00882 df->dpmu[j]->stat = malloc(3*sizeof(unsigned char));
00883 if(!df->dpmu[j]->stat) {
00884
00885 fprintf(fp_log,"Not enough memory for df->dpmu[j]->stat\n");
00886
00887 }
00888
00889 df->dpmu[j]->phasors = malloc(phnmr*sizeof(unsigned char *));
00890 if(!df->dpmu[j]->phasors) {
00891
00892 fprintf(fp_log,"Not enough memory df->dpmu[j]->phasors[][]\n");
00893 exit(1);
00894
00895 }
00896 if(temp_cfg->pmu[j]->fmt->phasor == '1') {
00897 for (i = 0; i < phnmr; i++)
00898 df->dpmu[j]->phasors[i] = malloc(9*sizeof(unsigned char));
00899 } else {
00900
00901 for (i = 0; i < phnmr; i++)
00902 df->dpmu[j]->phasors[i] = malloc(5*sizeof(unsigned char));
00903 }
00904
00905
00906
00907
00908 df->dpmu[j]->analog = malloc(annmr*sizeof(unsigned char *));
00909 if(!df->dpmu[j]->analog) {
00910 fprintf(fp_log,"Not enough memory df->dpmu[j]->analog[][]\n");
00911 exit(1);
00912 }
00913
00914 if(temp_cfg->pmu[j]->fmt->analog == '1') {
00915 for (i = 0; i < annmr; i++)
00916 df->dpmu[j]->analog[i] = malloc(9*sizeof(unsigned char));
00917 } else {
00918
00919 for (i = 0; i < annmr; i++)
00920 df->dpmu[j]->analog[i] = malloc(5*sizeof(unsigned char));
00921 }
00922
00923
00924
00925 if(temp_cfg->pmu[j]->fmt->freq == '1') {
00926
00927 df->dpmu[j]->freq = malloc(5*sizeof(unsigned char));
00928 df->dpmu[j]->dfreq = malloc(5*sizeof(unsigned char));
00929
00930
00931 } else {
00932
00933 df->dpmu[j]->freq = malloc(3*sizeof(unsigned char));
00934 df->dpmu[j]->dfreq = malloc(3*sizeof(unsigned char));
00935
00936 }
00937
00938
00939 df->dpmu[j]->digital = malloc(dgnmr* sizeof(unsigned char*));
00940 if(!df->dpmu[j]->digital) {
00941 fprintf(fp_log,"Not enough memory df->dpmu[j]->digital[][]\n");
00942 exit(1);
00943 }
00944
00945 for (i = 0; i < dgnmr; i++) {
00946 df->dpmu[j]->digital[i] = malloc(3*sizeof(unsigned char));
00947 }
00948
00949
00950
00951
00952 copy_cbyc (df->dpmu[j]->stat,stat,2);
00953 df->dpmu[j]->stat[2] = '\0';
00954 memset(stat,'\0',3);
00955
00956
00957 df->dpmu[j]->fmt = malloc(sizeof(struct format));
00958 df->dpmu[j]->fmt->freq = temp_cfg->pmu[j]->fmt->freq;
00959 df->dpmu[j]->fmt->analog = temp_cfg->pmu[j]->fmt->analog;
00960 df->dpmu[j]->fmt->phasor = temp_cfg->pmu[j]->fmt->phasor;
00961 df->dpmu[j]->fmt->polar = temp_cfg->pmu[j]->fmt->polar;
00962
00963
00964 df->dpmu[j]->phnmr = phnmr;
00965 df->dpmu[j]->annmr = annmr;
00966 df->dpmu[j]->dgnmr = dgnmr;
00967
00968
00969 if(temp_cfg->pmu[j]->fmt->phasor == '1') {
00970 for(i=0;i<phnmr;i++){
00971
00972 copy_cbyc (df->dpmu[j]->phasors[i],d,8);
00973 df->dpmu[j]->phasors[i][8] = '\0';
00974 d += 8;
00975 }
00976 } else {
00977 for(i=0;i<phnmr;i++){
00978 copy_cbyc (df->dpmu[j]->phasors[i],d,4);
00979 df->dpmu[j]->phasors[i][4] = '\0';
00980 d += 4;
00981 }
00982 }
00983
00984
00985
00986 if(temp_cfg->pmu[j]->fmt->freq == '1') {
00987 copy_cbyc (df->dpmu[j]->freq,d,4);
00988 df->dpmu[j]->freq[4] = '\0';
00989 d += 4;
00990
00991 copy_cbyc (df->dpmu[j]->dfreq,d,4);
00992 df->dpmu[j]->dfreq[4] = '\0';
00993 d += 4;
00994
00995 } else {
00996 copy_cbyc (df->dpmu[j]->freq,d,2);
00997 df->dpmu[j]->freq[2] = '\0';
00998 d += 2;
00999
01000 copy_cbyc (df->dpmu[j]->dfreq,d,2);
01001 df->dpmu[j]->dfreq[2] = '\0';
01002 d += 2;
01003 }
01004
01005
01006 if(temp_cfg->pmu[j]->fmt->analog == '1') {
01007
01008 for(i = 0; i<annmr; i++){
01009
01010 copy_cbyc (df->dpmu[j]->analog[i],d,4);
01011 df->dpmu[j]->analog[i][4] = '\0';
01012 d += 4;
01013
01014 }
01015 } else {
01016 for(i = 0; i<annmr; i++){
01017
01018 copy_cbyc (df->dpmu[j]->analog[i],d,2);
01019 df->dpmu[j]->analog[i][2] = '\0';
01020 d += 2;
01021 }
01022 }
01023
01024
01025 for(i = 0; i<dgnmr; i++) {
01026
01027 copy_cbyc (df->dpmu[j]->digital[i],d,2);
01028 df->dpmu[j]->digital[i][2] = '\0';
01029 d += 2;
01030 }
01031
01032 j++;
01033 }
01034
01035
01036 time_align(df);
01037
01038 } else {
01039
01040
01041 fprintf(fp_log,"NO CFG for data frames\n");
01042
01043 }
01044
01045 if((config_change == 14) ||(config_change == 10))
01046 return config_change;
01047 else return stat_status;
01048
01049 }
01050
01051
01052
01053
01054
01055
01056
01057 int check_statword(unsigned char stat[]) {
01058
01059 int ret = 0;
01060
01061 if(stat[1] == 0x0f) {
01062
01063 ret = 16;
01064 return ret;
01065
01066 } else if ((stat[0] & 0x04) == 0x04) {
01067
01068 fprintf(fp_log,"Configuration Change error\n");
01069 ret = 10;
01070 return ret;
01071
01072 } else if ((stat[0] & 0x40) == 0x40) {
01073
01074 fprintf(fp_log,"PMU error including configuration error\n");
01075 ret = 14;
01076 return ret;
01077
01078 } else if((stat[0] & 0x80) == 0x80) {
01079
01080 fprintf(fp_log,"Data invalid\n");
01081 ret = 15;
01082 return ret;
01083
01084 } else if ((stat[0] & 0x20) == 0x20) {
01085
01086 fprintf(fp_log,"PMU Sync error\n");
01087 ret = 13;
01088 return ret;
01089
01090 } else if ((stat[0] & 0x10) == 0x10) {
01091
01092 fprintf(fp_log,"Data sorting error\n");
01093 ret = 12;
01094 return ret;
01095
01096 } else if ((stat[0] & 0x08) == 0x08) {
01097
01098 fprintf(fp_log,"PMU Trigger error\n");
01099 ret = 11;
01100 return ret;
01101
01102 }
01103
01104 return ret;
01105 }
01106
01107
01108
01109
01110
01111
01112
01113
01114 void add_id_to_status_change_list(unsigned char idcode[]) {
01115
01116 fprintf(fp_log,"Inside add_id_to_status_change_list()\n");
01117 struct status_change_pmupdcid *t;
01118
01119 t = malloc(sizeof(struct status_change_pmupdcid));
01120 if(!t) {
01121 fprintf(fp_log,"No enough memory for struct (status_change_pmupdcid) t\n");
01122 }
01123 copy_cbyc(t->idcode,idcode,2);
01124 t->idcode[2] = '\0';
01125 t->pmuid_next = NULL;
01126
01127 pthread_mutex_lock(&mutex_status_change);
01128
01129 if(root_pmuid == NULL) {
01130
01131 t = root_pmuid;
01132
01133 } else {
01134
01135 struct status_change_pmupdcid *temp = root_pmuid;
01136 while(temp->pmuid_next!=NULL) {
01137 temp = temp->pmuid_next;
01138 }
01139
01140 temp->pmuid_next = t;
01141 }
01142
01143 pthread_mutex_unlock(&mutex_status_change);
01144
01145 }
01146
01147
01148
01149
01150
01151
01152
01153 void remove_id_from_status_change_list(unsigned char idcode[]) {
01154
01155 fprintf(fp_log,"Inside remove_id_from_status_change_list()\n");
01156 struct status_change_pmupdcid *tprev;
01157
01158 pthread_mutex_lock(&mutex_status_change);
01159
01160 if(root_pmuid == NULL) {
01161
01162 fprintf(fp_log,"***No Stat change***\n");
01163
01164 } else {
01165
01166 struct status_change_pmupdcid *temp = root_pmuid;
01167
01168 while(temp != NULL) {
01169 if(!ncmp_cbyc(temp->idcode,idcode,2)) {
01170
01171 break;
01172
01173 } else {
01174
01175 tprev = temp;
01176 temp = temp->pmuid_next;
01177 }
01178 }
01179
01180 if(!ncmp_cbyc(temp->idcode,root_pmuid->idcode,2)) {
01181
01182 root_pmuid = root_pmuid->pmuid_next;
01183 free(temp);
01184
01185 } else {
01186
01187 tprev->pmuid_next = temp->pmuid_next;
01188 free(temp);
01189
01190 }
01191 }
01192 pthread_mutex_unlock(&mutex_status_change);
01193 }
01194
01195
01196
01197
01198
01199 unsigned int to_intconvertor(unsigned char array[]) {
01200
01201 unsigned int n;
01202 n = array[0];
01203 n <<= 8;
01204 n |= array[1];
01205 return n;
01206 }
01207
01208
01209
01210
01211
01212 void long_int_to_ascii_convertor(unsigned long int n,unsigned char hex[]) {
01213
01214 hex[0] = n >> 24;
01215 hex[1] = n >> 16;
01216 hex[2] = n >> 8;
01217 hex[3] = n ;
01218
01219 }
01220
01221
01222
01223
01224
01225 void int_to_ascii_convertor(unsigned int n,unsigned char hex[]) {
01226
01227 hex[0] = n >> 8;
01228 hex[1] = n ;
01229
01230 }
01231
01232
01233
01234
01235
01236
01237 void copy_cbyc(unsigned char dst[],unsigned char *s,int size) {
01238
01239 int i;
01240 for(i = 0; i< size; i++)
01241 dst[i] = *(s + i);
01242 }
01243
01244
01245
01246
01247
01248 int ncmp_cbyc(unsigned char dst[],unsigned char src[],int size) {
01249
01250 int i,flag = 0;
01251 for(i = 0; i< size; i++) {
01252
01253 if(dst[i] != src[i]) {
01254
01255 flag = 1;
01256 break;
01257 }
01258 }
01259 return flag;
01260 }
01261
01262
01263
01264
01265
01266 void byte_by_byte_copy(unsigned char dst[],unsigned char src[],int index,int n) {
01267
01268 int i;
01269 for(i = 0;i<n; i++)
01270 dst[index + i] = src[i];
01271
01272 }
01273
01274
01275
01276
01277
01278 unsigned long int to_long_int_convertor(unsigned char array[]) {
01279
01280 unsigned long int n;
01281 n = array[0];
01282 n <<= 8;
01283 n |= array[1];
01284 n <<= 8;
01285 n |= array[2];
01286 n <<= 8;
01287 n |= array[3];
01288 return n;
01289
01290 }
01291
01292
01293
01294
01295
01296
01297 uint16_t compute_CRC(unsigned char *message,char length) {
01298
01299 uint16_t crc = 0x0ffff,temp,quick;
01300 int i;
01301
01302 for(i=0;i<length;i++){
01303 temp=(crc>>8)^message[i];
01304 crc<<=8;
01305 quick=temp ^ ( temp >>4);
01306 crc ^=quick;
01307 quick<<=5;
01308 crc ^=quick;
01309 quick <<=7;
01310 crc ^= quick;
01311 }
01312 return crc;
01313 }
01314