#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include "parser.h"
#include "global.h"
#include "dallocate.h"
#include "align_sort.h"
#include "connections.h"
#include "recreate.h"
#include <stdint.h>
Go to the source code of this file.
Functions | |
void | cfgparser (unsigned char st[]) |
void | remove_old_cfg (unsigned char idcode[], unsigned char frame[]) |
int | dataparser (unsigned char data[]) |
int | check_statword (unsigned char stat[]) |
void | add_id_to_status_change_list (unsigned char idcode[]) |
void | remove_id_from_status_change_list (unsigned char idcode[]) |
unsigned int | to_intconvertor (unsigned char array[]) |
void | long_int_to_ascii_convertor (unsigned long int n, unsigned char hex[]) |
void | int_to_ascii_convertor (unsigned int n, unsigned char hex[]) |
void | copy_cbyc (unsigned char dst[], unsigned char *s, int size) |
int | ncmp_cbyc (unsigned char dst[], unsigned char src[], int size) |
void | byte_by_byte_copy (unsigned char dst[], unsigned char src[], int index, int n) |
unsigned long int | to_long_int_convertor (unsigned char array[]) |
uint16_t | compute_CRC (unsigned char *message, char length) |
void add_id_to_status_change_list | ( | unsigned char | idcode[] | ) |
Definition at line 1114 of file parser.c.
References copy_cbyc(), fp_log, status_change_pmupdcid::idcode, mutex_status_change, status_change_pmupdcid::pmuid_next, and root_pmuid.
Referenced by dataparser().
01114 { 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 }
void byte_by_byte_copy | ( | unsigned char | dst[], | |
unsigned char | src[], | |||
int | index, | |||
int | n | |||
) |
Definition at line 1266 of file parser.c.
References i.
Referenced by create_cfgframe(), create_command_frame(), and create_dataframe().
01266 { 01267 01268 int i; 01269 for(i = 0;i<n; i++) 01270 dst[index + i] = src[i]; 01271 01272 }
void cfgparser | ( | unsigned char | st[] | ) |
WHILE EACH PMU IS HANDLED
PHASOR FACTORS
Definition at line 77 of file parser.c.
References format::analog, channel_names::angnames, for_each_pmu::annmr, for_each_pmu::anunit, for_each_pmu::cfg_cnt, cfgfirst, cfg_frame::cfgnext, for_each_pmu::cnext, copy_cbyc(), for_each_pmu::data_format, cfg_frame::data_rate, dgnames::dg_next, dgnames::dgn, for_each_pmu::dgnmr, for_each_pmu::dgunit, channel_names::first, for_each_pmu::fmt, for_each_pmu::fnom, fp_log, cfg_frame::fracsec, cfg_frame::framesize, free_cfgframe_object(), format::freq, i, for_each_pmu::idcode, cfg_frame::idcode, mutex_cfg, mutex_file, ncmp_cbyc(), cfg_frame::num_pmu, format::phasor, channel_names::phnames, for_each_pmu::phnmr, for_each_pmu::phunit, cfg_frame::pmu, format::polar, remove_id_from_status_change_list(), remove_old_cfg(), cfg_frame::soc, for_each_pmu::stn, cfg_frame::time_base, and to_intconvertor().
Referenced by PMU_process_TCP(), and PMU_process_UDP().
00077 { 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 /******************** PARSING BEGINGS *******************/ 00089 00090 pthread_mutex_lock(&mutex_file); 00091 00092 fp = fopen("cfg.bin","ab"); //Store configuration in a file 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 // Memory Allocation Begins 00106 // Allocate memory to framesize 00107 cfg->framesize = malloc(3*sizeof(unsigned char)); 00108 if(!cfg->framesize) { 00109 printf("No enough memory for cfg->framesize\n"); 00110 } 00111 00112 // Allocate memory to idcode 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 // Allocate memory to soc 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 // Allocate memory to fracsec 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 // Allocate memory to time_base 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 // Allocate memory to time_base 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 // Allocate memory to num_pmu 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 // Allocate memory to data_rate 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 //Copy sync word to file 00155 copy_cbyc(sync,(unsigned char *)s,2); 00156 sync[2] = '\0'; 00157 s = s + 2; 00158 00159 // Separate the FRAME SIZE 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 // Copy the cfg frame in the file 00167 fwrite(st, sizeof(unsigned char),framesize, fp); 00168 00169 //SEPARATE IDCODE 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 /**** Remove the id from the list of Stat change if it is present ****/ 00177 remove_id_from_status_change_list(cfg->idcode); 00178 00179 //SEPARATE SOC 00180 copy_cbyc(cfg->soc,(unsigned char *)s,4); 00181 cfg->soc[4] = '\0'; 00182 s =s + 4; 00183 00184 //SEPARATE FRACSEC 00185 copy_cbyc(cfg->fracsec,(unsigned char *)s,4); 00186 cfg->fracsec[4] = '\0'; 00187 s = s + 4; 00188 00189 //SEPARATE TIMEBASE 00190 copy_cbyc (cfg->time_base,(unsigned char *)s,4); 00191 cfg->time_base[4]='\0'; 00192 s = s + 4; 00193 00194 //SEPARATE PMU NUM 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 // Allocate Memeory For Each PMU 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 // Memory Allocation for stn 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 // Memory Allocation for idcode 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 // Memory Allocation for format 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 // Memory Allocation for phnmr 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 // Memory Allocation for annmr 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 // Memory Allocation for dgnmr 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 // Memory Allocation for fnom 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 // Memory Allocation for cfg_cnt 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 //SEPARATE STATION NAME 00275 copy_cbyc (cfg->pmu[j]->stn,(unsigned char *)s,16); 00276 cfg->pmu[j]->stn[16] = '\0'; 00277 s = s + 16; 00278 00279 //SEPARATE IDCODE 00280 copy_cbyc (cfg->pmu[j]->idcode,(unsigned char *)s,2); 00281 cfg->pmu[j]->idcode[2] = '\0'; 00282 s = s + 2; 00283 00284 //SEPARATE DATA FORMAT 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 // Extra field has been added to identify polar,rectangular,floating/fixed point 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 //SEPARATE PHASORS 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 //SEPARATE ANALOGS 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 //SEPARATE DIGITALS 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;//Index for PHNAMES 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 //SEPARATE ANALOG NAMES 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;//Index for ANGNAMES 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; //Index for number of dgwords 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 } //DGWORD WHILE ENDS 00417 00418 cfg->pmu[j]->cnext = cn;//Assign to pointers 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){ //Separate the Phasor conversion factors 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 }//if for PHASOR Factors ends 00442 00443 //ANALOG FACTORS 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){ //Separate the Phasor conversion factors 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 } // if for ANALOG Factors ends 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){ //Separate the Phasor conversion factors 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 } //if for Digital Words FActtors ends 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 }//While for PMU number ends 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 // Adjust the configuration object pointers 00509 // Lock the mutex_cfg 00510 pthread_mutex_lock(&mutex_cfg); 00511 00512 // Index is kept to replace the cfgfirst if it matches 00513 int index = 0; 00514 00515 if (cfgfirst == NULL) { // Main if 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 //Check if the configuration frame already exists 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 }// While ends 00540 00541 if(match) { 00542 00543 if(!index) { 00544 00545 // Replace the cfgfirst 00546 cfg->cfgnext = cfgfirst->cfgnext; 00547 free_cfgframe_object(cfgfirst); 00548 cfgfirst = cfg; 00549 // Get the new value of the CFG frame 00550 fclose(fp); 00551 remove_old_cfg(cfg->idcode,st); 00552 00553 } else { 00554 00555 // Replace in between cfg 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 { // No match and not first cfg 00564 00565 tprev_cfg->cfgnext = cfg; 00566 fclose(fp); 00567 } 00568 00569 } //Main if ends 00570 00571 pthread_mutex_unlock(&mutex_cfg); 00572 pthread_mutex_unlock(&mutex_file); 00573 00574 }
int check_statword | ( | unsigned char | stat[] | ) |
Definition at line 1057 of file parser.c.
References fp_log.
Referenced by dataparser().
01057 { 01058 01059 int ret = 0; 01060 01061 if(stat[1] == 0x0f) { 01062 //Programmer has used these bits as an indication for PMU data that has not arrived 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 // fprintf(fp_log,"RET from check_statword() %d\n",ret); 01104 return ret; 01105 }
uint16_t compute_CRC | ( | unsigned char * | message, | |
char | length | |||
) |
Definition at line 1297 of file parser.c.
References i.
Referenced by connect_pmu_tcp(), connect_pmu_udp(), create_cfgframe(), create_command_frame(), and create_dataframe().
01297 { 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 }
void copy_cbyc | ( | unsigned char | dst[], | |
unsigned char * | s, | |||
int | size | |||
) |
Definition at line 1237 of file parser.c.
References i.
Referenced by add_id_to_status_change_list(), assign_df_to_TSB(), cfgparser(), connect_pmu_tcp(), connect_pmu_udp(), create_cfgframe(), dataparser(), init_cfgparser(), remove_cfg_from_file(), remove_old_cfg(), and sort_data_inside_TSB().
01237 { 01238 01239 int i; 01240 for(i = 0; i< size; i++) 01241 dst[i] = *(s + i); 01242 }
int dataparser | ( | unsigned char | data[] | ) |
Definition at line 691 of file parser.c.
References add_id_to_status_change_list(), format::analog, data_for_each_pmu::analog, data_for_each_pmu::annmr, for_each_pmu::annmr, cfgfirst, cfg_frame::cfgnext, check_statword(), copy_cbyc(), data_for_each_pmu::dfreq, data_for_each_pmu::dgnmr, for_each_pmu::dgnmr, data_for_each_pmu::digital, data_frame::dnext, data_frame::dpmu, data_for_each_pmu::fmt, for_each_pmu::fmt, fp_log, data_frame::fracsec, data_frame::framesize, cfg_frame::framesize, data_for_each_pmu::freq, format::freq, i, data_frame::idcode, cfg_frame::idcode, mutex_cfg, ncmp_cbyc(), data_frame::num_pmu, cfg_frame::num_pmu, format::phasor, data_for_each_pmu::phasors, data_for_each_pmu::phnmr, for_each_pmu::phnmr, cfg_frame::pmu, format::polar, data_frame::soc, data_for_each_pmu::stat, time_align(), and to_intconvertor().
Referenced by PMU_process_TCP(), and PMU_process_UDP().
00691 { 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 //Skip SYN 00703 d += 2; 00704 00705 //SEPARATE FRAMESIZE 00706 copy_cbyc (framesize,d,2); 00707 framesize[2]='\0'; 00708 d += 2; 00709 00710 //SEPARATE IDCODE 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 // Check for the IDCODE in Configuration Frame 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 // idcode matches with cfg idcode 00737 if(match){ 00738 00739 // fprintf(fp_log,"Inside dataparser() data_frame and matched with cfg\n"); 00740 00741 //Allocate memory for data frame 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 // Allocate memory for df->framesize 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 // Allocate memory for df->idcode 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 // Allocate memory for df->soc 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 // Allocate memory for df->fracsec 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 // Allocate Memeory For Each PMU 00778 num_pmu = to_intconvertor(temp_cfg->num_pmu); 00779 d += 12; 00780 // fprintf(fp_log,"num_pmu %d\n",num_pmu); 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); //idcode = PMU/PDC 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 // Now start separating the data from data frame 00802 00803 //Copy Framesize 00804 d -= 14; 00805 copy_cbyc (df->framesize,d,2); 00806 df->framesize[2] = '\0'; 00807 d += 2; 00808 00809 //Copy IDCODE 00810 copy_cbyc (df->idcode,idcode,2); 00811 df->idcode[2] = '\0'; 00812 d += 2; 00813 00814 //Copy SOC 00815 copy_cbyc (df->soc,d,4); 00816 df->soc[4] = '\0'; 00817 d += 4; 00818 00819 //Copy FRACSEC 00820 copy_cbyc (df->fracsec,d,4); 00821 df->fracsec[4] = '\0'; 00822 d += 4; 00823 00824 //Copy NUM PMU 00825 df->num_pmu = num_pmu; 00826 00827 if(num_pmu > 1 ) 00828 d += 2; 00829 00830 // Separate the data for each PMU 00831 while(j<num_pmu) { 00832 00833 copy_cbyc (stat,d,2); 00834 stat[2] = '\0'; 00835 d += 2; 00836 00837 // Check Stat Word for each data block 00838 stat_status = check_statword(stat); 00839 00840 // If the data has not arrived 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 //Status for configuration bits have been changed. Add the pmu id to the 'status_change_pmupdcid linked list'. 00856 add_id_to_status_change_list(idcode); //idcode = PMU/PDC 00857 00858 // Allocate memory for stat 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 // Extract PHNMR, DGNMR, ANNMR 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 //Allocate memory for stat, Phasors, Analogs,Digitals and Phasors and Frequencies 00879 /* Memory Allocation Begins */ 00880 00881 // Allocate memory for stat 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 //Analogs 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 //Frequency 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 //Digital 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 /* Memory Allocation Ends */ 00950 00951 //Check stat word of each PMU data block 00952 copy_cbyc (df->dpmu[j]->stat,stat,2); 00953 df->dpmu[j]->stat[2] = '\0'; 00954 memset(stat,'\0',3); 00955 00956 // Copy FMT 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 // Copy num of phasors analogs and digitals 00964 df->dpmu[j]->phnmr = phnmr; 00965 df->dpmu[j]->annmr = annmr; 00966 df->dpmu[j]->dgnmr = dgnmr; 00967 00968 //Phasors 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 //Freq 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 //Analogs 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 // Digital 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 } //While ends 01034 01035 // Now start Time aligning and Sorting Operation for data_frame df 01036 time_align(df); 01037 01038 } else { 01039 01040 //No match for configuration frame 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 }
void int_to_ascii_convertor | ( | unsigned int | n, | |
unsigned char | hex[] | |||
) |
Definition at line 1225 of file parser.c.
Referenced by create_cfgframe(), create_command_frame(), create_dataframe(), and sort_data_inside_TSB().
void long_int_to_ascii_convertor | ( | unsigned long int | n, | |
unsigned char | hex[] | |||
) |
Definition at line 1212 of file parser.c.
Referenced by create_cfgframe(), and create_command_frame().
01212 { 01213 01214 hex[0] = n >> 24; 01215 hex[1] = n >> 16; 01216 hex[2] = n >> 8; 01217 hex[3] = n ; 01218 01219 }
int ncmp_cbyc | ( | unsigned char | dst[], | |
unsigned char | src[], | |||
int | size | |||
) |
Definition at line 1248 of file parser.c.
References i.
Referenced by assign_df_to_TSB(), cfgparser(), dataparser(), remove_cfg_from_file(), remove_id_from_status_change_list(), remove_llnode(), remove_old_cfg(), sort_data_inside_TSB(), and time_align().
01248 { 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 }
void remove_id_from_status_change_list | ( | unsigned char | idcode[] | ) |
Definition at line 1153 of file parser.c.
References fp_log, status_change_pmupdcid::idcode, mutex_status_change, ncmp_cbyc(), status_change_pmupdcid::pmuid_next, and root_pmuid.
Referenced by cfgparser().
01153 { 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 // If It is the first element(root) in the list 01180 if(!ncmp_cbyc(temp->idcode,root_pmuid->idcode,2)) { 01181 01182 root_pmuid = root_pmuid->pmuid_next; 01183 free(temp); 01184 // If it is any element other than first(root) in the list 01185 } else { 01186 01187 tprev->pmuid_next = temp->pmuid_next; 01188 free(temp); 01189 01190 } 01191 } 01192 pthread_mutex_unlock(&mutex_status_change); 01193 }
void remove_old_cfg | ( | unsigned char | idcode[], | |
unsigned char | frame[] | |||
) |
Definition at line 583 of file parser.c.
References copy_cbyc(), fp_log, cfg_frame::framesize, ncmp_cbyc(), and to_intconvertor().
Referenced by cfgparser().
00583 { //void remove_old_cfg(char idcode[],char frame[]) Begins 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 //Get file length 00597 fseek(file, 0, SEEK_END); 00598 fileLen = ftell(file); 00599 fseek(file, 0, SEEK_SET); 00600 // printf("BEFORE REMOVAL OF OLDFRAME FILE LEN %ld\n",fileLen); 00601 00602 while (fileLen != 0) /* Till the EOF */{ 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 //match IDCODE in cfg.bin file 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 //Place rest of lines in the new file 00625 fwrite(line, sizeof(unsigned char),framesize,newfile); 00626 free(line); 00627 fileLen -= framesize; 00628 } 00629 }//While ends 00630 00631 // The new cfg is copied in the ncfg.bin file 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 // If cfg.bin file still contains data copy it to ncfg.bin 00641 while (fileLen != 0) /* Till the EOF */{ 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 //This skips the last line of the file that contains already added cfg 00655 //hence we dont copy this line to ncfg.bin 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 //File renaming 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"); /* why didn't the file open? */ 00681 } 00682 00683 }
unsigned int to_intconvertor | ( | unsigned char | array[] | ) |
Definition at line 1199 of file parser.c.
Referenced by assign_df_to_TSB(), cfgparser(), connect_pmu_tcp(), connect_pmu_udp(), create_cfgframe(), create_dataframe(), dataparser(), free_cfgframe_object(), init_cfgparser(), PMU_process_TCP(), PMU_process_UDP(), recreate_cfg_objects(), remove_cfg_from_file(), remove_old_cfg(), and sort_data_inside_TSB().
01199 { 01200 01201 unsigned int n; 01202 n = array[0]; 01203 n <<= 8; 01204 n |= array[1]; 01205 return n; 01206 }
unsigned long int to_long_int_convertor | ( | unsigned char | array[] | ) |
Definition at line 1278 of file parser.c.
Referenced by clear_TSB(), create_cfgframe(), and time_align().
01278 { 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 }