00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <stdlib.h>
00033 #include <pthread.h>
00034 #include "parser.h"
00035 #include "global.h"
00036 #include "recreate.h"
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 void recreate_cfg_objects(){
00057
00058
00059
00060
00061 pthread_mutex_init(&mutex_file, NULL);
00062 pthread_mutex_init(&mutex_cfg, NULL);
00063 pthread_mutex_init(&mutex_MYSQL_CONN_ON_DATA, NULL);
00064 pthread_mutex_init(&mutex_MYSQL_CONN_ON_CFG, NULL);
00065
00066 static const char filename[] = "cfg.bin";
00067
00068 FILE *file = fopen (filename,"rb");
00069 unsigned char *line,frame_len[2];
00070 unsigned int framesize;
00071 unsigned long fileLen;
00072
00073 if (file != NULL) {
00074
00075
00076 fseek(file, 0, SEEK_END);
00077 fileLen = ftell(file);
00078 fseek(file, 0, SEEK_SET);
00079 printf("FILE LEN LEFT %ld\n",fileLen);
00080 printf("Creating objects for entries in cfg.bin\n");
00081
00082 while (fileLen != 0) {
00083
00084 fseek (file,2 ,SEEK_CUR);
00085 fread(frame_len, sizeof(unsigned char),2, file);
00086 fseek (file,-4 ,SEEK_CUR);
00087
00088 framesize = to_intconvertor(frame_len);
00089 line = malloc(framesize*sizeof(unsigned char));
00090 memset(line,'\0',sizeof(line));
00091 fread(line, sizeof(unsigned char),framesize,file);
00092 init_cfgparser(line);
00093 free(line);
00094 fileLen -= framesize;
00095 }
00096 fclose (file);
00097
00098 } else {
00099 perror (filename);
00100 }
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 void init_cfgparser(unsigned char st[]){
00110
00111 unsigned char *s;
00112 int cfglen = 0;
00113 int i,j,k,dgchannels;
00114 struct cfg_frame *cfg;
00115 struct channel_names *cn;
00116 unsigned long int l_phunit,l_anunit;
00117
00118 unsigned char *sync,*frame_size,*idcode_h,*soc,*fracsec,*time_base,*num_pmu,*stn,*idcode_l,*format,*phnmr,*annmr,*dgnmr;
00119 unsigned char *fnom,*cfg_cnt,*data_rate,*buf;
00120
00121 sync = malloc(3);
00122 frame_size = malloc(3);
00123 idcode_h= malloc(3);
00124 soc = malloc(5);
00125 fracsec = malloc(5);
00126 time_base = malloc(5);
00127 num_pmu = malloc(3);
00128 stn = malloc(17);
00129 idcode_l = malloc(3);
00130 format = malloc(5);
00131 phnmr = malloc(3);
00132 annmr = malloc(3);
00133 dgnmr = malloc(3);
00134
00135 fnom = malloc(3);
00136 cfg_cnt = malloc(3);
00137 data_rate = malloc(3);
00138 buf = malloc(9);
00139
00140
00141 memset(sync,'\0',3);
00142 memset(frame_size,'\0',3);
00143 memset(idcode_h,'\0',3);
00144 memset(soc,'\0',3);
00145 memset(fracsec,'\0',5);
00146 memset(time_base,'\0',5);
00147 memset(num_pmu,'\0',3);
00148 memset(stn,'\0',17);
00149 memset(idcode_l,'\0',3);
00150 memset(format,'\0',3);
00151 memset(phnmr,'\0',3);
00152 memset(annmr,'\0',3);
00153 memset(dgnmr,'\0',3);
00154
00155 memset(fnom,'\0',3);
00156 memset(cfg_cnt ,'\0',3);
00157 memset(data_rate,'\0',3);
00158 memset(buf,'\0',9);
00159
00160
00161
00162
00163 pthread_mutex_lock(&mutex_file);
00164
00165 cfg = malloc(sizeof(struct cfg_frame));
00166 if(!cfg) {
00167 printf("No enough memory for cfg\n");
00168 }
00169
00170 printf("Inside INIT cfgparser()\n");
00171 s = st;
00172
00173
00174 copy_cbyc (sync,s,2);
00175 sync[2] = '\0';
00176 s = s + 2;
00177
00178
00179 copy_cbyc (frame_size,s,2);
00180 frame_size[2]='\0';
00181 cfg->framesize = to_intconvertor(frame_size);
00182 printf("FRAME SIZE %d\n",cfg->framesize);
00183 s = s + 2;
00184 cfglen += 2;
00185
00186
00187 copy_cbyc (idcode_h,s,2);
00188 idcode_h[2] = '\0';
00189 cfg->idcode = to_intconvertor(idcode_h);
00190 printf("ID Code %d\n",cfg->idcode);
00191 s = s + 2;
00192 cfglen += 2;
00193
00194
00195 copy_cbyc (soc,s,4);
00196 soc[4] = '\0';
00197 sscanf((unsigned int *)soc,"%x", (unsigned int *)&cfg->soc);
00198 printf("SOC %ld\n",cfg->soc);
00199 s =s + 4;
00200 cfglen += 4;
00201
00202
00203 copy_cbyc (fracsec, s,4);
00204 fracsec[4] = '\0';
00205 cfg->fracsec = to_long_int_convertor(fracsec);
00206 printf("FracSec %ld\n",cfg->fracsec);
00207 s = s + 4;
00208 cfglen += 4;
00209
00210
00211 copy_cbyc (time_base, s,4);
00212 time_base[4]='\0';
00213 cfg->time_base = to_long_int_convertor(time_base);
00214 printf("Time Base %ld\n",cfg->time_base);
00215 s = s + 4;
00216 cfglen += 4;
00217
00218
00219 copy_cbyc (num_pmu, s,2);
00220 num_pmu[2] = '\0';
00221 cfg->num_pmu = to_intconvertor(num_pmu);
00222 printf("Number of PMU's %d\n",cfg->num_pmu);
00223 s = s + 2;
00224 cfglen += 2;
00225
00226
00227 cfg->pmu = malloc(cfg->num_pmu* sizeof(struct for_each_pmu *));
00228 if(!cfg->pmu) {
00229 printf("Not enough memory pmu[][]\n");
00230 exit(1);
00231 }
00232
00233 for (i = 0; i < cfg->num_pmu; i++) {
00234 cfg->pmu[i] = malloc(sizeof(struct for_each_pmu));
00235 }
00236
00237 j = 0;
00238
00240 while(j<cfg->num_pmu) {
00241
00242
00243 memset(cfg->pmu[j]->stn,'\0',17);
00244 copy_cbyc (cfg->pmu[j]->stn, s,16);
00245 cfg->pmu[j]->stn[16] = '\0';
00246
00247 printf("STATION NAME %s\n",cfg->pmu[j]->stn);
00248 s = s + 16;
00249 cfglen += 16;
00250
00251
00252 copy_cbyc (idcode_l, s,2);
00253 idcode_l[2]='\0';
00254 cfg->pmu[j]->idcode = to_intconvertor(idcode_l);
00255 printf("ID Code %d\n",cfg->pmu[j]->idcode);
00256 s = s + 2;
00257 cfglen += 2;
00258
00259
00260 copy_cbyc ((unsigned char *) cfg->pmu[j]->data_format, s,2);
00261 cfg->pmu[j]->data_format[2]='\0';
00262 printf("PMU format %s\n",cfg->pmu[j]->data_format);
00263 s = s + 2;
00264 cfglen += 2;
00265
00266 char hex = cfg->pmu[j]->data_format[1];
00267 hex <<= 4;
00268
00269
00270 cfg->pmu[j]->fmt = malloc(sizeof(struct format));
00271 cfg->pmu[j]->fmt->freq = (hex & 0x80) >> 7;
00272 cfg->pmu[j]->fmt->analog = (hex & 0x40) >> 6;
00273 cfg->pmu[j]->fmt->phasor = (hex & 0x20) >> 5;
00274 cfg->pmu[j]->fmt->polar = (hex & 0x10) >> 4;
00275
00276
00277 copy_cbyc (phnmr, s,2);
00278 phnmr[2]='\0';
00279 cfg->pmu[j]->phnmr = to_intconvertor(phnmr);
00280 printf("Phasors %d\n",cfg->pmu[j]->phnmr);
00281 s = s + 2;
00282 cfglen += 2;
00283
00284
00285 copy_cbyc (annmr, s,2);
00286 annmr[2]='\0';
00287 cfg->pmu[j]->annmr = to_intconvertor(annmr);
00288 printf("Analogs %d\n",cfg->pmu[j]->annmr);
00289 s = s + 2;
00290 cfglen += 2;
00291
00292
00293 copy_cbyc (dgnmr, s,2);
00294 dgnmr[2]='\0';
00295 cfg->pmu[j]->dgnmr = to_intconvertor(dgnmr);
00296 printf("Digitals %d\n",cfg->pmu[j]->dgnmr);
00297 s = s + 2;
00298 cfglen += 2;
00299
00300 cn = malloc(sizeof(struct channel_names));
00301 cn->first = NULL;
00302
00304 if(cfg->pmu[j]->phnmr != 0){
00305 cn->phnames = malloc((cfg->pmu[j]->phnmr) * sizeof(char*));
00306 if(!cn->phnames) {
00307 printf("Not enough memory cfg->pmu[j]->cn->phnames[][]\n");
00308 exit(1);
00309 }
00310
00311 for (i = 0; i < cfg->pmu[j]->phnmr; i++) {
00312
00313 cn->phnames[i] = malloc(17*sizeof(char));
00314 memset(cn->phnames[i],'\0',17);
00315
00316 }
00317
00318 cfg->pmu[j]->phunit = malloc(cfg->pmu[j]->phnmr*sizeof(float*));
00319 if(!cfg->pmu[j]->phunit) {
00320 printf("Not enough memory cfg.pmu[j]->phunit[][]\n");
00321 exit(1);
00322 }
00323
00324 for (i = 0; i < cfg->pmu[j]->phnmr; i++) {
00325 cfg->pmu[j]->phunit[i] = malloc(sizeof(float));
00326 }
00327
00328
00329 i = 0;
00330 while(i<cfg->pmu[j]->phnmr){
00331
00332 copy_cbyc (cn->phnames[i], s,16);
00333 cn->phnames[i][16] = '\0';
00334 printf("Phnames %s\n",cn->phnames[i]);
00335 s = s + 16;
00336 cfglen += 16;
00337 i++;
00338 }
00339 }
00340
00341
00342 if(cfg->pmu[j]->annmr != 0){
00343 cn->angnames = malloc((cfg->pmu[j]->annmr)*sizeof(char*));
00344 if(!cn->angnames) {
00345
00346 printf("Not enough memory cfg->pmu[j]->cn->phnames[][]\n");
00347 exit(1);
00348 }
00349
00350 for (i = 0; i < cfg->pmu[j]->annmr; i++) {
00351
00352 cn->angnames[i] = malloc(17*sizeof(char));
00353 memset(cn->angnames[i],'\0',17);
00354 }
00355
00356 cfg->pmu[j]->anunit = malloc(cfg->pmu[j]->annmr*sizeof(float*));
00357 if(!cfg->pmu[j]->anunit) {
00358 printf("Not enough memory cfg.pmu[j]->anunit[][]\n");
00359 exit(1);
00360 }
00361
00362 for (i = 0; i < cfg->pmu[j]->annmr; i++) {
00363 cfg->pmu[j]->anunit[i] = malloc(sizeof(float));
00364 }
00365
00366 i = 0;
00367
00368 while(i<cfg->pmu[j]->annmr){
00369 copy_cbyc (cn->angnames[i], s,16);
00370 cn->angnames[i][16]='\0';
00371 printf("ANGNAMES %s\n",cn->angnames[i]);
00372 s = s + 16;
00373 cfglen += 16;
00374 i++;
00375 }
00376 }
00377
00378
00379 if(cfg->pmu[j]->dgnmr != 0){
00380
00381 cfg->pmu[j]->dgunit = malloc(cfg->pmu[j]->dgnmr*sizeof(char*));
00382 if(!cfg->pmu[j]->dgunit) {
00383
00384 printf("Not enough memory cfg->pmu[j]->dgunit[][]\n");
00385 exit(1);
00386 }
00387
00388 for (i = 0; i < cfg->pmu[j]->dgnmr; i++) {
00389
00390 cfg->pmu[j]->dgunit[i] = malloc(5);
00391 }
00392 }
00393 struct dgnames *q;
00394 i = 0;
00395 while(i < cfg->pmu[j]->dgnmr) {
00396
00397 struct dgnames *temp1 = malloc(sizeof(struct dgnames));
00398 temp1->dgn = malloc(16*sizeof(char *));
00399 if(!temp1->dgn) {
00400
00401 printf("Not enough memory temp1->dgn\n");
00402 exit(1);
00403 }
00404
00405 for (i = 0; i < 16; i++) {
00406
00407 temp1->dgn[i] = malloc(17*sizeof(char));
00408
00409 }
00410
00411 temp1->dg_next = NULL;
00412
00413 for(dgchannels = 0;dgchannels < 16;dgchannels++){
00414
00415 memset(temp1->dgn[dgchannels],'\0',16);
00416 copy_cbyc (temp1->dgn[dgchannels], s,16);
00417 temp1->dgn[dgchannels][16] = '\0';
00418 s += 16;
00419 cfglen += 16;
00420 k = 0;
00421 printf("%s\n",temp1->dgn[dgchannels]);
00422 }
00423
00424 if(cn->first == NULL){
00425 cn->first = q = temp1;
00426
00427 } else {
00428
00429 while(q->dg_next!=NULL){
00430 q = q->dg_next;
00431 }
00432 q->dg_next = temp1;
00433 }
00434
00435 i++;
00436 }
00437
00438 cfg->pmu[j]->cnext = cn;
00439
00441 if(cfg->pmu[j]->phnmr != 0){
00442
00443 i = 0;
00444 while(i < cfg->pmu[j]->phnmr){
00445
00446 memset(buf,'\0',9);
00447 copy_cbyc (buf, s,4);
00448 buf[4] = '\0';
00449 l_phunit = to_long_int_convertor(buf);
00450 *cfg->pmu[j]->phunit[i] = l_phunit * 0.00001;
00451 printf("Phasor Fact %d = %f\n",i,*cfg->pmu[j]->phunit[i]);
00452 s = s + 4;
00453 cfglen += 4;
00454 i++;
00455 }
00456 }
00457
00458
00459 if(cfg->pmu[j]->annmr != 0){
00460
00461 i=0;
00462 while(i<cfg->pmu[j]->annmr){
00463
00464 memset(buf,'\0',9);
00465 copy_cbyc (buf, s,4);
00466 buf[4] = '\0';
00467 l_anunit = to_long_int_convertor(buf);
00468 *cfg->pmu[j]->anunit[i] = l_anunit*0,00001;
00469 printf("Analog Fact %d = %f\n",i,*cfg->pmu[j]->anunit[i]);
00470 s = s + 4;
00471 cfglen += 4;
00472 i++;
00473 }
00474
00475 }
00476
00478 if(cfg->pmu[j]->dgnmr != 0){
00479
00480 i = 0;
00481 while(i < cfg->pmu[j]->dgnmr ){
00482
00483 copy_cbyc(cfg->pmu[j]->dgunit[i],s,4);
00484 cfg->pmu[j]->dgunit[i][4] = '\0';
00485 printf("DGWORD %s\n",cfg->pmu[j]->dgunit[i]);
00486 s += 4;
00487 cfglen += 4;
00488 i++;
00489 }
00490 }
00491
00492 copy_cbyc (fnom, s,2);
00493 fnom[2]='\0';
00494 cfg->pmu[j]->fnom = to_intconvertor(fnom);
00495 printf("FREQUENCY %d\n",cfg->pmu[j]->fnom);
00496 s = s + 2;
00497 cfglen += 2;
00498
00499 copy_cbyc (cfg_cnt, s,2);
00500 cfg_cnt[2] = '\0';
00501 cfg->pmu[j]->cfg_cnt = to_intconvertor(cfg_cnt);
00502 printf("CFG CHANGE COUNT %d\n",cfg->pmu[j]->cfg_cnt);
00503 s = s + 2;
00504 cfglen += 2;
00505 j++;
00506 }
00507
00508
00509 copy_cbyc (data_rate, s,2);
00510 data_rate[2] = '\0';
00511 cfg->data_rate = to_intconvertor(data_rate);
00512 printf("Data Rate %d\n",cfg->data_rate);
00513 s += 2;
00514 cfglen += 2;
00515 cfg->cfgnext = NULL;
00516
00517
00518
00519 pthread_mutex_lock(&mutex_cfg);
00520
00521 if (cfgfirst == NULL) {
00522
00523 cfgfirst = cfg;
00524
00525 } else {
00526
00527 struct cfg_frame *temp_cfg = cfgfirst;
00528
00529
00530 while(temp_cfg->cfgnext != NULL){
00531
00532 temp_cfg = temp_cfg->cfgnext;
00533
00534 }
00535 temp_cfg->cfgnext = cfg;
00536
00537 }
00538
00539 pthread_mutex_unlock(&mutex_cfg);
00540 pthread_mutex_unlock(&mutex_file);
00541
00542 free(sync);
00543 free(frame_size);
00544 free(idcode_h);
00545 free(soc);
00546 free(fracsec);
00547 free(time_base);
00548 free(num_pmu);
00549 free(stn);
00550 free(idcode_l);
00551 free(format);
00552 free(phnmr);
00553 free(annmr);
00554 free(dgnmr);
00555 free(fnom);
00556 free(cfg_cnt);
00557 free(data_rate);
00558 free(buf);
00559
00560
00561 }