grand final code bro include all the headers in the same order as it is used in main and remove mai and just call snd_tm.head_point( TM_list *);

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers snd_tm_class.h Source File

snd_tm_class.h

00001 void adf_callme();
00002 
00003 #define S_FRAME_SIZE 48
00004 #define EoS_SIZE 120
00005 #define SEGMENT_SIZE 30
00006 #define TM_FRAME_SIZE 134
00007 
00008 class SND_TM{
00009     
00010     int diff_prev;
00011     int type0_no;                                               //number of type zero frames
00012     int type1_no;                                           
00013     int total_frames;                                           //number of type one packe (TMP)
00014     int segment_no;                                             //number of type one packe (TMP)
00015     unsigned char S_frame[S_FRAME_SIZE];
00016     unsigned char EoS[EoS_SIZE];
00017     TM_list * list_ptr;   
00018     unsigned char SCH40[5];                                     
00019     unsigned char FCCH80[10];
00020     bool reset_flag;                                            //for reseting all the static variables when new list is given.
00021     
00022     void differential_encode(unsigned char* ptr, int length){
00023     
00024         for(int i=0; i<length;i++){
00025         
00026             unsigned char s = ptr[i] , t;
00027             t = s ^ (s >> 1);
00028             (diff_prev == 0) ? t=t^0x00 : t=t^0x80 ;
00029             diff_prev = int(s & 0x01);
00030             ptr[i] = t;
00031         }
00032         
00033     }    
00034     
00035     int decide_type(unsigned char tmid){
00036         int type = 0;
00037     
00038         switch(tmid){
00039             case 0x1:
00040             case 0x2:
00041             case 0x3:
00042             case 0x4:
00043             case 0x5:
00044             case 0x6:
00045             case 0x7:
00046             case 0x8:
00047                 type = 0;
00048                 break;
00049             case 0xA:
00050             case 0xB:
00051             case 0xC:
00052             case 0xD:
00053             case 0xE:
00054                 type = 1;
00055                 break;
00056         }
00057     
00058         return type;
00059     }
00060     
00061     void data_number(){
00062 
00063         int type1_frame_no;
00064         TM_list *head = list_ptr;
00065         
00066         type0_no = 0;
00067         type1_no = 0;
00068         
00069         do{
00070             
00071             switch( decide_type(head->tmid) ){
00072                 case 0:
00073                         type0_no++;
00074                         break;
00075                 case 1:
00076                         type1_no++;
00077                     
00078             }
00079             
00080             head=head->next_TM;
00081             
00082         }
00083         while(head->next_TM != NULL);
00084         
00085         type1_frame_no = ( type1_no % 10 == 0 )? (type1_no / 10) : ( (type1_no / 10) + 1);
00086         total_frames = type0_no + type1_frame_no ;
00087         segment_no = (total_frames*2 % (SEGMENT_SIZE-1) == 0) ? ( total_frames*2/(SEGMENT_SIZE-1) ) : (total_frames*2/(SEGMENT_SIZE-1) + 1);            //subtracting 1 since the last Tframe is not detected
00088 
00089     }
00090     
00091     
00092     TM_list* next_type_structure(TM_list *ptr){
00093         
00094         if(ptr == NULL){
00095             return NULL;
00096         }
00097                         
00098         int temp = decide_type(ptr->tmid);
00099         
00100         if((temp == 0) && (ptr->next_TM != NULL) ){
00101             
00102             do{
00103                 ptr = ptr->next_TM;
00104                 temp =  decide_type(ptr->tmid);
00105             }   
00106             while(temp == 1 && ptr->next_TM != NULL);   
00107             
00108             if(temp == 1){
00109             return NULL;
00110             }           
00111                 
00112         }
00113         
00114         else if((temp == 1) && (ptr->next_TM != NULL)){
00115             
00116             do{
00117                 ptr = ptr->next_TM;
00118                 temp = decide_type(ptr->tmid);
00119             }   
00120             while(temp == 0 && ptr->next_TM != NULL);
00121             
00122             if(temp == 0){
00123             return NULL;
00124             }
00125                 
00126                     
00127         }
00128         
00129         else{
00130                 
00131 //          cout<<"\nNULL returned by next_type_structure\n";
00132             return NULL;
00133             
00134         }
00135         return ptr;
00136     }
00137     
00138     /*
00139         brief:      take input type 0  or  1 and return the address of the first node in the list that have that type
00140         parameter:  type 0 or 1
00141         return:     pointer of the first node having same type      
00142     */
00143     TM_list* first_type_structure(int type){
00144         TM_list* temp_ptr = list_ptr;
00145     
00146         if(type == 0){
00147             while((decide_type(temp_ptr->tmid) == 1)){
00148                 temp_ptr = temp_ptr->next_TM;
00149             
00150                 if(temp_ptr == NULL){
00151                     return NULL ;
00152                 }       
00153             }   
00154         }
00155         
00156         else if(type == 1){
00157             while((decide_type(temp_ptr->tmid) == 0) ){
00158                 temp_ptr = temp_ptr->next_TM;
00159                 
00160                 if(temp_ptr == NULL){
00161                     return NULL;    
00162                 }   
00163             }   
00164         }   
00165         return temp_ptr;
00166     }
00167     
00168     unsigned char TMframe_type1[TM_FRAME_SIZE];
00169     bool type1_frame_flag;                              //should be true for every new list
00170     
00171      unsigned char * type1_frame(){
00172         
00173         int i;
00174         static TM_list *pointer ;
00175         static int counter  = 0;
00176         
00177         if(type1_frame_flag){
00178             pointer = first_type_structure(1);
00179             counter = 0;
00180             type1_frame_flag = false;
00181         }
00182         
00183         
00184 
00185         for(i = 4 ; (i < 134) && (pointer != NULL) ; i++){
00186              TMframe_type1[i] = pointer->TM_string[counter];
00187              
00188               if(counter++ == 12){
00189                 counter = 0;
00190                 pointer = next_type_structure(pointer);
00191               }
00192         }
00193         
00194         // header
00195         TMframe_type1[0] = 1<<7 + ( (i-4)/10 )<<3;                          //( (i-4)/10 ) gives number of packets in the frame
00196             //insert time ;
00197         TMframe_type1[3] = CRC::crc16_gen(TMframe_type1,3);
00198         //end header
00199         
00200         if(pointer == NULL){
00201             for( ; i < 134 ; i++){                                          //repeating ;ast packet to fill up the extra space
00202                 TMframe_type1[i] = TMframe_type1[i-13]; 
00203             }
00204         
00205         }
00206         return TMframe_type1;
00207     
00208     }
00209     
00210     bool type0_frame_flag;
00211     
00212     unsigned char* type0_frame(){
00213         static TM_list *pointer ; 
00214         
00215         if(type0_frame_flag){
00216             pointer = first_type_structure(0);  
00217             type0_frame_flag = false;
00218         }            
00219         
00220         else {
00221             pointer = next_type_structure(pointer);
00222             
00223         }
00224         return pointer->TM_string;
00225     }
00226 
00227 
00228     
00229 void interleave( unsigned char *input, unsigned char *output ){
00230     
00231     unsigned int outState = 0;
00232     unsigned int outByte = 0;
00233     
00234     for( unsigned int i = 0 ; i < 36 ; ++i ){
00235         for(unsigned int j = 0 ; j < 30 ; ++j){
00236             unsigned int x = j*36+i;
00237             unsigned char tempBit = ((input[x >> 3]) >> (7-(x % 8))) & 1;
00238             switch(outState){
00239                 case 0:
00240                     outState = 1;
00241                     output[outByte] = tempBit << 7;
00242                     break;
00243                 case 1:
00244                     outState = 2;
00245                     output[outByte] += tempBit << 6;
00246                     break;
00247                 case 2:
00248                     outState = 3;
00249                     output[outByte] += tempBit << 5;
00250                     break;
00251                 case 3:
00252                     outState = 4;
00253                     output[outByte] += tempBit << 4;
00254                     break;
00255                 case 4:
00256                     outState = 5;
00257                     output[outByte] += tempBit << 3;
00258                     break;
00259                 case 5:
00260                     outState = 6;
00261                     output[outByte] += tempBit << 2;
00262                     break;
00263                 case 6:
00264                     outState = 7;
00265                     output[outByte] += tempBit << 1;
00266                     break;
00267                 case 7:
00268                     outState = 0;
00269                     output[outByte] += tempBit;
00270                     ++outByte;
00271                     break;
00272             }
00273         }
00274         for(unsigned int j = 0 ; j < 2 ; ++j){
00275             switch(outState){
00276                 case 0:
00277                     output[outByte] = 0;
00278                     outState = 1;
00279                     break;
00280                 case 1:
00281                     outState = 2;
00282                     break;
00283                 case 2:
00284                     outState = 3;
00285                     break;
00286                 case 3:
00287                     outState = 4;
00288                     break;
00289                 case 4:
00290                     outState = 5;
00291                     break;
00292                 case 5:
00293                     outState = 6;
00294                     break;
00295                 case 6:
00296                     outState = 7;
00297                     break;
00298                 case 7:
00299                     outState = 0;
00300                     ++outByte;
00301                     break;
00302             }
00303         }
00304     }
00305 }
00306 
00307     
00308     
00309     public:
00310         
00311         bool give_buffer ;
00312         bool last_buffer ;
00313         bool transmission_done;
00314         int dummy_counter;
00315         
00316         SND_TM(){
00317             dummy_counter = 0;
00318 //          list_ptr = x;
00319             
00320             diff_prev = 0;
00321             
00322             type0_no= 0;
00323             type1_no= 0;            
00324             //~ S-FRAME
00325             //~ SCH_64 * 2
00326             S_frame[0] = 0x46;
00327             S_frame[1] = 0x9d;
00328             S_frame[2] = 0xfb;
00329             S_frame[3] = 0xf0;
00330             S_frame[4] = 0xd2;
00331             S_frame[5] = 0xba;
00332             S_frame[6] = 0x89;
00333             S_frame[7] = 0xe4;
00334             
00335             S_frame[8] = 0x46;
00336             S_frame[9] = 0x9d;
00337             S_frame[10] = 0xfb;
00338             S_frame[11] = 0xf0;
00339             S_frame[12] = 0xd2;
00340             S_frame[13] = 0xba;
00341             S_frame[14] = 0x89;
00342             S_frame[15] = 0xe4;
00343     
00344             //~ FCCH
00345             for(int i = 16 ; i < 48 ; ++i ){
00346                 S_frame[i] = 0x00;
00347             }
00348     
00349             
00350             for(int i = 0 ; i < 24 ; ++i){
00351                  EoS[i] = 0x00;
00352             }
00353     
00354             //~ S-FRAME [1]
00355             //~ first two bits of S-FRAME
00356             EoS[23] = 0x01;
00357             
00358             EoS[24] = 0x1a;
00359             EoS[25] = 0x77;
00360             EoS[26] = 0xef;
00361             EoS[27] = 0xc3;
00362             EoS[28] = 0x4a;
00363             EoS[29] = 0xea;
00364             EoS[30] = 0x27;
00365             EoS[31] = 0x91;
00366             EoS[32] = 0x1a;
00367             EoS[33] = 0x77;
00368             EoS[34] = 0xef;
00369             EoS[35] = 0xc3;
00370             EoS[36] = 0x4a;
00371             EoS[37] = 0xea;
00372             EoS[38] = 0x27;
00373             EoS[39] = 0x90;
00374     
00375             for(int i = 40 ; i < 72 ; ++i){
00376                 EoS[i] = 0x00;
00377             }
00378             
00379             //~ S-FRAME [2]
00380             //~ first two bits of S-FRAME
00381             EoS[71] = 0x01;
00382             
00383             EoS[72] = 0x1a;
00384             EoS[73] = 0x77;
00385             EoS[74] = 0xef;
00386             EoS[75] = 0xc3;
00387             EoS[76] = 0x4a;
00388             EoS[77] = 0xea;
00389             EoS[78] = 0x27;
00390             EoS[79] = 0x91;
00391             EoS[80] = 0x1a;
00392             EoS[81] = 0x77;
00393             EoS[82] = 0xef;
00394             EoS[83] = 0xc3;
00395             EoS[84] = 0x4a;
00396             EoS[85] = 0xea;
00397             EoS[86] = 0x27;
00398             EoS[87] = 0x90;
00399             
00400             for(int i = 88 ; i < 120 ; ++i){
00401                 EoS[i] = 0x00;
00402             }
00403             
00404             for(int i = 0 ; i < 10 ; ++i){
00405                 FCCH80[i] = 0x00;
00406             }
00407             
00408             SCH40[0] = 0x0a;
00409             SCH40[1] = 0x3f;
00410             SCH40[2] = 0x46;
00411             SCH40[3] = 0xb4;
00412             SCH40[4] = 0x00;
00413             
00414 //          give_buffer = false;            
00415             last_buffer = false;
00416             transmission_done=false;
00417             
00418         }
00419         
00420         void head_pointer(TM_list * ptr){
00421             list_ptr = ptr ;
00422             type1_frame_flag = true;
00423             type0_frame_flag = true;
00424             make_DataStream_flag = true;
00425             transmit_data_flag = true;
00426             last_buffer = false;
00427             transmission_done=false;
00428             adf_callme();
00429         }
00430         
00431         unsigned char convoluted_frame[270];                    
00432                     
00433         Convolution ConvObj;
00434         
00435         void convolution (unsigned char * ptr){
00436             
00437             ConvObj.convolutionEncode(ptr, convoluted_frame);
00438             ConvObj.convolutionEncode(ptr + 67, convoluted_frame + 135);
00439         }
00440         
00441         unsigned char interleave_data[2][144];                  //initialize to zero how;
00442         
00443         bool make_DataStream_flag;
00444         
00445         int make_DataStream(){
00446             static bool state = true;
00447             static int counter = 0;
00448             static unsigned char * ptr;
00449             static bool flag = false;
00450             
00451             if(make_DataStream_flag){
00452                 data_number();
00453                 state = true;
00454                 counter = 0;
00455                 flag = false;
00456                 make_DataStream_flag = false;
00457             }
00458             
00459             if(state){
00460                     
00461                 if(counter < type0_no){
00462                     ptr = type0_frame(); 
00463                     flag = true;    
00464                 
00465                 }
00466             
00467                 else if(counter < total_frames ){
00468                     ptr = type1_frame();
00469                     flag = true;
00470                 }
00471                 
00472                 counter++;  
00473                 
00474             }
00475             
00476             state = !state;
00477             
00478             if (flag){
00479 //              cout<<"i m here";
00480                 
00481 //              printf("------inside convolution\r\n");
00482                 convolution(ptr);
00483 //              printf("-------out of convolution\n");
00484                 interleave(convoluted_frame,interleave_data[0]);
00485                 interleave(convoluted_frame+ 135,interleave_data[1]);
00486 //              printf("completed intrleave\n");
00487                 flag = false;
00488                 
00489             }
00490             
00491                     
00492             int temp =  (state == false) ? 0 : 1 ;
00493     
00494             return temp;
00495             
00496         }
00497         
00498         unsigned char transmit[112];            //112 bytes - half rolling buffer
00499         bool transmit_data_flag;            
00500             
00501         void transmit_data(){
00502             static int Tframe_counter = 0;                                  //contains the number of Tframes in a segment had been written  
00503             static int EOS_counter = 0;                                     //count no of byres of eos have been weitten in the buffer
00504             static int Sframe_Counter = 0;
00505             static int SCH40_counter = 0;
00506             static int string_space_counter = 0;
00507             static int FCCH80_counter = 0;
00508             int rolling_buffer_counter = 0;                                 //maximum it can be 1152 bits
00509             static bool Sframe_flag = true;
00510             static bool new_Tframe_flag = false;
00511             static bool FCCH80_flag = false;
00512             static bool data_flag = false;
00513             static int i;
00514             static bool repete_data_flag = false;
00515             static int segment_counter = 0;
00516             static bool diff_enc_flag = false;
00517                 
00518                 if(transmit_data_flag){
00519                     rolling_buffer_counter = 0;
00520                     Sframe_Counter = 0;
00521                     SCH40_counter = 0;
00522                     FCCH80_counter = 0;
00523                     Tframe_counter = 0;
00524                     EOS_counter = 0;
00525                     segment_counter = 0;
00526                     string_space_counter = 0;
00527                     
00528                     Sframe_flag = true;
00529                     new_Tframe_flag = false;
00530                     repete_data_flag = false;
00531                     data_flag = false;
00532                     FCCH80_flag = false;
00533                     transmit_data_flag = false;
00534                     diff_enc_flag = false;
00535                 }
00536                 
00537                 for(rolling_buffer_counter = 0 ; rolling_buffer_counter<112 ; rolling_buffer_counter++){
00538                     
00539                     if(diff_enc_flag){
00540                         transmit[rolling_buffer_counter] = 0;
00541                         continue;
00542                     }
00543                     
00544                     if(Sframe_flag){
00545                         transmit[rolling_buffer_counter] = S_frame[Sframe_Counter++];
00546                         
00547                         if(Sframe_Counter == 48){
00548 //                          printf("sframe quit\n");
00549                             Sframe_Counter = 0;
00550                             Sframe_flag = false;
00551                             new_Tframe_flag =true;
00552                         }
00553                         
00554                     }
00555                 
00556                     else if(new_Tframe_flag){
00557                         transmit[rolling_buffer_counter] = SCH40[SCH40_counter++];      
00558                         
00559                         if(SCH40_counter == 5 ){
00560                             SCH40_counter = 0;
00561                             new_Tframe_flag = false;
00562 //                          printf("new Tframe flag   quit\n");
00563 //                          data_flag = (Tframe_counter == 38)? false : true; 
00564 //                          put_F = !data_flag;
00565                             
00566                             repete_data_flag = (Tframe_counter == SEGMENT_SIZE -1)?true:false;
00567                             data_flag   =  !repete_data_flag;   
00568                             
00569                         }
00570 
00571                     }
00572                     
00573                     else if(data_flag){
00574                         
00575                         if(!string_space_counter  ){
00576                             i = make_DataStream();  
00577                         }
00578                         
00579                         transmit[rolling_buffer_counter] = interleave_data[i][string_space_counter++];
00580                         if(string_space_counter == 144){
00581 //                          printf("data_flag   quit\n");
00582                             string_space_counter = 0;
00583                             FCCH80_flag = true;
00584                             data_flag = false;
00585                         }
00586                         
00587                     }
00588                     
00589                     else if(repete_data_flag){
00590                         transmit[rolling_buffer_counter]  = interleave_data[i][string_space_counter];;
00591                         if(++string_space_counter == 144){
00592                             repete_data_flag = false;
00593                             FCCH80_flag = true;
00594                             string_space_counter = 0;
00595 //                          cout<<"repete_data_flag quit\n";
00596                         }
00597                     }
00598                     
00599                     else if(FCCH80_flag){
00600                         
00601                             transmit[rolling_buffer_counter] = FCCH80[FCCH80_counter++];
00602 //                          cout<<FCCH80_counter;
00603                             if(FCCH80_counter == 10){
00604 //                              printf("FCCH80   quit\n");
00605                                 FCCH80_counter = 0;
00606                                 FCCH80_flag = false;
00607                                 
00608                                 if(++Tframe_counter != SEGMENT_SIZE)
00609                                     new_Tframe_flag =  true;
00610                                     
00611                             }       
00612                 
00613                     }
00614                     
00615                     
00616                 
00617                     else if(Tframe_counter == SEGMENT_SIZE){    
00618                         
00619                         transmit[rolling_buffer_counter] = EoS[EOS_counter++];
00620                         
00621                         if(EOS_counter == 120){
00622                             diff_enc_flag = true; 
00623                             Tframe_counter = 0;
00624                             EOS_counter = 0;
00625                             printf("e");
00626                             if(++segment_counter == segment_no){
00627 //                              cout<<"transmit_counter = "<<++transmit_counter;
00628                                 last_buffer = true;
00629                                 break; 
00630                                 
00631                             }
00632                             else{
00633                                 Sframe_flag = true;
00634                                 continue;
00635                             }
00636                             
00637                         }
00638                         continue;
00639                     }                   
00640                                 
00641                 }
00642                 
00643             
00644                 
00645                 differential_encode(transmit,112);
00646 
00647                 if(diff_enc_flag){
00648                     diff_prev = 0;
00649                     diff_enc_flag = false;
00650                     Sframe_flag = true;
00651                     
00652                 }       
00653                 
00654                 dummy_counter++;
00655                 
00656         }
00657     
00658 }snd_tm;