align_sort.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TimeStampBuffer
struct  pmupdc_id_list

Defines

#define MAXTSB   5

Functions

void time_align (struct data_frame *df)
void assign_df_to_TSB (struct data_frame *df, int index)
void dispatch (int index)
void sort_data_inside_TSB (int index)
void clear_TSB (int index)
int create_dataframe (int index)
int create_cfgframe ()

Variables

int front
int rear
struct TimeStampBuffer TSB [MAXTSB]

Define Documentation

#define MAXTSB   5

Definition at line 30 of file align_sort.h.

Referenced by time_align().


Function Documentation

void assign_df_to_TSB ( struct data_frame df,
int  index 
)

Definition at line 168 of file align_sort.c.

References cfgfirst, cfg_frame::cfgnext, copy_cbyc(), data_frame::dnext, TimeStampBuffer::first_data_frame, fp_log, data_frame::fracsec, cfg_frame::fracsec, TimeStampBuffer::fracsec, free_dataframe_object(), data_frame::idcode, cfg_frame::idcode, pmupdc_id_list::idcode, TimeStampBuffer::idlist, ncmp_cbyc(), pmupdc_id_list::nextid, cfg_frame::num_pmu, pmupdc_id_list::num_pmu, data_frame::soc, cfg_frame::soc, TimeStampBuffer::soc, to_intconvertor(), and TSB.

Referenced by time_align().

00168                                                        { 
00169 
00170         fprintf(fp_log,"Inside assign_df_to_TSB()\n");
00171         /* Check if the TSB is used for the first time. If so we need to 
00172            allocate memory to its member variables */
00173         if(TSB[index].soc == NULL) { // 1 if
00174 
00175                 struct  cfg_frame *temp_cfg = cfgfirst;
00176         
00177                 TSB[index].soc = malloc(5);
00178                 TSB[index].fracsec = malloc(5);
00179 
00180                 memset(TSB[index].soc,'\0',5);
00181                 memset(TSB[index].fracsec,'\0',5);
00182                 
00183                 copy_cbyc((unsigned char *)TSB[index].soc,df->soc,4);
00184                 copy_cbyc((unsigned char *)TSB[index].fracsec,df->fracsec,4);
00185                 
00186                 TSB[index].first_data_frame = df; /* Assign df to the 'first_data_frame' in the data frame linked list of TSB[index] */
00187                 
00188                 /* Now we need to store the pmu/pdc id in the pmupdc_id_list that would be required while sorting */
00189                 struct pmupdc_id_list *temp_pmuid;
00190                 while(temp_cfg != NULL) {
00191                 
00192                         /* Create a node of the type 'pmupdc_id_list' and copy the pmu/pde id from the cfg to it */
00193                         struct pmupdc_id_list *pmuid = malloc(sizeof(struct pmupdc_id_list));
00194                         pmuid->idcode = malloc(3);
00195                         memset(pmuid->idcode,'\0',3);
00196                         copy_cbyc((unsigned char *)pmuid->idcode,temp_cfg->idcode,2);
00197                         pmuid->num_pmu = to_intconvertor(temp_cfg->num_pmu);
00198                         pmuid->nextid = NULL;
00199 
00200                         if(TSB[index].idlist == NULL) { /* Assign the pmuid to the idlist as it is the first id in the list */
00201 
00202                                 TSB[index].idlist = temp_pmuid = pmuid;
00203 
00204                         } else {
00205 
00206                                 temp_pmuid->nextid = pmuid;
00207                                 temp_pmuid = pmuid;
00208 
00209                         }                       
00210                       
00211                         temp_cfg = temp_cfg->cfgnext;                           
00212                 } // while ends . A pmu/pdc id list is created for the TSB[index]
00213 
00214         } else { // 1 if else 
00215 
00216                 struct  cfg_frame *temp_cfg = cfgfirst;
00217                 if(TSB[index].first_data_frame == NULL) { // 2 if 
00218 
00219                      /* After TSB[index] is cleared this is the first data frame for it. 
00220                         The memory for the member variables of TSB[index] has already 
00221                         been allocated. Hence after dispatch() and clear_TSB() operation
00222                         this TSB is to be assigned the data_frame for the first time. */
00223                         
00224                         copy_cbyc((unsigned char *)TSB[index].soc,df->soc,4);
00225                         copy_cbyc((unsigned char *)TSB[index].fracsec,df->fracsec,4);
00226                         
00227                         TSB[index].first_data_frame = df;     /* Assign df to the 'first_data_frame' in the data frame linked list of 
00228                                                                  TSB[index] */
00229                 
00230                      /* Now we need to store the pmu/pdc id in the pmupdc_id_list 
00231                         that would be required while sorting */
00232                         struct pmupdc_id_list *temp_pmuid;
00233                         while(temp_cfg != NULL) {
00234                         
00235                              /* Create a node of the type 'pmupdc_id_list' and 
00236                                 copy the pmu/pde id from the cfg to it */
00237 
00238                                 struct pmupdc_id_list *pmuid = malloc(sizeof(struct pmupdc_id_list));
00239                                 pmuid->idcode = malloc(3);
00240                                 memset(pmuid->idcode,'\0',3);
00241                                 copy_cbyc((unsigned char *)pmuid->idcode,temp_cfg->idcode,2);
00242                                 pmuid->num_pmu = to_intconvertor(temp_cfg->num_pmu);    
00243                                 pmuid->nextid = NULL;
00244         
00245                                 if(TSB[index].idlist == NULL) { /* Assign the pmuid to the idlist as it is the first id in the list */
00246                                 
00247                                         TSB[index].idlist = temp_pmuid = pmuid;
00248         
00249                                 } else {
00250 
00251                                         temp_pmuid->nextid = pmuid;
00252                                         temp_pmuid = pmuid;
00253         
00254                                 }                       
00255                               
00256                                 temp_cfg = temp_cfg->cfgnext;                           
00257                         } // while ends
00258                 
00259                 } else { // 2 if else
00260         
00261                         /* Traverse the data frames of TSB[index] and assign the df to 'dnext' of 
00262                         the last element in the data frame LL.*/
00263                         struct data_frame *temp_df,*check_df;
00264 
00265                         /* Need to check if df with same idcode and soc is already assigned to 
00266                            the TSB[index] */
00267                         check_df = TSB[index].first_data_frame;
00268                         while(check_df != NULL) {
00269                                         
00270                                 if(!ncmp_cbyc(check_df->idcode,df->idcode,2)) {
00271                                 
00272                                         fprintf(fp_log,"Data frame already inside TSB[%d]\n",index);
00273                                         free_dataframe_object(df);
00274                                         return;                                 
00275                 
00276                                 } else {
00277 
00278                                         check_df = check_df->dnext;
00279         
00280                                 }                                                       
00281                         }
00282                 
00283                         temp_df = TSB[index].first_data_frame;
00284                         while(temp_df->dnext != NULL) {
00285                         
00286                                 temp_df = temp_df->dnext;                       
00287                 
00288                         }
00289                         
00290                         temp_df->dnext = df;     
00291                 } // 2 if ends  
00292                 
00293         } // 1 if ends
00294 } 

