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 #include <stdio.h>
00046 #include <string.h>
00047 #include <stdlib.h>
00048 #include <pthread.h>
00049 #include <math.h>
00050 #include <float.h>
00051 #include <assert.h>
00052 #include <mysql.h>
00053 #include <errno.h>
00054 #include "parser.h"
00055 #include "global.h"
00056 #include "dallocate.h"
00057 #include "connections.h"
00058
00059 const int PI = 3.14;
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 void cfgparser(unsigned char st[]){
00070
00071 FILE *fp;
00072 unsigned char *s;
00073 int match = 0,dgchannels,i,j;
00074 struct cfg_frame *cfg;
00075 struct channel_names *cn;
00076 unsigned long int l_phunit,l_anunit;
00077
00078 unsigned char *sync,*frame_size,*idcode_h,*soc,*fracsec,*time_base,*num_pmu,*stn,*idcode_l,*format,*phnmr,*annmr,*dgnmr;
00079 unsigned char *fnom,*cfg_cnt,*data_rate,*buf;
00080
00081 sync = malloc(3*sizeof(unsigned char));
00082 frame_size = malloc(3*sizeof(unsigned char));
00083 idcode_h= malloc(3*sizeof(unsigned char));
00084 soc = malloc(5*sizeof(unsigned char));
00085 fracsec = malloc(5*sizeof(unsigned char));
00086 time_base = malloc(5*sizeof(unsigned char));
00087 num_pmu = malloc(3*sizeof(unsigned char));
00088 stn = malloc(17*sizeof(unsigned char));
00089 idcode_l = malloc(3*sizeof(unsigned char));
00090 format = malloc(5*sizeof(unsigned char));
00091 phnmr = malloc(3*sizeof(unsigned char));
00092 annmr = malloc(3*sizeof(unsigned char));
00093 dgnmr = malloc(3*sizeof(unsigned char));
00094
00095 fnom = malloc(3*sizeof(unsigned char));
00096 cfg_cnt = malloc(3*sizeof(unsigned char));
00097 data_rate = malloc(3*sizeof(unsigned char));
00098 buf = malloc(9*sizeof(unsigned char));
00099
00100
00101 memset(sync,'\0',3);
00102 memset(frame_size,'\0',3);
00103 memset(idcode_h,'\0',3);
00104 memset(soc,'\0',3);
00105 memset(fracsec,'\0',5);
00106 memset(time_base,'\0',5);
00107 memset(num_pmu,'\0',3);
00108 memset(stn,'\0',17);
00109 memset(idcode_l,'\0',3);
00110 memset(format,'\0',3);
00111 memset(phnmr,'\0',3);
00112 memset(annmr,'\0',3);
00113 memset(dgnmr,'\0',3);
00114
00115 memset(fnom,'\0',3);
00116 memset(cfg_cnt ,'\0',3);
00117 memset(data_rate,'\0',3);
00118 memset(buf,'\0',9);
00119
00120
00121
00122
00123 pthread_mutex_lock(&mutex_file);
00124
00125 fp = fopen("cfg.bin","ab");
00126
00127 if (fp == NULL)
00128 printf("File doesn't exist\n");
00129
00130 cfg = malloc(sizeof(struct cfg_frame));
00131 if(!cfg) {
00132 printf("No enough memory for cfg\n");
00133 }
00134
00135 printf("Inside cfgparser()\n");
00136 s = st;
00137
00138
00139 copy_cbyc (sync,(unsigned char *)s,2);
00140 sync[2] = '\0';
00141 s = s + 2;
00142
00143
00144 copy_cbyc (frame_size,(unsigned char *)s,2);
00145 frame_size[2]='\0';
00146 cfg->framesize = to_intconvertor(frame_size);
00147 printf("FRAME SIZE %d\n",cfg->framesize);
00148 s = s + 2;
00149
00150 size_t result;
00151
00152 for(i = 0;i<380;i++) printf("%x\t",st[i]);
00153 printf("\n");
00154
00155 result = fwrite(st, sizeof(unsigned char),cfg->framesize, fp);
00156 printf("No of bytes written %ld\n",(long int)result);
00157
00158 unsigned long fileLen;
00159 fseek(fp, 0, SEEK_END);
00160 fileLen = ftell(fp);
00161 fseek(fp, 0, SEEK_SET);
00162 printf("FILE LENGTH %ld\n",fileLen);
00163
00164
00165
00166 copy_cbyc (idcode_h,(unsigned char *)s,2);
00167 idcode_h[2] = '\0';
00168 cfg->idcode = to_intconvertor(idcode_h);
00169 printf("ID Code %d\n",cfg->idcode);
00170 s = s + 2;
00171
00172
00173 copy_cbyc (soc,(unsigned char *)s,4);
00174 soc[4] = '\0';
00175 cfg->soc = to_long_int_convertor(soc);
00176 printf("SOC %ld\n",cfg->soc);
00177 s =s + 4;
00178
00179
00180 copy_cbyc (fracsec,(unsigned char *)s,4);
00181 fracsec[4] = '\0';
00182 cfg->fracsec = to_long_int_convertor(fracsec);
00183 printf("FracSec %ld\n",cfg->fracsec);
00184 s = s + 4;
00185
00186
00187 copy_cbyc (time_base,(unsigned char *)s,4);
00188 time_base[4]='\0';
00189 cfg->time_base = to_long_int_convertor(time_base);
00190 printf("Time Base %ld\n",cfg->time_base);
00191 s = s + 4;
00192
00193
00194 copy_cbyc (num_pmu,(unsigned char *)s,2);
00195 num_pmu[2] = '\0';
00196 cfg->num_pmu = to_intconvertor(num_pmu);
00197 printf("Number of PMU's %d\n",cfg->num_pmu);
00198 s = s + 2;
00199
00200
00201 cfg->pmu = malloc(cfg->num_pmu* sizeof(struct for_each_pmu *));
00202 if(!cfg->pmu) {
00203 printf("Not enough memory pmu[][]\n");
00204 exit(1);
00205 }
00206
00207 for (i = 0; i < cfg->num_pmu; i++) {
00208 cfg->pmu[i] = malloc(sizeof(struct for_each_pmu));
00209 }
00210
00211 j = 0;
00212
00214 while(j<cfg->num_pmu) {
00215
00216
00217
00218 memset(cfg->pmu[j]->stn,'\0',17);
00219 copy_cbyc (cfg->pmu[j]->stn,(unsigned char *)s,16);
00220 cfg->pmu[j]->stn[16] = '\0';
00221
00222 printf("STATION NAME %s\n",cfg->pmu[j]->stn);
00223 s = s + 16;
00224
00225
00226 copy_cbyc (idcode_l,(unsigned char *)s,2);
00227 idcode_l[2]='\0';
00228 cfg->pmu[j]->idcode = to_intconvertor(idcode_l);
00229 printf("ID Code %d\n",cfg->pmu[j]->idcode);
00230 s = s + 2;
00231
00232
00233 copy_cbyc ((unsigned char *)cfg->pmu[j]->data_format,(unsigned char *)s,2);
00234 cfg->pmu[j]->data_format[2]='\0';
00235
00236 s = s + 2;
00237
00238 unsigned char hex = cfg->pmu[j]->data_format[1];
00239 hex <<= 4;
00240
00241
00242 cfg->pmu[j]->fmt = malloc(sizeof(struct format));
00243 if((hex & 0x80) == 0x80) cfg->pmu[j]->fmt->freq = '1'; else cfg->pmu[j]->fmt->freq = '0';
00244 if((hex & 0x40) == 0x40 ) cfg->pmu[j]->fmt->analog = '1'; else cfg->pmu[j]->fmt->analog = '0';
00245 if((hex & 0x20) == 0x20) cfg->pmu[j]->fmt->phasor = '1'; else cfg->pmu[j]->fmt->phasor = '0';
00246 if((hex & 0x10) == 0x10) cfg->pmu[j]->fmt->polar = '1'; else cfg->pmu[j]->fmt->polar = '0';
00247
00248
00249 copy_cbyc (phnmr,(unsigned char *)s,2);
00250 phnmr[2]='\0';
00251 cfg->pmu[j]->phnmr = to_intconvertor(phnmr);
00252 printf("Phasors %d\n",cfg->pmu[j]->phnmr);
00253 s = s + 2;
00254
00255
00256 copy_cbyc (annmr,(unsigned char *)s,2);
00257 annmr[2]='\0';
00258 cfg->pmu[j]->annmr = to_intconvertor(annmr);
00259 printf("Analogs %d\n",cfg->pmu[j]->annmr);
00260 s = s + 2;
00261
00262
00263 copy_cbyc (dgnmr,(unsigned char *)s,2);
00264 dgnmr[2]='\0';
00265 cfg->pmu[j]->dgnmr = to_intconvertor(dgnmr);
00266 printf("Digitals %d\n",cfg->pmu[j]->dgnmr);
00267 s = s + 2;
00268
00269 cn = malloc(sizeof(struct channel_names));
00270 cn->first = NULL;
00271
00273 if(cfg->pmu[j]->phnmr != 0){
00274 cn->phnames = malloc((cfg->pmu[j]->phnmr) * sizeof(unsigned char*));
00275 if(!cn->phnames) {
00276 printf("Not enough memory cfg->pmu[j]->cn->phnames[][]\n");
00277 exit(1);
00278 }
00279
00280 for (i = 0; i < cfg->pmu[j]->phnmr; i++) {
00281
00282 cn->phnames[i] = malloc(17*sizeof(unsigned char));
00283 memset(cn->phnames[i],'\0',17);
00284
00285 }
00286
00287 cfg->pmu[j]->phunit = malloc(cfg->pmu[j]->phnmr*sizeof(float*));
00288 if(!cfg->pmu[j]->phunit) {
00289 printf("Not enough memory cfg.pmu[j]->phunit[][]\n");
00290 exit(1);
00291 }
00292
00293 for (i = 0; i < cfg->pmu[j]->phnmr; i++) {
00294 cfg->pmu[j]->phunit[i] = malloc(sizeof(float));
00295 }
00296
00297
00298 i = 0;
00299 while(i<cfg->pmu[j]->phnmr){
00300
00301 copy_cbyc (cn->phnames[i],(unsigned char *)s,16);
00302 cn->phnames[i][16] = '\0';
00303 printf("Phnames %s\n",cn->phnames[i]);
00304 s = s + 16;
00305 i++;
00306 }
00307 }
00308
00309
00310 if(cfg->pmu[j]->annmr != 0){
00311 cn->angnames = malloc((cfg->pmu[j]->annmr)*sizeof(unsigned char*));
00312 if(!cn->angnames) {
00313
00314 printf("Not enough memory cfg->pmu[j]->cn->phnames[][]\n");
00315 exit(1);
00316 }
00317
00318 for (i = 0; i < cfg->pmu[j]->annmr; i++) {
00319
00320 cn->angnames[i] = malloc(17*sizeof(unsigned char));
00321 memset(cn->angnames[i],'\0',17);
00322 }
00323
00324 cfg->pmu[j]->anunit = malloc(cfg->pmu[j]->annmr*sizeof(float*));
00325 if(!cfg->pmu[j]->anunit) {
00326 printf("Not enough memory cfg.pmu[j]->anunit[][]\n");
00327 exit(1);
00328 }
00329
00330 for (i = 0; i < cfg->pmu[j]->annmr; i++) {
00331 cfg->pmu[j]->anunit[i] = malloc(sizeof(float));
00332 }
00333
00334 i = 0;
00335
00336 while(i<cfg->pmu[j]->annmr){
00337 copy_cbyc (cn->angnames[i],(unsigned char *)s,16);
00338 cn->angnames[i][16]='\0';
00339 printf("ANGNAMES %s\n",cn->angnames[i]);
00340 s = s + 16;
00341 i++;
00342 }
00343 }
00344
00345
00346 if(cfg->pmu[j]->dgnmr != 0){
00347
00348 cfg->pmu[j]->dgunit = malloc(cfg->pmu[j]->dgnmr*sizeof(unsigned char*));
00349 if(!cfg->pmu[j]->dgunit) {
00350
00351 printf("Not enough memory cfg->pmu[j]->dgunit[][]\n");
00352 exit(1);
00353 }
00354
00355 for (i = 0; i < cfg->pmu[j]->dgnmr; i++) {
00356
00357 cfg->pmu[j]->dgunit[i] = malloc(5);
00358 }
00359 }
00360 struct dgnames *q;
00361 i = 0;
00362 while(i < cfg->pmu[j]->dgnmr) {
00363
00364 struct dgnames *temp1 = malloc(sizeof(struct dgnames));
00365 temp1->dgn = malloc(16*sizeof(unsigned char *));
00366 if(!temp1->dgn) {
00367
00368 printf("Not enough memory temp1->dgn\n");
00369 exit(1);
00370 }
00371
00372 for (i = 0; i < 16; i++) {
00373
00374 temp1->dgn[i] = malloc(17*sizeof(unsigned char));
00375
00376 }
00377
00378 temp1->dg_next = NULL;
00379
00380 for(dgchannels = 0;dgchannels < 16;dgchannels++){
00381
00382 memset(temp1->dgn[dgchannels],'\0',16);
00383 copy_cbyc (temp1->dgn[dgchannels],(unsigned char *)s,16);
00384 temp1->dgn[dgchannels][16] = '\0';
00385 s += 16;
00386 printf("%s\n",temp1->dgn[dgchannels]);
00387 }
00388
00389 if(cn->first == NULL){
00390 cn->first = q = temp1;
00391
00392 } else {
00393
00394 while(q->dg_next!=NULL){
00395 q = q->dg_next;
00396 }
00397 q->dg_next = temp1;
00398 }
00399
00400 i++;
00401 }
00402
00403 cfg->pmu[j]->cnext = cn;
00404
00406 if(cfg->pmu[j]->phnmr != 0){
00407
00408 i = 0;
00409 while(i < cfg->pmu[j]->phnmr){
00410
00411 memset(buf,'\0',9);
00412 copy_cbyc (buf,(unsigned char *)s,4);
00413 buf[4] = '\0';
00414 l_phunit = to_long_int_convertor(buf);
00415 *cfg->pmu[j]->phunit[i] = l_phunit * 0.00001;
00416 printf("Phasor Fact %d = %f\n",i,*cfg->pmu[j]->phunit[i]);
00417 s = s + 4;
00418 i++;
00419 }
00420 }
00421
00422
00423 if(cfg->pmu[j]->annmr != 0){
00424
00425 i=0;
00426 while(i<cfg->pmu[j]->annmr){
00427
00428 memset(buf,'\0',9);
00429 copy_cbyc (buf,(unsigned char *)s,4);
00430 buf[4] = '\0';
00431 l_anunit = to_long_int_convertor(buf);
00432 *cfg->pmu[j]->anunit[i] = l_anunit*0,00001;
00433 printf("Analog Fact %d = %f\n",i,*cfg->pmu[j]->anunit[i]);
00434 s = s + 4;
00435 i++;
00436 }
00437
00438 }
00439
00441 if(cfg->pmu[j]->dgnmr != 0){
00442
00443 i = 0;
00444 while(i < cfg->pmu[j]->dgnmr ){
00445
00446 copy_cbyc(cfg->pmu[j]->dgunit[i],(unsigned char *)s,4);
00447 cfg->pmu[j]->dgunit[i][4] = '\0';
00448
00449 s += 4;
00450 i++;
00451 }
00452 }
00453
00454 copy_cbyc (fnom,(unsigned char *)s,2);
00455 fnom[2]='\0';
00456 cfg->pmu[j]->fnom = to_intconvertor(fnom);
00457 printf("FREQUENCY %d\n",cfg->pmu[j]->fnom);
00458 s = s + 2;
00459
00460 copy_cbyc (cfg_cnt,(unsigned char *)s,2);
00461 cfg_cnt[2] = '\0';
00462 cfg->pmu[j]->cfg_cnt = to_intconvertor(cfg_cnt);
00463 printf("CFG CHANGE COUNT %d\n",cfg->pmu[j]->cfg_cnt);
00464 s = s + 2;
00465 j++;
00466 }
00467
00468
00469 copy_cbyc (data_rate,(unsigned char *)s,2);
00470 data_rate[2] = '\0';
00471 cfg->data_rate = to_intconvertor(data_rate);
00472 printf("Data Rate %d\n",cfg->data_rate);
00473 s += 2;
00474 cfg->cfgnext = NULL;
00475
00476
00477
00478
00479 pthread_mutex_lock(&mutex_cfg);
00480
00481
00482 int index = 0;
00483
00484 if (cfgfirst == NULL) {
00485 printf("1\n");
00486 cfgfirst = cfg;
00487 fclose(fp);
00488
00489 } else {
00490
00491 struct cfg_frame *temp_cfg = cfgfirst,*tprev_cfg;
00492 tprev_cfg = temp_cfg;
00493
00494
00495 while(temp_cfg!=NULL){
00496
00497 if(cfg->idcode == temp_cfg->idcode) {
00498
00499 printf("CFG PRESENT NEED TO REPLACE\n");
00500 match = 1;
00501 break;
00502
00503 } else {
00504
00505 index++;
00506 tprev_cfg = temp_cfg;
00507 temp_cfg = temp_cfg->cfgnext;
00508
00509 }
00510 }
00511
00512 if(match) {
00513
00514 if(!index) {
00515
00516
00517 cfg->cfgnext = cfgfirst->cfgnext;
00518 free_cfgframe_object(cfgfirst);
00519 cfgfirst = cfg;
00520
00521 fclose(fp);
00522 remove_old_cfg(cfg->idcode,st);
00523
00524 } else {
00525
00526
00527 tprev_cfg->cfgnext = cfg;
00528 cfg->cfgnext = temp_cfg->cfgnext;
00529 free_cfgframe_object(temp_cfg);
00530 fclose(fp);
00531 remove_old_cfg(cfg->idcode,st);
00532 }
00533
00534 } else {
00535
00536 tprev_cfg->cfgnext = cfg;
00537 fclose(fp);
00538
00539 }
00540
00541 }
00542
00543 cfginsert(cfg);
00544 pthread_mutex_unlock(&mutex_cfg);
00545 pthread_mutex_unlock(&mutex_file);
00546
00547
00548 free(sync);
00549 free(frame_size);
00550 free(idcode_h);
00551 free(soc);
00552 free(fracsec);
00553 free(time_base);
00554 free(num_pmu);
00555 free(stn);
00556 free(idcode_l);
00557 free(format);
00558 free(phnmr);
00559 free(annmr);
00560 free(dgnmr);
00561 free(fnom);
00562 free(cfg_cnt);
00563 free(data_rate);
00564 free(buf);
00565
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575 void remove_old_cfg(int idcode,unsigned char frame[]) {
00576
00577 FILE *file,*newfile;
00578 int result;
00579 unsigned int idCODE,framesize;
00580 unsigned char *s,id[3],*line,frame_len[2];
00581 unsigned long fileLen;
00582
00583 file = fopen("cfg.bin","rb");
00584 newfile = fopen("ncfg.bin","wb");
00585 if (file != NULL) {
00586
00587
00588 fseek(file, 0, SEEK_END);
00589 fileLen = ftell(file);
00590 fseek(file, 0, SEEK_SET);
00591 printf("BEFORE REMOVAL OF OLDFRAME FILE LEN %ld\n",fileLen);
00592
00593 while (fileLen != 0) {
00594
00595 fseek (file,2 ,SEEK_CUR);
00596 fread(frame_len, sizeof(unsigned char),2, file);
00597 fseek (file,-4 ,SEEK_CUR);
00598
00599 framesize = to_intconvertor(frame_len);
00600 line = malloc(framesize*sizeof(unsigned char));
00601 memset(line,'\0',sizeof(line));
00602 fread(line, sizeof(unsigned char),framesize,file);
00603 s = line;
00604 s += 4;
00605
00606 copy_cbyc (id,s,2);
00607 id[2] = '\0';
00608 idCODE = to_intconvertor(id);
00609
00610 if(idCODE == idcode) {
00611
00612 printf("MATCH the new cfg with old cfg in file cfg.bin\n");
00613 break;
00614
00615 } else {
00616
00617
00618
00619 fwrite(line, sizeof(unsigned char),framesize,newfile);
00620 free(line);
00621 fileLen -= framesize;
00622 }
00623 }
00624
00625
00626 unsigned int len;
00627 unsigned char *p = frame;
00628 p += 2;
00629 copy_cbyc (frame_len,p,2);
00630 len = to_intconvertor(frame_len);
00631 fwrite(frame, sizeof(unsigned char),len,newfile);
00632
00633
00634 while (fileLen != 0) {
00635
00636 fseek (file,2 ,SEEK_CUR);
00637 fread(frame_len, sizeof(unsigned char),2, file);
00638 fseek (file,-4 ,SEEK_CUR);
00639
00640 framesize = to_intconvertor(frame_len);
00641 line = malloc(framesize*sizeof(unsigned char));
00642 memset(line,'\0',sizeof(line));
00643 fread(line, sizeof(unsigned char),framesize,file);
00644
00645 if(!ncmp_cbyc(line,frame,framesize)) {
00646
00647
00648
00649 break;
00650
00651 } else {
00652
00653 fwrite(frame, sizeof(unsigned char),framesize,newfile);
00654 free(line);
00655 fileLen -= framesize;
00656 }
00657
00658 }
00659
00660
00661 fclose (file);
00662 fclose(newfile);
00663
00664 if( remove( "cfg.bin" ) != 0 )
00665 perror( "Error deleting file" );
00666 result= rename("ncfg.bin","cfg.bin");
00667 if ( result == 0 )
00668 fputs ("File successfully renamed",stdout);
00669 else
00670 perror( "Error renaming file" );
00671
00672 } else {
00673 perror ("cfg.bin");
00674 }
00675
00676 }
00677
00678
00679
00680
00681
00682
00683
00684 void cfginsert(struct cfg_frame *cfg){
00685
00686 int i,j,k;
00687 char *cmd,*cmd2;
00688 MYSQL_RES *res1,*res2,*res3,*res4;
00689
00690 printf("INSIDE CFG INSERT\n");
00691
00692 cmd = malloc(1000);
00693 cmd2 = malloc(500);
00694 memset(cmd,'\0',1000);
00695 memset(cmd2,'\0',500);
00696
00697 pthread_mutex_lock(&mutex_MYSQL_CONN_ON_CFG);
00698
00699 sprintf(cmd2, "SELECT * FROM MAIN_CFG_TABLE WHERE PDC_ID = %d",cfg->idcode);
00700
00701
00702 if (mysql_query(conn_cfg,cmd2)) {
00703
00704 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00705 exit(1);
00706 }
00707
00708 res1 = mysql_use_result(conn_cfg);
00709 if(mysql_fetch_row(res1)== NULL) {
00710
00711 mysql_free_result(res1);
00712 printf("No Entry Make Insert into table\n");
00713 memset(cmd,'\0',1000);
00714 sprintf(cmd, "INSERT INTO MAIN_CFG_TABLE VALUES(%d,%ld,%ld,%ld,%d,%d)",cfg->idcode,cfg->soc,cfg->fracsec,
00715 cfg->time_base,cfg->num_pmu,cfg->data_rate);
00716 if (mysql_query(conn_cfg, cmd)) {
00717
00718 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00719 exit(1);
00720 }
00721 printf("%s\n",cmd);
00722
00723 j = 0;
00724 while(j < cfg->num_pmu){
00725
00726 memset(cmd,'\0',1000);
00727
00728
00729 sprintf(cmd,"INSERT INTO SUB_CFG_TABLE(PDC_ID,PMU_ID,SOC,FRACSEC,STN,PHNMR,ANNMR,DGNMR,FNOM) VALUES(%d,%d,%ld,%ld,\"%s\",%d,%d,%d,%d)",cfg->idcode,cfg->pmu[j]->idcode,cfg->soc,cfg->fracsec,cfg->pmu[j]->stn,cfg->pmu[j]->phnmr,cfg->pmu[j]->annmr,cfg->pmu[j]->dgnmr,cfg->pmu[j]->fnom);
00730
00731 printf("%s\n",cmd);
00732
00733 if (mysql_query(conn_cfg, cmd)) {
00734
00735 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00736 exit(1);
00737 }
00738
00739
00740 if(cfg->pmu[j]->phnmr != 0){
00741 for(i = 0;i<cfg->pmu[j]->phnmr;i++) {
00742
00743 memset(cmd,'\0',1000);
00744 sprintf(cmd,"INSERT INTO PHASOR(PDC_ID,PMU_ID,PHASOR_NAMES,PHUNITS) VALUES (%d,%d,\"%s\",%f)",cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->phnames[i],*cfg->pmu[j]->phunit[i]); printf("%s\n",cmd);
00745
00746 if (mysql_query(conn_cfg, cmd)) {
00747
00748 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00749 exit(1);
00750 }
00751 }
00752 }
00753
00754 if(cfg->pmu[j]->annmr != 0){
00755 for(i = 0;i<cfg->pmu[j]->annmr;i++) {
00756
00757 memset(cmd,'\0',1000);
00758 sprintf(cmd,"INSERT INTO ANALOG(PDC_ID,PMU_ID,ANALOG_NAMES,ANUNITS) VALUES(%d,%d,\"%s\",%f)",cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->angnames[i],*cfg->pmu[j]->anunit[i]); printf("%s\n",cmd);
00759 if (mysql_query(conn_cfg, cmd)) {
00760
00761 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00762 exit(1);
00763 }
00764 }
00765 }
00766
00767 if(cfg->pmu[j]->dgnmr != 0){
00768
00769 unsigned long int dunit;
00770 struct dgnames *temp_dnames = cfg->pmu[j]->cnext->first;
00771 for(i = 0;i<cfg->pmu[j]->dgnmr;i++) {
00772
00773 for(k = 0;k<16;k++) {
00774
00775 dunit = to_long_int_convertor(cfg->pmu[j]->dgunit[i]);
00776 memset(cmd,'\0',1000);
00777 sprintf(cmd,"INSERT INTO DIGITAL(PDC_ID,PMU_ID,DIGITAL_NAMES,DIGITAL_WORD) VALUES(%d,%d,\"%s\",%u)",cfg->idcode,cfg->pmu[j]->idcode,temp_dnames->dgn[k],(unsigned int)dunit);
00778
00779 if (mysql_query(conn_cfg, cmd)) {
00780
00781 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00782 exit(1);
00783 }
00784 }
00785
00786 temp_dnames = temp_dnames->dg_next;
00787 }
00788 }
00789
00790 j++;
00791 }
00792
00793 } else {
00794
00795
00796 mysql_free_result(res1);
00797 printf("Update the existing entry in table\n");
00798 memset(cmd,'\0',1000);
00799 sprintf(cmd, "UPDATE MAIN_CFG_TABLE SET SOC = %ld,FRACSEC = %ld,TIMEBASE = %ld,NUM_OF_PMU = %d,DATA_RATE = %d WHERE PDC_ID = %d",cfg->soc,cfg->fracsec,cfg->time_base,cfg->num_pmu,cfg->data_rate,cfg->idcode);
00800 printf("%s\n",cmd);
00801
00802 if (mysql_query(conn_cfg,cmd)) {
00803
00804 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00805 exit(1);
00806 }
00807 mysql_query(conn_cfg, "COMMIT");
00808
00809 int j = 0;
00810 while(j < cfg->num_pmu){
00811
00812 memset(cmd2,'\0',500);
00813 sprintf(cmd2, "SELECT * FROM SUB_CFG_TABLE WHERE PDC_ID = %d AND PMU_ID = %d AND STN = \"%s\"",cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->stn);
00814 if (mysql_query(conn_cfg, cmd2)) {
00815
00816 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00817 exit(1);
00818 }
00819 res2 = mysql_use_result(conn_cfg);
00820 printf("%s\n",cmd2);
00821
00822 if(mysql_fetch_row(res2)== NULL) {
00823
00824 mysql_free_result(res2);
00825 memset(cmd,'\0',1000);
00826 sprintf(cmd,"INSERT INTO SUB_CFG_TABLE(PDC_ID,PMU_ID,SOC,FRACSEC,STN,PHNMR,ANNMR,DGNMR,FNOM) VALUES(%d,%d,%ld,%ld,\"%s\",%d,%d,%d,%d)",cfg->idcode,cfg->pmu[j]->idcode,cfg->soc,cfg->fracsec,cfg->pmu[j]->stn,cfg->pmu[j]->phnmr,cfg->pmu[j]->annmr,cfg->pmu[j]->dgnmr,cfg->pmu[j]->fnom);
00827
00828 if (mysql_query(conn_cfg, cmd)) {
00829
00830 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00831 exit(1);
00832 }
00833 printf("%s\n",cmd);
00834
00835 } else {
00836
00837 mysql_free_result(res2);
00838 memset(cmd,'\0',1000);
00839 sprintf(cmd, "UPDATE SUB_CFG_TABLE SET PHNMR = %d,ANNMR = %d,DGNMR = %d,FNOM = %d WHERE PDC_ID = %d AND PMU_ID = %d AND STN = \"%s\"",cfg->pmu[j]->phnmr,cfg->pmu[j]->annmr,cfg->pmu[j]->dgnmr,cfg->pmu[j]->fnom,cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->stn);
00840
00841 if (mysql_query(conn_cfg, cmd)) {
00842
00843 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00844 exit(1);
00845 }
00846
00847 mysql_query(conn_cfg, "COMMIT");
00848 printf("%s\n",cmd);
00849 }
00850
00851 if(cfg->pmu[j]->phnmr !=0){
00852 for(i = 0;i<cfg->pmu[j]->phnmr;i++) {
00853
00854 memset(cmd2,'\0',500);
00855 sprintf(cmd2, "SELECT * FROM PHASOR WHERE PDC_ID = %d AND PMU_ID = %d AND PHASOR_NAMES = \"%s\"",cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->phnames[i]);
00856 if (mysql_query(conn_cfg, cmd2)) {
00857
00858 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00859 exit(1);
00860 }
00861 printf("%s\n",cmd2);
00862 res3 = mysql_use_result(conn_cfg);
00863 if(mysql_fetch_row(res3)== NULL) {
00864
00865 mysql_free_result(res3);
00866 memset(cmd,'\0',1000);
00867
00868 sprintf(cmd,"INSERT INTO PHASOR(PDC_ID,PMU_ID,PHASOR_NAMES,PHUNITS) VALUES (%d,%d,\"%s\",%f)",cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->phnames[i],*cfg->pmu[j]->phunit[i]);
00869 if (mysql_query(conn_cfg, cmd)) {
00870
00871 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00872 exit(1);
00873 }
00874 printf("%s\n",cmd);
00875
00876 } else {
00877
00878 mysql_free_result(res3);
00879 memset(cmd,'\0',1000);
00880 sprintf(cmd,"UPDATE PHASOR SET PHUNITS = %f WHERE PDC_ID = %d AND PMU_ID = %d AND PHASOR_NAMES = \"%s\"",*cfg->pmu[j]->phunit[i],cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->phnames[i]);
00881 if (mysql_query(conn_cfg, cmd)) {
00882
00883 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00884 exit(1);
00885 }
00886 mysql_query(conn_cfg, "COMMIT");
00887 printf("%s\n",cmd);
00888
00889 }
00890 }
00891 }
00892
00893 if(cfg->pmu[j]->annmr !=0){
00894 for(i = 0;i<cfg->pmu[j]->annmr;i++) {
00895
00896 memset(cmd2,'\0',500);
00897 sprintf(cmd2, "SELECT * FROM ANALOG WHERE PDC_ID = %d AND PMU_ID = %d AND ANALOG_NAMES = \"%s\"",cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->angnames[i]);
00898 if (mysql_query(conn_cfg, cmd2)) {
00899
00900 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00901 exit(1);
00902 }
00903 res4 = mysql_use_result(conn_cfg);
00904 if(mysql_fetch_row(res4)== NULL) {
00905
00906 mysql_free_result(res4);
00907 memset(cmd,'\0',1000);
00908 sprintf(cmd,"INSERT INTO ANALOG(PDC_ID,PMU_ID,ANALOG_NAMES,ANUNITS) VALUES (%d,%d,\"%s\",%f)",cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->angnames[i],*cfg->pmu[j]->anunit[i]);
00909 if (mysql_query(conn_cfg, cmd)) {
00910
00911 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00912 exit(1);
00913 }
00914 printf("%s\n",cmd);
00915
00916 } else {
00917
00918 mysql_free_result(res4);
00919 memset(cmd,'\0',1000);
00920 sprintf(cmd,"UPDATE ANALOG SET ANUNITS = %f WHERE PDC_ID = %d AND PMU_ID = %d AND ANALOG_NAMES = \"%s\"",*cfg->pmu[j]->anunit[i],cfg->idcode,cfg->pmu[j]->idcode,cfg->pmu[j]->cnext->angnames[i]);
00921 if (mysql_query(conn_cfg, cmd)) {
00922
00923 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00924 exit(1);
00925 }
00926 mysql_query(conn_cfg, "COMMIT");
00927 printf("%s\n",cmd);
00928 }
00929 }
00930 }
00931 j++;
00932 }
00933 }
00934 pthread_mutex_unlock(&mutex_MYSQL_CONN_ON_CFG);
00935 free(cmd);
00936 free(cmd2);
00937
00938 }
00939
00940
00941
00942
00943
00944
00945
00946
00947 int dataparser(unsigned char data[]) {
00948
00949 struct cfg_frame *temp_cfg;
00950 int match = 0,i,j = 0;
00951 int stat_status,config_change = 0;
00952 unsigned int t_id,num_pmu,phnmr,annmr,dgnmr;
00953 float fp_r,fp_i,fp_real,fp_imaginary,fp_analogs;
00954 unsigned long int f_r,f_i,f_analogs,f_freq,f_dfreq,l_soc,l_fracsec;
00955 float fp_freq,fp_dfreq;
00956
00957 unsigned char *sync,*framesize,*idcode,*soc,*fracsec,*stat,*phasors,*analogs,*digital,*freq,*dfreq,*d;
00958 unsigned char *fp_left,*fp_right;
00959 unsigned char *f_left,*f_right;
00960 unsigned char *fp_analog,*f_analog;
00961 char *cmd;
00962
00963 cmd = malloc(500);
00964 sync = malloc(3*sizeof(unsigned char));
00965 framesize = malloc(3*sizeof(unsigned char));
00966 idcode = malloc(3*sizeof(unsigned char));
00967 soc = malloc(5*sizeof(unsigned char));
00968 fracsec = malloc(5*sizeof(unsigned char));
00969 stat = malloc(3*sizeof(unsigned char));
00970 phasors = malloc(9*sizeof(unsigned char));
00971 analogs = malloc(5*sizeof(unsigned char));
00972 digital = malloc(3*sizeof(unsigned char));
00973 freq = malloc(5*sizeof(unsigned char));
00974 dfreq = malloc(5*sizeof(unsigned char));
00975
00976 memset(cmd,'\0',500);
00977 memset(sync,'\0',3);
00978 memset(framesize,'\0',3);
00979 memset(idcode,'\0',3);
00980 memset(soc,'\0',5);
00981 memset(fracsec,'\0',5);
00982 memset(stat,'\0',3);
00983 memset(phasors,'\0',9);
00984 memset(analogs,'\0',5);
00985 memset(digital,'\0',3);
00986 memset(freq,'\0',5);
00987 memset(dfreq,'\0',5);
00988
00989
00990 fp_left = malloc(5);
00991 fp_right = malloc(5);
00992 f_left = malloc(3);
00993 f_right = malloc(3);
00994 fp_analog = malloc(5);
00995 f_analog = malloc(3);
00996
00997 memset(fp_left,'\0',5);
00998 memset(fp_right,'\0',5);
00999 memset(f_left,'\0',3);
01000 memset(f_right,'\0',3);
01001 memset(fp_analog,'\0',5);
01002 memset(f_analog,'\0',3);
01003
01004 d = data;
01005
01006 d += 2;
01007
01008
01009 copy_cbyc (framesize,d,2);
01010 framesize[2] = '\0';
01011 d += 2;
01012
01013
01014 copy_cbyc (idcode,d,2);
01015 idcode[2] ='\0';
01016 d += 2;
01017
01018 pthread_mutex_lock(&mutex_cfg);
01019
01020 temp_cfg = cfgfirst;
01021 t_id = to_intconvertor(idcode);
01022 printf("ID Code %d\n",t_id);
01023
01024 while(temp_cfg != NULL){
01025
01026 if(t_id == temp_cfg->idcode) {
01027
01028 match = 1;
01029 break;
01030
01031 } else {
01032
01033 temp_cfg = temp_cfg->cfgnext;
01034
01035 }
01036 }
01037 pthread_mutex_unlock(&mutex_cfg);
01038
01039 pthread_mutex_lock(&mutex_MYSQL_CONN_ON_DATA);
01040
01041 if(match){
01042
01043 printf("Inside dataparser() data_frame and matched with cfg\n");
01044
01045
01046 num_pmu = temp_cfg->num_pmu;
01047
01048
01049 copy_cbyc (soc,d,4);
01050 soc[4] = '\0';
01051 l_soc = to_long_int_convertor(soc);
01052 d += 4;
01053
01054
01055 copy_cbyc (fracsec,d,4);
01056 fracsec[4] = '\0';
01057 l_fracsec = to_long_int_convertor(fracsec);
01058 d += 4;
01059
01060
01061 while(j < num_pmu) {
01062
01063 copy_cbyc (stat,d,2);
01064 stat[2] = '\0';
01065 d += 2;
01066
01067
01068 stat_status = check_statword(stat);
01069
01070
01071
01072 if(stat_status == 16) {
01073
01074 memset(stat,'\0',3);
01075 j++;
01076 continue;
01077
01078 } else if((stat_status == 14)||(stat_status == 10)) {
01079
01080 memset(stat,'\0',3);
01081 config_change = stat_status;
01082 j++;
01083 continue;
01084
01085 }
01086
01087
01088 phnmr = temp_cfg->pmu[j]->phnmr;
01089 annmr = temp_cfg->pmu[j]->annmr;
01090 dgnmr = temp_cfg->pmu[j]->dgnmr;
01091
01092
01093 if(phnmr != 0) {
01094 if(temp_cfg->pmu[j]->fmt->phasor == '1') {
01095
01096 for(i = 0;i<phnmr;i++){
01097
01098 memset(fp_left,'\0',5);
01099 memset(fp_right,'\0',5);
01100 copy_cbyc (fp_left,d,4);
01101 fp_left[4] = '\0';
01102 d += 4;
01103
01104 copy_cbyc(fp_right,d,4);
01105 fp_right[4] = '\0';
01106 d += 4;
01107
01108 fp_r = decode_ieee_single(fp_left);
01109 fp_i = decode_ieee_single(fp_right);
01110
01111 if(temp_cfg->pmu[j]->fmt->polar == '1') {
01112
01113 fp_real = fp_r*cos(f_i);
01114 fp_imaginary = fp_r*sin(f_i);
01115
01116 memset(cmd,'\0',500);
01117 sprintf(cmd,"INSERT INTO PHASOR_MEASUREMENTS SELECT %d,%d,%ld,%ld,\"%s\",%f,%f FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT,PHASOR WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d AND PHASOR.PHASOR_NAMES = \"%s\"",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,temp_cfg->pmu[j]->cnext->phnames[i],fp_real,fp_imaginary,temp_cfg->idcode,temp_cfg->pmu[j]->idcode,temp_cfg->pmu[j]->cnext->phnames[i]);
01118
01119 if (mysql_query(conn_data, cmd)) {
01120
01121 fprintf(stderr, "%s\n", mysql_error(conn_data));
01122 exit(1);
01123 }
01124 printf("%s\n",cmd);
01125
01126 } else {
01127
01128 fp_real = fp_r;
01129 fp_imaginary = fp_i;
01130 memset(cmd,'\0',500);
01131 sprintf(cmd,"INSERT INTO PHASOR_MEASUREMENTS SELECT %d,%d,%ld,%ld,\"%s\",%f,%f FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT,PHASOR WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d AND PHASOR.PHASOR_NAMES = \"%s\"",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,temp_cfg->pmu[j]->cnext->phnames[i],fp_real,fp_imaginary,temp_cfg->idcode,temp_cfg->pmu[j]->idcode,temp_cfg->pmu[j]->cnext->phnames[i]);
01132 if (mysql_query(conn_data, cmd)) {
01133
01134 fprintf(stderr, "%s\n", mysql_error(conn_data));
01135 exit(1);
01136 }
01137 printf("%s\n",cmd);
01138
01139 }
01140
01141 }
01142 } else {
01143
01144 for(i = 0;i < phnmr; i++){
01145
01146 memset(f_left,'\0',3);
01147 memset(f_right,'\0',3);
01148 copy_cbyc (f_left,d,2);
01149 f_left[2] = '\0';
01150 d += 2;
01151
01152 copy_cbyc(f_right,d,2);
01153 f_right[2] = '\0';
01154 d += 2;
01155
01156 f_r = to_intconvertor(f_left);
01157 f_i = to_intconvertor(f_right);
01158
01159 if(temp_cfg->pmu[j]->fmt->polar == '1') {
01160
01161 fp_real = *temp_cfg->pmu[j]->phunit[i] *f_r*cos(f_i);
01162 fp_imaginary = *temp_cfg->pmu[j]->phunit[i] *f_r*sin(f_i);
01163
01164 memset(cmd,'\0',500);
01165 sprintf(cmd,"INSERT INTO PHASOR_MEASUREMENTS SELECT %d,%d,%ld,%ld,\"%s\",%f,%f FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT,PHASOR WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d AND PHASOR.PHASOR_NAMES = \"%s\"",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,temp_cfg->pmu[j]->cnext->phnames[i],fp_real,fp_imaginary,temp_cfg->idcode,temp_cfg->pmu[j]->idcode,temp_cfg->pmu[j]->cnext->phnames[i]);
01166 if (mysql_query(conn_data, cmd)) {
01167
01168 fprintf(stderr, "%s\n", mysql_error(conn_data));
01169 exit(1);
01170 }
01171 printf("%s\n",cmd);
01172
01173 } else {
01174
01175 fp_real = *temp_cfg->pmu[j]->phunit[i] *f_r;
01176 fp_imaginary = *temp_cfg->pmu[j]->phunit[i] *f_i;
01177
01178 memset(cmd,'\0',500);
01179 sprintf(cmd,"INSERT INTO PHASOR_MEASUREMENTS SELECT %d,%d,%ld,%ld,\"%s\",%f,%f FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT,PHASOR WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d AND PHASOR.PHASOR_NAMES = \"%s\"",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,temp_cfg->pmu[j]->cnext->phnames[i],fp_real,fp_imaginary,temp_cfg->idcode,temp_cfg->pmu[j]->idcode,temp_cfg->pmu[j]->cnext->phnames[i]);
01180 if (mysql_query(conn_data, cmd)) {
01181
01182 fprintf(stderr, "%s\n", mysql_error(conn_data));
01183 exit(1);
01184 }
01185 printf("%s\n",cmd);
01186 }
01187 }
01188 }
01189 }
01190
01191 if(temp_cfg->pmu[j]->fmt->freq == '1') {
01192
01193 memset(freq,'\0',5);
01194 copy_cbyc (freq,d,4);
01195 freq[4] = '\0';
01196 d += 4;
01197
01198 memset(dfreq,'\0',5);
01199 copy_cbyc (dfreq,d,4);
01200 dfreq[4] = '\0';
01201 d += 4;
01202
01203 fp_freq = decode_ieee_single(freq);
01204 fp_dfreq = decode_ieee_single(dfreq);
01205
01206 memset(cmd,'\0',500);
01207 sprintf(cmd,"INSERT INTO FREQUENCY_MEASUREMENTS SELECT %d,%d,%ld,%ld,%f,%f FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,fp_freq,fp_dfreq,temp_cfg->idcode,temp_cfg->pmu[j]->idcode);
01208 if (mysql_query(conn_data, cmd)) {
01209
01210 fprintf(stderr, "%s\n", mysql_error(conn_data));
01211 exit(1);
01212 }
01213 printf("%s\n",cmd);
01214
01215 } else {
01216 memset(freq,'\0',5);
01217 copy_cbyc (freq,d,2);
01218 freq[2] = '\0';
01219 f_freq = to_intconvertor(freq);
01220 d += 2;
01221
01222 memset(dfreq,'\0',5);
01223 copy_cbyc (dfreq,d,2);
01224 dfreq[2] = '\0';
01225
01226 f_dfreq = to_intconvertor(dfreq);
01227 d += 2;
01228
01229 memset(cmd,'\0',500);
01230 sprintf(cmd,"INSERT INTO FREQUENCY_MEASUREMENTS SELECT %d,%d,%ld,%ld,%ld,%ld FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,f_freq,f_dfreq,temp_cfg->idcode,temp_cfg->pmu[j]->idcode);
01231 if (mysql_query(conn_data, cmd)) {
01232
01233 fprintf(stderr, "%s\n", mysql_error(conn_data));
01234 exit(1);
01235 }
01236 printf("%s\n",cmd);
01237
01238 }
01239
01240
01241 if(annmr != 0) {
01242 if(temp_cfg->pmu[j]->fmt->analog == '1') {
01243
01244 for(i = 0; i < annmr; i++){
01245
01246 memset(analogs,'\0',5);
01247 copy_cbyc(analogs,d,4);
01248 analogs[4] = '\0';
01249 fp_analogs = decode_ieee_single(analogs);
01250 fp_analogs = *temp_cfg->pmu[j]->anunit[i]*fp_analogs;;
01251 memset(cmd,'\0',500);
01252 sprintf(cmd,"INSERT INTO ANALOG_MEASUREMENTS SELECT %d,%d,%ld,%ld,\"%s\",%f FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT,ANALOG WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d AND ANALOG.ANALOG_NAMES = \"%s\"",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,temp_cfg->pmu[j]->cnext->angnames[i],fp_analogs,temp_cfg->idcode,temp_cfg->pmu[j]->idcode,temp_cfg->pmu[j]->cnext->angnames[i]);
01253 if (mysql_query(conn_data, cmd)) {
01254
01255 fprintf(stderr, "%s\n", mysql_error(conn_data));
01256 exit(1);
01257 }
01258 printf("%s\n",cmd);
01259 d += 4;
01260
01261 }
01262 } else {
01263 for(i = 0; i < annmr; i++){
01264
01265 memset(analogs,'\0',5);
01266 copy_cbyc (analogs,d,2);
01267 analogs[2] = '\0';
01268 f_analogs = to_intconvertor(analogs);
01269 f_analogs = *temp_cfg->pmu[j]->anunit[i]*f_analogs ;
01270 memset(cmd,'\0',500);
01271 sprintf(cmd,"INSERT INTO ANALOG_MEASUREMENTS SELECT %d,%d,%ld,%ld,\"%s\",%ld FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT,ANALOG WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d AND ANALOG.ANALOG_NAMES = \"%s\"",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,temp_cfg->pmu[j]->cnext->angnames[i],f_analogs,temp_cfg->idcode,temp_cfg->pmu[j]->idcode,temp_cfg->pmu[j]->cnext->angnames[i]);
01272 if (mysql_query(conn_data, cmd)) {
01273
01274 fprintf(stderr, "%s\n", mysql_error(conn_data));
01275 exit(1);
01276 }
01277 printf("%s\n",cmd);
01278 d += 2;
01279 }
01280 }
01281 }
01282
01283
01284 if(dgnmr != 0) {
01285
01286 unsigned int dgword;
01287
01288 for(i = 0; i<dgnmr; i++) {
01289
01290 memset(digital,'\0',3);
01291 copy_cbyc (digital,d,2);
01292 digital[2] = '\0';
01293 dgword = to_intconvertor(digital);
01294
01295 memset(cmd,'\0',500);
01296 sprintf(cmd,"INSERT INTO DIGITAL_MEASUREMENTS SELECT %d,%d,%ld,%ld,%u FROM MAIN_CFG_TABLE MCT,SUB_CFG_TABLE SCT WHERE MCT.PDC_ID = %d AND SCT.PMU_ID = %d ",temp_cfg->idcode,temp_cfg->pmu[j]->idcode,l_soc,l_fracsec,dgword,temp_cfg->idcode,temp_cfg->pmu[j]->idcode);
01297 if (mysql_query(conn_data, cmd)) {
01298
01299 fprintf(stderr, "%s\n", mysql_error(conn_data));
01300 exit(1);
01301 }
01302 printf("%s\n",cmd);
01303 d += 2;
01304 }
01305 }
01306 j++;
01307 }
01308
01309 } else {
01310
01311 printf("NO CFG for data frames\n");
01312
01313
01314 }
01315
01316 pthread_mutex_unlock(&mutex_MYSQL_CONN_ON_DATA);
01317
01318 free(cmd);
01319 free(sync);
01320 free(framesize);
01321 free(idcode);
01322 free(soc);
01323 free(fracsec);
01324 free(stat);
01325 free(phasors);
01326 free(analogs);
01327 free(digital);
01328 free(freq);
01329 free(dfreq);
01330
01331 free(fp_left);
01332 free(fp_right);
01333 free(f_left);
01334 free(f_right);
01335 free(fp_analog);
01336 free(f_analog);
01337
01338
01339 if((config_change == 14) ||(config_change == 10))
01340 return config_change;
01341 else return stat_status;
01342
01343 }
01344
01345
01346
01347
01348
01349
01350
01351
01352 int check_statword(unsigned char stat[]) {
01353
01354 int ret = 0;
01355
01356
01357 if(stat[0] == 0x0f) {
01358
01359 ret = 16;
01360 return ret;
01361
01362 } else if ((stat[0] & 0x04) == 0x04) {
01363
01364 printf("Configuration Change error\n");
01365 ret = 10;
01366 return ret;
01367
01368 } else if ((stat[0] & 0x40) == 0x40) {
01369
01370 printf("PMU error including configuration error\n");
01371 ret = 14;
01372 return ret;
01373
01374 } else if((stat[0] & 0x80) == 0x80) {
01375
01376 printf("Data invalid\n");
01377 ret = 15;
01378 return ret;
01379
01380 } else if ((stat[0] & 0x20) == 0x20) {
01381
01382 printf("PMU Sync error\n");
01383 ret = 13;
01384 return ret;
01385
01386 } else if ((stat[0] & 0x10) == 0x10) {
01387
01388 printf("Data sorting error\n");
01389 ret = 12;
01390 return ret;
01391
01392 } else if ((stat[0] & 0x08) == 0x08) {
01393
01394 printf("PMU Trigger error\n");
01395 ret = 11;
01396 return ret;
01397
01398 }
01399 printf("RET from check_statword() %d\n",ret);
01400 return ret;
01401 }
01402
01403
01404
01405
01406
01407
01408 unsigned int to_intconvertor(unsigned char array[]) {
01409
01410 unsigned int n;
01411 n = array[0];
01412 n <<= 8;
01413 n |= array[1];
01414 return n;
01415 }
01416
01417
01418
01419
01420
01421 unsigned long int to_long_int_convertor(unsigned char array[]) {
01422
01423 unsigned long int n;
01424 n = array[0];
01425 n <<= 8;
01426 n |= array[1];
01427 n <<= 8;
01428 n |= array[2];
01429 n <<= 8;
01430 n |= array[3];
01431 return n;
01432
01433 }
01434
01435
01436
01437
01438
01439 float decode_ieee_single(const void *v) {
01440
01441 const unsigned char *data = v;
01442 int s, e;
01443 unsigned long src;
01444 long f;
01445 float value;
01446
01447 src = ((unsigned long)data[0] << 24) |
01448 ((unsigned long)data[1] << 16) |
01449 ((unsigned long)data[2] << 8) |
01450 ((unsigned long)data[3]);
01451
01452 s = (src & 0x80000000UL) >> 31;
01453 e = (src & 0x7F800000UL) >> 23;
01454 f = (src & 0x007FFFFFUL);
01455
01456 if (e == 255 && f != 0) {
01457
01458 value = DBL_MAX;
01459
01460 } else if (e == 255 && f == 0 && s == 1) {
01461
01462 value = -DBL_MAX;
01463 } else if (e == 255 && f == 0 && s == 0) {
01464
01465 value = DBL_MAX;
01466 } else if (e > 0 && e < 255) {
01467
01468 f += 0x00800000UL;
01469 if (s) f = -f;
01470 value = ldexp(f, e - 150);
01471 } else if (e == 0 && f != 0) {
01472
01473 if (s) f = -f;
01474 value = ldexp(f, -149);
01475 } else if (e == 0 && f == 0 && s == 1) {
01476
01477 value = 0;
01478 } else if (e == 0 && f == 0 && s == 0) {
01479
01480 value = 0;
01481 } else {
01482
01483 printf("s = %d, e = %d, f = %lu\n", s, e, f);
01484 assert(!"Woops, unhandled case in decode_ieee_single()");
01485 }
01486
01487 return value;
01488 }
01489
01490
01491
01492
01493
01494 void copy_cbyc(unsigned char dst[],unsigned char *s,int size) {
01495
01496 int i;
01497 for(i = 0; i< size; i++)
01498 dst[i] = *(s + i);
01499 }
01500
01501
01502
01503
01504
01505 int ncmp_cbyc(unsigned char dst[],unsigned char src[],int size) {
01506
01507 int i,flag = 0;
01508 for(i = 0; i< size; i++) {
01509
01510 if(dst[i] != src[i]) {
01511
01512 flag = 1;
01513 break;
01514 }
01515 }
01516 return flag;
01517 }
01518