werwere

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by Siva ram

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers COM_SND_TM.h Source File

COM_SND_TM.h

00001 //void adf_not_SDcard();
00002  
00003 //~ #include"SDcard.h"
00004 /*#define T_frames 30
00005 unsigned char buffer_112_counter,sent_tm = 0;
00006 int disk_read(uint8_t *, uint64_t),j;
00007 bool new_session = true;
00008 bool last_buffer = false;
00009 bool first_block = true;
00010 bool last_block =false;
00011 bool repeat_T_frame = false;
00012 unsigned int counter_in_block=0,diff_prev=0;
00013 unsigned char  EOS_counter = 0;
00014 unsigned char enable_T_frame = false;
00015 uint64_t  start_block_num ;           // Read from TC
00016 uint64_t  end_block_num ;            // Read from TC
00017 uint64_t  Number_of_blocks_to_read ;
00018 uint64_t  block_counter = 0;
00019 unsigned char buffer_512[512], buffer_112[112],counter_in_S_frame=0,counter_in_SCH=0;
00020 unsigned char make_EOS_flag = true;
00021 bool reset = false; */
00022  
00023  
00024  
00025  
00026  
00027  
00028  
00029 bool sent_tmfrom_SDcard;
00030  
00031  
00032  
00033 #define S_FRAME_SIZE 48
00034 #define ISR 40
00035 #define TM_FRAME_SIZE 134           //in bytes
00036 #define T_FRAME_SIZE 159
00037 #define EOS_SIZE 120
00038 #define MAX_ADF_LEN 65535
00039  
00040 const unsigned char S_frame[] = {0x46,0x9D,0xFB,0xF0,0xD2,0xBA,0x89,0xE4,0x46,0x9D,0xFB,0xF0,0xD2,0xBA,0x89,0xE4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00041 const unsigned char EoS[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x91,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x91,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00042 const unsigned char FCCH80[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00043 const unsigned char SCH40[] = {0x0a,0x3f,0x46,0xb4,0x00};
00044 const unsigned char EXOR_SEQ[] = {0x55,0xF1,0x51,0x3D,0xA7,0x42,0xAE,0x2E,0x2E,0x28,0x76,0x80,0x62,0x41,0xC4,0x75,0x4D,0x87,0xDB,0xE1,0x02,0x61,0x60,0x96,0x9C,0xCB,0x8A,0xBD,0x37,0xE4,0x72,0x5D,0xF2,0x19,0x62,0x06,0x9A,0xF9,0x38,0xB0,0x49,0x75,0xD0,0x17,0x1B,0xCC,0x0B,0xEB,0x1B,0x50,0xA2,0x2A,0x8E,0xFA,0x4D,0x6F,0x1B,0xF8,0x0F,0x97,0x39,0x25,0x60,0x55,0x9A,0xDF,0x1D,0x10,0x7F,0xBD,0x3E,0xBF,0xE5,0x68,0x02,0xD1,0x99,0x0D,0xDF,0x84,0x2E,0x15,0xE3,0x08,0xD7,0x44,0x10,0x41,0xCE,0x93,0xF6,0x59,0x71,0xD2,0xF7,0x1C,0x4A,0x44,0x2D,0xA9,0x44,0x98,0x3A,0x00,0x71,0xCC,0x2A,0x35,0xC1,0x81,0xCF,0x44,0xF7,0x6A,0x0E,0xE4,0xF2,0xFC,0xD6,0x0B,0xA8,0x95,0x7B,0x86,0xB5,0xF8,0x33,0xE9,0xBF,0x6A,0x0E,0xE6,0x53,0x82};
00045  
00046 #define print_tm(ptr) {\
00047     for( int i = 0; i < 134; i++ ){\
00048         gPC.putc(ptr[i]);\
00049     }\
00050 }
00051  
00052 //void send_tm_from_SD_card(){} 
00053  
00054 class SND_TM{
00055  
00056     Base_tm * head_ptr;
00057  
00058 //    transmit data variables
00059     unsigned char Tframe_c;                                      //_c represents counter, counts Tframe written in a segment
00060     unsigned char EOS_c;                                         //count no of bytes of eos have been weitten in the rolling buffer
00061     unsigned char Sframe_c;                                      //count no of bytes of Sframe have been weitten in the rolling buffer
00062     unsigned char SCH40_c;                                       //count no of bytes of SCH40 have been weitten in the rolling buffer
00063     unsigned char data_c;
00064     unsigned char FCCH80_c;
00065     unsigned char RB_c;                                          //rolling buffer counter
00066     unsigned char segment_c;                                    //counts the numver of segments sent
00067     unsigned char MAX_SEGMENT;
00068     bool Sframe;                                                //Sframe = 1bit of CF1
00069     bool SCH40_f;
00070     bool FCCH80_f;
00071     bool data_f;
00072     bool repeat_data_f;
00073     bool transmit_data_f;
00074     bool eos_f;
00075     bool junk_f;
00076     bool segment_r;                                    //flag checking if more sessions are required
00077  
00078  
00079 //  type 1 frame
00080  
00081     Base_tm *T0F_pointer ;
00082  
00083     int diff_prev;
00084     void differential_encode(unsigned char* ptr, int length){
00085         for(int i=0; i<length;i++){
00086             unsigned char s = ptr[i] , t;
00087             t = s ^ (s >> 1);
00088             (diff_prev == 0) ? t=t^0x00 : t=t^0x80 ;
00089             diff_prev = int(s & 0x01);
00090             ptr[i] = t;
00091         }
00092     }
00093  
00094     int type0_no;                                               //number of type zero frames
00095     int type1_no;
00096     int total_frames;                                           //number of type one packe (TMP)
00097     int SEGMENT_NO;                                             //number of type one packe (TMP)
00098     int TOTAL_SEGMENT_NO;
00099     int LAST_SEGMENT_SIZE;
00100     void inline data_number(){
00101         int type1_frame_no;
00102         Base_tm*head = head_ptr;
00103         type0_no = 0;
00104         type1_no = 0;
00105         while(head != NULL){
00106             switch( GETshort_or_long_tm(head) ){
00107                 case 0:
00108                     type0_no++;
00109                     break;
00110                 case 1:
00111                     type1_no++;
00112                     break;
00113             }
00114  
00115             head=head->next_TM;
00116         }
00117         type1_frame_no = ( type1_no % 10 == 0 )? (type1_no / 10) : ( (type1_no / 10) + 1);
00118         total_frames = type0_no + type1_frame_no ;
00119         TOTAL_SEGMENT_NO = (total_frames*2 % (ISR) == 0) ? ( total_frames*2/(ISR) ) : (total_frames*2/(ISR) + 1);            //subtracting 1 since the last SCH40_f is not detected
00120         SEGMENT_NO = TOTAL_SEGMENT_NO;
00121         if(SEGMENT_NO > MAX_SEGMENT)
00122         {
00123             SEGMENT_NO = MAX_SEGMENT;
00124         }
00125         if(SEGMENT_NO == 1) segment_r = 0; else segment_r = 1;
00126         TOTAL_SEGMENT_NO -= SEGMENT_NO;
00127         LAST_SEGMENT_SIZE = total_frames*2%ISR?total_frames*2%ISR:ISR;
00128     }
00129  
00130 #define next_type_structure(ptr){\
00131         unsigned char temp = GETshort_or_long_tm(ptr);\
00132         if((temp == 0) && (ptr->next_TM != NULL) ){\
00133             do{\
00134                 ptr = ptr->next_TM;\
00135                 temp =  GETshort_or_long_tm(ptr);\
00136             }\
00137             while(temp == 1 && ptr->next_TM != NULL);\
00138             if(temp == 1){\
00139                 ptr = NULL;\
00140             }\
00141         }\
00142         else if((temp == 1) && (ptr->next_TM != NULL)){\
00143             do{\
00144                 ptr = ptr->next_TM;\
00145                 temp = GETshort_or_long_tm(ptr);\
00146             }\
00147             while(temp == 0 && ptr->next_TM != NULL);\
00148             if(temp == 0){\
00149                 ptr = NULL;\
00150             }\
00151         }\
00152         else{\
00153             ptr = NULL;\
00154         }\
00155     }
00156  
00157     /*
00158         brief:      take input type 0  or  1 and return the address of the first node in the list that have that type
00159         parameter:  type 0 or 1
00160         return:     pointer of the first node having same type
00161     */
00162     #define exor(ptr) { for(int i=0 ; i<134; i++ ) ptr[i]=ptr[i]^EXOR_SEQ[i];}
00163  
00164  
00165     #define first_type_structure( type,return_ptr) {\
00166         Base_tm* temp_ptr = head_ptr;\
00167         if(type == 0){\
00168             while(GETshort_or_long_tm(temp_ptr) == 1){\
00169                 temp_ptr = temp_ptr->next_TM;\
00170                 if(temp_ptr == NULL){\
00171                     return_ptr = NULL ;\
00172                 }\
00173             }\
00174         }\
00175         else if(type == 1){\
00176             while(GETshort_or_long_tm(temp_ptr) == 0){\
00177                 temp_ptr = temp_ptr->next_TM;\
00178                 if(temp_ptr == NULL){\
00179                     return_ptr = NULL;\
00180                 }\
00181             }\
00182         }\
00183         return_ptr = temp_ptr;\
00184     }
00185  
00186  
00187  
00188     Base_tm *T1F_pointer ;
00189     int T1F_counter;
00190     unsigned char TMframe_type1[TM_FRAME_SIZE];
00191     bool type1_frame_flag;                                               //should be true for every new list
00192  
00193     #define type1_frame(ptr){\
00194         int i=4;\
00195         if(type1_frame_flag){\
00196             first_type_structure(1,T1F_pointer);\
00197             T1F_counter = 0;\
00198             type1_frame_flag = false;\
00199         }\
00200         for(i = 4 ; (i < 134) && (T1F_pointer != NULL) ; i++){\
00201              TMframe_type1[i] = T1F_pointer->TM_string[T1F_counter];\
00202               if(T1F_counter++ == 12){\
00203                 T1F_counter = 0;\
00204                 next_type_structure(T1F_pointer);\
00205               }\
00206         }\
00207         TMframe_type1[0] = (1<<7) + (( (i-4)/13 )<<3);\
00208         TMframe_type1[3] = crc8_gen(TMframe_type1,3);\
00209         if(T1F_pointer == NULL && i>12){\
00210             for( ; i < 134 ; i++){\
00211                 TMframe_type1[i] = TMframe_type1[i-13];\
00212             }\
00213         }\
00214         ptr=TMframe_type1;\
00215     }
00216  
00217  
00218     bool type0_frame_flag;
00219  
00220  
00221  #define type0_frame(ptr){\
00222         if(type0_frame_flag){\
00223             first_type_structure(0,T0F_pointer);\
00224             type0_frame_flag = false;\
00225         }\
00226         else {\
00227             next_type_structure(T0F_pointer);\
00228         }\
00229         ptr =  T0F_pointer->TM_string;\
00230     }
00231   
00232     unsigned char convoluted_frame[270];
00233     Convolution ConvObj2;
00234     void convolution (unsigned char * ptr){
00235         ConvObj2.convolutionEncode(ptr, convoluted_frame);
00236         ConvObj2.convolutionEncode(ptr + 67, convoluted_frame + 135);
00237     }
00238  
00239  
00240     unsigned char interleave_data[2][144];
00241     unsigned char DS_index;
00242     bool DS_state;
00243     int DS_counter;
00244     bool DS_f;
00245     bool make_DataStream_f;
00246  
00247 #define make_DataStream {\
00248     unsigned char* ptr;\
00249         if(make_DataStream_f == true){\
00250             DS_state = true;\
00251             DS_counter = 0;\
00252             DS_f = false;\
00253             make_DataStream_f = false;\
00254         }\
00255         if(DS_state){\
00256             if(DS_counter < total_frames-type0_no){\
00257                 type1_frame(ptr); \
00258                 DS_f = true;\
00259             }\
00260             else if(DS_counter < total_frames ){\
00261                 type0_frame(ptr);\
00262                 DS_f = true;\
00263             }\
00264             DS_counter++;\
00265         }\
00266         DS_state = !DS_state;\
00267         if (DS_f){\
00268             DS_f = false;\
00269             exor(ptr);\
00270             convolution(ptr);\
00271             interleave(convoluted_frame,interleave_data[0]);\
00272             interleave(convoluted_frame+ 135,interleave_data[1]);}\
00273         DS_index = (DS_state==true)?1:0;\
00274     }
00275  
00276  
00277 //
00278     public:
00279  
00280         bool reset_adf;
00281         SND_TM(){
00282             MAX_SEGMENT = (MAX_ADF_LEN-EOS_SIZE)/( S_FRAME_SIZE + ISR*T_FRAME_SIZE);
00283                             //(max length - eos size)/(sframe length + tFrameLength*ISR)
00284         }
00285  
00286  
00287  
00288         void head_pointer(Base_tm* ptr){
00289             head_ptr = ptr ;
00290             type1_frame_flag = true;
00291             type0_frame_flag = true;
00292             make_DataStream_f = true;
00293             transmit_data_f = true;
00294             sent_tmfrom_SDcard = false;
00295             diff_prev = 0;
00296             data_number();
00297         }
00298  
00299  
00300         void inline transmit_data(unsigned char * transmit , bool * last_buffer){
00301             if(transmit_data_f){
00302                 RB_c = 0;
00303                 Sframe_c = 0;
00304                 SCH40_c = 0;
00305                 FCCH80_c = 0;
00306                 Tframe_c = 0;
00307                 EOS_c = 0;
00308                 segment_c = 0;
00309                 data_c = 0;
00310  
00311                 Sframe = true;
00312                 SCH40_f = false;
00313                 data_f = false;
00314                 FCCH80_f = false;
00315                 transmit_data_f = false;
00316                 reset_adf = false;
00317                 *last_buffer = false;
00318             }
00319  
00320             for(RB_c = 0 ; RB_c<112 ; RB_c++){
00321  
00322                 if(junk_f || *last_buffer){
00323                     transmit[RB_c] = 0x00;
00324                     continue;
00325                 }
00326  
00327                 if(Sframe){
00328                     transmit[RB_c] = S_frame[Sframe_c++];
00329                     if(Sframe_c == 48){
00330  //                     printf("exit s frame\n");
00331                         Sframe_c = 0;
00332                         Sframe = false;
00333                         SCH40_f =true;
00334                     }
00335                     continue;
00336                 }
00337 //
00338                 if(SCH40_f){
00339                     transmit[RB_c] = SCH40[SCH40_c++];
00340                     if(SCH40_c == 5 ){
00341                         SCH40_c = 0;
00342                         SCH40_f = false;
00343  
00344  
00345                         if(Tframe_c >= LAST_SEGMENT_SIZE && segment_r == 0)
00346                         {
00347                             repeat_data_f = true;
00348                             DS_index = !DS_index;
00349                         }
00350                         else
00351                         {
00352                             data_f = true;
00353                             make_DataStream;
00354                         }
00355 //                      printf("exit SCH40 frame\n");
00356                     }
00357                     continue;
00358                 }
00359 //
00360                 if(data_f){
00361                     transmit[RB_c] = interleave_data[DS_index][data_c++];
00362                     if(data_c == 144){
00363                         data_c = 0;
00364                         FCCH80_f = true;
00365                         data_f = false;
00366 //                        printf("exit Data\n");
00367                     }
00368                     continue;
00369                 }
00370                 if(repeat_data_f){
00371                     transmit[RB_c] = interleave_data[DS_index][data_c++];
00372                     if(data_c == 144){
00373                         data_c = 0;
00374                         FCCH80_f = true;
00375                         repeat_data_f = false;
00376 //                        printf("exit Data\n");
00377                     }
00378                     continue;
00379                 }
00380  
00381                 if(FCCH80_f){
00382                     transmit[RB_c] = FCCH80[FCCH80_c++];
00383                     if(FCCH80_c == 10){
00384                         FCCH80_c = 0;
00385                         FCCH80_f = false;
00386 //                        printf("exit FCCH80\n");
00387                         if(++Tframe_c != ISR)
00388                             SCH40_f = true;
00389                         else{
00390                             Tframe_c = 0;
00391                             segment_c++;
00392                             if(segment_c >= SEGMENT_NO - 1 && TOTAL_SEGMENT_NO == 0) segment_r = 0; else segment_r = 1;
00393                             if(segment_c == SEGMENT_NO)
00394                                 {eos_f = true;}
00395                             else{
00396                                 Sframe = true;
00397                             }
00398                         }
00399                     }
00400                     continue;
00401                 }
00402  
00403                 if(eos_f){
00404                     transmit[RB_c] = EoS[EOS_c++];
00405                     if(EOS_c == EOS_SIZE){
00406                         EOS_c = 0;
00407                         eos_f = false;
00408                         if(segment_c == SEGMENT_NO && segment_r == 0){
00409                               *last_buffer = true;
00410                                 }
00411                           else if(segment_c == SEGMENT_NO && segment_r != 0){
00412                                 segment_c = 0;
00413                                 SEGMENT_NO = TOTAL_SEGMENT_NO;
00414                                 if(SEGMENT_NO > MAX_SEGMENT)
00415                                 {
00416                                     SEGMENT_NO = MAX_SEGMENT;
00417                                 }
00418                                 if(SEGMENT_NO == 1) segment_r = 0; else segment_r = 1; 
00419                                 TOTAL_SEGMENT_NO -= SEGMENT_NO;
00420                                 reset_adf = true;//what does this do??
00421                                 Sframe = true;
00422                                 junk_f = true;
00423                            }
00424                     }
00425                     continue;
00426                 }
00427             }
00428             differential_encode(transmit,112);
00429             if(reset_adf == true){
00430                 diff_prev = 0;
00431                 junk_f = false;
00432             }
00433  
00434         }
00435  
00436 };
00437 SND_TM snd_tm;