Here is the call graph for this function:

Here is the caller graph for this function:

void clear_TSB ( int  index  ) 

Definition at line 569 of file align_sort.c.

References data_frame::dnext, TimeStampBuffer::first_data_frame, fp_log, data_frame::fracsec, free_dataframe_object(), front, pmupdc_id_list::idcode, TimeStampBuffer::idlist, pmupdc_id_list::nextid, data_frame::soc, to_long_int_convertor(), and TSB.

Referenced by dispatch().

00569                           { // 
00570 
00571         fprintf(fp_log,"Inside clear_TSB()\n");
00572         unsigned long int tsb_soc,tsb_fracsec;
00573         tsb_soc = to_long_int_convertor((unsigned char *)TSB[front].soc);
00574         tsb_fracsec = to_long_int_convertor((unsigned char *)TSB[front].fracsec);
00575 
00576         memset(TSB[index].soc,'\0',5);
00577         memset(TSB[index].fracsec,'\0',5);
00578 
00579         struct pmupdc_id_list *t_list,*r_list;
00580         t_list = TSB[index].idlist;
00581 
00582         while(t_list != NULL) {
00583         
00584                 r_list = t_list->nextid;
00585                 free(t_list->idcode);
00586                 free(t_list);
00587                 t_list = r_list;                        
00588         }
00589 
00590         struct data_frame *t,*r;
00591         t = TSB[index].first_data_frame;
00592 
00593         while(t != NULL) {
00594         
00595                 r = t->dnext;
00596                 free_dataframe_object(t);
00597                 t = r;                  
00598         }
00599 
00600         TSB[index].first_data_frame = NULL;
00601         TSB[index].idlist = NULL;
00602 }

Here is the call graph for this function:

Here is the caller graph for this function:

int create_cfgframe (  ) 

Definition at line 771 of file align_sort.c.

References channel_names::angnames, for_each_pmu::annmr, for_each_pmu::anunit, byte_by_byte_copy(), for_each_pmu::cfg_cnt, cfgfirst, cfgframe, cfg_frame::cfgnext, CFGSYNC, for_each_pmu::cnext, compute_CRC(), 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::fnom, fp_log, cfg_frame::fracsec, cfg_frame::framesize, i, for_each_pmu::idcode, int_to_ascii_convertor(), long_int_to_ascii_convertor(), cfg_frame::num_pmu, PDC_IDCODE, channel_names::phnames, for_each_pmu::phnmr, for_each_pmu::phunit, cfg_frame::pmu, cfg_frame::soc, for_each_pmu::stn, cfg_frame::time_base, to_intconvertor(), and to_long_int_convertor().

Referenced by UL_tcp_connection(), and UL_udp().

00771                       { 
00772 
00773         fprintf(fp_log,"Inside create_cfgframe()()\n");
00774         struct cfg_frame *temp_cfg;
00775         int total_frame_size = 0,count = 0;
00776         unsigned char datarate[2],soc[4],fracsec[4]; // hard coded
00777         int total_num_pmu = 0;
00778         unsigned char time_base[4];
00779         unsigned int fsize,num_pmu,phnmr,dgnmr,annmr;
00780         unsigned int data_rate,temp_data_rate;
00781         unsigned long int sec,frac = 0,temp_tb,tb;
00782         uint16_t chk;
00783 
00784         sec = (long int)time (NULL);
00785         long_int_to_ascii_convertor(sec,soc);
00786         long_int_to_ascii_convertor(frac,fracsec);
00787 
00788         temp_cfg = cfgfirst;
00789 
00790         while(temp_cfg != NULL) {
00791         
00792                  if(count == 0) { // Copy the soc,fracsec,timebase from the first CFG to the combined CFG
00793                         
00794                           //SEPARATE TIMBASE    
00795                           tb = to_long_int_convertor(temp_cfg->time_base);
00796                           copy_cbyc (time_base,temp_cfg->time_base,4);
00797 
00798                           data_rate = to_intconvertor(temp_cfg->data_rate);
00799                           copy_cbyc (datarate,temp_cfg->data_rate,2);
00800 
00801                           fsize = to_intconvertor(temp_cfg->framesize);
00802                           total_frame_size += fsize;
00803                           count++;                                 
00804                           // count used to count num of cfg
00805                           num_pmu = to_intconvertor(temp_cfg->num_pmu); 
00806                           total_num_pmu += num_pmu;     
00807                           temp_cfg = temp_cfg->cfgnext;
00808 
00809                  } else {
00810 
00811                           fsize = to_intconvertor(temp_cfg->framesize);
00812                           total_frame_size += fsize;
00813                           total_frame_size -= 24;
00814 
00815                           // take the Lowest Timebase
00816                           temp_tb = to_long_int_convertor(temp_cfg->time_base);
00817                           if(temp_tb < tb) {
00818         
00819                                 copy_cbyc (time_base,temp_cfg->time_base,4);
00820                                 tb = temp_tb;
00821                         
00822                           }
00823 
00824                           // take the highest data rate
00825                           temp_data_rate = to_intconvertor(temp_cfg->data_rate);
00826                           if(temp_data_rate > data_rate) {
00827 
00828                                 copy_cbyc (datarate,temp_cfg->data_rate,2);
00829                                 data_rate = temp_data_rate;                             
00830                           }     
00831                           count++;                                 
00832                           // count used to count num of cfg
00833                           num_pmu = to_intconvertor(temp_cfg->num_pmu); 
00834                           total_num_pmu += num_pmu;     
00835                           temp_cfg = temp_cfg->cfgnext;
00836                 }
00837                 
00838         } // While ENDS 
00839 
00840         cfgframe = malloc((total_frame_size + 1)*sizeof(unsigned char)); // Allocate memory for data frame
00841         cfgframe[total_frame_size] = '\0';
00842 
00843         // Start the Combined CFG frame creation 
00844         int z = 0;
00845         byte_by_byte_copy(cfgframe,CFGSYNC,z,2); // SYNC
00846         z += 2;
00847 
00848         unsigned char temp[3];
00849         memset(temp,'\0',3);
00850         int_to_ascii_convertor(total_frame_size,temp);
00851         byte_by_byte_copy(cfgframe,temp,z,2); // FRAME SIZE
00852         z += 2;
00853         
00854         unsigned char tmp[2];
00855         tmp[0]= cfgframe[2];
00856         tmp[1]= cfgframe[3];
00857         int newl;
00858         newl = to_intconvertor(tmp);
00859         printf("CFG Frame Len %d\n",newl);
00860         
00861         memset(temp,'\0',3);
00862         int_to_ascii_convertor(PDC_IDCODE,temp);
00863         byte_by_byte_copy(cfgframe,temp,z,2); // PDC ID
00864         z += 2;
00865 
00866         byte_by_byte_copy(cfgframe,soc,z,4); //SOC
00867         z += 4;
00868         byte_by_byte_copy(cfgframe,fracsec,z,4); //FRACSEC
00869         z += 4;
00870         byte_by_byte_copy(cfgframe,time_base,z,4); //TIMEBASE
00871         z += 4;
00872 
00873         memset(temp,'\0',3);
00874         int_to_ascii_convertor(total_num_pmu,temp);
00875         byte_by_byte_copy(cfgframe,temp,z,2); // No of PMU
00876         z += 2;
00877 
00878         int i,j; 
00879         temp_cfg = cfgfirst;
00880         while(temp_cfg != NULL) { // 1
00881 
00882                 num_pmu = to_intconvertor(temp_cfg->num_pmu);   
00883                 j = 0;
00884 
00885                 while (j < num_pmu) { //2
00886 
00887                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->stn,z,16); // STN
00888                         z += 16;
00889 
00890                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->idcode,z,2); // IDCODE
00891                         z += 2;
00892 
00893                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->data_format,z,2); // FORMAT
00894                         z += 2;
00895 
00896                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->phnmr,z,2); // PHNMR
00897                         z += 2;
00898 
00899                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->annmr,z,2); // ANNMR
00900                         z += 2;
00901 
00902                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->dgnmr,z,2); // DGNMR
00903                         z += 2; 
00904 
00905                         phnmr = to_intconvertor(temp_cfg->pmu[j]->phnmr);
00906                         annmr = to_intconvertor(temp_cfg->pmu[j]->annmr);
00907                         dgnmr = to_intconvertor(temp_cfg->pmu[j]->dgnmr);
00908 
00909                         // Copy Phasor Names
00910                         if(phnmr != 0){
00911                         
00912                                 for(i = 0; i<phnmr;i++) {
00913 
00914                                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->cnext->phnames[i],z,16); // Phasor Names
00915                                         z += 16;                                                
00916                                 }
00917                         }
00918 
00919                         // Copy Analog Names
00920                         if(annmr != 0){
00921                         
00922                                 for(i = 0; i<annmr;i++) {
00923                 
00924                                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->cnext->angnames[i],z,16); // Analog Names
00925                                         z += 16;                                                                                        
00926                                 }
00927                         }
00928                 
00929                         // Copy Digital Names
00930                         if(dgnmr != 0) {
00931                                 
00932                                 struct dgnames *temp_dgname = temp_cfg->pmu[j]->cnext->first;
00933                                 while (temp_dgname != NULL) {
00934         
00935                                         for(i = 0;i<16;i++) {
00936 
00937                                                 byte_by_byte_copy(cfgframe,temp_dgname->dgn[i],z,16); // Digital Names
00938                                                 z += 16;                                
00939                                 
00940                                         } // Copy 16 channel names of digital word      
00941 
00942                                         temp_dgname = temp_dgname->dg_next;
00943                                 } // Go to next Digital word
00944                         
00945                         }
00946 
00947                         // PHUNIT
00948                         if(phnmr != 0){
00949 
00950                                 for (i = 0; i<phnmr;i++) {
00951 
00952                                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->phunit[i],z,4); // PHUNIT
00953                                         z += 4;                                                                         
00954                                 }
00955                         }       
00956 
00957                         // ANUNIT
00958                         if(annmr != 0){
00959 
00960                                 for (i = 0; i<annmr;i++) {
00961 
00962                                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->anunit[i],z,4); // ANUNIT
00963                                         z += 4;                                                                         
00964                                         
00965                                 }
00966                         }       
00967 
00968                         // DGUNIT
00969                         if(dgnmr != 0){
00970 
00971                                 for (i = 0; i<dgnmr;i++) {
00972                                         
00973                                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->dgunit[i],z,4); // DGUNIT
00974                                         z += 4;                                                                         
00975                                 }       
00976 
00977                         }               
00978         
00979                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->fnom,z,2); // FNOM
00980                         z += 2;                                                                         
00981 
00982                         byte_by_byte_copy(cfgframe,temp_cfg->pmu[j]->cfg_cnt,z,2); // CFGCNT
00983                         z += 2;                                                                         
00984                         
00985                         j++; // index for pmu_num
00986 
00987                 } // while 2
00988 
00989                 temp_cfg = temp_cfg->cfgnext;   // Take next CFG        
00990 
00991         } // while 1
00992         
00993 
00994         byte_by_byte_copy(cfgframe,datarate,z,2); // DATA RATE
00995         z += 2;                                                                         
00996 
00997         chk = compute_CRC(cfgframe,z);
00998         cfgframe[z++] = (chk >> 8) & ~(~0<<8);  /* CHKSUM high byte; */
00999         cfgframe[z++] = (chk ) & ~(~0<<8);      /* CHKSUM low byte;  */
01000         return z;       
01001 } 

Here is the call graph for this function:

Here is the caller graph for this function:

int create_dataframe ( int  index  ) 

Definition at line 611 of file align_sort.c.

References data_for_each_pmu::analog, format::analog, data_for_each_pmu::annmr, byte_by_byte_copy(), compute_CRC(), dataframe, DATASYNC, data_for_each_pmu::dfreq, data_for_each_pmu::dgnmr, data_for_each_pmu::digital, data_frame::dnext, data_frame::dpmu, TimeStampBuffer::first_data_frame, data_for_each_pmu::fmt, fp_log, data_frame::fracsec, data_frame::framesize, data_for_each_pmu::freq, format::freq, i, data_frame::idcode, int_to_ascii_convertor(), data_frame::num_pmu, PDC_IDCODE, format::phasor, data_for_each_pmu::phasors, data_for_each_pmu::phnmr, data_frame::soc, data_for_each_pmu::stat, to_intconvertor(), and TSB.

Referenced by dispatch().

00611                                 {
00612 
00613         fprintf(fp_log,"Inside create_dataframe()\n");
00614         int total_frame_size = 0;
00615         unsigned char temp[3];
00616         struct data_frame *temp_df;
00617         unsigned int fsize;
00618         uint16_t chk;
00619 
00620         temp_df = TSB[index].first_data_frame;
00621 
00622         while(temp_df != NULL) {
00623                 
00624                  fsize = to_intconvertor(temp_df->idcode);
00625                  fsize = to_intconvertor(temp_df->framesize);
00626                  total_frame_size = total_frame_size + fsize;
00627                  total_frame_size -= 16; // skip SYNC + FRAMESIZE + idcode + soc + fracsec + checksum
00628                  temp_df = temp_df->dnext;
00629         }               
00630 
00631         total_frame_size = total_frame_size + 18/* SYNC + FRAMESIZE + idcode + soc + fracsec + checksum + outer stat */; 
00632 
00633         dataframe = malloc((total_frame_size + 1)*sizeof(char)); // Allocate memory for data frame
00634         if(!dataframe) {
00635                 
00636                 fprintf(fp_log,"No enough memory for dataframe\n");
00637         }       
00638         dataframe[total_frame_size] = '\0';     
00639 
00640         // Start the data frame creation 
00641         int z = 0;
00642         byte_by_byte_copy(dataframe,DATASYNC,z,2); // SYNC
00643         z += 2;
00644 
00645         memset(temp,'\0',3);
00646         int_to_ascii_convertor(total_frame_size,temp);
00647         byte_by_byte_copy(dataframe,temp,z,2); // FRAME SIZE
00648         z += 2;
00649 
00650         memset(temp,'\0',3);
00651         int_to_ascii_convertor(PDC_IDCODE,temp);
00652         byte_by_byte_copy(dataframe,temp,z,2); // PDC ID
00653         z += 2;
00654 
00655         byte_by_byte_copy(dataframe,(unsigned char *)TSB[index].soc,z,4); //SOC
00656         z += 4;
00657         byte_by_byte_copy(dataframe,(unsigned char *)TSB[index].fracsec,z,4); //FRACSEC
00658         z += 4;
00659 
00660         unsigned char stat[2]; //Outer Stat
00661         stat[0] = 0x00;
00662         stat[1] = 0x00;
00663         byte_by_byte_copy(dataframe,stat,z,2); //outer stat
00664         z += 2;
00665 
00666         temp_df = TSB[index].first_data_frame;
00667         while(temp_df != NULL) { // 1
00668 
00669                 int j = 0;
00670                 while(j < temp_df->num_pmu) { // 2
00671 
00672                         if(temp_df->dpmu[j]->stat[1] == 0x0f) {
00673                                 // Copy STAT
00674 
00675                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->stat,z,2); // STAT
00676                                 z += 2;
00677                                 j++;
00678                                 continue;
00679                         }
00680 
00681                         //Copy STAT
00682                         byte_by_byte_copy(dataframe,temp_df->dpmu[j]->stat,z,2); // STAT
00683 
00684                         z += 2;
00685 
00686                         int i = 0;
00687                         //Copy Phasors
00688                         if(temp_df->dpmu[j]->phnmr != 0) {
00689                                 if(temp_df->dpmu[j]->fmt->phasor == '1') {
00690                                         while(i < temp_df->dpmu[j]->phnmr) {
00691         
00692                                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->phasors[i],z,8); // Phasors
00693                                                 z += 8;
00694                                                 i++;
00695                                         }
00696                                 } else {
00697                                         while(i < temp_df->dpmu[j]->phnmr) {
00698                                 
00699                                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->phasors[i],z,4); // Phasors
00700                                                 z += 4;
00701                                                 i++;
00702                                         }
00703                                 }                       
00704 
00705                         }
00706                         //Copy FREQ
00707                         if(temp_df->dpmu[j]->fmt->freq == '1') {
00708 
00709                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->freq,z,4); // FREQ
00710                                 z += 4;
00711                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->dfreq,z,4); // FREQ
00712                                 z += 4;
00713 
00714                          } else {
00715                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->freq,z,2); // FREQ
00716                                 z += 2;
00717                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->dfreq,z,2); // FREQ
00718                                 z += 2;
00719 
00720                         }                                               
00721         
00722                         // Copy Analogs
00723                         if(temp_df->dpmu[j]->annmr != 0) {
00724                                 if(temp_df->dpmu[j]->fmt->analog == '1') {
00725                 
00726                                         for(i = 0; i<temp_df->dpmu[j]->annmr; i++){                                     
00727                 
00728                                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->analog[i],z,4); // ANALOGS
00729                                                 z += 4;
00730                                          }
00731                                 } else {
00732         
00733                                         for(i = 0; i<temp_df->dpmu[j]->annmr; i++){                                     
00734                 
00735                                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->analog[i],z,2); // ANALOGS
00736                                                 z += 2;
00737                                          }
00738                                 }                                               
00739                         }
00740                         //Copy DIGITAL
00741                         i = 0;
00742                         if(temp_df->dpmu[j]->dgnmr != 0) {
00743                                 while(i < temp_df->dpmu[j]->dgnmr) {
00744         
00745                                                 byte_by_byte_copy(dataframe,temp_df->dpmu[j]->digital[i],z,2); // DIGITAL
00746                                                 z += 2;
00747                                                 i++;
00748                                 }                       
00749                         }       
00750                         j++;                    
00751                 } // 2 while
00752                 
00753                 temp_df = temp_df->dnext;       
00754         } // 1 while
00755         // Attach a checksum
00756 
00757         chk = compute_CRC(dataframe,z);
00758         dataframe[z++] = (chk >> 8) & ~(~0<<8);  /* CHKSUM high byte; */
00759         dataframe[z++] = (chk ) & ~(~0<<8);     /* CHKSUM low byte;  */
00760 
00761         return z;       
00762 } 

Here is the call graph for this function:

Here is the caller graph for this function:

void dispatch ( int  index  ) 

Definition at line 301 of file align_sort.c.

References clear_TSB(), Upper_Layer_Details::config_change, create_dataframe(), dataframe, fp_log, mutex_Upper_Layer_Details, Upper_Layer_Details::next, Upper_Layer_Details::pdc_addr, Upper_Layer_Details::port, Upper_Layer_Details::sockfd, sort_data_inside_TSB(), TCPPORT, Upper_Layer_Details::tcpup, Upper_Layer_Details::thread_id, UDPPORT, Upper_Layer_Details::UL_data_transmission_off, Upper_Layer_Details::UL_upper_pdc_cfgsent, and ULfirst.

Referenced by time_align().

00301                          {
00302 
00303         int size,flag = 0;
00304         fprintf(fp_log,"Inside dispatch()\n");
00305         sort_data_inside_TSB(index);
00306         dataframe = NULL;        
00307         pthread_mutex_lock(&mutex_Upper_Layer_Details);
00308         struct Upper_Layer_Details *temp_pdc = ULfirst;
00309 
00310         while(temp_pdc != NULL ) {
00311 
00312                 if((temp_pdc->UL_upper_pdc_cfgsent == 1) && (temp_pdc->UL_data_transmission_off == 0)) {                        
00313 
00314                         if(flag == 0) {
00315 
00316                                 size = create_dataframe(index); 
00317                                 flag = 1;
00318                         }
00319 
00320                         if(temp_pdc->config_change == 1) {
00321                 
00322                                 dataframe[14] = 0x04;
00323                                 dataframe[15] = 0x00;
00324                                 
00325                         } else {
00326 
00327                                 dataframe[14] = 0x00;
00328                                 dataframe[15] = 0x00;
00329 
00330                         }
00331 
00332                         if(temp_pdc->port == UDPPORT) {
00333                                 
00334                                 if (sendto(temp_pdc->sockfd,dataframe, size, 0,
00335                                                 (struct sockaddr *)&temp_pdc->pdc_addr,sizeof(temp_pdc->pdc_addr)) == -1)
00336                                             perror("sendto");   
00337                                 else        fprintf(fp_log,"\nDispatched toUPDC ");                                     
00338 
00339         
00340                         } else if((temp_pdc->port == TCPPORT) && (temp_pdc->tcpup == 1)) {
00341 
00342                                  if(send(temp_pdc->sockfd,dataframe,size, 0)== -1) {
00343                                                 perror("send"); 
00344                                                 printf("TCP connection closed\n");      
00345                                                 temp_pdc->tcpup = 0;
00346                                                 pthread_cancel(temp_pdc->thread_id);            
00347                                 }                       
00348                         }               
00349                 }
00350                 temp_pdc = temp_pdc->next;
00351         }
00352         pthread_mutex_unlock(&mutex_Upper_Layer_Details);
00353         if(dataframe != NULL)   free(dataframe);
00354 
00355         clear_TSB(index);
00356 }

Here is the call graph for this function:

Here is the caller graph for this function:

void sort_data_inside_TSB ( int  index  ) 

Definition at line 364 of file align_sort.c.

References copy_cbyc(), data_frame::dnext, data_frame::dpmu, TimeStampBuffer::first_data_frame, fp_log, TimeStampBuffer::fracsec, data_frame::fracsec, data_frame::framesize, i, pmupdc_id_list::idcode, data_frame::idcode, TimeStampBuffer::idlist, int_to_ascii_convertor(), ncmp_cbyc(), pmupdc_id_list::nextid, data_frame::num_pmu, pmupdc_id_list::num_pmu, TimeStampBuffer::soc, data_frame::soc, data_for_each_pmu::stat, to_intconvertor(), and TSB.

Referenced by dispatch().

00364                                      { 
00365 
00366         fprintf(fp_log,"sort_data_inside_TSB()\n");
00367         struct pmupdc_id_list *temp_list;
00368         struct data_frame *prev_df,*curr_df,*sorted_df,*r_df,*s_df,*last_df,*p_df;
00369         int match = 0;
00370         unsigned int id_check;
00371  
00372         /* Pointer track_df will hold the address of the last sorted data_frame object. 
00373         Thus we assign to the 'track_df->dnext ' the next sorted data_frame  object and so on */
00374 
00375         temp_list = TSB[index].idlist; /* Starting ID required for sorting */
00376         last_df = TSB[index].first_data_frame;
00377         p_df = TSB[index].first_data_frame;
00378 
00379         curr_df = last_df;
00380         sorted_df = prev_df = NULL;
00381 
00382         while(temp_list != NULL) { // 1 while
00383 
00384                                 match = 0;
00385                                 while(curr_df != NULL) { // 2. Traverse the pmu id in TSB and sort 
00386                 
00387                                         if(!ncmp_cbyc(curr_df->idcode,(unsigned char *)temp_list->idcode,2)){
00388 
00389                                                 match = 1;
00390                                                 break;
00391 
00392                                         } else {
00393 
00394                                                 prev_df = curr_df; 
00395                                                 curr_df = curr_df->dnext;                                               
00396 
00397                                         }
00398                                 
00399                                 } // 2 while ends       
00400                                 
00401                                 if (match == 1) {
00402 
00403                                         if(prev_df == NULL) {
00404 
00405                                                 r_df = curr_df;
00406                                                 s_df = curr_df->dnext;
00407                                                 if(sorted_df == NULL) {
00408 
00409                                                         sorted_df = r_df;
00410                                                         TSB[index].first_data_frame = sorted_df;
00411                                                 } else {
00412 
00413                                                         sorted_df->dnext = r_df;
00414                                                         sorted_df = r_df;
00415                                                 }
00416                                                 sorted_df->dnext = s_df ;
00417                                                 curr_df = last_df = s_df;
00418 
00419                                         } else {
00420 
00421                                                 if(sorted_df == NULL) {
00422 
00423                                                                 r_df = curr_df;
00424                                                                 s_df = r_df->dnext;
00425                                                                 prev_df->dnext = s_df;
00426                                                                 sorted_df = r_df;
00427                                                                 TSB[index].first_data_frame = sorted_df;
00428                                                                 sorted_df->dnext = last_df ;
00429                                                                 curr_df = last_df;
00430                                                                 prev_df = NULL;
00431 
00432                                                 } else {//if(sorted_df != NULL) {
00433 
00434                                                                 r_df = curr_df;
00435                                                                 s_df = r_df->dnext;
00436                                                                 prev_df->dnext = s_df;
00437                                                                 sorted_df->dnext = r_df;
00438                                                                 sorted_df = r_df;
00439                                                                 sorted_df->dnext = last_df ;
00440                                                                 curr_df = last_df;
00441                                                                 prev_df = NULL;
00442                                                 }                                       
00443                                         }                                
00444                                         
00445                                 }  else {  // id whose data frame didnot arrive No match
00446 
00447                                         char *idcode;
00448                                         idcode = malloc(3);
00449 
00450                                         struct data_frame *df = malloc(sizeof(struct data_frame));
00451                                         if(!df) {
00452 
00453                                             fprintf(fp_log,"Not enough memory df\n");
00454                                         }
00455                                         df->dnext = NULL;
00456 
00457 
00458                                         // Allocate memory for df->framesize
00459                                         df->framesize = malloc(3);
00460                                         if(!df->framesize) {
00461 
00462                                              fprintf(fp_log,"Not enough memory df->idcode\n");
00463                                              exit(1);
00464                                         }
00465 
00466                                         // Allocate memory for df->idcode
00467                                         df->idcode = malloc(3);
00468                                         if(!df->idcode) {
00469 
00470                                              fprintf(fp_log,"Not enough memory df->idcode\n");
00471                                              exit(1);
00472                                         }
00473 
00474                                         // Allocate memory for df->soc
00475                                         df->soc = malloc(5);
00476                                         if(!df->soc) {
00477 
00478                                              fprintf(fp_log,"Not enough memory df->soc\n");
00479                                              exit(1);
00480                                         }
00481 
00482                                         // Allocate memory for df->fracsec
00483                                         df->fracsec = malloc(5);
00484                                         if(!df->fracsec) {
00485                                             fprintf(fp_log,"Not enough memory df->fracsec\n");
00486                                             exit(1);
00487                                         }
00488 
00489                                         unsigned int size = (16/* sync,fsize,idcode,soc,fracsec,checksum */ + (temp_list->num_pmu)*2)*sizeof(unsigned char);
00490                                         
00491                                         df->num_pmu = temp_list->num_pmu ;
00492                                         //Copy FRAMESIZE
00493                                         int_to_ascii_convertor(size,df->framesize);
00494                                         df->framesize[2] = '\0';
00495                                                                                 
00496                                         //Copy IDCODE                   
00497                                         copy_cbyc (df->idcode,(unsigned char *)temp_list->idcode,2);
00498                                         df->idcode[2] = '\0';
00499 
00500                                         //Copy SOC                                              
00501                                         copy_cbyc (df->soc,(unsigned char *)TSB[index].soc,4);
00502                                         df->soc[4] = '\0';
00503 
00504                                         //Copy FRACSEC                                          
00505                                         copy_cbyc (df->fracsec,(unsigned char *)TSB[index].fracsec,4);
00506                                         df->fracsec[4] = '\0';
00507 
00508                                         df->dpmu = malloc(temp_list->num_pmu * sizeof(struct data_for_each_pmu *));
00509                                         if(!df->dpmu) {
00510                                              fprintf(fp_log,"Not enough memory df->dpmu[][]\n");
00511                                              exit(1);
00512                                         }
00513 
00514                                         for (i = 0; i < temp_list->num_pmu; i++) {
00515                                               df->dpmu[i] = malloc(sizeof(struct data_for_each_pmu));
00516                                         }
00517                                         
00518                                         int j = 0;              
00519                                         // PMU data has not come  
00520                                         while(j < temp_list->num_pmu) {
00521 
00522                                                 df->dpmu[j]->stat = malloc(3);
00523                                                 if(!df->dpmu[j]->stat) {
00524                                                         fprintf(fp_log,"Not enough memory for df->dpmu[j]->stat\n");
00525                                                 }
00526                                                 
00527                                                 df->dpmu[j]->stat[0] = 0x00;
00528                                                 df->dpmu[j]->stat[1] = 0x0F;
00529                                                 df->dpmu[j]->stat[2] = '\0';
00530                                                 j++;
00531                                         }
00532 
00533                                         if(sorted_df == NULL) {
00534 
00535                                                                 r_df = df;
00536                                                                 sorted_df = r_df;
00537                                                                 TSB[index].first_data_frame = sorted_df;
00538                                                                 sorted_df->dnext = last_df ;
00539                                                                 curr_df = last_df;
00540                                                                 prev_df = NULL;
00541                                         } else {
00542                                                                 r_df = df;
00543                                                                 sorted_df->dnext = r_df;
00544                                                                 sorted_df = r_df;
00545                                                                 sorted_df->dnext = last_df ;
00546                                                                 curr_df = last_df;
00547                                                                 prev_df = NULL;
00548                                         }                               
00549                                 }
00550 
00551                         temp_list = temp_list->nextid;  //go for next ID
00552 
00553         } // 1. while ends
00554 
00555         p_df = TSB[index].first_data_frame;
00556         while(p_df != NULL){
00557 
00558                 id_check = to_intconvertor(p_df->idcode);
00559                 p_df = p_df->dnext;
00560         }
00561 } 

Here is the call graph for this function:

Here is the caller graph for this function:

void time_align ( struct data_frame df  ) 

Definition at line 69 of file align_sort.c.

References assign_df_to_TSB(), dispatch(), fp_log, data_frame::fracsec, front, i, MAXTSB, mutex_on_TSB, ncmp_cbyc(), rear, data_frame::soc, to_long_int_convertor(), and TSB.

Referenced by dataparser().

00069                                        {
00070 
00071         int flag = 0;
00072         fprintf(fp_log,"Inside time_align()\n");
00073         pthread_mutex_lock(&mutex_on_TSB);
00074         if(front == -1) { // TSB is used for the first time
00075         
00076                 front = rear = 0;
00077                 assign_df_to_TSB(df,front);     
00078                 pthread_mutex_unlock(&mutex_on_TSB);
00079                 return; 
00080         
00081         } else {
00082 
00083                 unsigned long int df_soc,df_fracsec,tsb_soc,tsb_fracsec;
00084 
00085                 df_soc = to_long_int_convertor(df->soc);
00086                 df_fracsec = to_long_int_convertor(df->fracsec);
00087                 tsb_soc = to_long_int_convertor((unsigned char *)TSB[front].soc);
00088                 tsb_fracsec = to_long_int_convertor((unsigned char *)TSB[front].fracsec);
00089 
00090                 if((df_soc < tsb_soc) || ((df_soc == tsb_soc) && (df_fracsec < tsb_fracsec))) {
00091                         
00092                         fprintf(fp_log,"df_soc %ld tsb_soc %ld df_fracsec %ld tsb_fracsec %ld\n",df_soc,tsb_soc,df_fracsec,tsb_fracsec);
00093                         fprintf(fp_log,"The Data frame is too old\n");
00094                         pthread_mutex_unlock(&mutex_on_TSB);
00095                         return; 
00096                 }
00097 
00098                 if(rear >= front) {
00099                         for(i = rear; i>= front; i--) {
00100                                         
00101                                 if(!ncmp_cbyc ((unsigned char *)TSB[i].soc,df->soc,4)) {
00102                                         
00103                                         if(!ncmp_cbyc ((unsigned char *)TSB[i].fracsec,df->fracsec,3)) { /* 3 bytes is actual fraction of seconds and 1 
00104                                                                                          byte is Time quality  */ 
00105                                                 flag = 1;
00106                                                 break;
00107                 
00108                                         } 
00109         
00110                                 } else {
00111                                                 
00112                                         continue;
00113                                         
00114                                 } //if for soc ends
00115                         }// for ends                                    
00116                 } else {
00117 
00118                         for(i = rear; i< front; i++) {
00119                                         
00120                                 if(!ncmp_cbyc ((unsigned char *)TSB[i].soc,df->soc,4)) {
00121                                         
00122                                         if(!ncmp_cbyc ((unsigned char *)TSB[i].fracsec,df->fracsec,3)) { /* 3 bytes is actual fraction of seconds and 1 
00123                                                                                          byte is Time quality  */ 
00124                                                 flag = 1;
00125                                                 break;
00126                                         } 
00127         
00128                                 } else {
00129                                                 
00130                                         continue;
00131                                         
00132                                 } //if for soc ends
00133                         }// for ends                                    
00134                 }
00135                  
00136         } // if other than the front = -1
00137 
00138         if(flag) {
00139 
00140                 fprintf(fp_log,"Matched with TSB[%d]\n",i);
00141                 //Assign to existing TSB
00142                 assign_df_to_TSB(df,i);                         
00143 
00144         } else {
00145 
00146                 rear = (rear + 1) % MAXTSB;
00147 
00148                 if(front == rear) { // All TSB are full. Dispatch the TSB[rear] and use it to assign new df
00149                 
00150                         dispatch(rear); 
00151                         front = (front + 1) % MAXTSB;
00152                         assign_df_to_TSB(df,rear);
00153                                 
00154                 } else {
00155 
00156                         assign_df_to_TSB(df,rear);                                      
00157 
00158                 }
00159         }
00160         pthread_mutex_unlock(&mutex_on_TSB);
00161 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int front

Definition at line 36 of file align_sort.h.

int rear

Definition at line 36 of file align_sort.h.

struct TimeStampBuffer TSB[MAXTSB]
Generated on Tue Jun 7 13:35:33 2011 for iPDC-(PhasorDataConcentrator) by  doxygen 1.6.3